Nagi Nagi - 8 months ago 53
JSON Question

PHP Paypal REST create payment malfromed JSON response

Sorry about this (last resort, short of shooting myself in the head ;-); it's obviously my fault somewhere but I've spent days searching here and elsewhere, reading docs, trying various things etc. But it still won't work.

Some one please point me what's wrong here:

<?php

$data = array(
"intent" => "sale",
"redirect_urls" => array(
"return_url" => "http://.../ok.html",
"cancel_url" => "http://.../cancel.html"
),
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(
"amount" => array(
"total" => "1.23",
"currency" => "GBP"
)
)
);

$ch = curl_init("https://api.sandbox.paypal.com/v1/payments/payment");

curl_setopt($ch, CURLOPT_VERBOSE, true);
$logfile = fopen("curl_log.txt", "w");
curl_setopt($ch, CURLOPT_STDERR, $logfile);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer <long token returned from REST call',
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$res = curl_exec($ch);

curl_close($ch);
fclose($logfile);

?>


Error:

{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"developer.paypal.com/webapps/de‌​veloper/docs/api/…;}

Answer Source

update your $data array like,

$data = array(
    "intent" => "sale",
    "payer" => array(
            "payment_method" => "paypal"
        ),
    "transactions" =>array( 
            array(
                "amount" => array(
                        "total" => "1.23",
                        "currency" => "GBP")
            )
        ),
    "redirect_urls" => array(
            "return_url" => "http://.../ok.html",
            "cancel_url" => "http://.../cancel.html"
     )
);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download