我知道这个问题在这里被问了一遍又一遍。但是没有一个方法对我有效。
我正试图验证与沙盒PayPal交易。我已经研究并发现了不同的代码,但他们都没有响应我的代码或大多数PayPal返回无效
这是我的代码
header("HTTP/1.1 200 OK");
// parse the paypal URL
$req = 'cmd=' . urlencode('_notify-validate');
foreach ($_GET as $key => $value) {
if (!is_array($value)) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}else if (is_array($value)) {
$paymentArray = explode(' ', $value[0]);
$paymentCurrency = urlencode(stripslashes($paymentArray[0]));
$paymentGross = urlencode(stripslashes($paymentArray[1]));
$req .= '&mc_currency=' . $paymentCurrency . '&mc_gross=' . $paymentGross;
}
}
由于某种原因,我无法使用_POST获取任何信息,所以我尝试使用_GET。什么好主意吗?
echo $req;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__)."/api_cert_chain.crt");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.sandbox.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);
if (strcmp ($res, "VERIFIED") == 0) {
echo "valid";
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
echo "invalid";
// log for manual investigation
}
毕竟,要么我得到无效或什么都没有…
我也把证书放在我的服务器根目录
你试过使用这个吗:
$data = file_get_contents('php://input');
?
我的错…事实证明PayPal确实在后台发送信号…我实际上是在尝试重定向(交易完成后PayPal带回到原始来源)…它不会从贝宝获得太多信息。所以总的来说,代码工作得很好,以防有人想知道。