UncategorizedIntegration of PayPal Payment Gateway with Laravel

Integration of PayPal Payment Gateway with Laravel

PayPal Integration is one of the easiest ways to integrate into the website using Laravel. With this article, we are going to learn about “the integration of PayPal Payment gateway in a particular website with the help of Laravel.

  • You must have the necessary Laravel application installed in your device.
  • We are using PHP for integration
  • Open the website developer.PayPal.com
  • If you do not have a PayPal account you should sign up in the PayPal. This website will be used to create a sandbox account to test our application instead of using all live transactions.
  • In the website create a sandbox buyer and seller (merchant) account.
  • After creating sandbox account you have to create a sandbox application to integrate sandbox application in Laravel application.

sandbox account

Here we will use this Client Id and Client Secret. We have to copy this Client Id and Client secret in our application and put it in .env file

Creation of a new application:
First create a new application in command prompt, enter the following command in the console window:

C:\> laravel new PayPal_payment_integration

After the creation of a new application type:

C:\laravel\PayPal>payment_integration>php artisian serve

You will get the output:

Laravel Development server started:

Laravel Development server

Installation of PayPal SDK:

  • Now we have to install PayPal SDK using composer

PayPal SDK

You can check the sample code that we have to use at
http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html

  • The package got installed successfully

install the package

  • Type the following code in cmd:
  • C:\laravel\PayPal>payment_integration> subl /
  • In the env file: we will create PAYPAL CLIENT, PAYPAL SECRET AND PAYPAL
  • MODE=sandbox. Client and Secret will be copied as shown in the image below:

sandbox account

  • Paste the above text in the window that will look like below:

paypal account

Create a file in config:

  • Create a new file in the config with name PayPal.php. It will be the config file. Set all the parameters for PayPal API.
  • Type the following code as shown below in the same env file.

code

Create a basic form for payment process:

  • Now we are going to form a Laravel application. In this, we need to create some basic form for the payment process.
  • Go to the resources used and create a new file with the name “paywithPayPal.blade.php”
    Type the code in the file as shown below:

create a new file

    • To check this form we need to create a route file.

Create a route file:

  • After creating a route file type the following code:

route file

  • On running the following code we get the output as follows:
    Type the following code in the console window:
    C:\laravel\PayPal>payment_integration>php artisian

console window

We have this basic form as the output, if we will enter some amount and click on Pay with PayPal it will go to the PayPal sandbox payment page.

Now we have to create the code for the page.

  • Create a controller:
  • Create a controller for this page
    Open the console window and type the following code
    C:\laravel\PayPal>payment_integration> php artisian make:controller Payment Controller.
  • The controller will be created.

controller

  • In the payment controller type the following code:

code (2)
// the code in the white box can be copied from this link: http://PayPal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.htmlSetting a route for PayPal:

  • Set a route for PayPal in web.php. Type the following code in the web.php tab:

route file

Create a method controller:

In the payment controller window, we are going to create a method controller. So type the following code below after this code as shown in the image: $this->_api_context->setconfig($PayPal_conf[‘setting’]);

Your PayPal method controller is now set up.

Now,

When part 1 is done we have to go to the link of developer.PayPal.com and go to the page where Rest API samples are written. In that page click on Step II: Execute After success step 2 source code link is given.

Go to the Step 2 in the PayPal developer page:

Now we have to execute the payment as this is the second step required to complete the PayPal checkout. It will display the code after the buyer has paid and the payment is to be executed.

    • After this code, we need to get the Payment Id and Forget Payment Id. After one session is expired then we need to remove that payment id and get a new payment Id.
    • Next, if the payer Id is empty then the session will be failed.

See the following code

PayPal developer page

  • When there is a payment failure we will have to show a red error box and when the payment is successful we have to show the syntax is green depicting the failure and the success respectively. So in the last, we have to go again to the paywithPayPal.blade.php and write the following code:

syntax

After all the codes have been written it’s time for some output for which the code has been written.
The test sample to log in and payment:

  • Now we will run the application. Open the browser in which output has to be displayed.
  • Enter some amount which has to be paid.
  • Click on the tab Pay with PayPal. It will redirect to sandbox PayPal.

test sample

  • It will ask for the credentials like username (email id) and password.
  • Go to the page where you created the buyer id and use the password for that id only which you had created in the beginning.
  • Click on the Login button after entering the credentials.
    After this the integration of your website with PayPal payment gateway is complete.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -