Paypal Smart Button Error: /v2/checkout/orders returned status: 422

tejaskap
Contributor
Contributor

I am creating a Ecommerce website and trying to intregrate Paypal. But sadly I keep getting this error whenever I press the paypal Express button. 

<script
    src="https://www.paypal.com/sdk/js?client-id={{ PAYPAL_CLIENT_ID }}">
</script>

<script>

	function getCookie(name) {
	    let cookieValue = null;
	    if (document.cookie && document.cookie !== '') {
	        const cookies = document.cookie.split(';');
	        for (let i = 0; i < cookies.length; i++) {
	            const cookie = cookies[i].trim();
	            // Does this cookie string begin with the name we want?
	            if (cookie.substring(0, name.length + 1) === (name + '=')) {
	                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                break;
	            }
	        }
	    }
	    return cookieValue;
	}
	const csrftoken = getCookie('csrftoken');

	function sendOrderConfirmed(details) {
		return fetch("{% url 'cart:confirmed-order' %}", {
			method: "post",
			body: JSON.stringify(details),
			headers: {
				"Content-type": "application/json",
				"X-CSRFTOKEN": csrftoken
			}
		})
	}

	paypal.Buttons({
        createOrder: function (data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: '{{ order.get_total }}'
                    }
                }]
            });
        },
	    onApprove: function(data, actions) {
	    	// This function captures the funds from the transaction.
	    	return actions.order.capture().then(function(details) {
	    		console.log(details);
	    		// This function shows a transaction success message to your buyer.
	    		sendOrderConfirmed().then(res => {
	    			setTimeout(function() {
	    				window.location.replace("{{ CALLBACK_URL }}")
	    			}, 3000);
	    		})	    		
	    	});
	    }
	}).render('#paypal-button-container');
	//This function displays Smart Payment Buttons on your web page.
</script>

 

Script code given above and error images added below. too.  There is no problem with the decimal format, I checked that already. Screenshot_1.png

Login to Me Too
1 REPLY 1

dke01
Contributor
Contributor

To view the exact error 

  1. Open your web brewers inspector / debug window (press F12)
  2. View the 'network' tab
  3. Reproduce the error - by clicking the checkout / paynow paypal button
  4. View the network traffic and click on the row that returns the HTTP 422 error
  5. View the Response 
    1. pay attention to the fields: 
      1. issue
      2. description

Untitled.png

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.