PayPal account integration in Laravel: How to implement PayPal as a payment gateway in Laravel?
To implement PayPal as a payment gateway in Laravel, you can use the PayPal PHP SDK and the Laravel Cashier package. Here are the steps to set it up:
-
Install the required packages: First, you need to install the PayPal PHP SDK and Laravel Cashier package. You can install them using Composer.
To install the PayPal PHP SDK, run the following command in your terminal:
composer require paypal/rest-api-sdk
To install Laravel Cashier, add the following line to your
composer.json
file:"laravel/cashier": "^10.0"
Then, run the following command to install Laravel Cashier:
composer install
-
Configure PayPal: Next, you need to configure your PayPal account credentials. Create a new file
.env.paypal
in the.env
directory and add the following variables:PAYPAL_CLIENT_ID=your_client_id PAYPAL_SECRET=your_secret PAYPAL_MODE=sandbox
Replace
your_client_id
andyour_secret
with your PayPal client ID and secret. SetPAYPAL_MODE
tosandbox
for testing orlive
for production. -
Set up Laravel Cashier: Now, you need to set up Laravel Cashier to use PayPal as the payment gateway. First, create a new file
app/Providers/PayPalServiceProvider.php
and add the following code:namespace App\Providers; use Illuminate\Support\ServiceProvider; use Laravel\Cashier\Cashier; class PayPalServiceProvider extends ServiceProvider { public function boot() { Cashier::usePaymentGateway(function () { return new \PayPal\Rest\PayPal( new \PayPal\Auth\OAuthTokenCredential( env('PAYPAL_CLIENT_ID'), env('PAYPAL_SECRET') ) ); }); } }
Then, register the service provider in
config/app.php
:'providers' => [ // ... App\Providers\PayPalServiceProvider::class, ],
-
Create a PayPal subscription: To create a PayPal subscription, you can use Laravel Cashier's
createSubscription
method. Here's an example of how to create a monthly subscription for a user:$user = App\User::find(1); $user->newSubscription('default', $gateway) ->create($amount, $period);
Replace
App\User
with your user model anddefault
with the name of your subscription plan. Set$gateway
to the instance of Laravel Cashier'sGatewayManager
:use Laravel\Cashier\Billable; use Laravel\Cashier\Contracts\PaymentGateway; use Laravel\Cashier\GatewayManager; $gateway = new GatewayManager;
Set
$amount
to the price of your subscription and$period
to the subscription period (e.g.,'month'
for monthly subscriptions). -
Handle PayPal webhooks: To handle PayPal webhooks, you need to set up a route and controller to receive the webhook events. Here's an example of how to set it up:
Route::post('paypal/webhook', 'App\Http\Controllers\PayPalWebhookController@handleWebhook'); class PayPalWebhookController extends Controller { public function handleWebhook(Request $request) { $signature_verified = false; try { $credentials = new \PayPal\Auth\OAuthTokenCredential( env('PAYPAL_CLIENT_ID'), env('PAYPAL_SECRET') ); $config = new \PayPal\Rest\Config([ 'mode' => env('PAYPAL_MODE'), 'client_id' => $credentials->getClientId(), 'client_secret' => $credentials->getSecret(), ]); $webhook = new \PayPal\Webhook\Webhook($config); $webhook->setEventType(\PayPal\Webhook\WebhookEventType::PAYMENT); $webhook->setRawBody($request->getContent()); $webhook->setSignature($request->get('PayPal-Signature')); $webhook->setRequestId($request->get('PayPal-Request-Id')); $webhook->setSignatureAlgorithm($request->get('PayPal-Signature-Algorithm')); $webhook->setFingerprint($request->get('PayPal-Fingerprint')); $signature_verified = $webhook->verify(); if ($signature_verified) { // Handle the webhook event here } } catch (\Exception $e) { // Log the error here } if (!$signature_verified) { return response()->json(['error' => 'Webhook signature verification failed'], 401); } return response()->json(['message' => 'Webhook received']); } }
This example sets up a route for the PayPal webhook and a controller to handle the webhook events. The controller verifies the webhook signature and handles the event if it's valid.
That's it! You've now implemented PayPal as a payment gateway in Laravel using the PayPal PHP SDK and Laravel Cashier.