PayPal first redirect crash, second one redirect the user successfully

130 views Asked by At

these days, I implemented the PayPal-Account-Payment Payflow into my checkout. At this point everything works perfect. But sometimes I get an error when I try to redirect my customers to complete the order on paypal.com. When I refresh the page and submit all data again it works.

Here is my current PHP-Code:

$sdkConfig = array(
    "mode" => "LIVE"
);

$cred = new \PayPal\Auth\OAuthTokenCredential(<**>, <**>, $sdkConfig);

$apiContext = new \PayPal\Rest\ApiContext($cred, 'Request'.time());
$apiContext->setConfig($sdkConfig);

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

$amount = new \PayPal\Api\Amount();
$amount->setCurrency("EUR");
$amount->setTotal($this->total);

$transaction = new \PayPal\Api\Transaction();
$transaction->setDescription("Complete your order.");
$transaction->setAmount($this->amount);
$transaction->setItemList($this->itemList);

$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://example.com/order/success/");
$redirectUrls->setCancelUrl("https://example.com/order/cancel/");

$payment = new \PayPal\Api\Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));

$payment->create($apiContext);

$redirectUrl = $payment->links[1]["href"];

header("Location: ".$redirectUrl);

The error I receive sometimes at the first call: Fatal error: Cannot use object of type PayPal\Api\Links as array in /htdocs/lib/Checkout.php on line 503

1

There are 1 answers

0
Hauke On

The paypal sample code does something different to get the url form the payment object:

$payment->create($apiContext);
$approvalUrl = $payment->getApprovalLink();

Guess your error has something todo with your direct access to payment->props.