php - sending Paypal payment call returning 400 malformed request error -
i trying create paypal payment using php. keep getting 400 malformed request error. believe problem following string:
$postdata =' { "intent": "sale" "redirect_urls": { "return_url": ' . $url_success .', "cancel_url": ' . $url_cancel .' }, "payer": { "payment_method": "paypal" }, "transactions": [ { "amount": { "total": "' . $saletotal .'", "currency": "usd" }, "description": "test payment." } ] } ';
i sending string using curl in following line
curl_setopt($ch, curlopt_postfields, $postdata);
here string printed out:
{ "intent": "sale" "redirect_urls": { "return_url": "http://localhost/~user/test/controller/paypaltestview.php", "cancel_url": "http://localhost/~user/test/controller/canceltestview.php" }, "payer": { "payment_method": "paypal" }, "transactions": [ { "amount": { "total": "8.31782", "currency": "usd" }, "description": "test payment." } ] }
here response get:
{"name":"malformed_request","message":"the request json not formed.","information_link": "https://developer.paypal.com/webapps/developer/docs/api/#malformed_request", "debug_id":"7d56ae815e584"}
i'm pretty sure problem in $postdata, although have no idea what's wrong. tried following example: paypal rest api bad request, still isn't working. problem similar example in sending , receiving authentication token successfully. problem when trying send payment api call. see i'm doing wrong? thank you!
i got same problem before. did put data first in array:
$data = array( "intent"=>"sale", "redirect_urls"=>array( "return_url"=>"<return site>", "cancel_url"=>"<cancel site>" ), "payer"=>array( "payment_method"=>"paypal" ), "transactions"=>array( array( "amount"=>array( "total"=>"7.47", "currency"=>"usd" ), "description"=>"this payment transaction description." ) ) );
and http header:
$header = array( "content-type:application/json", "authorization:bearer <token here>"
);
then on postfield, encode data using json_encode:
curl_setopt($ch, curlopt_postfields, json_encode($data));
i hope help.
Comments
Post a Comment