[英]Trigger rails controller function - Paypal Website Standard IPN
I've got a Paypal IPN that comes into a PaymentNotificationsController
in my app. 我的应用程序中有一个PayPal IPN进入
PaymentNotificationsController
。 However, some variables depend on the number of items in a cart, so i want to extract them before creating the PaymentNotification. 但是,某些变量取决于购物车中的商品数量,因此我想在创建PaymentNotification之前提取它们。
So far, i've got: 到目前为止,我有:
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
However, when the notification comes from PayPal, it comes in the form of item_name1, item_number1, quantity1, item_name2, item_number2, quantity2
and so on. 但是,当通知来自PayPal时,它以
item_name1, item_number1, quantity1, item_name2, item_number2, quantity2
等形式出现。 Even if its just one item, it would come as item_name1, item_number1, quantity1, option1
and so on. 即使它只是一个项目,它也会作为
item_name1, item_number1, quantity1, option1
等等。
I have this function to try and extract the variables, but i don't know how to trigger the function. 我有这个函数来尝试提取变量,但我不知道如何触发该函数。 I tried using a
before_action
at the top of the controller but it didn't work. 我尝试在控制器顶部使用
before_action
但它不起作用。 Returned wrong number of arguments(0 for 1)
: 返回
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
So i'm asking, how do i trigger the function to extract the variables and put them into params[:item_number], params[:item_name], params[:quantity] for each item in the cart so if there are two items, two separate PaymentNotifications would be created? 所以我问,我如何触发提取变量的函数并将它们放入参数[:item_number],params [:item_name],params [:quantity]为购物车中的每个项目,如果有两个项目,将创建两个单独的PaymentNotifications?
Note : Both methods are in the same PaymentNotificationsController
. 注意 :两种方法都在同一个
PaymentNotificationsController
。
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance!
提前致谢!
You should have payment_id so you can find it by using gem 'paypal-sdk-rest'
你应该有payment_id所以你可以使用
gem 'paypal-sdk-rest'
找到它
payment = PayPal::SDK::REST::Payment.find payment_id
then you could see all details in payment object 然后你可以看到付款对象中的所有细节
I assume your method extract_ipn_items_params
already fetches the data you require, you can remove the params argument to the method, as the params is always available in the actions/methods
of the controller. 我假设你的方法
extract_ipn_items_params
已经获取了你需要的数据,你可以删除方法的params参数,因为params总是在控制器的actions/methods
中可用。
ITEM_PARAM_PREFIXES = ["item_name", "item_number", "quantity"]
def extract_ipn_items_params
mod_params = Hash.new{|k, v| k[v] = {} }
ITEM_PARAM_PREFIXES.each do |item_data_key|
key_tracker = 1
loop do
current_key = (item_data_key + key_tracker.to_s).to_sym
if params.include? current_key
mod_params[key_tracker][item_data_key] = params[current_key]
else
break
end
key_tracker += 1
end
end
mod_params
end
The method returns a hash of hashes like: 该方法返回哈希的散列,如:
{1 => {item_name: 'Item 1', item_number: 1084, quantity: 15}}
, if you have nested attributes set up for a user, I think you should be able to do something like, not really sure, but should be possible: {1 => {item_name: 'Item 1', item_number: 1084, quantity: 15}}
,如果您为用户设置了嵌套属性,我认为您应该可以做一些类似的事情,但不是很确定,但应该有可能:
user.update(payment_notifications_attributes: extract_ipn_items_params)
Let me know if that works for you. 如果这对您有用,请告诉我。
UPDATE UPDATE
Based on the Github Gist, here's something I was able to come up with: 基于Github Gist,这是我能够提出的:
class PaymentNotificationsController < ApplicationController
protect_from_forgery except: [:create]
ITEM_PARAM_PREFIXES = ["item_name", "item_number", "quantity", "option_name"]
def create
extract_ipn_items_params.each do |key, values|
# this approach loops through all the returned results, nested attributes may help abstract this though
PaymentNotification.create(values)
render nothing: true
end
def details
# params.extract_ipn_items_params #this doesn't exist as params is an instance of ActionController::Parameters
PaymentNotification.update_attributes(line_item_id: params[:item_number], product_title: params[:item_name], option_name: params[:option_name], quantity: params[:quantity])
end
private
def additional_attributes
# create this for additional merge attributes. A better place for these would be the parent of this
{
params: params,
cart_id: params[:invoice],
status: params[:payment_status],
transaction_id: params[:txn_id],
first_name: params[:first_name],
last_name: params[:last_name],
email: params[:payer_email],
address_name: params[:address_name],
address_street: params[:address_street],
address_city: params[:address_city],
address_state: params[:address_state],
address_zip: params[:address_zip],
address_country: params[:address_country]
}
end
def extract_ipn_items_params
mod_params = Hash.new{|k, v| k[v] = {}.merge(additional_attributes) }
ITEM_PARAM_PREFIXES.each do |item_data_key|
key_tracker = 1
loop do
current_key = (item_data_key + key_tracker.to_s).to_sym
if params.include? current_key
mod_params[key_tracker][item_data_key] = params[current_key]
else
break
end
key_tracker += 1
end
end
mod_params
end
end
Let me know if that fixes your problem. 如果这可以解决您的问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.