这是代码表单页 根据不同,可以先设置为 沙盒测试 然后在该为正式的
<!-- <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="book1"> -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="book1">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="add" value="1">
//paypal账号开通后,会给你分发 一个商户邮箱,一个买家邮箱 以便进行测试
<!-- <input type="hidden" name="business" value="finance-facilitator@ctmaster.cn"> -->
<input type="hidden" name="business" value="正式邮箱">
<input type="hidden" name="cancel_return" value="取消支付,返回的网址">
<input type="hidden" name="lc" value="US">
<!--商品名称-->
<input type="hidden" name="item_name" value="商品名称">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="item_number" value="商品编号">
<input type="hidden" name="amount" value="商品价格">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="return" value="购买成功后返回的网址">
<input type="hidden" name="no_note" value="1">
<!--no_shipping=1 不需要送货地址-->
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="charset" value="utf-8">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="notify_url" value="购买成功后,给服务器端返回的回调地址">
<input type="hidden" name="on0" value="其他商户参数">
<input type="hidden" name="cbt" value="Home">
</form>
<script type="text/javascript">
$("#book1").submit();
</script>
传参数过程中,中文好像不支持,我是先 utf8编码 然后在传递的
下面是 回调处理操作
function test(){
define("DEBUG", 0);
define("USE_SANDBOX", 1);
//自己设置记录文件
define("LOG_FILE", "test.txt");
$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;
}
//因为file_get_contents不支持https所以用curl
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, 0);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 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 = "";
// curl_setopt($ch, CURLOPT_CAINFO, $cert);
// curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
$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);
// }
curl_close($ch);
}
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp($res, "VERIFIED") == 0) {
// if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] ') . "Verified IPN:" . PHP_EOL, 3, LOG_FILE);
// }
// 检查付款状态
// 检查 txn_id 是否已经处理过(商家从买家收款的原始交易识别号,针对该号码注册事 件。)
// 检查 receiver_email 是否是您的 PayPal 账户中的 EMAIL 地址
// 检查付款金额和货币单位是否正确
// 处理这次付款,包括写数据库
$item_number = $_POST['item_number'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$option_name1 = $_POST['option_name1'];
$url = 自己网站处理数据的url;
// file_put_contents($filename, '$url' . $url . PHP_EOL, FILE_APPEND);
// exit;
$html = file_get_contents($url);
} else
if (strcmp($res, "INVALID") == 0) {
// if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] ') . "Invalid IPN:" . PHP_EOL, 3, LOG_FILE);
// }
} else {
error_log(date('[Y-m-d H:i e] ') . "none" . PHP_EOL, 3, LOG_FILE);
}
}
test();