PHP code example of sudiptpa / paypal-rest

1. Go to this page and download the library: Download sudiptpa/paypal-rest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sudiptpa / paypal-rest example snippets


use Omnipay\Omnipay;

$gateway = Omnipay::create('PayPalRest_Rest');

$gateway->setClientId('xxxxxxxxxxx');
$gateway->setSecret('xxxxxxxxxxx');
$gateway->setTestMode('xxxxxxxxxxx');

$accessToken = $gateway->getToken();
 or
$accessToken = $gateway->createToken()->send();

$gateway->setToken($accessToken);

$payload = [
    'amount' => 20,
    'transactionId' => '1001',
    'transactionReference' => 'INV-1001',
    'currency' => 'USD',
    'items' => [
        [
            'name' => 'Test Product 1',
            'description' => 'A sample description',
            'quantity' => 1,
            'price' => 20,
            'sku' => 'ITEM-CODE1',
            'category' => 'PHYSICAL_GOODS',
            'reference' => 'ITEM',
        ]
    ],
    'cancelUrl' => 'https://example.com/cancel/url',
    'returnUrl' => 'https://example.com/return/url',
];

$response = $gateway->purchase($payload)->send();

if ($response && $response->isSuccessful()) {
    // handle the success

    if ($response->isRedirect()) {
        $response->redirect();
    }

    // do something else
}

// handle the failure

$response = $gateway->completePurchase([
    'transactionReference' => 'PAYPAL-ORDER-ID',
])->send();

if ($response && $response->isSuccessful() && $response->isCaptured()) {
    // handle success
}

// handle failure

$response = $gateway->fetchPurchase([
    'transactionReference' => 'PAYPAL-ORDER-ID',
])->send();

if ($response && $response->isSuccessful()) {
    // handle success
}

// handle failure