Table of contents
Introduction:
PayPal is a popular payment gateway that is used by many businesses and individuals around the world. With PayPal, users can send and receive payments online securely and conveniently.
Integrating PayPal with a web application can be a complex task, especially when dealing with security and PCI compliance issues. However, there are many payment processing libraries available that make the integration process much simpler. One such library is thephpleague/omnipay-paypal. In this article, we will go through the process of integrating PayPal using thephpleague/omnipay-paypal in Laravel.What is Omnipay?
Omnipay is a PHP library that provides a consistent and easy to use interface for different payment gateways. It abstracts away the differences between different gateways and provides a unified interface for developers to work with. Omnipay supports over 50 different payment gateways, including PayPal, Stripe, and Authorize.Net. This makes it a popular choice for developers who need to integrate payment processing functionality into their web applications.
What is Laravel?
Laravel is a popular PHP web framework that is known for its elegant syntax and expressive features. It provides a robust set of tools and features for building web applications. Like database migrations, routing, middleware, and login and user login and register module.
Laravel is popular among developers that emphasise maintainability and scalability because of its high testing and code quality.
Prerequisites:
Before we begin, there are a few prerequisites that need to integrate PayPal using thephpleague/omnipay-paypal in Laravel.
These include:
A PayPal business account
A web server with PHP 7.3 or later installed
Composer installed on the local machine
A Laravel project set up and running
Once these prerequisites have been met, we can move on to the integration process.
Step 1: Installing Omnipay-PayPal
The first step in integrating PayPal using thephpleague/omnipay-paypal in Laravel is to install the library. To do this, we need to add it to the list of dependencies in our Laravel project. We can do this by running the following command in the terminal:
composer require league/omnipay paypal
This command will download and install the latest version of the library along with its dependencies. Once the installation is complete, we need to add the service provider to our Laravel project. We can do this by adding the following line to the providers array in our config/app.php file:
'providers' => [
...
Omnipay\OmnipayServiceProvider::class,
...
],
This line tells Laravel to load the Omnipay service provider when the application is booted.
Step 2: Creating a PayPal Gateway
The next step is to create a PayPal gateway object. This object will be used to interact with the PayPal API and process payments. To create the gateway, we can use the following code:
use Omnipay\Omnipay;
$gateway = Omnipay::create('PayPal_Express');
$gateway->setUsername('API_USERNAME');
$gateway->setPassword('API_PASSWORD');
$gateway->setSignature('API_SIGNATURE');
$gateway->setTestMode(true);
In this code, we first import the Omnipay class and create a new instance of the PayPal_Express gateway. We then set the API credentials and enable test mode. It is important to note that the API credentials used here must be from a PayPal business account.
Step 3: Creating a Payment Request
Once the gateway object has been created, we can use it to create a payment request. The payment request contains all the information required to process a payment, such as an amount, currency, and payment method.
To create a payment request, we can use the following code:
$response = $gateway->purchase([
'amount' => '10.00',
'currency' => 'USD',
'returnUrl' => 'http://localhost/payment/success',
'cancelUrl' => 'http://localhost/payment/cancel',
])->send();
if ($response->isRedirect()) {
// Redirect to PayPal
$response->redirect();
} else {
// Payment failed
echo $response->getMessage();
}
In this code, we use the purchase() method of the gateway object to create a payment request. We specify the amount, currency, return URL, and cancel URL as parameters.
The return URL is the URL that PayPal will redirect the user to after the payment is completed. And the cancel URL is the URL that PayPal will redirect the user to if they cancel the payment.
Once the payment request has been created, we check if it is a redirect response. If it is, we redirect the user to the PayPal checkout page by calling the redirect() method of the response object. If the payment request fails for any reason, we display an error message to the user.
Step 4: Processing a Payment
After the user completes the payment on the PayPal checkout page, it will be redirected back to the return URL.
We need to handle this return request and process the payment using the gateway object. We can do this using the following code:
$response = $gateway->completePurchase([
'amount' => '10.00',
'currency' => 'USD',
'returnUrl' => 'http://localhost/payment/success',
'cancelUrl' => 'http://localhost/payment/cancel',
])->send();
if ($response->isSuccessful()) {
// Payment successful
$transactionId = $response->getTransactionReference();
echo "Payment successful. Transaction ID: " . $transactionId;
} else {
// Payment failed
echo $response->getMessage();
}
In this code, we use the completePurchase() method of the gateway object to processing the payment. We specify the same parameters as we did in the payment request.
If the payment is successful, we retrieve the transaction reference using the getTransactionReference() method of the response object and display a success message to the user. If the payment fails, we display an error message.
Conclusion:
Integrating PayPal using thephpleague/omnipay-paypal in Laravel is a straightforward process that can be completed in just a few steps.
This makes it easier to maintain and scale our web applications in the future. With the popularity of PayPal and the ease of integration provided by Omnipay. There is no reason not to include PayPal as a payment option in our web applications.