简体   繁体   中英

call javascript function inside rails hidden_field_tag

I'm setting up Braintree in a simple site. Braintree is a payment plataform. In their examples they use a hiddel_field_tag as follows:

<%= hidden_field_tag :tr_data, Braintree::TransparentRedirect.transaction_data(
  :redirect_url => confirm_payment_url,
  :transaction => {:type => "sale", :amount => @amount }
) %>

now, in my case I want to set the amount of the transaction via a javascript function because I have only three options, and not via the server. I've tried many things, like

<%= hidden_field_tag :tr_data, Braintree::TransparentRedirect.transaction_data(
  :redirect_url => confirm_payment_url,
  :transaction => {:type => "sale", :amount => "return amountFunction();" }
) %>

where

function amountFunction()
{
  var amount = $('input[name=optionsRadios]:radio:checked').val();
  return amount;
}

but is not working. I'm still in the learning phase with rails, as you can see. So the questions is, how do I change a variable or add a function to a hidden_field_tag? I could also make :amount => "variable-set-by-javascript", but have not been able to do so.

I work at Braintree. We missed this question, but hopefully you contacted support or figured out your problem.

The Braintree::TransparentRedirect.transaction_data method runs on the server before the form is rendered, so it needs an actual amount rather than a string that will later be evaluated and return an amount.

In addition, you generally can't / shouldn't set the amount client side / outside the transaction_data . That would allow someone to change the amount in the browser and submit a transaction for whatever amount they wanted -- something you want to avoid, as it could cost you money.

Since you posted this question, we've launched Braintree.js which would allow you to post the entire form to your server, and verify the amount is valid there before submitting the form to Braintree . I'd recommend using that integration method for any new integrations, or upgrading to it if you need to make other changes to your payment flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM