Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PayPal integration with PHP

Sunday, May 7th, 2017 by Servage

paypalAccepting payments is quite common on business websites. There are many payment processors to choose from, but PayPal has been the leading payment processor for a long time. Implementing a shopping cart or checkout system on a website is not a simple task, but with these instructions you should be able to integrate a simple single-item checkout process with your own website.

Making a Payment Button

First, we have to make a payment button that a customer can click to pay their order. PayPal offers multiple buttons for adding a product to a shopping cart, creating a subscription payment, and so on. For buying a single item, the buy now button is the most suitable. You can create your own payment button in the tools section of your PayPal account, or you can customize the snippet below for your needs.

<form target="paypal" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">

<input type="hidden" name="business" value="your-paypal-email@example.com">

<input type="hidden" name="cmd" value="_xclick">

<input type="hidden" name="item_name" value="Your Product Name">

<input type="hidden" name="amount" value="19.99">

<input type="hidden" name="custom" value="gs32kD3Kdas3Kax420kg">

<input type="hidden" name="return" value="https://example.com/purchase-completed">

<input type="hidden" name="currency_code" value="USD">

<input type="image" name="submit" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal">

<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif">

</form>

As you can see, the buy now button is a simple HTML form that sends a POST request to www.paypal.com. When a customer clicks the payment button, they are redirected to PayPal to make their payment. Most of the input fields in the form are quite self-explanatory. The custom field is a pass-through variable, meaning PayPal doesn’t do anything with the value. It simply passes it back to your website once the payment has been completed. The custom field can therefore be used as an internal tracking number for orders. It allows you to see which order has been paid for.

Another thing to note is that the form is submitted to www.sandbox.paypal.com instead of the real www.paypal.com website. PayPal gives all developers access to a sandbox environment, which is an identical environment compared to the official PayPal system that you can use to test payments without using real money. You should sign up for a sandbox account and create test buyer and seller accounts in the dashboard. When you create the test accounts, you can give any amount of balance to the test buyer account and use that to test payments.

For a complete explanation and even more fields that you can use in the form, refer to the PayPal developer documentation at developer.paypal.com.

Creating the IPN Listener

When the customer has completed their payment. PayPal sends a notification back to your website as a POST request to an address you can specify in your PayPal account settings. This notification is called Instant Payment Notification (IPN). PayPal sends you back information about the order, such as when it was paid and how much the customer paid. Among this information is the custom field, which you can get using $_POST[‘custom’]. Because the product price is specified in the HTML form that the customer could simply edit and pay a different price, you should verify that the paid price matches the price of the product using the $_POST[‘mc_gross’] value that PayPal sends.

Fortunately, you don’t have to write the whole IPN verification script from scratch. PayPal provides simple examples that you can copy to your project: www.github.com/paypal/ipn-code-samples. Implement the above two checks with help of the sample scripts and you have a simple PayPal checkout system ready for your website.

PayPal integration with PHP, 5.0 out of 5 based on 3 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.