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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵