简体   繁体   English

在rails内部调用javascript函数hidden_​​field_tag

[英]call javascript function inside rails hidden_field_tag

I'm setting up Braintree in a simple site. 我正在一个简单的站点中设置Braintree。 Braintree is a payment plataform. Braintree是一种付款平台。 In their examples they use a hiddel_field_tag as follows: 在他们的示例中,他们使用hiddel_field_tag如下:

<%= 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. 现在,就我而言,我想通过JavaScript函数设置交易金额,因为我只有三个选项,而不是通过服务器。 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. 如您所见,我仍在学习Rails。 So the questions is, how do I change a variable or add a function to a hidden_field_tag? 所以问题是,如何更改变量或将函数添加到hidden_​​field_tag? I could also make :amount => "variable-set-by-javascript", but have not been able to do so. 我也可以使用:amount =>“ variable-set-by-javascript”,但无法做到这一点。

I work at Braintree. 我在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. Braintree::TransparentRedirect.transaction_data方法在呈现表单之前在服务器上运行,因此它需要实际数量,而不是字符串,该字符串稍后将被评估并返回一个数量。

In addition, you generally can't / shouldn't set the amount client side / outside the transaction_data . 另外,您通常不能/不应在transaction_data之外设置客户端的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 . 自从您发布此问题以来,我们已经启动了Braintree.js ,它将允许您将整个表单发布到您的服务器,并在将表单提交给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. 我建议您将该集成方法用于任何新的集成,如果需要对付款流程进行其他更改,请升级到该集成方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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