PayPal is a company operating a worldwide online payments system that supports online money transfers and serves as an electronic alternative to the traditional money exchange system. The company operates as a payment gateway for online sellers, auction sites, and many other commercial users, for that it charges a fee in exchange for benefits. PayPal’s payment system also called PayPal inshort.
A payment gateway is a merchant service provided by an any-commerce application service provider that authorizes credit card or direct payments processing fore-businesses, online retailers, bricks and clicks, or traditional brick and mortar. The payment gateway used to provide by a bank to its customers, but can be provided by a specialized financial service provider as a separate service, such as a payment service provider. A payment gateway facilitates a payment transaction by the transfer of information between a payment portal (such as a website or mobile phone) and the front end processor or acquiring the bank.
• First, we will install the PayPal SDK using composer.
• Next, we 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 an account, to buy from some merchant.
• We have created both merchant and buyer test accounts.
• You will have 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 CLIENT_ID, SECRET and paste those in your .env file for security.
• Next, we will create a new file paypal.php, at \ config directory.
• Place the following content in the file
You can add any form, the way you want, here for demo purposes, We just have 1 input field to enter an amount and a button to submit a form.
We will create a new controller to manage all the PayPal related PHP stuff.
The above command will create a new controller at /app/http/Controllerswith name PaymentController
First We will initialize the PayPal configuration and settings in the _construct function.
So now after the amount is entered in the form and when clicked on the ‘Pay with PayPal’ button, the following function 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.
• Payer
A resource representing a Payer that funds a payment For PayPal account payments, set payment method to ‘paypal’.
• Item information
(Optional) Lets you specify item wise information
• Amount
Lets you specify a payment amount. You can also specify additional charges such as shipping, tax.
• Transaction
A transaction defines the contract of payment — what is the payment for and who is fulfilling it.
• Redirect URLs
Set the URLs that the buyer must be redirected to after payment approval/ cancellation.
• Payment
A Payment Resource; create one using the above types and intent set to ‘sale’
• Create Payment
Create a payment by calling the ‘create’ method passing it a valid API context. (See bootstrap.php for more on ApiContext) The returned object contains the state and the URL to which the buyer must be redirected for payment approval
So after the payment is made, we need to tell the user whether the payment is a success or a failure. The following function executes after payment is processed.
Determine if the user approved the payment or not.
if (isset($_GET[‘success’]) && $_GET[‘success’] == ‘true’) {
Get the payment Object by passing paymentId payment id was previously stored in session.
PaymentExecution object includes information necessary to execute a PayPal account payment. The payer_id is added to the request query parameters when the user is redirected from PayPal’s Website to your site.
And finally, if the result of the payment is a success, we will show a success message and a failure message if the payment fails.
We will store the messages in the Session and show them in the view.
Check out the images below,
Wants to learn more about Laravel? We are providing a complete solution related to Laravel for your Websites. Contact us to know more about the PayPal payment gateway integration in Laravel.