简体   繁体   中英

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. 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.

You need to make sure it's defined in a sufficiently-global scope before the PayPal button executes.

If it is defined in a different function, not before the button is clicked, you will receive that error.

A quick fix would be to set window.myPrice = '100'; , and then reference value: window.myPrice from your button

Of course, there are cleaner non-global-variable ways to do this.

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