Search code examples
reactjspaypalpaypal-sandbox

How to add a quantity in paypal orders in reactjs


I'm trying to add the qty of an order. this is what I have and it wont work :(.

it works without qty but then it defaults to 1. Also how would i add a second product? it will only allow me to have the one

        window.paypal.Buttons({
        createOrder: (data, actions, err) => {
            return actions.order.create({
                intent: "CAPTURE",
                purchase_units: [
                    {
                        description: "cool tablet",
                        amount: {
                            currency_code: "CAD",
                            value: 650.00,
                        },
                        quantity: 2,
                    },
                    {
                        description: "ink",
                        amount: {
                            currency_code: "CAD",
                            value: 777.00,
                        }
                    }
                ]
            })
        },
        onApprove: async (data, actions)=>{
            const order = await (actions.order.capture());
            console.log(order);
        },
        onError: (err) => {
            console.log(err);
        }
    })
        .render(paypal.current)
}, []);

Solution

  • You need a single purchase unit, a single amount with the required breakdown object, and an items array with line item detail. See the API reference at https://developer.paypal.com/docs/api/orders/v2/#orders_create

    Here is an example:

      "purchase_units": [{
          "description": "DESCRIPTION GOES HERE",
          "amount": {
            "value": "3.00",
            "currency_code": "CAD",
            "breakdown": {
              "item_total": {
                "currency_code": "CAD",
                "value": "3.00"
              }
            }
          },
          "items": [
            {
              "name": "item one",
              "quantity": "1",
              "unit_amount": {
                "currency_code": "CAD",
                "value": "1.00"
              }
            },
            {
              "name": "item two",
              "quantity": "1",
              "unit_amount": {
                "currency_code": "CAD",
                "value": "2.00"
              }
            }
          ]
        }
      ]