PayPal Payment Gateway Integration in PHP
The world is constantly changing, that is why businesses should keep up with its breakneck speed to simply survive the rat race. To retain existing clients and attract new ones, companies should adapt really fast to customers’ needs. They should make their journey as easy as possible. That is why so many companies throughout the world have integrated PayPal services. Maybe it is the right time for you to do the same? We will show you how. But first of all, let’s take a look at what PayPal is.
What is PayPal?
PayPal is a global payments system, which supports secure, effortless, and quick money transfers between parties, such as sellers and buyers. A lot of companies in different industries have already implemented the system and are reaping the benefits. Here is the list of just a few corporations which successfully exploit this technology:
- e-commerce: Magento, Shopify, BigCommerce, 3dcart, Wix
- marketplaces: eBay, Swappa, eCRATER
- fintech: Xero, QuickBooks, Invoice2go
- information technology: Zoho, Infusionsoft, Constant Contact
Fees on PayPal
You can create a PayPal account at no extra cost. There is also no fee if someone makes purchases from your website, but the account holder is charged with a fee for every transaction.
How does PayPal work?
To create an easy customer journey for your clients, you can offer a PayPal payment gateway. To do so, follow a 6 stage process to implement the system into your website.
6 stages of PayPal integration in PHP
Stage 1 Understand your customer’s journey
First of all, you need to understand how your customer’s journey will look like on your website. If you establish an online store, the easiest way to make a purchase on your website might look like this:
- You have a product page with a Buy Now button
- The buyer clicks on the button
- He or she is redirected to the PayPal page
- The customer either complete payment or cancel it
- Depending on what your buyer chooses, he/she is redirected to the product page or “Thank you” page
- In case of completion, an email is sent to the buyer’s email box
Stage 2 Add “BUY NOW” button to your PHP product page
Go to your PayPal account and get into the All Tools section. Scroll down and click on the PayPal buttons section and choose the Buy Now button.
After clicking, you will get the page with 3 steps:
- In Step 1 “choose a button type and enter your payment details” select a type of the button, then name it and provide an ID, and pricing information.
- In Step 2 “track inventory, profit & loss (optional)” uncheck the Save button in PayPal
- In Step 3 “customize advanced features (optional)” you can customize many features: for example, enable a feature that gives the buyer an opportunity to leave a comment or shipping address.
In Step 3 remember to add the link of your product page to the “take customer to this URL when they cancel their checkout” field so that the buyer could be redirected to the initial page in case of canceling the purchase.
Also, add the link to the “Thank you” page to the “take customer to this URL when they finish checkout” field for the buyer to know the purchase is completed successfully.
Upon clicking the Create button, you get a piece of code, which you copy and paste into your product page PHP code.
Now if the buyer wants to buy the product, there will be the Buy Now button.
Stage 3 Create a new PHP page to handle IPN
IPN stands for Instant Payment Notification which notifies you of PayPal transactions.
To handle IPN, go to the PayPal development page and click on Dashboards and find IPN simulator. Keep it open.
Find Process IPN messages on the PayPal development website and find a piece of PHP code – IPN listener, which is presented as an example. You can use it for your implementation. It has 2 steps.
- Step 1 Read the post data
- Step 2 Post IPN data back to PayPal to validate
Copy the code with Steps 1 and 2. Create a new PHP file and paste the code there. Name it handler.php. Upload it on your server to make it visible.
Copy Inspect IPN validation result and paste it right down into the file. This helps you send an email to the buyer and also redirects him or her on your Thank you page. Save the file.
Stage 4 Manage IPN
Copy the URL of the uploaded file. Go to the IPN simulator and post the link into the IPN handler URL box. In Transaction type choose Web Accept and in payment_status choose Completed. Do not forget to fill out the item_name and item_number fields.
You can also change other information if needed. When everything is ready, click on the Send IPN button. In case of success, you will get “IPN was sent and the handshake was verified” message at the top of the page.
Stage 5 Manage email
Open the code again to send an email to the buyer. To do this, you need to use PHP mailer library. Find it in Google by typing phpmailer.
You can click on the first link which leads to GitHub. Read the information provided on the website and copy the Composer from there.
Open your CMD and install the composer there. Once the library is installed, upload it to your server.
While you are uploading the library, copy a piece of PHP code which is under the composer on the GitHub website. Paste the code at the top of the handler.php file.
When everything is done, you will get Vendor piece of code in your handler.php. Write PHP HTML code under it to send an email. Do not forget to write the name of the sender, his/her address, subject line, the body of the email, add an attachment.
Stage 6 Manage Thank you page
It must be the easiest step of the whole process as you have already done it in Stage 2 where you created the “Buy Now” button of PayPal.
Just make sure once again that you have added the link of the “Thank you” page to the “take customer to this URL when they finish checkout” field.
Now you know how to implement PayPal in PHP. The process described here is one of many ways to integrate this payment system, so you can also conduct thorough research on the PayPal website to get full information on the system implementation.
Also, the process is quite difficult for those who are not familiar with programming and does not have advanced coding skills. Thus, we advise considering the option of hiring a person who is familiar with PayPal integration in PHP.