Advertisement
toymachiner62

ipn

Jan 28th, 2012
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Paypal_ipn extends CI_Controller {
  4.  
  5.     public function index() {
  6.         // read the post from PayPal system and add 'cmd'
  7.         $req = 'cmd=_notify-validate';
  8.         $header = null;
  9.  
  10.         foreach ($_POST as $key => $value) {
  11.             $value = urlencode(stripslashes($value));
  12.             $req .= "&$key=$value";
  13.         }
  14.  
  15.         // post back to PayPal system to validate
  16.         $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  17.         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  18.         $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  19.         $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
  20.  
  21.         // assign posted variables to local variables
  22.         //$item_name = $_POST['item_name'];
  23.         //$item_number = $_POST['item_number'];
  24.        
  25.         if(isset($_POST['payment_status'])) {
  26.             $payment_status = $_POST['payment_status'];
  27.         }
  28.         if(isset($_POST['mc_gross'])) {
  29.             $payment_amount = $_POST['mc_gross'];
  30.         }
  31.         if(isset($_POST['mc_currency'])) {
  32.             $payment_currency = $_POST['mc_currency'];
  33.         }
  34.         if(isset($_POST['txn_id'])) {
  35.             $txn_id = $_POST['txn_id'];
  36.         }
  37.         if(isset($_POST['invoice'])) {
  38.             $invoice = $_POST['invoice'];
  39.         }
  40.         if(isset($_POST['payment_date'])) {
  41.             $payment_date = $_POST['payment_date']; // This is in PST timezone
  42.         }
  43.         if(isset($_POST['payment_type'])) {
  44.             $payment_type = $_POST['payment_type'];
  45.         }
  46.         if(isset($_POST['pending_reason'])) {
  47.             $pending_reason = $_POST['pending_reason'];
  48.         }
  49.         if(isset($_POST['receiver_email'])) {
  50.             $receiver_email = $_POST['receiver_email'];
  51.         }
  52.         if(isset($_POST['payer_email'])) {
  53.             $payer_email = $_POST['payer_email'];
  54.         }
  55.         if(isset($_POST['first_name'])) {
  56.             $first_name = $_POST['first_name'];
  57.         }
  58.         if(isset($_POST['last_name'])) {
  59.             $last_name = $_POST['last_name'];
  60.         }
  61.        
  62.        
  63.        
  64.        
  65.        
  66.        
  67.         //$trackingId = $_POST['trackingId'];
  68.        
  69.         //$payment_fee = $_POST['mc_fee'];
  70.        
  71.        
  72.        
  73.        
  74.        
  75.        
  76.         //$contact_phone = $_POST['contact_phone'];
  77.        
  78.  
  79.         if (!$fp) {
  80.             // HTTP ERROR
  81.         } else {
  82.             fputs ($fp, $header . $req);
  83.             while (!feof($fp)) {
  84.                 $res = fgets ($fp, 1024);
  85.                 if (strcmp ($res, "VERIFIED") == 0) {
  86.                     log_message('error', "*********************************");
  87.                     $log = var_export($_POST, true);
  88.                     $log = str_replace(array("\r","\n"), '', $log);
  89.                    
  90.                     log_message('error', $log);
  91.                     log_message('error', "*********************************");
  92.                     //log_message('error', "<pre>$_POST</pre>");
  93.                     log_message('error', "invoice = $invoice");
  94.                     //log_message('error', "tracking_id = $trackingId");
  95.                     log_message('error', "payment_status = $payment_status");
  96.                     log_message('error', "payment_amount = $payment_amount");
  97.                     //log_message('error', "payment_fee = $payment_fee");
  98.                     log_message('error', "payment_type = $payment_type");
  99.                     log_message('error', "payment_currency = $payment_currency");
  100.                     log_message('error', "payment_date = $payment_date");
  101.                     log_message('error', "pending_reason = $pending_reason");
  102.                     log_message('error', "first_name = $first_name");
  103.                     log_message('error', "last_name = $last_name");
  104.                     //log_message('error', "contact_phone = $contact_phone");
  105.                     log_message('error', "txn_id = $txn_id");
  106.                     log_message('error', "receiver_email = $receiver_email");
  107.                     log_message('error', "payer_email = $payer_email");
  108.                    
  109.                     // check the payment_status is Completed
  110.                     // check that txn_id has not been previously processed
  111.                     // check that receiver_email is your Primary PayPal email
  112.                     // check that payment_amount/payment_currency are correct
  113.                     // process payment
  114.                 }
  115.                 else if (strcmp ($res, "INVALID") == 0) {
  116.                     log_message('error', "Problem with IPN. res = $res");
  117.                 }
  118.             }
  119.             fclose ($fp);
  120.         }
  121.    
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement