【发布时间】:2016-10-25 06:16:18
【问题描述】:
我的应用中有一个 PaymentNotificationsController 中的 Paypal IPN。但是,一些变量取决于购物车中的商品数量,所以我想在创建 PaymentNotification 之前提取它们。
到目前为止,我有:
class PaymentNotificationsController < ApplicationController
protect_from_forgery except: [:create]
def create
PaymentNotification.create!(params: params,
item_number: params[:item_number], item_name: params[:item_name], quantity: params[:quantity]
render nothing: true
end
end
但是,当通知来自 PayPal 时,它以item_name1, item_number1, quantity1, item_name2, item_number2, quantity2 等形式出现。
即使它只是一项,它也会以item_name1, item_number1, quantity1, option1 等形式出现。
我有这个函数来尝试提取变量,但我不知道如何触发这个函数。我尝试在控制器顶部使用before_action,但它不起作用。返回wrong number of arguments(0 for 1):
ITEM_PARAM_PREFIXES = ["item_name", "item_number", "quantity"]
def extract_ipn_items_params(params)
item_params = []
loop do
item_num_to_test = item_params.length + 1
item_num_suffix = item_num_to_test.to_s
possible_param_name = ITEM_PARAM_PREFIXES[0] + item_num_suffix
if params.include?(possible_param_name)
this_item_params = {}
ITEM_PARAM_PREFIXES.each do |prefix|
this_item_params[prefix] = params[prefix + item_num_suffix]
end
item_params.push this_item_params
else
return item_params
end
end
end
所以我在问,我如何触发函数来提取变量并将它们放入购物车中每个项目的 params[:item_number]、params[:item_name]、params[:quantity] 中,如果有两个项目,是否会创建两个单独的 PaymentNotification?
注意:两种方法都在同一个PaymentNotificationsController中。
任何帮助将不胜感激。提前致谢!
【问题讨论】:
标签: ruby-on-rails ruby paypal controller