obydul / larapal
LaraPal is a laravel plugin for processing payments through PayPal.
Installs: 3 454
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- laravel/framework: >=7.0
README
Introduction
By using this plugin you can process or refund payments from PayPal in your Laravel application.
PayPal API Credentials
This package uses the classic paypal express checkout. Refer to this link on how to create API credentials:
https://developer.paypal.com/docs/classic/api/apiCredentials/#create-an-api-signature
Installation
- Use following command to install:
composer require obydul/larapal
- Laravel 5.5 uses package auto-discovery, so doesn't require you to manually add the ServiceProvider. If you don't use auto-discovery, add the service provider to your
$providers
array inconfig/app.php
file like:
Obydul\LaraPal\LarapalServiceProvider::class
- Run the following command to publish configuration:
php artisan vendor:publish --provider="Obydul\LaraPal\LarapalServiceProvider"
Installation completed.
Configuration
- After installation, you need to set paypal credentialsin .env file.
LARAPAL_MODE=sandbox # sandbox or live LARAPAL_API_USERNAME= # paypal api username LARAPAL_API_PASSWORD= # paypal api password LARAPAL_API_SIGNATURE= # paypal api signature
- Now optimize the app:
php artisan optimize && php artisan config:clear
.
Usage
Following are some ways through which you can access the LaraPal provider:
// Import the class namespaces first, before using it directly use Obydul\LaraPal\Services\ExpressCheckout; // Create object instance $paypal = new ExpressCheckout(); // Redirect user to PayPal to obtain charging permissions $paypal->doExpressCheckout(123.45, 'LaraPal Test Checkout', 'invoice_id', 'USD'); // single payment $paypal->doExpressMultipleCheckout($items, 'invoice_id', 'USD', false, $customFields); // multiple items // Perform payment, token and PayerID are being returned with GET response from PayPal $paypal->doSinglePayment($_GET['token'], $_GET['PayerID']; // single payment $paypal->doMultiplePayment($_GET['token'], $_GET['PayerID']; // multiple payment // Perform refund based on transaction ID $paypal->doRefund($transactionId, 'invoice_id', false, 0, 'USD', ''); // full refund $paypal->doRefund($transactionId, 'invoice_id', true, 5.15, 'USD', ''); // partial refund // Get transaction details $details = $paypal->getTransactionDetails($transactionId);
doExpressCheckout
// Structure - invoice ID must be unique doExpressCheckout(AMOUNT, 'DESCRIPTION', 'INVOICE', 'CURRENCY', SHIPPING, CUSTOMFIELDS); doExpressMultipleCheckout(ITEMS, 'INVOICE', 'CURRENCY', SHIPPING, CUSTOMFIELDS); // Normal call doExpressCheckout(123.45, 'LaraPal Test Checkout', 'invoice_id', 'USD'); // Pass custom fields to your order $customFields = array( 'identifier' => "Example.com/ID", 'customerEmail' => "customer@email.com", ); // Now do the express checkout. If you don't like to pass custom fields, then remove $customFields. $paypal->doExpressCheckout(123.45, 'LaraPal Test Checkout', 'invoice_id', 'USD', false, $customFields); // multiple items payment $items = array( array( "name" => "Product 1", "price" => "12.25", "quantity" => "1", "product_id" => 111 ), array( "name" => "Product 2", "price" => "25.50", "quantity" => "1", "product_id" => 112 ) ); $paypal->doExpressMultipleCheckout($items, $invoice_id, 'USD', false, $customFields);
doRefund
// Structure doExpressCheckout(TRANSACTION_ID, 'INVOICE_ID', 'IS_PARTIAL', PARTIAL_AMOUNT, CURRENCY, NOTE); // Full refund doRefund($transactionId, 'invoice_id', false, 0, 'USD', '') // Partial refund doRefund($transactionId, 'invoice_id', true, 12.25, 'USD', '') // you can pass note also
Example
After installing LaraPal, create routes and create a controller named 'PayPalController':
// Routes Route::get('payment-status', 'PayPalController@paymentStatus')->name('payment-status'); Route::get('single-payment', 'PayPalController@singlePayment'); Route::get('multiple-payment', 'PayPalController@multipleItemsPayment'); Route::get('do-the-payment', 'PayPalController@doThePayment'); Route::get('refund-payment', 'PayPalController@doRefund'); Route::get('cancel-payment', 'PayPalController@cancelPayment'); // Controller php artisan make:controller PayPalController
Now in your PayPalController add this: PayPalController.php
Now just visit the URL to make an action:
http://example.com/single-payment http://example.com/multiple-payment http://example.com/refund-payment
After successful payment, you will be redirected to http://example.com/payment-status
and will see a message like: Success! Transaction ID: 9TR987531T2702301
.
License
The MIT License (MIT). Please see License File for more information.
Others
Inspired by paypal-express-checkout and thank you, romaonthego.
In case of any issues, kindly create one on the Issues section.
Thank you for installing LaraPal.