|
|
|
PHP - สอบถามวิธีรับค่า IPN ที่ Paypal ส่งกลับโดยเอาข้อมูลที่ได้มาใส่ใน Server ของเรา |
|
|
|
|
|
|
|
ผมลอง ใช้ตัว IPN simulator
https://developer.paypal.com/webapps/developer/applications/ipn_simulator
ส่งค่า ดูก็สามารถรับที่ส่งกลับมาได้อ่าครับ
แต่พอลองจ่ายจริงมันไม่สามารถรับค่าIPN ที่ส่งกลับมาจาก paypal ได้ อ่าครับ ผมต้องแก้ตรงไหนอ่ะครับ
paypal.inc.php
<?php
/*
* This file contain paypal settings and some functions.
* Taken from "PHP Toolkit for PayPal v0.50" with some stuff
* removed ( because i don't need them ) and slightly modified
*
* */
$AB = $_REQUEST['amount'];
$_POST['tax'] = "5";
//echo $AB;
$INV = ", Invoice No : ".$PForder['OD_verify_code'];
$paypal = array();
$_POST['quantity'] = 1;
$paypal['business'] = "info@________.com";
$paypal['site_url'] = "http://www.________.com";
$paypal['image_url'] = "http://www.________.com/images/logo.jpg";
$paypal['success_url'] = "http://www.________.com/function/paypal/PaypalSuccess.php";
$paypal['cancel_url'] = "http://www.________.com/function/paypal/PaypalError.php";
$paypal['notify_url'] = "http://www.________.com/function/paypal/paypal_ipn.php";
$paypal['return_method'] = "2"; //1=GET 2=POST --> Use post since we will need the return values to check if order is valid
$paypal['currency_code'] = "THB"; //['USD,GBP,JPY,CAD,EUR'] unitmoney
$paypal['lc'] = "US"; //language
$paypal['url'] = "https://www.paypal.com/cgi-bin/webscr";
//$paypal['url'] = "https://www.sandbox.paypal.com/cgi-bin/webscr";
$paypal['post_method'] = "fso"; //fso=fsockopen(); curl=curl command line libCurl=php compiled with libCurl support
$paypal['curl_location'] = "/usr/local/bin/curl";
$paypal['bn'] = "toolkit-php";
$paypal['cmd'] = "_xclick";
//Payment Page Settings
$paypal['display_comment'] = "0"; //0=yes 1=no
$paypal['comment_header'] = "Comments";
$paypal['continue_button_text'] = "Continue >>";
$paypal['background_color'] = ""; //""=white 1=black
$paypal['display_shipping_address'] = "1"; //""=yes 1=no --> We already asked for the shipping address so tell paypal not to ask it again
$paypal['display_comment'] = "1"; //""=yes 1=no
//Product Settings
$paypal['item_name'] = isset($INV) ? $INV: "";
$paypal['item_number'] = isset($PForder['ID_Order']) ? $PForder['ID_Order']: "";
$paypal['amount'] = isset($AB) ? $AB: "";
$paypal['on0'] = isset($_POST['on0']) ? $_POST['on0']: "";
$paypal['os0'] = isset($_POST['os0']) ? $_POST['os0']: "";
$paypal['on1'] = isset($_POST['on1']) ? $_POST['on1']: "";
$paypal['os1'] = isset($_POST['os1']) ? $_POST['os1']: "";
$paypal['quantity'] = isset($_POST['quantity']) ? $_POST['quantity']: "";
$paypal['edit_quantity'] = ""; //1=yes ""=no
$paypal['invoice'] = isset($PForder['OD_verify_code']) ? $PForder['OD_verify_code']: "";
$paypal['tax'] = isset($_POST['tax']) ? $_POST['tax']: "";
//Shipping and Taxes
$paypal['shipping_amount'] = isset($PForder['OD_Chipping']) ? $PForder['OD_Chipping']: "";
$paypal['shipping_amount_per_item'] = "";
$paypal['handling_amount'] = "";
$paypal['custom_field'] = "";
//Customer Settings
$paypal['firstname'] = isset($PForder['OD_FN_Send']) ? $PForder['OD_FN_Send']: "";
$paypal['lastname'] = isset($PForder['OD_LN_Send']) ? $PForder['OD_LN_Send']: "";
$paypal['address1'] = isset($PForder['OD_AD_Send']) ? $PForder['OD_AD_Send']: "";
$paypal['address2'] = isset($PForder['OD_AD2_Send']) ? $PForder['OD_AD2_Send']: "";
$paypal['city'] = isset($PForder['OD_CY_Send']) ? $PForder['OD_CY_Send']: "";
$paypal['state'] = isset($PForder['OD_SP_Send']) ? $PForder['OD_SP_Send']: "";
$paypal['email'] = isset($PForder['OD_EM_Send']) ? $PForder['OD_EM_Send']: "";
$paypal['country'] = isset($PForder['OD_CT_Send']) ? $PForder['OD_CT_Send']: "";
$paypal['zip'] = isset($PForder['OD_ZP_Send']) ? $PForder['OD_ZP_Send']: "";
$paypal['phone_1'] = isset($PForder['OD_PH_Send']) ? $PForder['OD_PH_Send']: "";
$paypal['phone_2'] = isset($PForder['OD_PH2_Send']) ? $PForder['OD_PH2_Send']: "";
$paypal['phone_3'] = isset($PForder['OD_PH3_Send']) ? $PForder['OD_PH3_Send']: "";
//country
/********************************************************************************
*
* PAYPAL FUNCTIONS
*
********************************************************************************/
//create variable names to perform additional order processing
function create_local_variables()
{
$array_name = array();
$array_name['business'] = $_POST['business'];
$array_name['receiver_email'] = $_POST['receiver_email'];
$array_name['receiver_id'] = $_POST['receiver_id'];
$array_name['item_name'] = $_POST['item_name'];
$array_name['item_number'] = $_POST['item_number'];
$array_name['quantity'] = $_POST['quantity'];
$array_name['invoice'] = $_POST['invoice'];
$array_name['custom'] = $_POST['custom'];
$array_name['memo'] = $_POST['memo'];
$array_name['tax'] = $_POST['tax'];
$array_name['option_name1'] = $_POST['option_name1'];
$array_name['option_selection1'] = $_POST['option_selection1'];
$array_name['option_name2'] = $_POST['option_name2'];
$array_name['option_selection2'] = $_POST['option_selection2'];
$array_name['num_cart_items'] = $_POST['num_cart_items'];
$array_name['mc_gross'] = $_POST['mc_gross'];
$array_name['mc_fee'] = $_POST['mc_fee'];
$array_name['mc_currency'] = $_POST['mc_currency'];
$array_name['settle_amount'] = $_POST['settle_amount'];
$array_name['settle_currency'] = $_POST['settle_currency'];
$array_name['exchange_rate'] = $_POST['exchange_rate'];
$array_name['payment_gross'] = $_POST['payment_gross'];
$array_name['payment_fee'] = $_POST['payment_fee'];
$array_name['payment_status'] = $_POST['payment_status'];
$array_name['pending_reason'] = $_POST['pending_reason'];
$array_name['reason_code'] = $_POST['reason_code'];
$array_name['payment_date'] = $_POST['payment_date'];
$array_name['txn_id'] = $_POST['txn_id'];
$array_name['txn_type'] = $_POST['txn_type'];
$array_name['payment_type'] = $_POST['payment_type'];
$array_name['for_auction'] = $_POST['for_auction'];
$array_name['auction_buyer_id'] = $_POST['auction_buyer_id'];
$array_name['auction_closing_date'] = $_POST['auction_closing_date'];
$array_name['auction_multi_item'] = $_POST['auction_multi_item'];
$array_name['first_name'] = $_POST['first_name'];
$array_name['last_name'] = $_POST['last_name'];
$array_name['payer_business_name'] = $_POST['payer_business_name'];
$array_name['address_name'] = $_POST['address_name'];
$array_name['address_street'] = $_POST['address_street'];
$array_name['address_city'] = $_POST['address_city'];
$array_name['address_state'] = $_POST['address_state'];
$array_name['address_zip'] = $_POST['address_zip'];
$array_name['address_country'] = $_POST['address_country'];
$array_name['address_status'] = $_POST['address_status'];
$array_name['payer_email'] = $_POST['payer_email'];
$array_name['payer_id'] = $_POST['payer_id'];
$array_name['payer_status'] = $_POST['payer_status'];
$array_name['notify_version'] = $_POST['notify_version'];
$array_name['verify_sign'] = $_POST['verify_sign'];
return $array_name;
}
//this function creates a comma separated value file from an array.
function create_csv_file($file,$data)
{
// the return value
$success = false;
//check for array
if (is_array($data)) {
$post_values = array_values($data);
//build csv data
foreach ($post_values as $i) {
$csv.="\"$i\",";
}
//remove the last comma from string
$csv = substr($csv,0,-1);
//check for existence of file
if(file_exists($file) && is_writeable($file)) {
$mode="a";
} else {
$mode="w";
}
//create file pointer
$fp=@fopen($file,$mode);
//write to file
fwrite($fp,$csv . "\n");
//close file pointer
fclose($fp);
$success = true;
}
return $success;
}
//posts transaction data using fsockopen.
function fsockPost($url,$data)
{
$postData = '';
// return value
$info = '';
//Parse url
$web=parse_url($url);
//build post string
foreach ($data as $i=>$v) {
$postData.= $i . "=" . urlencode($v) . "&";
}
// we must append cmd=_notify-validate to the POST string
// so paypal know that this is a confirmation post
$postData .= "cmd=_notify-validate";
//Set the port number
if ($web['scheme'] == "https") {
$web['port'] = "443";
$ssl = "ssl://";
} else {
$web['port'] = "80";
$ssl = "";
}
//Create paypal connection
$fp = @fsockopen($ssl . $web[host], $web[port], $errnum, $errstr,30);
//Error checking
if(!$fp) {
echo "$errnum: $errstr";
} else {
//Post Data
fputs($fp, "POST $web[path] HTTP/1.1\r\n");
fputs($fp, "Host: $web[host]\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ".strlen($postData)."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $postData . "\r\n\r\n");
// loop through the response from the server
$info = array();
while (!feof($fp)) {
$info[] = @fgets($fp, 1024);
}
//close fp - we are done with it
fclose($fp);
// join the results into a string separated by comma
$info = implode(",", $info);
}
return $info;
}
//Display Paypal Hidden Variables
function showVariables() {
global $paypal;
?>
<!-- PayPal Configuration -->
<input type="hidden" name="business" value="<?php echo $paypal['business']?>">
<input type="hidden" name="cmd" value="<?php echo $paypal['cmd']?>">
<input type="hidden" name="image_url" value="<?php echo "{$paypal['site_url']}{$paypal['image_url']}"; ?>">
<input type="hidden" name="return" value="<?php echo "{$paypal['site_url']}{$paypal['success_url']}"; ?>">
<input type="hidden" name="cancel_return" value="<?php echo "{$paypal['site_url']}{$paypal['cancel_url']}"; ?>">
<input type="hidden" name="notify_url" value="<?php echo "{$paypal['site_url']}{$paypal['notify_url']}"; ?>">
<input type="hidden" name="rm" value="<?php echo $paypal['return_method']?>">
<input type="hidden" name="currency_code" value="<?php echo $paypal['currency_code']?>">
<input type="hidden" name="lc" value="<?php echo $paypal['lc']?>">
<input type="hidden" name="bn" value="<?php echo $paypal['bn']?>">
<input type="hidden" name="cbt" value="<?php echo $paypal['continue_button_text']?>">
<!-- Payment Page Information -->
<input type="hidden" name="no_shipping" value="<?php echo $paypal['display_shipping_address']?>">
<input type="hidden" name="no_note" value="<?php echo $paypal['display_comment']?>">
<input type="hidden" name="cn" value="<?php echo $paypal['comment_header']?>">
<input type="hidden" name="cs" value="<?php echo $paypal['background_color']?>">
<!-- Product Information -->
<input type="hidden" name="item_name" value="<?php echo $paypal['item_name']?>">
<input type="hidden" name="amount" value="<?php echo $paypal['amount']?>">
<input type="hidden" name="quantity" value="<?php echo $paypal['quantity']?>">
<input type="hidden" name="item_number" value="<?php echo $paypal['item_number']?>">
<input type="hidden" name="undefined_quantity" value="<?php echo $paypal['edit_quantity']?>">
<input type="hidden" name="on0" value="<?php echo $paypal['on0']?>">
<input type="hidden" name="os0" value="<?php echo $paypal['os0']?>">
<input type="hidden" name="on1" value="<?php echo $paypal['on1']?>">
<input type="hidden" name="os1" value="<?php echo $paypal['os1']?>">
<!-- Shipping and Misc Information -->
<input type="hidden" name="shipping" value="<?php echo $paypal['shipping_amount']?>">
<input type="hidden" name="shipping2" value="<?php echo $paypal['shipping_amount_per_item']?>">
<input type="hidden" name="handling" value="<?php echo $paypal['handling_amount']?>">
<input type="hidden" name="tax" value="<?php echo $paypal['tax']?>">
<input type="hidden" name="custom" value="<?php echo $paypal['custom_field']?>">
<input type="hidden" name="invoice" value="<?php echo $paypal['invoice']?>">
<!-- Customer Information -->
<input type="hidden" name="first_name" value="<?php echo $paypal['firstname']?>">
<input type="hidden" name="last_name" value="<?php echo $paypal['lastname']?>">
<input type="hidden" name="address1" value="<?php echo $paypal['address1']?>">
<input type="hidden" name="address2" value="<?php echo $paypal['address2']?>">
<input type="hidden" name="city" value="<?php echo $paypal['city']?>">
<input type="hidden" name="state" value="<?php echo $paypal['state']?>">
<input type="hidden" name="zip" value="<?php echo $paypal['zip']?>">
<input type="hidden" name="email" value="<?php echo $paypal['email']?>">
<input type="hidden" name="night_phone_a" value="<?php echo $paypal['phone_1']?>">
<input type="hidden" name="night_phone_b" value="<?php echo $paypal['phone_2']?>">
<input type="hidden" name="night_phone_c" value="<?php echo $paypal['phone_3']?>">
<?php
}
?>
IPN
<?php
// CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
// Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
// Set this to 0 once you go live or don't require logging.
error_reporting (E_ALL & ~E_NOTICE & ~E_DEPRECATED);
@include_once("../../setting/DatabaseSetting.php");
@include("../OrderDataAccess.php");
define("DEBUG", 1);
// Set to 0 once you're ready to go live
define("USE_SANDBOX", 1);
define("LOG_FILE", "./ipn.log");
$address_street = isset($_REQUEST['address_street']) ? $_REQUEST['address_street']: "";
$address_name = isset($_REQUEST['address_name']) ? $_REQUEST['address_name']: "";
$address_status = isset($_REQUEST['address_status']) ? $_REQUEST['address_status']: "";
$address_city = isset($_REQUEST['address_city']) ? $_REQUEST['address_city']: "";
$address_county_code = isset($_REQUEST['address_county_code']) ? $_REQUEST['address_county_code']: "";
$address_county = isset($_REQUEST['address_county']) ? $_REQUEST['address_county']: "";
$address_zip = isset($_REQUEST['address_zip']) ? $_REQUEST['address_zip']: "";
$mc_gross = isset($_REQUEST['mc_gross']) ? $_REQUEST['mc_gross']: "";
$mc_shipping = isset($_REQUEST['mc_shipping']) ? $_REQUEST['mc_shipping']: "";
$mc_fee = isset($_REQUEST['mc_fee']) ? $_REQUEST['mc_fee']: "";
$mc_handling = isset($_REQUEST['mc_handling']) ? $_REQUEST['mc_handling']: "";
$payer_id = isset($_REQUEST['payer_id']) ? $_REQUEST['payer_id']: "";
$payer_status = isset($_REQUEST['payer_status']) ? $_REQUEST['payer_status']: "";
$payer_email = isset($_REQUEST['payer_email']) ? $_REQUEST['payer_email']: "";
$payment_date = isset($_REQUEST['payment_date']) ? $_REQUEST['payment_date']: "";
$payment_status = isset($_REQUEST['payment_status']) ? $_REQUEST['payment_status']: "";
$payment_type = isset($_REQUEST['payment_type']) ? $_REQUEST['payment_type']: "";
$protection_eligibility = isset($_REQUEST['protection_eligibility']) ? $_REQUEST['protection_eligibility']: "";
$settle_amount = isset($_REQUEST['settle_amount']) ? $_REQUEST['settle_amount']: "";
$custom = isset($_REQUEST['custom']) ? $_REQUEST['custom']: "";
$business = isset($_REQUEST['business']) ? $_REQUEST['business']: "";
$charset = isset($_REQUEST['charset']) ? $_REQUEST['charset']: "";
$tax = isset($_REQUEST['tax']) ? $_REQUEST['tax']: "";
$first_name = isset($_REQUEST['first_name']) ? $_REQUEST['first_name']: "";
$txn_id = isset($_REQUEST['txn_id']) ? $_REQUEST['txn_id']: "";
$exchange_rate = isset($_REQUEST['exchange_rate']) ? $_REQUEST['exchange_rate']: "";
$notify_version = isset($_REQUEST['notify_version']) ? $_REQUEST['notify_version']: "";
$settle_currency = isset($_REQUEST['settle_currency']) ? $_REQUEST['settle_currency']: "";
$verify_sign = isset($_REQUEST['verify_sign']) ? $_REQUEST['verify_sign']: "";
// Read POST data
// reading posted data directly from $_REQUEST causes serialization
// issues with array data in POST. Reading raw POST data from input stream instead.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
// Post IPN data back to PayPal to validate the IPN data is genuine
// Without this step anyone can fake IPN data
if(USE_SANDBOX == true) {
$paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
$paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}
$ch = curl_init($paypal_url);
if ($ch == FALSE) {
return FALSE;
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(DEBUG == true) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}
// CONFIG: Optional proxy configuration
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
// Set TCP timeout to 30 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below. Ensure the file is readable by the webserver.
// This is mandatory for some environments.
//$cert = __DIR__ . "./cacert.pem";
//curl_setopt($ch, CURLOPT_CAINFO, $cert);
$res = curl_exec($ch);
if (curl_errno($ch) != 0) // cURL error
{
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
exit;
} else {
// Log the entire HTTP response if debug is switched on.
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:".
curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
// Split response headers and payload
list($headers, $res) = explode("\r\n\r\n", $res, 2);
}
curl_close($ch);
}
// Inspect IPN validation result and act accordingly
if (strcmp ($res, "VERIFIED") == 0) {
// check whether the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your PayPal email
// check that payment_amount/payment_currency are correct
// process payment and mark item as paid.
// assign posted variables to local variables
//$item_name = $_REQUEST['item_name'];
//$item_number = $_REQUEST['item_number'];
//$payment_status = $_REQUEST['payment_status'];
//$payment_amount = $_REQUEST['mc_gross'];
//$payment_currency = $_REQUEST['mc_currency'];
//$txn_id = $_REQUEST['txn_id'];
//$receiver_email = $_REQUEST['receiver_email'];
//$payer_email = $_REQUEST['payer_email'];
Backpaypal($address_street,$address_name,$address_status,
$address_city,$address_county_code,$address_county,$address_zip,$mc_gross,$mc_shipping,$mc_fee,$mc_handling,
$payer_id,$payer_status,$payer_email,$payment_date,$payment_status,
$payment_type,$protection_eligibility,$settle_amount,
$custom,$business,$charset,$tax,$first_name,$txn_id,$exchange_rate,$notify_version,$settle_currency,$verify_sign);
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
}
} else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
// Add business logic here which deals with invalid IPN messages
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
}
}
?>
|
ประวัติการแก้ไข 2014-11-27 23:10:13 2014-11-27 23:10:55 2014-11-27 23:17:05
|
|
|
|
Date :
2014-11-27 23:09:03 |
By :
kamasaigis2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|