skip to Main Content

I’m using the PayPal smart payment buttons to sell a single item on my website, but I can’t find in any of the documentation how to ask for quantity.

Is this even possible to do with the smart payment buttons API?

I just want to offer the customer the ability to change the quantity of the item they wish to buy. I don’t mind if it’s in the form or during the checkout stage within PayPal.

Here’s my code so far, it’s pretty standard as I’m trying to keep it simple:

<script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({
        style: {
            size: 'responsive',
            shape: 'pill',
            label: 'checkout',
        },
        // Set up the transaction
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: '8.99'
                    }
                }]
            });
        },

        // Finalize the transaction
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                // Show a success message to the buyer
                alert('Transaction completed by ' + details.payer.name.given_name + '!');
            });
        }


    }).render('#paypal-button-container');
</script>

2

Answers


  1. To implement quantity field on your client. you have to breakdown the amount field and then add “item_total” and “items” field in the purchase units.

    for your reference:

    createOrder: function(data, actions) {
         console.log(data);
           console.log(actions);
         return actions.order.create({
           purchase_units: [{
    
             amount: {
               value: (<HTMLInputElement>document.getElementById("number1")).value,
             breakdown: {
               item_total:{
                 "currency_code": "EUR",
                     value: (<HTMLInputElement>document.getElementById("number1")).value,
                   },
                   tax_total: {
                     "currency_code": "EUR",
                     "value": "00.00"
                   },
             }  
             },
    
               items: [
                 {
                   "name": "",
                   "description": ".......",
                   "sku": "sku01",
                   "unit_amount": {
                     "currency_code": "EUR",
                     "value": "20.00"
                   },
                   "quantity": (<HTMLInputElement>document.getElementById("number")).value,
    
                 }]
    
           }],
    
    Login or Signup to reply.
  2. I have done this but without breaking out the bill.
    I made the final price "amount" as the price multiplied by the quantity and in my database I am saving all the details of the bill.
    This is my javascript code I used:

     amount: {
             value: "<?= $_SESSION['price'] * $_SESSION['quantity'] ?>" ,
             currency_code: 'EUR'
     },
    

    You can use Javascript instead of server code like this,

    amount: {
             value: document.getElementById("quantity").value * document.getElementById("price").value ,
             currency_code: 'EUR'
     },
    

    In both codes, values should be set before rendering the buttons, which means before loading the page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search