Roilld Roilld - 1 month ago 22
PHP Question

PayPal REST API through PHP SDK return "Incoming JSON request does not map to API request"

I'm trying to create and execute a payment with PayPal REST API through PHP SDK (sandbox environment) like showing below. The payment creation (

$payment->create
) work fine but the payment execution (
$payment->execute
) return
"Incoming JSON request does not map to API request"
.
The JSON request is created by the SDK, then what can be the problem?
Thanks in advance.

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

$item = new Item();
$item->setName('Any name')
->setCurrency('EUR')
->setQuantity(1)
->setPrice(0.99);

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

$details = new Details();
$details->setTax(0)
->setSubtotal(0.99);

$amount = new Amount();
$amount->setCurrency('EUR')
->setTotal(0.99)
->setDetails($details);

$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Any description')
->setInvoiceNumber(uniqid());

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(BASE_URL.'/payment/?success=true')
->setCancelUrl(BASE_URL.'/payment/?success=false');

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

try {
$payment->create($apiContext);

$execution = new PaymentExecution();
$result = $payment->execute($execution, $apiContext);

} catch (Exception $ex) {
//Function for extract the error message,
//the error message can be showing with
//a simple var_dump($ex)
$exception = self::getException($ex);
}



Answer

It was enough put

$payment->setIntent('authorize')

instead of

$payment->setIntent('sale')

and eliminate the execution

$execution = new PaymentExecution(); $result = $payment->execute($execution, $apiContext);

Then, after

$payment->create

I used the "href" from

$payment->links

to do the redirect. Everything went perfectly. Thank you all.

Comments