PHP Curl Paypal 沙盒 [英] PHP Curl Paypal Sandbox
本文介绍了PHP Curl Paypal 沙盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 CURL 和 Paypal 的 Developer Sandbox?当我尝试这段代码时,它在 print_r($lines); 中说我需要登录到沙盒,我怎样才能让它发送我的浏览器 cookie?
Is it possible to use CURL and Paypal's Developer Sandbox? When I try this code it says in print_r($lines); that I need to login to the Sandbox, how can I make it send my browser cookie?
此代码来自 http://leepeng.blogspot.com/2006/04/standard-paypal-php-integration.html
$orderno = $_SESSION["ss_last_orderno"];
$ppAcc = "[SELLER HERE]";
$at = "[AT HERE]"; //PDT Identity Token
$url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; //Test
//$url = "https://www.paypal.com/cgi-bin/webscr"; //Live
$tx = $_REQUEST['tx']; //this value is return by PayPal
$cmd = "_notify-synch";
$post = "tx=$tx&at=$at&cmd=$cmd";
//Send request to PayPal server using CURL
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec ($ch); //returned result is key-value pair string
$error = curl_error($ch);
if (curl_errno($ch) != 0) //CURL error
exit("ERROR: Failed updating order. PayPal PDT service failed.");
$longstr = str_replace("
", "", $result);
$lines = split("
", $longstr);
print_r($lines);
//parse the result string and store information to array
if ($lines[0] == "SUCCESS") {
//successful payment
$ppInfo = array();
for ($i=1; $i<count($lines); $i++) {
$parts = split("=", $lines[$i]);
if (count($parts)==2) {
$ppInfo[$parts[0]] = urldecode($parts[1]);
}
}
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =
"##$curtime##
".
"PayPal Transaction Information...
".
"Txn Id: ".$ppInfo["txn_id"]."
".
"Txn Type: ".$ppInfo["txn_type"]."
".
"Item Number: ".$ppInfo["item_number"]."
".
"Payment Date: ".$ppInfo["payment_date"]."
".
"Payment Type: ".$ppInfo["payment_type"]."
".
"Payment Status: ".$ppInfo["payment_status"]."
".
"Currency: ".$ppInfo["mc_currency"]."
".
"Payment Gross: ".$ppInfo["payment_gross"]."
".
"Payment Fee: ".$ppInfo["payment_fee"]."
".
"Payer Email: ".$ppInfo["payer_email"]."
".
"Payer Id: ".$ppInfo["payer_id"]."
".
"Payer Name: ".$ppInfo["first_name"]." ".$ppInfo["last_name"]."
".
"Payer Status: ".$ppInfo["payer_status"]."
".
"Country: ".$ppInfo["residence_country"]."
".
"Business: ".$ppInfo["business"]."
".
"Receiver Email: ".$ppInfo["receiver_email"]."
".
"Receiver Id: ".$ppInfo["receiver_id"]."
";
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
}
//Payment failed
else {
print "Please try again...";
//Delete order information
//Redirect to failed page
}
推荐答案
这应该有效:
$tid = $_GET['tx'];
$auth_token = "zzzzzzzzzzzzzzzzzzzz";
$paypal_url = "www.sandbox.paypal.com";
$url = "https://" . $paypal_url . "/cgi-bin/webscr";
$post_vars = "cmd=_notify-synch&tx=" . $tid . "&at=" . $auth_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'cURL/PHP');
$fetched = curl_exec($ch);
$lines = explode("
", $fetched);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// 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
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$itemname = $keyarray['num_cart_items'];
$amount = $keyarray['mc_gross'];
echo ("<h2>Thank you for your purchase!</h2>");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
echo ("<h2>Sorry, something went wrong</h2>");
// log for manual investigation
}
还有 $_GET['tx'] 查找在 ~5 分钟后停止工作
Also $_GET['tx'] look up stops working after ~5 minutes
这篇关于PHP Curl Paypal 沙盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文