weixin_33693070 2016-02-06 16:50 采纳率: 0%
浏览 32

Ajax提交问题-Rails

I am not convinced my solution to submit a braintree transaction via Rails UJS and Ajax is correct, the reason being is i am seeing two ajax calls as opposed to one when submitting.

For example when turning ajax off (so removing remote: true) submitting a form seems to wait for a nonce before submitting the payment (I believe braintree takes over the submit event)

Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here", "first_name"=>"", "last_name"=>"", "payment_method_nonce"=>"nonce_here_populated"}

But when submitting with remote: true enabled the first call is

Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here=", "first_name"=>"", "last_name"=>""} 

So i read that i need to wait for that nonce using the onPaymentMethodRecieved callback function, append the nonce to the form and then submit

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();
  }
});

This results in two calls being made

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"}

Only problem being is i have this in my controller which dont think is correct

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

my concern i guess is the two calls being made and my controller code.

Has anyone got any experience with Ajax calls with Braintree and Rails

Thanks

  • 写回答

1条回答 默认 最新

  • weixin_33709364 2016-02-08 17:07
    关注

    Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

    You might be running into a problem with the default behavior of your submit button, which is something you could try disabling. However, there's nothing here to think that is the issue. Most likely there is a problem elsewhere in your setup. I would really recommend contacting Braintree support with your full code samples to help them walk through your issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么