Search code examples
phppaypalpaypal-rest-sdk

How to get the approvalURL for PayPal Plus integration?


I followed the official instructions: https://www.paypalobjects.com/digitalassets/c/website/marketing/emea/de/de/paypal-plus-center/PayPal_PLUS_integration_guide.pdf

08. Integrating PayPal PLUS

Prior to rendering the payment wall a payment resource must be created. The corresponding API call is “create payment”. When creating the payment resource am ount, currency and items details must be submitted.

..Okay. Understand. So i need to Create a Payment, grab the Url and then i can render the PayPalPlus-Object finally.

I followed https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payments/CreatePaymentUsingPayPal.php as example script and one ohter. But i cant receive anything from $payment->getApprovalLink()

am i completely wrong?

which example can i use to create a payment and render the paypal-plus thing?


Solution

  • <?php
    require '../vendor/autoload.php';
    $apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
    'xxx',     // ClientID
    'xxx'      // ClientSecret
    )
    );
    
    $apiContext->setConfig(
    array(
    'mode' => 'sandbox'
    )
    );
    
    $item1 = new \PayPal\Api\Item();
    $item1->setName('Test item')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setPrice(1);
    
    $itemList = new \PayPal\Api\ItemList();
    $itemList->setItems(array($item1));
    
    $payer = new \PayPal\Api\Payer();
    $payer->setPaymentMethod('paypal');
    
    $amount = new \PayPal\Api\Amount();
    $amount->setCurrency('USD');
    $amount->setTotal('1');
    
    $transaction = new \PayPal\Api\Transaction();
    $transaction->setAmount($amount);
    $transaction->setItemList($itemList);
    //$transaction->setInvoiceNumber(uniqid());
    //$transaction->setDescription('Description');
    
    $redirectUrl = new \PayPal\Api\RedirectUrls();
    $redirectUrl->setReturnUrl('http://localhost');
    $redirectUrl->setCancelUrl('http://localhost');
    
    $payment = new \PayPal\Api\Payment();
    $payment->setIntent('sale');
    $payment->setPayer($payer);
    $payment->setRedirectUrls($redirectUrl);
    $payment->setTransactions(array($transaction));
    
    $payment = $payment->create($apiContext);
    
    $approvalUrl = $payment->getApprovalLink();
    
    echo $approvalUrl;