PayPal REST API through PHP SDK return “Incoming J

2019-03-04 13:57发布

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); 
}

2条回答
你好瞎i
2楼-- · 2019-03-04 14:41

Am not a PHP dev (.Net), so based on reading the above, check into this section in your code:

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

You are sending a new PaymentExecution without the payer_id and the Payment.Id which you would obtain after the user approves your paypal Payment request that you created.

That said, I don't see that part (though as above, not a PHP dev) so I could be wrong. The steps are:

  1. Create the Payment

  2. Go into the Approval Flow -> user is redirected to Paypal and approves the Payment you created in #1 -> and is redirected back to your site (the returnUrl)

    a. the PayerID will be in the querystring in this process

    b. the paymentId will also be in the querystring in this process

  3. After the user is redirected back to your site from PayPal (your returnUrl) - Execute a new Payment, set it's id to the one you obtained (#2b), sending the PaymentExecution with its PayerID set to the what you you obtained (#2a)

In c# (you'll need to convert this to PHP):

    var _payment = new Payment { id = the_payment_id_you_obtained };
    var _payExec = new PaymentExecution { payer_id = the_payer_id_you_obtained };

    var _response = _payment.Execute(context, _payExec);

Hth...

查看更多
仙女界的扛把子
3楼-- · 2019-03-04 14:48

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.

查看更多
登录 后发表回答