Sure, let’s focus on setting up PayPal Sandbox integration in your Laravel application using the srmklive/paypal
package. This guide will help you configure PayPal Sandbox and handle payments through it.
Step-by-Step Guide to Add PayPal Sandbox Integration to Laravel
Step 1: Install the PayPal Package
- Require the Package:
- Run the following command to install the
srmklive/paypal
packagecomposer require srmklive/paypal
- Publish the Configuration:
- Publish the configuration file using the artisan command
php artisan vendor:publish --provider="Srmklive\PayPal\Providers\PayPalServiceProvider"
- Publish the configuration file using the artisan command
Step 2: Configure PayPal
- Set Up PayPal Sandbox API Credentials:
- Go to the PayPal Developer Dashboard and log in.
- Create a new app under the “Sandbox” environment to get your Client ID and Secret.
- Add these credentials to your
.env
PAYPAL_CLIENT_ID=your_paypal_sandbox_client_id
PAYPAL_SECRET=your_paypal_sandbox_secret PAYPAL_MODE=sandbox
- Configure PayPal in
config/paypal.php
:- Ensure your configuration file reflects the following settings
return [ 'mode' => env('PAYPAL_MODE', 'sandbox'), 'sandbox' => [ 'client_id' => env('PAYPAL_CLIENT_ID'), 'client_secret' => env('PAYPAL_SECRET'), 'app_id' => '', ], 'live' => [ 'client_id' => '', 'client_secret' => '', 'app_id' => '', ], 'payment_action' => 'Sale', 'currency' => 'USD', 'notify_url' => '', 'locale' => '', 'validate_ssl' => true, ];
- Ensure your configuration file reflects the following settings
Step 3: Create Payment Routes and Controller
- Define Routes:
- Add the following routes to your
routes/web.php
use App\Http\Controllers\PayPalController; Route::get('paypal/checkout', [PayPalController::class, 'checkout'])->name('paypal.checkout'); Route::get('paypal/success', [PayPalController::class, 'success'])->name('paypal.success'); Route::get('paypal/cancel', [PayPalController::class, 'cancel'])->name('paypal.cancel');
- Add the following routes to your
- Create PayPal Controller:
- Generate a new controller
php artisan make:controller PayPalController
- Generate a new controller
- Implement Payment Methods in PayPalController:
- Update your
PayPalController
with the following codenamespace App\Http\Controllers; use Illuminate\Http\Request; use Srmklive\PayPal\Services\PayPal as PayPalClient; class PayPalController extends Controller { public function checkout() { $paypal = new PayPalClient; $paypal->setApiCredentials(config('paypal')); $token = $paypal->getAccessToken(); $paypal->setAccessToken($token); $response = $paypal->createOrder([ "intent" => "CAPTURE", "purchase_units" => [ [ "amount" => [ "currency_code" => "USD", "value" => "100.00" ] ] ], "application_context" => [ "return_url" => route('paypal.success'), "cancel_url" => route('paypal.cancel'), ] ]); if (isset($response['id']) && $response['id'] != null) { foreach ($response['links'] as $link) { if ($link['rel'] === 'approve') { return redirect()->away($link['href']); } } return redirect()->route('paypal.cancel')->with('error', 'Something went wrong.'); } else { return redirect()->route('paypal.cancel')->with('error', $response['message'] ?? 'Something went wrong.'); } } public function success(Request $request) { $paypal = new PayPalClient; $paypal->setApiCredentials(config('paypal')); $token = $paypal->getAccessToken(); $paypal->setAccessToken($token); $response = $paypal->capturePaymentOrder($request['token']); if (isset($response['status']) && $response['status'] == 'COMPLETED') { return redirect()->route('home')->with('success', 'Transaction complete.'); } else { return redirect()->route('home')->with('error', $response['message'] ?? 'Something went wrong.'); } } public function cancel() { return redirect()->route('home')->with('error', 'You have canceled the transaction.'); } }
- Update your
Step 4: Update Your Views
- Create a Checkout Button:
- Add a button in your view (e.g.,
resources/views/welcome.blade.php
) to initiate the PayPal checkout process:<a href="{{ route('paypal.checkout') }}" class="btn btn-primary">Pay with PayPal</a>
- Add a button in your view (e.g.,
- Display Success and Error Messages:
- Ensure your views can display session messages for success and error notifications. For example, in
resources/views/layouts/app.blade.php
@if (session('success')) <div class="alert alert-success"> {{ session('success') }} </div> @endif @if (session('error')) <div class="alert alert-danger"> {{ session('error') }} </div> @endif
- Ensure your views can display session messages for success and error notifications. For example, in
Testing Your Integration
- Use PayPal Sandbox Accounts:
- Use your PayPal sandbox account to test the payment process. Ensure you’re using sandbox credentials (
sandbox
mode) in your.env
file.
- Use your PayPal sandbox account to test the payment process. Ensure you’re using sandbox credentials (
By following these steps, you can integrate PayPal Sandbox for handling payments in your Laravel application. This setup allows you to process payments securely and handle success and cancellation scenarios appropriately.
Leave a Reply