Hello Laravel enthusiasts, welcome back to justlaravel.com. Here in this post, I will let you know how to integrate PayPal payment gateway in Laravel.
There are many payment gateways to integrate into your laravel applications. Previously I have written about Stripe payment gateway integration, you can check it here. Here I will tell you the one of the famous payment gateway PayPal.
Project on Github
You can watch the video on YouTube here.
- First I will install PayPal SDK using composer.
- Next, I need to login to PayPal developer mode, create a new sandbox account and get some keys like client_id and secret for testing this integration.
- Login to developer.paypal.com to create a merchant account, so that you can sell or to credit funds to your account.
- You can also create buyer account, to buy from some merchant.
- I have created both merchant and buyer test accounts.
- You will need to create a new app to get the client_id and secret keys.
- After creating your app click on the app it shows you client_id and secret keys.
- Copy those and paste those in your .env file for security.
- Next, I will create a new file paypal.php, at \config directory.
- Place the following content in the file
Project on Github
You can add any form, the way you want, here for demo purposes, I just have 1 input field to enter the amount and a button to submit the form.
PayPal Payment Integration – justlaravel.com
I will create a new controller to manage all the PayPal related PHP stuff.
The above command will create a new controller at /app/http/Controllers with name PaymentController
First I will initialize the PayPal configuration and settings in the _construct function.
So now after some amount is entered in the form and when clicked on the ‘Pay with PayPal’ button, the following functionality executes.
In the above function, the terminology like Payer, Item is all from the PayPal SDK, so let me explain those terminologies form PayPal official docs here.
A resource representing a Payer that funds a payment For PayPal account payments, set payment method to ‘paypal’.
(Optional) Lets you specify item wise information
Lets you specify a payment amount. You can also specify additional details such as shipping, tax.
A transaction defines the contract of a payment – what is the payment for and who is fulfilling it.
Set the URLs that the buyer must be redirected to after payment approval/ cancellation.
A Payment Resource; create one using the above types and intent set to ‘sale’
Create a payment by calling the ‘create’ method passing it a valid apiContext. (See bootstrap.php for more on ApiContext) The return object contains the state and the URL to which the buyer must be redirected to for payment approval
Project on Github
So after the payment is made, we need to tell the user whether the payment is a success or a failure. The following functionality executes after a payment is processed.
Project on Github