[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/ext/modules/payment/paypal/ -> standard_ipn.php (source)

   1  <?php
   2  /*
   3    $Id: standard_ipn.php 1778 2008-01-09 23:37:44Z user $
   4  
   5    osCMax Power E-Commerce
   6    http://oscdox.com
   7  
   8    Copyright 2008 osCMax
   9  
  10    Released under the GNU General Public License
  11  */
  12  
  13    chdir('../../../../');
  14    require ('includes/application_top.php');
  15  
  16    $parameters = 'cmd=_notify-validate';
  17  
  18    reset($HTTP_POST_VARS);
  19    while (list($key, $value) = each($HTTP_POST_VARS)) {
  20      $parameters .= '&' . $key . '=' . urlencode(stripslashes($value));
  21    }
  22  
  23    if (MODULE_PAYMENT_PAYPAL_STANDARD_GATEWAY_SERVER == 'Live') {
  24      $server = 'www.paypal.com';
  25    } else {
  26      $server = 'www.sandbox.paypal.com';
  27    }
  28  
  29    $fsocket = false;
  30    $curl = false;
  31    $result = false;
  32  
  33    if ( (PHP_VERSION >= 4.3) && ($fp = @fsockopen('ssl://' . $server, 443, $errno, $errstr, 30)) ) {
  34      $fsocket = true;
  35    } elseif (function_exists('curl_exec')) {
  36      $curl = true;
  37    } elseif ($fp = @fsockopen($server, 80, $errno, $errstr, 30)) {
  38      $fsocket = true;
  39    }
  40  
  41    if ($fsocket == true) {
  42      $header = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n" .
  43                'Host: ' . $server . "\r\n" .
  44                'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
  45                'Content-Length: ' . strlen($parameters) . "\r\n" .
  46                'Connection: close' . "\r\n\r\n";
  47  
  48      @fputs($fp, $header . $parameters);
  49  
  50      $string = '';
  51      while ([email protected]feof($fp)) {
  52        $res = @fgets($fp, 1024);
  53        $string .= $res;
  54  
  55        if ( ($res == 'VERIFIED') || ($res == 'INVALID') ) {
  56          $result = $res;
  57  
  58          break;
  59        }
  60      }
  61  
  62      @fclose($fp);
  63    } elseif ($curl == true) {
  64      $ch = curl_init();
  65  
  66      curl_setopt($ch, CURLOPT_URL, 'https://' . $server . '/cgi-bin/webscr');
  67      curl_setopt($ch, CURLOPT_POST, true);
  68      curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  69      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  70      curl_setopt($ch, CURLOPT_HEADER, false);
  71      curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  72      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  73  
  74      $result = curl_exec($ch);
  75  
  76      curl_close($ch);
  77    }
  78  
  79    if ($result == 'VERIFIED') {
  80      if (isset($HTTP_POST_VARS['invoice']) && is_numeric($HTTP_POST_VARS['invoice']) && ($HTTP_POST_VARS['invoice'] > 0)) {
  81        $order_query = tep_db_query("select orders_status, currency, currency_value from " . TABLE_ORDERS . " where orders_id = '" . $HTTP_POST_VARS['invoice'] . "' and customers_id = '" . (int)$HTTP_POST_VARS['custom'] . "'");
  82        if (tep_db_num_rows($order_query) > 0) {
  83          $order = tep_db_fetch_array($order_query);
  84  
  85          if ($order['orders_status'] == MODULE_PAYMENT_PAYPAL_STANDARD_PREPARE_ORDER_STATUS_ID) {
  86            $sql_data_array = array('orders_id' => $HTTP_POST_VARS['invoice'],
  87                                    'orders_status_id' => MODULE_PAYMENT_PAYPAL_STANDARD_PREPARE_ORDER_STATUS_ID,
  88                                    'date_added' => 'now()',
  89                                    'customer_notified' => '0',
  90                                    'comments' => '');
  91  
  92            tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);
  93  
  94  
  95            tep_db_query("update " . TABLE_ORDERS . " set orders_status = '" . (MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID > 0 ? (int)MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID : (int)DEFAULT_ORDERS_STATUS_ID) . "', last_modified = now() where orders_id = '" . (int)$HTTP_POST_VARS['invoice'] . "'");
  96          }
  97  
  98          $total_query = tep_db_query("select value from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . $HTTP_POST_VARS['invoice'] . "' and class = 'ot_total' limit 1");
  99          $total = tep_db_fetch_array($total_query);
 100  
 101          $comment_status = $HTTP_POST_VARS['payment_status'] . ' (' . ucfirst($HTTP_POST_VARS['payer_status']) . '; ' . $currencies->format($HTTP_POST_VARS['mc_gross'], false, $HTTP_POST_VARS['mc_currency']) . ')';
 102  
 103          if ($HTTP_POST_VARS['payment_status'] == 'Pending') {
 104            $comment_status .= '; ' . $HTTP_POST_VARS['pending_reason'];
 105          } elseif ( ($HTTP_POST_VARS['payment_status'] == 'Reversed') || ($HTTP_POST_VARS['payment_status'] == 'Refunded') ) {
 106            $comment_status .= '; ' . $HTTP_POST_VARS['reason_code'];
 107          }
 108  
 109          if ($HTTP_POST_VARS['mc_gross'] != number_format($total['value'] * $order['currency_value'], $currencies->get_decimal_places($order['currency']))) {
 110            $comment_status .= '; PayPal transaction value (' . tep_output_string_protected($HTTP_POST_VARS['mc_gross']) . ') does not match order value (' . number_format($total['value'] * $order['currency_value'], $currencies->get_decimal_places($order['currency'])) . ')';
 111          }
 112  
 113          $sql_data_array = array('orders_id' => $HTTP_POST_VARS['invoice'],
 114                                  'orders_status_id' => (MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID > 0 ? (int)MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID : (int)DEFAULT_ORDERS_STATUS_ID),
 115                                  'date_added' => 'now()',
 116                                  'customer_notified' => '0',
 117                                  'comments' => 'PayPal IPN Verified [' . $comment_status . ']');
 118  
 119          tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);
 120        }
 121      }
 122    } else {
 123      if (tep_not_null(MODULE_PAYMENT_PAYPAL_STANDARD_DEBUG_EMAIL)) {
 124        $email_body = '$HTTP_POST_VARS:' . "\n\n";
 125  
 126        reset($HTTP_POST_VARS);
 127        while (list($key, $value) = each($HTTP_POST_VARS)) {
 128          $email_body .= $key . '=' . $value . "\n";
 129        }
 130  
 131        $email_body .= "\n" . '$HTTP_GET_VARS:' . "\n\n";
 132  
 133        reset($HTTP_GET_VARS);
 134        while (list($key, $value) = each($HTTP_GET_VARS)) {
 135          $email_body .= $key . '=' . $value . "\n";
 136        }
 137  
 138        tep_mail('', MODULE_PAYMENT_PAYPAL_STANDARD_DEBUG_EMAIL, 'PayPal IPN Invalid Process', $email_body, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
 139      }
 140  
 141      if (isset($HTTP_POST_VARS['invoice']) && is_numeric($HTTP_POST_VARS['invoice']) && ($HTTP_POST_VARS['invoice'] > 0)) {
 142        $check_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where orders_id = '" . $HTTP_POST_VARS['invoice'] . "' and customers_id = '" . (int)$HTTP_POST_VARS['custom'] . "'");
 143        if (tep_db_num_rows($check_query) > 0) {
 144          $comment_status = $HTTP_POST_VARS['payment_status'];
 145  
 146          if ($HTTP_POST_VARS['payment_status'] == 'Pending') {
 147            $comment_status .= '; ' . $HTTP_POST_VARS['pending_reason'];
 148          } elseif ( ($HTTP_POST_VARS['payment_status'] == 'Reversed') || ($HTTP_POST_VARS['payment_status'] == 'Refunded') ) {
 149            $comment_status .= '; ' . $HTTP_POST_VARS['reason_code'];
 150          }
 151  
 152          tep_db_query("update " . TABLE_ORDERS . " set orders_status = '" . ((MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID > 0) ? MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID : DEFAULT_ORDERS_STATUS_ID) . "', last_modified = now() where orders_id = '" . $HTTP_POST_VARS['invoice'] . "'");
 153  
 154          $sql_data_array = array('orders_id' => $HTTP_POST_VARS['invoice'],
 155                                  'orders_status_id' => (MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID > 0) ? MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID : DEFAULT_ORDERS_STATUS_ID,
 156                                  'date_added' => 'now()',
 157                                  'customer_notified' => '0',
 158                                  'comments' => 'PayPal IPN Invalid [' . $comment_status . ']');
 159  
 160          tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);
 161        }
 162      }
 163    }
 164  
 165    require ('includes/application_bottom.php');
 166  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7