[ Index ] |
PHP Cross Reference of osCMax 2.0.4 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 $Id: ipn.php 3 2006-05-27 04:59:07Z user $ 4 5 osCMax Power E-Commerce 6 http://oscdox.com 7 8 Copyright 2006 osCMax 9 10 Released under the GNU General Public License 11 */ 12 13 class paypal_ipn { 14 // $_debug 0 == none, 1 == loose or 2 == strict 15 var $_debug,$_debug_string, $key,$_response_string,$_debug_email,$_paypal_ipn_id; 16 17 function paypal_ipn($email,$post_vars='',$session_id,$debug='0') { 18 $this->_debug_email = $email; 19 $this->_debug = $debug; 20 $transaction_list = array('web_accept','cart','send_money','reversal'); //accepted transactions 21 if ( !in_array($post_vars['txn_type'],$transaction_list) ) { 22 if ($this->_debug) $this->send_email(UNKNOWN_TXN_TYPE,sprintf(UNKNOWN_TXN_TYPE_MSG, $post_vars['txn_type'])); 23 } else if(strlen($post_vars['txn_id']) == 17) { 24 //Looks like a PayPal transaction 25 $this->_init($post_vars,$session_id); 26 } else { 27 if ($this->_debug) $this->send_email(UNKNOWN_POST,sprintf(UNKNOWN_POST_MSG,$_SERVER['REMOTE_ADDR'])); 28 } 29 } 30 31 //For now it seems that the only custom variable required is the session id 32 /* 33 34 function set_custom_vars($var_array,$custom_list) { 35 reset($var_array); 36 while(list($key,$val) = each ($var_array)) { 37 $customer_var = split('=',$val); 38 for($i=0;$i < count($custom_list); $i++) { 39 if( !strcmp($custom_list[$i],$customer_var[0])) $this->key[$customer_var[0]] = $customer_var[1]; 40 } 41 } 42 } 43 */ 44 function _init($post_vars,$session_id) { 45 $this->_debug_string = ''; 46 $this->key = array(); 47 $this->_response_string = 'cmd=_notify-validate'; 48 reset($post_vars); 49 foreach ($post_vars as $var => $val) { 50 if ($this->_debug) $this->_debug_string .= $var . '=' . $val .'&'; 51 if (get_magic_quotes_gpc()) $val = stripslashes($val); 52 if (!strcasecmp($var,'cmd') || !eregi("^[_0-9a-z-]{1,34}$",$var)) { 53 unset($var); unset($val); 54 } 55 if ($var != '') { 56 if(!strcmp($var,'custom')){ 57 //assumes the custom variable is always specifiec as an array 58 //$this->set_custom_vars(explode('&',$val),$custom_list); 59 $this->key[$session_id] = $val; 60 } else { 61 $this->key[$var] = $val; 62 } 63 $this->_response_string .= '&' . $var . '=' . urlencode($val); 64 } 65 } 66 unset($post_vars); 67 if ($this->_debug > 1) $this->debug_email(); 68 if(!$this->_debug) unset($this->_debug_string); 69 } 70 71 function authenticate($domain) { 72 $paypal_response = ''; 73 $curl_flag = function_exists('curl_exec'); 74 if($curl_flag) { 75 $ch = @curl_init(); 76 @curl_setopt($ch, CURLOPT_URL, "https://$domain/cgi-bin/webscr"); 77 @curl_setopt($ch, CURLOPT_POST, true); 78 @curl_setopt($ch, CURLOPT_POSTFIELDSIZE, 0); 79 @curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_response_string); 80 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 81 @curl_setopt($ch, CURLOPT_TIMEOUT, 60); 82 $paypal_response = @curl_exec($ch); 83 @curl_close($ch); 84 if($paypal_response == '') $curl_flag = false; 85 } 86 if(!$curl_flag) { 87 $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; 88 $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 89 $header .= "Content-Length: ".strlen($this->_response_string)."\r\n\r\n"; 90 $socket = 'ssl://'; $port = '443'; 91 $fp = @fsockopen ($socket.$domain,$port, $errno, $errstr, 30); 92 if(!$fp) { 93 $socket = 'tcp://'; $port = '80'; 94 $fp = @fsockopen ($socket.$domain,$port, $errno, $errstr, 30); 95 } 96 if(!$fp) { 97 $paypal_https_response = @file('https://'.$domain.'/cgi-bin/webscr?'.$this->_response_string); 98 $paypal_response = @$paypal_https_response[0]; 99 if (!$paypal_response) { 100 $paypal_http_response = @file('http://'.$domain.'/cgi-bin/webscr?'.$this->_response_string); 101 $paypal_response = @$paypal_http_response[0]; 102 if (!$paypal_response && ($this->_debug > 1)) $this->send_email(HTTP_ERROR,sprintf(HTTP_ERROR_MSG,$curl_flag,$socket,$domain,$port)); 103 } 104 } else { 105 @fputs($fp, $header . $this->_response_string); 106 while (!feof($fp)) { 107 $paypal_response .= @fgets($fp, 1024); 108 } 109 @fclose($log); 110 } 111 unset($this->_response_string); 112 } 113 114 /*if($this->_debug > 1) { 115 $log = @fopen("ipn.txt", "w"); 116 @fwrite($log,$paypal_response); 117 @fclose($log); 118 }*/ 119 120 if (strstr($paypal_response,'VERIFIED')) { 121 if($this->_debug > 1) $this->send_email(RESPONSE_VERIFIED,sprintf(RESPONSE_MSG,$curl_flag,$socket,$domain,$port,$paypal_response)); 122 return true; 123 } else if (strstr($paypal_response,'INVALID')) { 124 if($this->_debug > 1) $this->send_email(RESPONSE_INVALID,sprintf(RESPONSE_MSG,$curl_flag,$socket,$domain,$port,$paypal_response)); 125 return false; 126 } else { 127 if($this->_debug) $this->send_email(RESPONSE_UNKNOWN,sprintf(RESPONSE_MSG,$curl_flag,$socket,$domain,$port,$paypal_response)); 128 return false; 129 } 130 } 131 132 //Test both receiver email address and business ID 133 function validate_receiver_email($receiver_email,$business) { 134 if(!strcmp($receiver_email,$this->key['receiver_email']) && !strcmp($business,$this->key['business'])) { 135 if($this->_debug > 1) $this->send_email(EMAIL_RECEIVER,sprintf(EMAIL_RECEIVER_MSG,$receiver_email,$business,$this->key['receiver_email'],$this->key['business'])); 136 return true; 137 } else { 138 if($this->_debug) $this->send_email(EMAIL_RECEIVER,sprintf(EMAIL_RECEIVER_ERROR_MSG,$receiver_email,$business,$this->key['receiver_email'],$this->key['business'],$this->key['txn_id'])); 139 return false; 140 } 141 } 142 143 function unique_txn_id() { 144 $txn_id_query = tep_db_query("select paypal_ipn_id,txn_id from " . TABLE_PAYPAL_IPN . " where txn_id = '" . $this->key['txn_id'] . "'"); 145 if (!tep_db_num_rows($txn_id_query)) { //txn_id doesn't exist 146 return true; 147 } else { 148 if($this->_debug > 1) $this->send_email(TXN_DUPLICATE,sprintf(TXN_DUPLICATE_MSG,$this->key['txn_id'])); 149 return false; 150 } 151 } 152 153 //retrieve the id value of an associated parameter from db table 154 //i.e get_name_id('txn_type_id','txn_type_name',$this->key['txn_type'],$languages_id,TABLE_PAYPAL_IPN_TXN_TYPE) 155 //However this seems to be an overkill especially as we might be racing against the customer's 156 //eagerness to click the PayPal continue button! 157 /*function get_name_id($column_name,$column_id,$column_value,$languages_id,$table_name) { 158 $sql_query = tep_db_query("select ".$column_name." from " . $table_name . " where ".$column_id." = '" . $column_value . "' and language_id = '".(int)$languages_id."'"); 159 if(!tep_db_num_rows($sql_query)) { 160 return ''; 161 } else { 162 $result = tep_db_fetch_array($sql_query); 163 return $result[$column_name]; 164 } 165 }*/ 166 167 function insert_ipn_txn() { 168 //because get_name_id() is not being used, $languages_id is no longer needed 169 $txn_type_id = array ('web_accept' => 1, 'cart' => 2 , 'send_money' => 3, 'reversal' => 4 ); 170 $reason_code_id = array ('chargeback' => 1, 'guarantee' => 2 , 'buyer_complaint' => 3, 'other' => 4 ); 171 $payment_type_id = array ('instant' => 1, 'echeck' => 2 ); 172 $payment_staus_id = array ('Completed' => 1, 'Pending' => 2 , 'Failed' => 3, 'Denied' => 4, 'Refunded' => 5, 'Cancelled' => 6); 173 $pending_reason_id = array ('echeck' => 1, 'multi-currency' => 2 , 'intl' => 3, 'verify' => 4, 'address' => 5, 'upgrade' => 6, 'unilateral' => 7, 'other' => 8); 174 $mc_currency_id = array ('USD' => 1, 'GBP' => 2 , 'EUR' => 3, 'CAD' => 4, 'JPY' => 5 ); 175 $address_status_id = array ('confirmed' => 1, 'unconfirmed' => 2 ); 176 $sql_data_array = array( 177 'txn_type' => $txn_type_id[$this->key['txn_type']], 178 'reason_code' => $reason_code_id[$this->key['reason_code']], 179 'payment_type' => $payment_type_id[$this->key['payment_type']], 180 'payment_status' => $payment_staus_id[$this->key['payment_status']], 181 'pending_reason' => $pending_reason_id[$this->key['pending_reason']], 182 'invoice' => $this->key['invoice'], 183 'mc_currency' => $mc_currency_id[$this->key['mc_currency']], 184 'first_name' => $this->key['first_name'], 185 'last_name' => $this->key['last_name'], 186 'payer_business_name' => $this->key['payer_business_name'], 187 'address_name' => $this->key['address_name'], 188 'address_street' => $this->key['address_street'], 189 'address_city' => $this->key['address_city'], 190 'address_state' => $this->key['address_state'], 191 'address_zip' => $this->key['address_zip'], 192 'address_country' => $this->key['address_country'], 193 'address_status' => $address_status_id[$this->key['address_status']], 194 'address_owner' => $this->key['address_owner'], 195 'payer_email' => $this->key['payer_email'], 196 'ebay_address_id' => $this->key['ebay_address_id'], 197 'payer_id' => $this->key['payer_id'], 198 'payer_status' => $this->key['payer_status'], 199 'payment_date' => $this->key['payment_date'], 200 'business' => $this->key['business'], 201 'receiver_email' => $this->key['receiver_email'], 202 'receiver_id' => $this->key['receiver_id'], 203 'paypal_address_id' => $this->key['papal_address_id'], 204 'txn_id' => $this->key['txn_id'], 205 'notify_version' => $this->key['notify_version'], 206 'verify_sign' => $this->key['verify_sign'], 207 'date_added' => 'now()'); 208 tep_db_perform(TABLE_PAYPAL_IPN, $sql_data_array); 209 $this->_paypal_ipn_id = tep_db_insert_id(); 210 $sql_data_array = array( 211 'paypal_ipn_id' => $this->_paypal_ipn_id, 212 'num_cart_items' => $this->key['txn_type'] == 'cart' ? $this->key['num_cart_items'] : 1, 213 'mc_gross' => $this->key['mc_gross'], 214 'mc_fee' => $this->key['mc_fee'], 215 'payment_gross' => $this->key['payment_gross'], 216 'payment_fee' => $this->key['payment_fee'], 217 'settle_amount' => $this->key['settle_amount'], 218 'settle_currency' => $this->key['settle_currency'], 219 'exchange_rate' => $this->key['exchange_rate']); 220 tep_db_perform(TABLE_PAYPAL_IPN_ORDERS, $sql_data_array); 221 $sql_data_array = array( 222 'paypal_ipn_id' => $this->_paypal_ipn_id, 223 'memo' => tep_db_prepare_input($this->key['memo'])); 224 tep_db_perform(TABLE_PAYPAL_IPN_ORDERS_MEMO, $sql_data_array); 225 if($this->_debug > 1) $this->send_email(IPN_TXN_INSERT,sprintf(IPN_TXN_INSERT_MSG,$this->_paypal_ipn_id)); 226 } 227 228 function get_paypal_ipn_id() { 229 return $this->_paypal_ipn_id; 230 } 231 232 //returns the IPN transaction type 233 //For Version 1.5 they are web_accept,cart,send_money and reversal 234 function txn_type() { 235 return $this->key['txn_type']; 236 } 237 238 //Debug function to output the IPN to the display 239 function debug_info() { 240 $debug_string = ''; 241 reset($this->key); 242 foreach ($this->key as $var => $val) $debug_string .= "$var => $val\n<br/>"; 243 return $debug_string; 244 } 245 246 //Debug function to email the PayPal account holder 247 function debug_email() { 248 $debug_mail_string = sprintf(DEBUG_MSG,str_replace('&', "\r\n", $this->_debug_string ),str_replace('&', "\r\n", $this->_response_string )); 249 $this->send_email(DEBUG,$debug_mail_string); 250 } 251 252 function send_email($subject='',$msg='') { 253 $this->_email('', $this->_debug_email, $subject, nl2br($msg), STORE_NAME, $this->_debug_email); 254 } 255 256 function _email($to_name,$to_address,$subject,$text,$from_name,$from_address) { 257 $from_address = strtolower(trim($from_address)); 258 $subject = STORE_NAME." PayPal IPN: ".$subject; 259 $msg = strip_tags($text); 260 tep_mail($to_name, $to_address, $subject, $msg, $from_name, $from_address); 261 } 262 }//end class 263 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Jan 1 13:43:16 2010 | Cross-referenced by PHPXref 0.7 |