简体   繁体   English

无法将变量传递给 Paypal 智能按钮 api 的值

[英]Can't pass variable to value for Paypal Smart Button api

I am trying to integrate the smart button api for paypal on my website.我正在尝试在我的网站上为 paypal 集成智能按钮 api。 This is the code:这是代码:

 paypal.Buttons({
createOrder: function(data, actions) {
  // This function sets up the details of the transaction, including the amount and line item details.
  return actions.order.create({
    purchase_units: [{
      amount: {
        value: '10'
      }
    }]
  });
}
  }).render('#paypal-button-container');

I have my price variable like this:我的价格变量是这样的:

var price = '100'

I have tried adding price instead of the '100' but I keep getting the uncaught reference error telling me that it has not been defined yet.我尝试添加价格而不是“100”,但我不断收到未捕获的参考错误,告诉我它尚未定义。

You need to make sure it's defined in a sufficiently-global scope before the PayPal button executes.在执行 PayPal 按钮之前,您需要确保它在足够全局的 scope 中定义。

If it is defined in a different function, not before the button is clicked, you will receive that error.如果它是在不同的 function 中定义的,而不是在单击按钮之前,您将收到该错误。

A quick fix would be to set window.myPrice = '100';一个快速的解决方法是设置window.myPrice = '100'; , and then reference value: window.myPrice from your button ,然后参考value: window.myPrice从您的按钮

Of course, there are cleaner non-global-variable ways to do this.当然,有更简洁的非全局变量方法可以做到这一点。

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

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