Introduction
PayPal is one of the most popular payment processing systems globally, and integrating it into your Laravel application can provide a secure and efficient way for customers to transact online. In this tutorial, we’ll go through the steps involved in adding PayPal functionality to a Laravel application.
Setting up the Environment
First, make sure you have Laravel installed. If not, install Laravel via Composer:
composer global require laravel/installer
laravel new payment-app
cd payment-app
Creating a PayPal Sandbox Account
Before integrating PayPal into your application, you should set up a sandbox account at the PayPal Developer website for testing. Navigate to https://developer.paypal.com/ and sign up for a developer account.
Installing Required Packages
Next, install the necessary package to help integrate PayPal with Laravel:
composer require srmklive/paypal
Once installed, publish the configuration file:
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
Configuration
Edit the config/paypal.php
configuration file with your sandbox credentials:
'client_id' => 'your-paypal-client-id',
'secret' => 'your-paypal-secret',
'settings' => [
'mode' => 'sandbox',
// Other settings...
]
Creating the PayPal Controller
Create a controller to handle PayPal transactions:
php artisan make:controller PayPalController
Setting Up Routes
In your web.php
file, set up the routes for your payment functions:
Route::get('payment', 'PayPalController@payment')->name('payment');
Route::get('cancel', 'PayPalController@cancel')->name('payment.cancel');
Route::get('payment/success', 'PayPalController@success')->name('payment.success');
Building the Controller
In PayPalController.php
, start by setting up the necessary namespaces and extend the base controller class:
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
class PayPalController extends Controller
{
/* ... */
}
Add functions to handle payment processing:
public function payment()
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$paypalToken = $provider->getAccessToken();
$response = $provider->setCurrency('USD')->setExpressCheckout(
[
'items' => [
[
'name' => 'Product 1',
'price' => 100,
'qty' => 1
]
],
'invoice_id' => uniqid(),
'invoice_description' => "Order #{
$invoice->id} Invoice",
'return_url' => route('payment.success'),
'cancel_url' => route('payment.cancel'),
]
);
return redirect($response['paypal_link']);
}
public function cancel()
{
// Handle cancellation
}
public function success(Request $request)
{
// Handle successful transaction
}
Here, the payment()
function initializes a new PayPal client with your credentials, creates an invoice with items, and redirects the user to PayPal for payment processing. The cancel()
and success()
functions will handle the payment cancellation and successful payment cases, respectively.
Conclusion
In this tutorial, we’ve covered the basic setup for integrating PayPal into a Laravel application. By following these steps, you can begin processing payments securely and efficiently in your Laravel projects. Remember that PayPal offers advanced features, which you can implement as needed when you’re comfortable with these basics.