Skip to content

Instantly share code, notes, and snippets.

@blbwd
Last active Mar 14, 2016
Embed
What would you like to do?
Simple PayPal Integration
<?php
echo "<h1>Welcome, Guest</h1>";
echo "<h1>Payment Canceled</h1>";
?>
<?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>
<?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;
}
?>
<?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 ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment