php - Sandbox PayPal approval link leads to dashboard instead of payment after logging in

770

I am using PayPal REST API in sandbox mode to test payments.

I create the approval link, then redirect user to said link.

PayPal then asks for credentials which I insert as a sandbox test buyer. But after logging in, PayPal does not redirect me to complete the approval of payment, and instead straight to My Account summary from where I cannot approve the payment. However if the user was already logged in, the payment approval proceeds as expected.

Is it the problem with how was the link created or is it a bug?

This is the code used to create the link

<?php
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;

class PaypalFactory
{
private const CLIENT_ID = 'yyy';
private const CLIENT_SECRET = 'xxx';

private function __construct()
{
}

public static final function getContext()
{
    return new ApiContext(
        new OAuthTokenCredential(
            self::CLIENT_ID,
            self::CLIENT_SECRET
        )
    );
}

public static final function createPaymentLink(
    int $orderId,
    string $currency,
    float $totalPrice,
    float $deliveryPrice,
    string $returnUrl,
    string $cancelUrl): string
{
    $payer = new Payer();
    $payer
        ->setPaymentMethod("paypal");

    $details = new Details();
    $details
        ->setShipping($deliveryPrice)
        ->setSubtotal($totalPrice);


    $item = new Item();
    $item
        ->setName('products and shipping')
        ->setCurrency($currency)
        ->setPrice($totalPrice)
        ->setQuantity(1);

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


    $amount = new Amount();
    $amount
        ->setCurrency($currency)
        ->setTotal($totalPrice + $deliveryPrice)
        ->setDetails($details);

    $transaction = new Transaction();
    $transaction
        ->setItemList($itemList)
        ->setAmount($amount)
        ->setDescription("Products from ")
        ->setInvoiceNumber($orderId);

    $redirectUrls = new RedirectUrls();
    $redirectUrls
        ->setReturnUrl($returnUrl)
        ->setCancelUrl($cancelUrl);

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

    try {
        $payment->create(PaypalFactory::getContext());
    } catch (Exception $ex) {
        print $ex;
    }

    return $payment->getApprovalLink();
}

}
15

Answer

Solution:

Purging all browser cookies for paypal domains is good advice, and may help. The problem you're experiencing is probably a general sandbox issue (that won't happen in live mode), and not a problem with your code.

However, seeing your code/solution, I do have suggestions for trying some newer/better things:

  1. Instead of the old PayPal-PHP-SDK for v1/payments, use the new Checkout-PHP-SDK for v2/orders
  2. Instead of redirecting the buyer to the approval URL, give the approval token to the Javascript code of Smart Payment Buttons, which will display an in-context window that keeps your site loaded in the background. Here's a demo pattern

People are also looking for solutions to the problem: php - FacadeIgnitionExceptionsViewException?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.