Skip to content
Advertisement

PayPal Orders v2 create returning 422 (Unprocessable Entity) when passing amount discount

I’m using the PayPal REST SDK client side JavaScript to create an order and I have the below code:

    paypal.Buttons({
    style: {
        size: 'responsive',
        layout: 'vertical'
    },
    createOrder: function(data, actions) {
        // This function sets up the details of the transaction, including the amount and line item details.
        return actions.order.create({
            intent: 'CAPTURE',
            purchase_units: [{
                amount: {
                    currency_code: 'AUD',
                    value: '70.00',
                    breakdown: {
                        discount: {
                            currency_code: 'AUD',
                            value: '5.00'
                        }
                    }
                },
                description: 'xxxxxxxxxxx'
                // custom_id: '',
            }]
        });
    },
    onApprove: function(data, actions) {
        // This function captures the funds from the transaction.
        return actions.order.capture().then(function(details) {
            // This function shows a transaction success message to your buyer.
            alert('Transaction completed by ' + details.payer.name.given_name);
        });
    }
}).render('.purchase-modal');

}

This returns the aforementioned error and status code 422 (Unprocessable Entity).

This chunk of the code appears to be the problem, if it is removed the error goes away:

breakdown: {
    discount: {
        currency_code: 'AUD',
        value: '5.00'
    }
}

I checked and this appears to be valid?

Am I doing something wrong here?

Advertisement

Answer

It helps to read the whole JSON response error message from the JavaScript console or browser Network tab.

{
   "name":"UNPROCESSABLE_ENTITY",
   "details":[
      {
         "field":"/purchase_units/0/amount/value",
         "value":"70.00",
         "issue":"AMOUNT_MISMATCH",
         "description":"Should equal item_total + tax_total + shipping + handling + insurance - shipping_discount - discount."
      }
   ],
   "message":"The requested action could not be performed, semantically incorrect, or failed business validation.",
   "debug_id":"3c22979dc723a",
   "links":[
      {
         "href":"https://developer.paypal.com/docs/api/orders/v2/#error-AMOUNT_MISMATCH",
         "rel":"information_link",
         "method":"GET"
      }
   ]
}

Note particularly the message in “description”.

So, there you have it. If the specified total value is 70 and the discount is 5, you need to tell PayPal what the things were that added up to be 75(!) before such a “discount” was applied. Otherwise this “discount” value is meaningless by itself, PayPal doesn’t know what to do with it, and the 422 Unprocessable error ensues to remove any ambiguity about the situation.


Side note…

You aren’t using the “REST SDK” here, since that’s specific to the server side. Client side code is the JavaScript SDK. If you do want to use the compatible REST APIs for a server-side integration (recommended), implement two routes on your server, one for “Set Up Transaction” and one for “Capture Transaction”, documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/

Then change your JavaScript code to the server demo’s: https://developer.paypal.com/demo/checkout/#/pattern/server , linked up to those two routes of yours

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement