1. Explain how PayPal works (IPN and PDT process). Chapter One
  2. Give a good example to show how register work with PayPal, containing database, controller and front end form with jQuery functionalities. And explain how to setup a PayPal account (Sandbox and real PayPal business account example). Chapter Two

This tutorial gives a big picture of PayPal process (PDT and IPN), and a real project from starting point to the end. Good explanation and pictures, detailed comments in code line. Hopefully, it will be useful for all readers.

Chapter Three

After setup PayPal sandbox working with a real project, we can start to setup our business PayPal account and start to publish our project and earn money.

Setup Genuine PayPal Business Account

The genuine PayPal business account panel is a little bit different from Sandbox, but other settings are quite the same. So I just point out where the settings are, and settings should be the same as previous examples.

First apply for your business PayPal account, but we skip this step, because PayPal business account is quite different in different situations and companies. As you see, there are many personal sensitive details, I have to mask. So, click on Profile link.

Then, in the source code, comment out everything related to ‘sandbox’, and change the token, and uncomment the code next to sandbox code.

I have highlighted the changes in code:

1. You need to change redirect URL to PayPal, instead of sandbox: (CAUTION: the snippet contains ‘>’ and ‘&’, those should be ‘>’ and ‘&’)

public function registerSubmit()
{
// more code here ...
// return url to paypal
// return json_encode(array('result' => true, 'url' => "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HA9DZBCKXKCL2&item_number=".$itemId."&amount=".$this->amount));
        return json_encode(array('result' => true, 'url' => "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SSZF9WEUY87GS&item_number=".$itemId."&amount=".$this->amount));

2. _PDT changes:

private function _PDT()
{
// more code here ...
// $auth_token = "_PJaHiwRfwMmWzW-9nuPuSguYxC-1d9KpxaasaNANtIvyOcmqY6jXNkRmxW";
$auth_token = "OxDenzKmrWPyEXU0YzIg2zs-VAe7ufCADyjbfxF_RpREL4rLEslZrSa21R4";$req .= "&tx=$tx_token&at=$auth_token";// post back to PayPal system to validate$header = "POST /cgi-bin/webscr HTTP/1.0rn";

// $header .= "Host: www.sandbox.paypal.comrn";
$header .= "Host: www.paypal.comrn";

$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";

// $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // open socket
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // open socket
// more code here ...
}

3. _IPN changes:

private function _PDT()
{
// more code here ...
// $url= 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$url= 'https://www.paypal.com/cgi-bin/webscr';
// more code here ...
// curl_setopt($ch, CURLOPT_HEADER , array('Host: www.sandbox.paypal.com'));
curl_setopt($ch, CURLOPT_HEADER , array('Host: www.paypal.com'));
// more code here ...
}

Then go to ‘My selling tools’ and you can setup you PDT, IPN, and PayPal button.

Then, DONE! Thank you!

I hope you enjoyed my quick tutorial on how to use Paypal to setup automatic payments.

Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.


No Reader comments

Learn JavaScript for free!
Free course: Introduction to JavaScript

Yours when you take up a free 14-day SitePoint Premium trial.