gpt4 book ai didi

ruby-on-rails - 触发 rails controller 功能 - Paypal Website Standard IPN

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:59 25 4
gpt4 key购买 nike

我有一个 Paypal IPN 进入我的应用程序中的 PaymentNotificationsController。但是,一些变量取决于购物车中的商品数量,因此我想在创建 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 等形式出现。

我有这个函数来尝试提取变量,但我不知道如何触发该函数。我尝试在 Controller 顶部使用 before_action 但它不起作用。返回的参数数量错误(1 为 0):

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] 中,如果有两个项目,将创建两个单独的 PaymentNotifications?

注意:这两种方法都在同一个PaymentNotificationsController中。

如有任何帮助,我们将不胜感激。提前致谢!

最佳答案

我假设你的方法 extract_ipn_items_params 已经获取了你需要的数据,你可以删除方法的 params 参数,因为参数总是在 actions/methods 中可用 Controller 。

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

该方法返回一个散列的散列,例如:

{1 => {item_name: 'Item 1', item_number: 1084, quantity: 15}},如果你为用户设置了嵌套属性,我想你应该可以做一些类似的事情,不太确定,但应该是可能的:

user.update(payment_notifications_attributes: extract_ipn_items_params)

让我知道这是否适合您。

更新

基于 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

如果这能解决您的问题,请告诉我。

关于ruby-on-rails - 触发 rails controller 功能 - Paypal Website Standard IPN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com