br1tas br1tas - 3 years ago 147
reST (reStructuredText) Question

PayPal Incoming JSON request does not map to API

I'm trying to create payment with PayPal and I get errors.

Errors:


400{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"f79dc9a739991"}exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:202 Stack trace: #0 /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php(73): PayPal\Core\PayPalHttpConnection->execute('{"intent":"sale...') #1 /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(104): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL) #2 /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php(577): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"intent":"sale...', NULL, Object(PayPal\Rest\ApiContext), NULL) #3 /home/nork/domains/nork.lt/public_html/cms/xxx/checkout.php(73): PayPal\Api\Payment->create(Object(PayPal\Rest\ApiContext)) #4 {main}


Code:

<?php

use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;

if(isset($_GET['type']) && isset($_GET['amount']) && isset($_GET['gold']))
{
if($_GET['type'] == 'paypal') // If payment is PayPal
{
require 'system/payments/paypal.php';

$gold = $_GET['gold'];
$amount = $_GET['amount'];
$pricePerVnt = 0.00;
$productName = '';
$shipping = 0.00;

switch($gold)
{
case 'first':
{
$productName = 'first name';
$pricePerVnt = 1.19;
break;
}
case 'second':
{
$productName = 'second name';
$pricePerVnt = 0.21;
break;
}
}
if(!$pricePerVnt) die();

$totalAmount = $amount * $pricePerVnt;

$payer = new Payer();
$payer->setPaymentMethod('paypal');

$item = new Item();
$item->setName($productName)->setCurrency('USD')->setQuantity('1')->setPrice($pricePerVnt);

$itemList = new ItemList();
$itemList->setItems($item);

$amount = new Amount();
$amount->setCurrency('USD')->setTotal($totalAmount);

$transaction = new Transaction();
$transaction->setAmount($pricePerVnt)->setDescription($productName)->setInvoiceNumber(uniqid());

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('http://google.lt')->setCancelUrl('http://cancel.lt');

$payment = new Payment();
$payment->setIntent('sale')->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions($transaction);

try {
$payment->create($paypal);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode(); // Prints the Error Code
echo $ex->getData(); // Prints the detailed error message
die($ex);
} catch (Exception $ex) {
die($ex);
}
//print_r($payment);

//echo $approvalUrl = $payment->getApprovalLink();
//header('Location: {$approvalUrl}');
}
}

?>

Answer Source

The answer appears to be that you are not sending sufficient request values:

{"intent":"sale...', NULL)

Note the null. In order to see your error dump in more details, you can do tweak the var_dump complexity:

ini_set('xdebug.var_display_max_depth', 10);
ini_set('xdebug.var_display_max_children', 256);
ini_set('xdebug.var_display_max_data', 1024);

Then you will see the full JSON and can figure out what was missing or malformed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download