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.
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
The library follows PayPal REST Orders v2 API, and below are the supported features.
- Create Order
- Show Order Details
- Capture Payment for Order
- Add Tracking for an Order
- Verify Webhook Signature
- List Webhooks
- Delete Webhook
If you want the features other than mentioned above, then feel free to submit a PR by following the coding standard.
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();
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);
$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
For general usage instructions, please see the main Omnipay repository.
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.
The MIT License (MIT). Please see License File for more information.