Simple PayPal integration on your website

Online payment is the hype of today’s web world. For getting any service or product we prefer to online payment method and PayPal is the most secure payment gateway. If you have a very basic knowledge of PHP, you can easily implement the simple PayPal gateway in your site.

The Process to implement PayPal on your website:

We can simplify the process into 3 steps – PayPal button, PDT and confirmation. 

PayPal button

Put this code in the PHP file template where the payment button will appear.

<?php
include 'paypal-pdt-functions.php';
$payment_data = isset($_GET['tx'])
? process_pdt($_GET['tx'])
: FALSE;
$success_url = 'http://www.yourdomain.com/success/';
$cancel_url = 'http://www.yourdomain.com/cancel/';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>PayPal PDT Sample</title>
</head>
<body>
<h1>PayPal PDT Sample</h1>
<p>Click the Buy Now button and make a dummy payment. When you return the PDT data will be printed.</p>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" accept-charset="utf-8">
<p>
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="charset" value="utf-8" />
<input type="hidden" name="business" value="sanjay_1237380148_biz@programmer.net" />
<input type="hidden" name="item_name" value="WordPress Customization" />
<input type="hidden" name="item_number" value="WP001" />
<input type="hidden" name="amount" value="50.00" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="return" value="<?php echo $success_url; ?>" />
<input type="hidden" name="cancel_return" value="<?php echo $cancel_url; ?>" />
<input type="hidden" name="bn" value="Business_BuyNow_WPS_SE" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" name="submit" alt="Buy Now" />
</p>
</form>
</body>
</html>
view raw index.php hosted with ❤ by GitHub

You have to change the respective values like  email, item name, item number, amount, currency, success page link, cancel page link and form action live link.

PDT function

Create a PHP file called ‘paypal-pdt-functions.php’ and put the following code in that file. PDT means payment data transfer. When any payment made successfully, PayPal returns all the payment related info to the return URL so that you can get all those values in your application process.

<?php
function process_pdt($tx)
{
// Init cURL
$request = curl_init();
$pdt_identity_token = 'gvK-79xTEGKi7vlJJnvlNuBz-lgJnWCmK8l2jryNGEe4P7sp4hEh2Mbf4yW';
// Set request options
curl_setopt_array($request, array
(
CURLOPT_URL => 'https://www.sandbox.paypal.com/cgi-bin/webscr',
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query(array
(
'cmd' => '_notify-synch',
'tx' => $tx,
'at' => $pdt_identity_token,
)),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
//CURLOPT_SSL_VERIFYPEER => TRUE,
//CURLOPT_CAINFO => 'cacert.pem',
));
// Execute request and get response and status code
$response = curl_exec($request);
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
// Close connection
curl_close($request);
// Validate response
if($status == 200 AND strpos($response, 'SUCCESS') === 0)
{
// Remove SUCCESS part (7 characters long)
$response = substr($response, 7);
// Urldecode it
$response = urldecode($response);
// Turn it into associative array
preg_match_all('/^([^=\r\n]++)=(.*+)/m', $response, $m, PREG_PATTERN_ORDER);
$response = array_combine($m[1], $m[2]);
// Fix character encoding if needed
if(isset($response['charset']) AND strtoupper($response['charset']) !== 'UTF-8')
{
foreach($response as $key => &$value)
{
$value = mb_convert_encoding($value, 'UTF-8', $response['charset']);
}
$response['charset_original'] = $response['charset'];
$response['charset'] = 'UTF-8';
}
// Sort on keys
ksort($response);
// Done!
return $response;
}
return FALSE;
}
?>
view raw paypal-pdt-functions.php hosted with ❤ by GitHub

You have to enable PDT from your PayPal account profile and you will get a PDT identity token, just copy that token and put the token value in this file.

Confirmation

You have a confirmation page, say success URL. Just put the following code on that page template. It will return the array values to show what you will get when any payment made successfully.

<?php if($payment_data)
echo 'Payer Email: ' . $payment_data['payer_email'];
if($_GET):
?>
<hr/>
<h2>Details</h2>
<pre>GET: <?php print_r($_GET) ?></pre>
<pre>PDT: <?php print_r($payment_data) ?></pre>
<?php endif ?>
view raw success.php hosted with ❤ by GitHub

This tutorial shows the process using sandbox mode. So, it is recommended to test everything on sandbox mode first and after successful testing you can put the total payment process on live mode.

Demo | Source code

Share this article:

3 thoughts on “Simple PayPal integration on your website”

  1. Hi Sanjay

    Thanks for the script.
    I have tried following instructions exactly and have changed all the settings to my own account’s and uploaded to my host’s server, but when I try with my own sandbox account, everything comes back blank on the success page.

    There’s nothing to get from the url, and the page is completely blank.
    Also, when I try without PDT, the url comes back with no tx id, etc either.

    I have set my return address under PDT under my main PayPal account (can’t find anywhere to do this for my sandbox account)
    and have used the identity token from there.
    I also tried just for chance, the signature from the sandbox account from the token, but with no luck.

    I have tried with the account details from this tutorial and it shows the data in the url and gets the data in the page, but nothing shows under PDT.

    Is there anything you could please suggest?
    Is this possibly a problem with my sandbox account? I read somewhere that sandbox accounts created after 2012 don’t show the info in the url, not sure how true this is, sure they would have fixed this by now.

    Please help

    Many thanks
    Brad

  2. Hello Sanjay, thank you for this tutorial, i actually using your demo and works great.
    I tried to use it in my website, replace my token and my sandbox account and the auto return is not working anymore, can you please explain to me your sandbox account configuration?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.