sudiptpa/paypal-rest

PayPal REST API gateway for Omnipay payment processing library.

v3.1 2023-10-20 02:54 UTC

This package is auto-updated.

Last update: 2024-11-20 05:14:15 UTC


README

PayPal REST API driver for the Omnipay PHP payment processing library

Omnipay is a framework agnostic, multi-gateway payment processing library for PHP. This package implements PayPal REST API support for Omnipay.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Installation

Omnipay is installed via Composer. To install, simply add it to your composer.json file:

{
    "require": {
        "sudiptpa/paypal-rest": "~3.0"
    }
}

And run composer to update your dependencies:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update

Usage

The library follows PayPal REST Orders v2 API, and below are the supported features.

Orders API v2

If you want the features other than mentioned above, then feel free to submit a PR by following the coding standard.

Initiate Gateway Request

use Omnipay\Omnipay;

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

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

Access Token

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

Note, the Access Token is not stored in the gateway at this point.

Management of the Access Token is not (yet) included in this library. You should implement your own method for saving and reusing the Access Token until expired to avoid hitting PayPal query limits by generating a token for each API call.

You can set a previously retrieved Access Token in the gateway as follows:

$gateway->setToken($accessToken);

API Calls

$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

Capture

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

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

// handle failure

Fetch PayPal Order

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

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

// handle failure

For general usage instructions, please see the main Omnipay repository.

Support

If you are having general issues with Omnipay, we suggest posting on Stack Overflow. Be sure to add the omnipay tag so it can be easily found.

If you want to keep up to date with release anouncements, discuss ideas for the project, or ask more detailed questions, there is also a mailing list which you can subscribe to.

If you believe you have found a bug, please report it using the GitHub issue tracker, or better yet, fork the library and submit a pull request.

License

The MIT License (MIT). Please see License File for more information.