9

I would like to add more properties in the paypal's actions.order.create function. Properties like tax_total, shipping, insurance, list of items that the customer bought etc.

I have implemented the code from their website and it works fine, What is left is adding the extra details in the code. This is their documentation: https://developer.paypal.com/docs/api/orders/v2/#orders_create.

<script>
    paypal.Buttons({
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        // value: '350.00',
                        breakdown: {
                            item_total: '300.00',
                             tax_total: '20.00',
                             shipping: '10.00',
                             handling: '10.00',
                             insurance: '10.00'
                         },
                     }
                 }]
             });
         },
         onApprove: function(data, actions) {
             // Capture the funds from the transaction
             return actions.order.capture().then(function(details) {
                 // Show a success message to your buyer
                 console.log(data, actions);
                 alert('Transaction completed by ' + details.payer.name.given_name);
                 // Call your server to save the transaction
                 return fetch('/paypal-transaction-complete', {
                     method: 'post',
                     headers: {
                         'content-type': 'application/json',
                         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                     },
                     body: JSON.stringify({
                         orderID: data.orderID
                     })
                 });
             });
         }
     }).render('#paypal-button-container');
</script>

The code above is what I have tried, I expected that the breakdown properties will be part of the payload as shown in the documentation but I get an error from the console instead.

So what will be the right way to add these properties? Thanks in advance :)

party-ring
  • 1,761
  • 1
  • 16
  • 38
Dr.P
  • 97
  • 1
  • 8

1 Answers1

3

I found an answer here:

https://github.com/paypal/paypal-checkout-components/issues/1098

The example in that link had a currency code that didn't work for me. Below is a similar example:

var createOrder = function createOrder(data, actions) { 
    return actions.order.create({
      "purchase_units": [
        {
          "reference_id": 1234,
          "description": "Attempt n.1 for Quote ID 1234",
          "amount": {
            "currency_code": "USD",
            "value": 14.4,
            "breakdown": {
              "item_total": { "currency_code":"USD", "value":"12"},
              "shipping": { "currency_code":"USD", "value":"1"},
              "tax_total": { "currency_code":"USD", "value":"1.4"},
              "discount": { "currency_code":"USD", "value":"0"}
            }
          },
          "items": [
            {
              "name": "OnePlus 6 T-rex 12\" name for 14\"\" blabla \" more double quotes",
              "unit_amount": {
                "currency_code": "USD",
                "value": 12
              },
              "tax": {
                "currency_code": "USD",
                "value": 1.4
              },
              "quantity": 1,
              "sku": "OnePlus61",
              "category": "PHYSICAL_GOODS"
            }
          ],
          "shipping": {
            "address": {
              "address_line_1": "Some line 1",
              "address_line_2": "Some line 2",
              "admin_area_2": "Some city",
              "admin_area_1": "some state",
              "postal_code": "12345",
              "country_code": "GB"
            }
          }
        }
      ]
    });
  };

Note that each amount must add up to the total value (in this case 14.4). Also note that in this example the values for the keys tax_total and tax must match. This may not be the case if you have more than one item as part of the order.

Mr. J
  • 307
  • 2
  • 9