Laravel 8 with PayPal Express Check Out

jesse songok
2 min readJul 5, 2021

Introduction

I had applied for a software developer position in a certain company. They gave a technical assessment, which included creating a simple e-commerce application using PHP. A payment gateway was to be integrated for the checkout process. The rules of engagement were to use any framework or libraries to accomplish the task in less than 5hours.

I decided to use Laravel, due to its ability to quickly prototype applications. The version of Laravel was version 8. For handling payments, I also choose to use PayPal because I already had experience using it on another project (Nextjs).

PayPal

I was able to scaffold the application, created an authentication module, users module, products module, shop module and installed a Cart package. It took a while to implement checkout and eventual payment using PayPal.

I had not used PayPal with Laravel before and was kind of shooting in the dark. Google was my friend, but finding the right package that was tested with Laravel 8 proved to be a daunting task.

The only package that came close in terms of usage and documentation was the srmklive/paypal package. I tried using the following tutorial How to Integrate PayPal in Laravel 8 Tutorial but I hit a snug.

I resorted to going back to the PayPal developer documentation. It highlighted how to integrate the Express Checkout Button which I gladly did. Also, it gave instructions on how to use their PayPal REST SDKs.

I created a new sandbox environment for my application on PayPal. Did set up my development environment by installing the PayPal Checkout-PHP-SDK using composer.

composer require paypal/paypal-checkout-sdk 1.0.1

The PayPal checkout PHP SDK documentation only provides instructions on how to implement checkout on vanilla PHP. But not its implementation on the Laravel framework.

Implementing PayPal Express Checkout Button

I added the express checkout button on the checkout view page. Used the JavaScript fetch.API to POST/route the order to my Laravel Controller for processing.

Payment controller

I created a payment controller using the Laravel artisan command.

php artisan make:controller PaymentsController

On the payment controller, I added the PayPal packages i had installed earlier

use PayPalCheckoutSdk\Core\PayPalHttpClient;

use PayPalCheckoutSdk\Core\SandBoxEnvironment;

use PayPalCheckoutSdk\Orders\OrdersCreateRequest;

use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;

I created a static function to set up my environment with the correct environment variables. For capturing and processing the order, a payment function was used. The results returned were either successful or not. If successful, the value was to be returned to our Check Out script so that it proceeds to run the onApproved function.

Conclusion

The implementation does not follow the SOLID principles of programming. In the next article, I will be highlighting how I refactored the code on my payment controller and also optimize the JavaScript files to improve on performance.

--

--

jesse songok

A code enthusiast who like experimenting with new technologies, building small projects to solve my everyday problems