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-sdkTo install Laravel Cashier, add the following line to your
composer.jsonfile:"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.paypalin the.envdirectory and add the following variables:PAYPAL_CLIENT_ID=your_client_id PAYPAL_SECRET=your_secret PAYPAL_MODE=sandboxReplace
your_client_idandyour_secretwith your PayPal client ID and secret. SetPAYPAL_MODEtosandboxfor testing orlivefor 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.phpand 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
createSubscriptionmethod. 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\Userwith your user model anddefaultwith the name of your subscription plan. Set$gatewayto the instance of Laravel Cashier'sGatewayManager:use Laravel\Cashier\Billable; use Laravel\Cashier\Contracts\PaymentGateway; use Laravel\Cashier\GatewayManager; $gateway = new GatewayManager;Set
$amountto the price of your subscription and$periodto 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.