cancel
Showing results for 
Search instead for 
Did you mean: 

PayPal REST API Express Checkout Variable Amount.

Contributor

PayPal REST API Express Checkout Variable Amount.

Hello, 

 

I am using the PayPal REST API in order to integrate an Express Checkout button. As of right now, the program is using the "0.01" as the total with the amount variable. I have this button displaying in an HTML and I would like to pass in a variable value for the amount so that each PayPal button that I create is passed its own individual value that is being pulled from a MongoDB database and accessed using the "payment.amount" call but I can't seem to use that same name on the PayPal button to call the value. Is there any way to do this so that each time the button is repeated it could pull its respective values? 

6 REPLIES 6
Moderator

Re: PayPal REST API Express Checkout Variable Amount.

Hi, can you post the button code to have a look?

Contributor

Re: PayPal REST API Express Checkout Variable Amount.

<td><div id="paypal-button-container"></div>
 <script src="https://www.paypalobjects.com/api/checkout.js"></script>
 <script>
 paypal.Button.render({
  
 env: 'sandbox', // sandbox | production
  
 style: {
 label: 'paypal',
 size: 'small', // small | medium | large | responsive
 shape: 'rect', // pill | rect
 color: 'blue', // gold | blue | silver | black
 tagline: false
 },
  
 // PayPal Client IDs - replace with your own
 // Create a PayPal app: https://developer.paypal.com/developer/applications/create
 client: {
 sandbox: '',
 production: '<insert production client id>'
 },
  
 // Show the buyer a 'Pay Now' button in the checkout flow
 commit: true,
  
 // payment() is called when the button is clicked
 payment: function(data, actions) {
  
 // Make a call to the REST api to create the payment
 return actions.payment.create({
 payment: {
 transactions: [
 {
 amount: { total: '0.01', currency: 'USD' }
 }
 ]
 }
 });
 },
  
 // onAuthorize() is called when the buyer approves the payment
 onAuthorize: function(data, actions) {
  
 // Make a call to the REST api to execute the payment
 return actions.payment.execute().then(function() {
 window.alert('Payment Complete!');
 });
 }
  
 }, '#paypal-button-container');
  
 </script></td>
Contributor

Re: PayPal REST API Express Checkout Variable Amount.

Sorry the HTML wouldnt post, but basically I am trying to pass a value called "payment.amount" to total which is store in an mlab database.

Moderator

Re: PayPal REST API Express Checkout Variable Amount.

Hi, ok, in general, with ExpressCheckout JS4 you can set a variable amount depending on how you develop your code.

You can have 2 options:

1) Using JS Server-Side: JS4 Server-side
In this way the client just call the server which will create a SetExpressCheckout/API call where the amount is set dinamically based on your needs as the server creates the payment URL with the amount that you decided

Example
payment: function() {
            return paypal.request.post(CREATE_PAYMENT_URL).then(function(data) {
                return data.id;
            });
        },

The Server will send a response (eg. the PayKey) that will be used by the JS to forward the buyer to the PayPal checkout with your amount

2) Using JS Client-Side: JS4 Client-side

In this solution the amount is hard-coded in the JS button code

payment: function(data, actions) {
            return actions.payment.create({
                payment: {
                    transactions: [
                        {
                            amount: { total: '1.00', currency: 'USD' }
                        }
                    ]
                }
            });
        },

Of course you can write as well here your own code where your website/server creates every time a button code with a different amount. That means that you don't write down the button code as you see but you create each time a new button code where the amount is a variable.

In both the solutions you need to develop your own code to achieve that result for a dynamic amount.

Contributor

Re: PayPal REST API Express Checkout Variable Amount.

Is there a way to use a parameter in the url to replace total:'0.01' and currency: 'USD' in the script ?

(params like https://myWebSite.comL?amt=50.0&cur=EUR)

 

Moderator

Re: PayPal REST API Express Checkout Variable Amount.

You could pull values into your JS code using the searchParams function within JS, see this SO link for more on that:

https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters