问题
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:
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.
来源:https://stackoverflow.com/questions/35243643/ajax-submit-issue-rails