📄 paypal_functions.php
字号:
* Create order record from IPN data
*/
function ipn_create_order_array($new_order_id, $txn_type) {
$sql_data_array = array('order_id' => $new_order_id,
'txn_type' => $txn_type,
'module_name' => 'paypal (ipn-handler)',
'module_mode' => 'IPN',
'reason_code' => $_POST['reason_code'],
'payment_type' => $_POST['payment_type'],
'payment_status' => $_POST['payment_status'],
'pending_reason' => $_POST['pending_reason'],
'invoice' => $_POST['invoice'],
'mc_currency' => $_POST['mc_currency'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'payer_business_name' => $_POST['payer_business_name'],
'address_name' => $_POST['address_name'],
'address_street' => $_POST['address_street'],
'address_city' => $_POST['address_city'],
'address_state' => $_POST['address_state'],
'address_zip' => $_POST['address_zip'],
'address_country' => $_POST['address_country'],
'address_status' => $_POST['address_status'],
'payer_email' => $_POST['payer_email'],
'payer_id' => $_POST['payer_id'],
'payer_status' => $_POST['payer_status'],
'payment_date' => datetime_to_sql_format($_POST['payment_date']),
'business' => $_POST['business'],
'receiver_email' => $_POST['receiver_email'],
'receiver_id' => $_POST['receiver_id'],
'txn_id' => $_POST['txn_id'],
'parent_txn_id' => $_POST['parent_txn_id'],
'num_cart_items' => $_POST['num_cart_items'],
'mc_gross' => $_POST['mc_gross'],
'mc_fee' => $_POST['mc_fee'],
'settle_amount' => $_POST['settle_amount'],
'settle_currency' => $_POST['settle_currency'],
'exchange_rate' => $_POST['exchange_rate'],
'notify_version' => $_POST['notify_version'],
'verify_sign' => $_POST['verify_sign'],
'date_added' => 'now()',
'memo' => $_POST['memo']
);
return $sql_data_array;
}
/**
* Create order-history record from IPN data
*/
function ipn_create_order_history_array($insert_id) {
$sql_data_array = array ('paypal_ipn_id' => $insert_id,
'txn_id' => $_POST['txn_id'],
'parent_txn_id' => $_POST['parent_txn_id'],
'payment_status' => $_POST['payment_status'],
'pending_reason' => $_POST['pending_reason'],
'date_added' => 'now()'
);
return $sql_data_array;
}
/**
* Create order-update from IPN data
*/
function ipn_create_order_update_array($txn_type) {
$sql_data_array = array('payment_type' => $_POST['payment_type'],
'txn_type' => $txn_type,
'parent_txn_id' => $_POST['parent_txn_id'],
'payment_status' => $_POST['payment_status'],
'pending_reason' => $_POST['pending_reason'],
'payer_email' => $_POST['payer_email'],
'payer_id' => $_POST['payer_id'],
'business' => $_POST['business'],
'receiver_email' => $_POST['receiver_email'],
'receiver_id' => $_POST['receiver_id'],
'notify_version' => $_POST['notify_version'],
'verify_sign' => $_POST['verify_sign'],
'last_modified' => 'now()'
);
if (isset($_POST['payer_business_name']) && $_POST['payer_business_name'] != '') $sql_data_array = array_merge($sql_data_array,
array('payer_business_name' => $_POST['payer_business_name']));
if (isset($_POST['address_name']) && $_POST['address_name'] != '') $sql_data_array = array_merge($sql_data_array,
array('address_name' => $_POST['address_name'],
'address_street' => $_POST['addrss_street'],
'address_city' => $_POST['address_city'],
'address_state' => $_POST['address_state'],
'address_zip' => $_POST['address_zip'],
'address_country' => $_POST['address_country']));
if (isset($_POST['reason_code']) && $_POST['reason_code'] != '') $sql_data_array = array_merge($sql_data_array, array('reason_code' => $_POST['reason_code']));
if (isset($_POST['invoice']) && $_POST['invoice'] != '') $sql_data_array = array_merge($sql_data_array, array('invoice' => $_POST['invoice']));
if (isset($_POST['mc_gross']) && $_POST['mc_gross'] > 0) $sql_data_array = array_merge($sql_data_array, array('mc_gross' => $_POST['mc_gross']));
if (isset($_POST['mc_fee']) && $_POST['mc_fee'] > 0) $sql_data_array = array_merge($sql_data_array, array('mc_fee' => $_POST['mc_fee']));
if (isset($_POST['settle_amount']) && $_POST['settle_amount'] > 0) $sql_data_array = array_merge($sql_data_array, array('settle_amount' => $_POST['settle_amount']));
if (isset($_POST['first_name']) && $_POST['first_name'] != '') $sql_data_array = array_merge($sql_data_array, array('first_name' => $_POST['first_name']));
if (isset($_POST['last_name']) && $_POST['last_name'] != '') $sql_data_array = array_merge($sql_data_array, array('last_name' => $_POST['last_name']));
if (isset($_POST['mc_currency']) && $_POST['mc_currency'] != '') $sql_data_array = array_merge($sql_data_array, array('mc_currency' => $_POST['mc_currency']));
if (isset($_POST['settle_currency']) && $_POST['settle_currency'] != '') $sql_data_array = array_merge($sql_data_array, array('settle_currency' => $_POST['settle_currency']));
if (isset($_POST['num_cart_items']) && $_POST['num_cart_items'] > 0) $sql_data_array = array_merge($sql_data_array, array('num_cart_items' => $_POST['num_cart_items']));
if (isset($_POST['exchange_rate']) && $_POST['exchange_rate'] > 0) $sql_data_array = array_merge($sql_data_array, array('exchange_rate' => $_POST['exchange_rate']));
return $sql_data_array;
}
/**
* simulator
*/
function ipn_simulate_ipn_handler($count) {
global $db;
$sql = "select * from " . TABLE_PAYPAL_TESTING . " order by paypal_ipn_id desc limit " . (int)$count;
$paypal_testing = $db->execute($sql);
while (!$paypal_testing->EOF) {
$paypal_fields[] = $paypal_testing->fields;
$paypal_testing->moveNext();
}
$paypal_fields = array_reverse($paypal_fields);
foreach ($paypal_fields as $value) {
foreach($value as $i=>$v) {
$postdata .= $i . "=" . urlencode(stripslashes($v)) . "&";
}
$address = HTTP_SERVER . DIR_WS_CATALOG . 'ipn_main_handler.php?' . $postdata;
$response = ipn_fopen($address);
echo $response;
}
}
/**
* Debug to file
*/
function ipn_fopen($filename) {
$response = '';
$fp = @fopen($filename,'rb');
if ($fp) {
$response = getRequestBodyContents($fp);
fclose($fp);
}
return $response;
}
function getRequestBodyContents(&$handle) {
if ($handle) {
while(!feof($handle)) {
$line .= @fgets($handle, 1024);
}
return $line;
}
return false;
}
/**
* Verify IPN by sending it back to PayPal for confirmation
*/
function ipn_postback($mode = 'IPN') {
$info = '';
$header = '';
$scheme = 'http://';
//if (ENABLE_SSL == 'true') $scheme = 'https://';
//Parse url
$web = parse_url($scheme . (defined('MODULE_PAYMENT_PAYPAL_HANDLER') ? MODULE_PAYMENT_PAYPAL_HANDLER : 'www.paypal.com/cgi-bin/webscr'));
if (isset($_POST['test_ipn']) && $_POST['test_ipn'] == 1) {
$web = parse_url($scheme . 'www.sandbox.paypal.com/cgi-bin/webscr');
}
//build post string
$postdata = '';
$postback = '';
$postback_array = array();
foreach($_POST as $key=>$value) {
$postdata .= $key . "=" . urlencode(stripslashes($value)) . "&";
$postback .= $key . "=" . urlencode(stripslashes($value)) . "&";
$postback_array[$key] = $value;
}
if ($mode == 'PDT') {
$postback .= "cmd=_notify-synch";
$postback .= "&tx=" . $_GET['tx'];
$postback .= "&at=" . MODULE_PAYMENT_PAYPAL_PDTTOKEN;
$postback_array['cmd'] = "_notify-sync";
$postback_array['tx'] = $_GET['tx'];
$postback_array['at'] = substr(MODULE_PAYMENT_PAYPAL_PDTTOKEN, 0, 5) . '**********' . substr(MODULE_PAYMENT_PAYPAL_PDTTOKEN,-5);
} elseif ($mode == 'IPN') {
$postback .= "cmd=_notify-validate";
$postback_array['cmd'] = "_notify-validate";
}
if ($postdata == '=&') {
ipn_debug_email('IPN FATAL ERROR :: No POST data to process -- Bad IPN data');
return array('info' => $info, 'postdata' => $postdata );
}
$postdata_array = $_POST;
ksort($postdata_array);
if ($mode == 'IPN') {
ipn_debug_email('IPN INFO - POST VARS received (sorted):' . "\n" . stripslashes(urldecode(print_r($postdata_array, true))));
if (sizeof($postdata_array) == 0) die('Nothing to process. Please return to home page.');
}
if (MODULE_PAYMENT_PAYPAL_TESTING == 'Test') {
$info = "VERIFIED";
ipn_debug_email('IPN INFO - POST VARS sent back for validation: ' . "\n" . 'TEST MODE.' . "\n" . stripslashes(print_r($postback_array, true)));
} else {
//Set the port number
if($web['scheme'] == "https") {
$web['port']="443"; $ssl = "ssl://";
} else {
$web['port']="80"; $ssl = "";
}
$proxy = $web;
if (CURL_PROXY_REQUIRED == 'True' && CURL_PROXY_SERVER_DETAILS != '') {
$proxy = parse_url($scheme . CURL_PROXY_SERVER_DETAILS);
$ssl = ($ssl == '') ? 'http://' : $ssl;
}
//Post Data
if (CURL_PROXY_REQUIRED == 'True' && CURL_PROXY_SERVER_DETAILS != '') {
$header = "POST " . $ssl . $web[host] . $web[path] . " HTTP/1.1\r\n";
$header .= "Host: $proxy[host]\r\n";
} else {
$header = "POST $web[path] HTTP/1.1\r\n";
$header .= "Host: $web[host]\r\n";
}
$header .= "Content-type: application/x-www-form-urlencoded\r\n";
$header .= "Content-length: " . strlen($postback) . "\r\n";
$header .= "Connection: close\r\n\r\n";
ipn_debug_email('IPN INFO - POST VARS to be sent back for validation: ' . "\n" . 'To: ' . $ssl . $proxy['host'] . ':' . $proxy['port'] . "\n" . $header . stripslashes(print_r($postback_array, true)));
//Create paypal connection
if (MODULE_PAYMENT_PAYPAL_IPN_DEBUG == 'Yes') {
$fp=fsockopen($ssl . $proxy['host'], $proxy['port'], $errnum, $errstr, 30);
} else {
$fp=@fsockopen($ssl . $proxy['host'], $proxy['port'], $errnum, $errstr, 30);
}
if(!$fp) {
ipn_debug_email('IPN FATAL ERROR :: Could not establish fsockopen. ' . "\n" . 'Host Details = ' . $ssl . $proxy['host'] . ':' . $proxy['port'] . ' (' . $errnum . ') ' . $errstr . "\n" . (CURL_PROXY_REQUIRED == 'True' && CURL_PROXY_SERVER_DETAILS != '' ? "\n" . $ssl . $web[host] . $web[path] : '') . "\n Trying again without SSL ...");
$ssl = 'http://';
$proxy['port'] = '80';
$fp=@fsockopen($ssl . $proxy['host'], $proxy['port'], $errnum, $errstr, 30);
}
if(!$fp) {
ipn_debug_email('IPN FATAL ERROR :: Could not establish fsockopen. ' . "\n" . 'Host Details = ' . $ssl . $proxy['host'] . ':' . $proxy['port'] . ' (' . $errnum . ') ' . $errstr . "\n" . (CURL_PROXY_REQUIRED == 'True' && CURL_PROXY_SERVER_DETAILS != '' ? "\n" . $ssl . $web[host] . $web[path] : '') . "\n Trying again without specified protocol ...");
$ssl = '';
$fp=@fsockopen($ssl . $proxy['host'], $proxy['port'], $errnum, $errstr, 30);
}
if(!$fp) {
ipn_debug_email('IPN FATAL ERROR :: Could not establish fsockopen. ' . "\n" . 'Host Details = ' . $ssl . $proxy['host'] . ':' . $proxy['port'] . ' (' . $errnum . ') ' . $errstr . "\n" . (CURL_PROXY_REQUIRED == 'True' && CURL_PROXY_SERVER_DETAILS != '' ? "\n" . $ssl . $web[host] . $web[path] : ''));
die();
}
fputs($fp, $header . $postback . "\r\n\r\n");
$header_data = '';
//loop through the response from the server
while(!feof($fp)) {
$line = @fgets($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// this is a header row
$headerdone = true;
$header_data .= $line;
} else if ($headerdone) {
// header has been read. now read the contents
$info[] = $line;
}
}
//close fp - we are done with it
fclose($fp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -