Paypal paylater set button amount

243 views Asked by At

I have a paypal page that accepts paypal pay later. I want to dynamically set the button amount with javascript.

This is the code

<script src="https://www.paypal.com/sdk/js?client-id=&currency=GBP&enable-funding=paylater,card&components=messages,buttons">
</script>

<div id="paypal-button-container"></div>

<script>
var amount = 90;
//$('#paypal').data('pp-amount',amount);

  paypal.Buttons({
    createOrder: function(data, actions) {
        return actions.order.create({
          purchase_units: [{
            amount: {
              value: amount
            }
          }]
        });
      },
      
    //return url after successful payment
    onAuthorize: function(data, actions) {

          console.log(data, actions);
          actions.payment.execute().then(function(data) {
            console.log(data);
            // Show a success page to the buyer
          });
        }
}).render('#paypal-button-container');
</script>

<div id="paypal" data-pp-message data-pp-amount="90"> </div>

I added the paypal id into this <div id="paypal" data-pp-message data-pp-amount="90"> </div>

I am trying to dynamically set this value

data-pp-amount="90"

but this doesn't work $('#paypal').data('pp-amount',amount);

1

There are 1 answers

0
Gandalf On

I did it this way

<script src="https://www.paypal.com/sdk/js?client-id=&currency=GBP&enable-funding=paylater,card&components=messages,buttons">
</script>

<div id="paypal-button-container"></div>
<div class="pp-message"> </div>
<script>
var am = 90;


    
  paypal.Buttons({
    createOrder: function(data, actions) {
        return actions.order.create({
          purchase_units: [{
            amount: {
              value: am
            }
          }]
        });
      },
      
    //return url after successful payment
    onAuthorize: function(data, actions) {

          console.log(data, actions);
          actions.payment.execute().then(function(data) {
            console.log(data);
            // Show a success page to the buyer
          });
        }
}).render('#paypal-button-container');

paypal.Messages({
      amount: am,
      placement: 'product',
      style: {
        layout: 'text',
        logo: {
          type: 'primary',
          position: 'top'
        }
      }
    })
    .render('.pp-message');
</script>