samedi 28 février 2015

paypal node.js API malformed request

I'm creating an array for the items field in the JSON doc required to create a paypal payment.


I can create the payment with dummy data, all looks good. However when I use real data via an iteration to create the object, after the .create request I get a MALFORMED_REQUEST error.


Code is:



var items = [];
for(i = 0; i < req.body.parts.length; i++)
{
part = req.body.parts[i];
items.push ({"name": part.part_display_name, "description": part.SKU, "sku": part.SKU, "price": part.price, "currency": "USD", "quantity": 1});
};
console.log (items);


Example output:



[ { name: 'Detective Handle Checker Grips',
description: 'vyctorslab_pistol_2015A_mowrer_handle_detective_checker_black-rubber-and-brass',
sku: 'vyctorslab_pistol_2015A_mowrer_handle_detective_checker_black-rubber-and-brass',
price: 50,
currency: 'USD',
quantity: 1 },
{ name: 'Detective Hammer',
description: 'vyctorslab_pistol_2015A_mowrer_hammer_detective_base_steel',
sku: 'vyctorslab_pistol_2015A_mowrer_hammer_detective_base_steel',
price: 50,
currency: 'USD',
quantity: 1 } ]


It certainly looks like the required JSON, however when creating the payment as follows:



var payment = {
"intent": "order",
"payer": {"payment_method": "paypal"},
"redirect_urls": {"return_url": return_url, "cancel_url": cancel_url},
"transactions": [
{"amount": req.body.total_price, "description": "Maykrlab model purchase.","item_list": items }
]
};


Output is:



{ intent: 'order',
payer: { payment_method: 'paypal' },
redirect_urls:
{ return_url: 'http://ift.tt/1E0AbEk',
cancel_url: 'http://ift.tt/186R2bT' },
transactions:
[ { amount: 500,
description: 'Maykrlab model purchase.',
item_list: [Object] } ] }


All still seems to be OK, the [Object] in the last line above is the items JSON created earlier.


However, the call to paypal is returned with:



{"response":{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link"
:"http://ift.tt/1eUIDbv","debug_id":"4f5997d207eca","httpStatusCode
":400},"httpStatusCode":400}


I believe I'm not correctly turning the items object created by the iteration into the correct format for posting to the paypal API. Can anyone advise? It's probably a case of needing to use .stringify in some place?


Aucun commentaire:

Enregistrer un commentaire