【问题标题】:Ajax Submit issue - RailsAjax 提交问题 - Rails
【发布时间】: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


    【解决方案1】:

    全面披露:我在 Braintree 工作。如果您还有任何问题,请随时联系support

    您的提交按钮的默认行为可能会遇到问题,即something you could try disabling。但是,这里没有什么可以认为这是问题所在。您的设置中的其他地方很可能存在问题。我真的建议您联系Braintree support 并提供完整的代码示例,以帮助他们解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 2014-08-15
      相关资源
      最近更新 更多