This is the error I get
name: 'VALIDATION_ERROR',
details: [
{
field: 'purchase_units[0]',
issue: 'Item amount must add up to specified amount subtotal (or total if amount details not specified)'
}
],
message: 'Invalid request - see details',
information_link: 'https://developer.paypal.com/docs/api/payments/#errors',
debug_id: '74ac8660674d8',
httpStatusCode: 400
This is my /pay post route
app.post('/pay' , (req , res) => {
let cart = new Cart(req.session.cart)
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/failed"
},
"transactions": [{
"item_list": {
"items": getItems(cart.items)
},
"amount": {
"currency": "USD",
"total": cart.totalPrice
},
"description": "This is the payment description."
}]
};
This is the getItems function
function getItems(cart) {
let itemsArray = [];
for (const [idx, item] of Object.entries(cart)) {
itemsArray.push({
"name": item.item.product_name,
"price": item.price,
"currency": "USD",
"quantity": item.qty
});
}
return itemsArray;
}
When I console.log the cart.totalPrice it is correct as it should be (Which is the total of all the items in the cart) Any idea what is going wrong here. I am a bit new to all this stuff so a bit confused
For the current (non-deprecated) APIs, see examples in Set up standard payments
Make 2 routes on your server, one for 'Create Order' and one for 'Capture Order', documented here. Both routes should return only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you should store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id
, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller.
Pair those 2 routes with the current JS SDK frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server