Search code examples
phppaypalpayment-gatewaypaypal-sandboxpaypal-rest-sdk

Php - Paypal v2 checkout orders returned status 422 when passing tax amount


I am using Standard PayPal Checkout

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

<script>

  paypal.Buttons({

   createOrder: function(data, actions) {
    return actions.order.create({
     "purchase_units": [{
        "amount": {
          "currency_code": "AUD",
          "value": "273.9",
          "breakdown": {
            "item_total": {  /* Required when including the `items` array */
              "currency_code": "AUD",
              "value": "249",
              "tax": {
                "value": "24.9",
                "currency_code": "AUD"
               },
            }
          }
        },
        
         "shipping": {
           "name": {
           "full_name": "TestBuyer"
           },
          "address": {
            "addline_1": "1234 Main St",
            "area_2": "New San Jose",
            "area_1": "CA",
            "poscode": "9513",
            "cou_code": "AT"
         }
      },  

        "items": [
          {
            "name": "First Product Name", 
            "description": "Optional descriptive text..",
            "unit_amount": {
              "currency_code": "AUD",
              "value": "249"
            },
            "tax": {
                "value": "24.9",
                "currency_code": "AUD"
               },
            "quantity": "1"
          },   
        ]
      }]
  });
},


    // Finalize the transaction after payer approval

    onApprove: (data, actions) => {

      return actions.order.capture().then(function(orderData) {

        // Successful capture! For dev/demo purposes:

        console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));

        const transaction = orderData.purchase_units[0].payments.captures[0];

        alert(`Transaction ${transaction.status}: ${transaction.id}\n\nSee console for all available details`);

        // When ready to go live, remove the alert and show a success message within this page. For example:

        // const element = document.getElementById('paypal-button-container');

        // element.innerHTML = '<h3>Thank you for your payment!</h3>';

        // Or go to another URL:  actions.redirect('thank_you.html');

      });

    }

  }).render('#paypal-button-container');

</script>

I need tax value in above : like :

Sub-total  $249
Tax        $24.9
Total      $273.9

I added tax field in above but its giving me below error message :

Error: /v2/checkout/orders returned status 422 (Corr ID: 86fc10bc7f8cc). {"name":"UNPROCESSABLE_ENTITY","details":[{"field":"/purchase_units/@reference_id=='default'/amount/breakdown/item_total/value","value":"273.9","issue":"ITEM_TOTAL_MISMATCH","description":"Should equal sum of (unit_amount * quantity) across all items for a given purchase_unit"}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"86fc10bc7f8cc","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-ITEM_TOTAL_MISMATCH","rel":"information_link","method":"GET"}]}


Solution

  • The breakdown object needs a tax_total. Together with the item_total it should sum to the value in amount.

    See the v2 Orders API reference for amount breakdown.

    {
      "purchase_units": [
        {
          "amount": {
            "currency_code": "AUD",
            "value": "273.9",
            "breakdown": {
              "item_total": {
                "currency_code": "AUD",
                "value": "249"
              },
              "tax_total": {
                "value": "24.9",
                "currency_code": "AUD"
              }
            }
          },
          "items": [
            {
              "name": "First Product Name",
              "description": "Optional descriptive text..",
              "unit_amount": {
                "currency_code": "AUD",
                "value": "249"
              },
              "tax": {
                "value": "24.9",
                "currency_code": "AUD"
              },
              "quantity": "1"
            }
          ]
        }
      ]
    }