Whenever I try to process a payment I receive a 422 error: Unprocessable entity
.
The problem occurs when I dynamically want to capture the purchased item details that I receive from the redux store.
I tried to follow this format (duplicate): PayPal Checkout (javascript) with Smart Payment Buttons create order problem
Which is the same as in the PayPal docs: https://developer.paypal.com/docs/api/orders/v2/#definition-item
However, this does not help. Here's my code
import React, {useState, useEffect, useRef} from 'react';
import {useSelector, useDispatch} from 'react-redux'
const mapState = ({ cart }) => ({
itemsInCart: cart.itemsInCart,
total: cart.total
})
const Cart = props => {
const {itemsInCart} = useSelector(mapState);
useEffect(() => {
const itemsObj = itemsInCart.map(item => {
return {
name: item.name,
description: item.description,
unit_amount: {
currency_code: "USD",
value: item.price
},
quantity: "1"
}
})
window.paypal
.Buttons({
createOrder: (data, actions) => {
return actions.order.create({
purchase_units: [
{
description: "Your purchase details",
amount: {
currency_code: 'USD',
value: document.getElementById('totalAmount').innerHTML
},
items: itemsObj
},
],
});
}
If I change the items
property to item_list
as I have seen in some documentations, the payment goes through but the items object is not captured. I can't figure out why.