【发布时间】:2016-02-06 16:50:58
【问题描述】:
我不相信我通过 Rails UJS 和 Ajax 提交 Braintree 事务的解决方案是正确的,原因是我在提交时看到两个 ajax 调用而不是一个。
例如,当关闭 ajax 时(因此删除远程:true)提交表单似乎在提交付款之前等待一个随机数(我相信 Braintree 接管了提交事件)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here", "first_name"=>"", "last_name"=>"", "payment_method_nonce"=>"nonce_here_populated"}
但是当使用 remote: true 提交时,第一次调用是
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here=", "first_name"=>"", "last_name"=>""}
所以我读到我需要使用onPaymentMethodRecieved 回调函数等待该随机数,将随机数附加到表单然后提交
braintree.setup(gon.client_token, 'dropin', {
container: 'dropin-container',
onPaymentMethodReceived: function (paymentMethod) {
$('#braintree-transaction-form').append("<input id='p_nonce' type='hidden' name='payment_method_nonce' value='" + paymentMethod.nonce + "'></input>");
$("#braintree-transaction-form input[type='submit']").submit();
}
});
这会导致两次调用
1)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here=", "first_name"=>"", "last_name"=>""}
2)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here", "first_name"=>"", "last_name"=>"", "payment_method_nonce"=>"nonce_here_populated"}
唯一的问题是我的控制器中有这个,但我认为这是不正确的
def create
nonce = params[:payment_method_nonce]
render action: :new and return unless nonce # Is this the way to wait for the nonce
@result = Braintree::Transaction.sale(
amount: 2500,
payment_method_nonce: nonce,
customer: {
first_name: params[:first_name],
last_name: params[:last_name]
}
)
end
我想我担心的是正在进行的两个调用和我的控制器代码。
有没有人有过使用 Braintree 和 Rails 进行 Ajax 调用的经验
谢谢
【问题讨论】:
标签: ruby-on-rails ruby ajax braintree