你可以到www.paypal.com注册一个账号,然后在developer.paypal.com下面登陆,会发现生成了一个测试的商家账号和一个测试的买家账号,这两个账号是准备着方便你进行对接调试的。
/**
* 通过PDT验证付款后paypal返回的数据
* @param type $tx 交易流水号,通过Get获取
* @param type $pdt_identity_token 商家唯一身份标记
* @return 订单明细数据
* @throws \Exception
*/
public function verifyReturn($tx, $pdt_identity_token) {
if(empty($tx))
{
throw new \Exception("Unexpected response from PayPal or Others.");
}
$encoded_data = http_build_query(array
(
'cmd' => '_notify-synch',
'tx' => strtoupper($tx),
'at' => $pdt_identity_token,
));
// $encoded_data = 'cmd=_notify-synch&tx=$tx&at=$pdt_identity_token';
if ($this->use_curl)
{
$this->curlPost($encoded_data);
}
else
{
$this->fsockPost($encoded_data);
}
$status = strpos($this->response_status, '200');
// check responses, if first 7 letters are SUCCESS then we're good
if($this->response_status == 200 && strpos($this->response, "SUCCESS") !== false)
{
// get rid of success
$curlResponse = substr($this->response, 7);
// decode
$curlResponse = urldecode($curlResponse);
// make associative array
preg_match_all('/^([^=\r\n]++)=(.*+)/m', $curlResponse, $m, PREG_PATTERN_ORDER);
$curlResponse = array_combine($m[1], $m[2]);
// keysort to keep in order
ksort($curlResponse);
// end
return $curlResponse;
}
else
{
throw new \Exception("Invalid response status: ".$this->response_status);
}
}
九域程序胡静 2015/12/3 12:13:28
protected function curlPost($encoded_data) {
if ($this->use_ssl) {
$uri = '
https://'.$this->getPaypalHost().'/cgi-bin/webscr';
$this->post_uri = $uri;
} else {
$uri = '
http://'.$this->getPaypalHost().'/cgi-bin/webscr';
$this->post_uri = $uri;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
// curl_setopt($ch, CURLOPT_CAINFO,
// dirname(__FILE__)."/cert/cert_key.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: ".$this->getPaypalHost()));
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
// curl_setopt($ch, CURLOPT_HEADER, true);
// if ($this->force_tls_v1) {
// curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
// }
$this->response = curl_exec($ch);
$this->response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
if ($this->response === false || $this->response_status == '0') {
$errno = curl_errno($ch);
$errstr = curl_error($ch);
throw new \Exception("cURL error: [$errno] $errstr");
}
}