I am trying to call PayPal's CreatePayment API (REST API) function but getting this error:
[name] => MALFORMED_REQUEST
[message] => Incoming JSON request does not map to API request
API URL: https://api.sandbox.paypal.com/v1/payments/payment
Headers - PHP Array:
(
[0] => Content-Type: application/json
[1] => Authorization: Bearer A21AAH....SQ
)
json data (I verified it's valid json):
{
"intent":"sale",
"redirect_urls":"{\"return_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/return_placeholder.php\", \"cancel_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/cancel_placeholder.php\"}",
"payer":"{\"payment_method\": \"paypal\"}",
"transactions":"[ {\"amount\":{ \"total\": \"1.00\", \"currency\": \"USD\"},\"item_list\":{ \"items\": [ {\"quantity\": \"1\",\"name\": \"TestAppRain Ticket\",\"price\": \"1\",\"currency\": \"USD\",\"description\": \"TestAppRain Ticket\",\"tax\": \"0\" } ]},\"description\": \"TestAppRain Ticket\",\"invoice_number\": \"123456\",\"custom\": \"ENCRYPTED_USERID_HERE\" }]"
}
I'm unclear how to hand the array to the API, but I believe Paypal wants the data in json format. Here is what I'm doing:
function TalkToPayPal($paypalURL, $data, $headers){
global $ppClientID, $ppSecret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalURL);
if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}else{
curl_setopt($ch, CURLOPT_HEADER, false);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $ppClientID . ":" . $ppSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
if(empty($result)){
return null;
}
else
{
$jsonObject = json_decode($result);
if(json_last_error() == JSON_ERROR_NONE){
return $jsonObject;
}else{
return null;
}
}
}
function CreatePayPalPayment($data, $headers){
global $ppDNS;
$ppURL = "https://$ppDNS/v1/payments/payment";
$jsonObj = TalkToPayPal($ppURL, $data, $headers);
if($jsonObj){
$errorString = "";
if($jsonObj->error){
$errorString .= $jsonObj->error . " | ";
}
if($jsonObj->error_description){
$errorString .= $jsonObj->error_description;
}
if($errorString){
return "ERROR: " . $errorString;
}else{
return $jsonObj;
}
}else{
return "ERROR: PayPal call to Create Payment failed.";
}
}
$response = CreatePayPalPayment($json, $headers);
What am I doing wrong?
Yours is not a valid JSON because you are escaping the "
for some reason:
{
"intent":"sale",
"redirect_urls":"{\"return_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/return_placeholder.php\", \"cancel_url\": \"http:\/\/localhost\/ProgrammingProjects\/TestAppRain\/web\/cancel_placeholder.php\"}",
"payer":"{\"payment_method\": \"paypal\"}",
"transactions":"[ {\"amount\":{ \"total\": \"1.00\", \"currency\": \"USD\"},\"item_list\":{ \"items\": [ {\"quantity\": \"1\",\"name\": \"TestAppRain Ticket\",\"price\": \"1\",\"currency\": \"USD\",\"description\": \"TestAppRain Ticket\",\"tax\": \"0\" } ]},\"description\": \"TestAppRain Ticket\",\"invoice_number\": \"123456\",\"custom\": \"ENCRYPTED_USERID_HERE\" }]"
}
remove the escapes and it should be fine
this is a valid JSON (taken from https://developer.paypal.com/docs/api/overview/)
"intent": "sale",
"redirect_urls": {
"return_url": "https://example.com/your_redirect_url.html",
"cancel_url": "https://example.com/your_cancel_url.html"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [{
"amount": {
"total": "7.47",
"currency": "USD"
}
}]
Here the JSON you should use:
{
"intent": "sale",
"redirect_urls": {
"return_url": "http://localhost/ProgrammingProjects/TestAppRain/web/return_placeholder.php",
"cancel_url": "http://localhost/ProgrammingProjects/TestAppRain/web/cancel_placeholder.php"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "1.00",
"currency": "USD"
},
"item_list": {
"items": [
{
"quantity": "1",
"name": "TestAppRainTicket",
"price": "1",
"currency": "USD",
"description": "TestAppRainTicket",
"tax": "0"
}
]
},
"description": "TestAppRainTicket",
"invoice_number": "123456",
"custom": "ENCRYPTED_USERID_HERE"
}
]
}