Advertisement
Guest User

Paypal

a guest
Jan 9th, 2013
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2.  
  3. // STEP 1: Read POST data
  4.  
  5. // reading posted data from directly from $_POST causes serialization
  6. // issues with array data in POST
  7. // reading raw POST data from input stream instead.
  8. $raw_post_data = file_get_contents('php://input');
  9. $raw_post_array = explode('&', $raw_post_data);
  10. $myPost = array();
  11. foreach ($raw_post_array as $keyval) {
  12.   $keyval = explode ('=', $keyval);
  13.   if (count($keyval) == 2)
  14.      $myPost[$keyval[0]] = urldecode($keyval[1]);
  15. }
  16. // read the post from PayPal system and add 'cmd'
  17. $req = 'cmd=_notify-validate';
  18. if(function_exists('get_magic_quotes_gpc')) {
  19.    $get_magic_quotes_exists = true;
  20. }
  21. foreach ($myPost as $key => $value) {        
  22.    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
  23.         $value = urlencode(stripslashes($value));
  24.    } else {
  25.         $value = urlencode($value);
  26.    }
  27.    $req .= "&$key=$value";
  28. }
  29.  
  30.  
  31. // STEP 2: Post IPN data back to paypal to validate
  32.  
  33. $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
  34. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  35. curl_setopt($ch, CURLOPT_POST, 1);
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  40. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  41. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
  42.  
  43. // In wamp like environments that do not come bundled with root authority certificates,
  44. // please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
  45. // of the certificate as shown below.
  46. // curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
  47. if( !($res = curl_exec($ch)) ) {
  48.     // error_log("Got " . curl_error($ch) . " when processing IPN data");
  49.     curl_close($ch);
  50.     exit;
  51. }
  52. curl_close($ch);
  53.  
  54.  
  55. // STEP 3: Inspect IPN validation result and act accordingly
  56.  
  57. if (strcmp ($res, "VERIFIED") == 0) {
  58.     // check whether the payment_status is Completed
  59.     // check that txn_id has not been previously processed
  60.     // check that receiver_email is your Primary PayPal email
  61.     // check that payment_amount/payment_currency are correct
  62.     // process payment
  63.  
  64.     // assign posted variables to local variables
  65.     $item_name = $_POST['item_name'];
  66.     $item_number = $_POST['item_number'];
  67.     $payment_status = $_POST['payment_status'];
  68.     $payment_amount = $_POST['mc_gross'];
  69.     $payment_currency = $_POST['mc_currency'];
  70.     $txn_id = $_POST['txn_id'];
  71.     $receiver_email = $_POST['receiver_email'];
  72.     $payer_email = $_POST['payer_email'];
  73. } else if (strcmp ($res, "INVALID") == 0) {
  74.     // log for manual investigation
  75. }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement