我正在使用一种基于Paypal网站示例代码的PHP类,同时与CodeIgniter一起使用。我使用IPN模拟器测试IPN监听器。我的电子邮件已发送,所以我知道正在访问它。问题在于,我总是收到INVALID的响应,但我不知道为什么。这是我第一次将PayPal集成到我的网站之中。可能是什么原因导致了这个问题?
这是我的类:
这是我的CI控制器:
这是我的类:
<?php
class Paypal {
public $sandbox = false;
private $_url;
public $verified = false;
public $fields = array();
public $post_fields = array();
public $result;
public function __construct($params = array()){
$this->sandbox = (isset($params['sandbox'])) ? $params['sandbox'] : false;
$this->_url = ($this->sandbox) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr';
}
public function run(){
$this->verified = false;
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$this->post_fields[$keyval[0]] = urldecode($keyval[1]);
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if (function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($this->post_fields 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";
}
// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init($this->_url);
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, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
// the directory path of the certificate as shown below:
curl_setopt($ch, CURLOPT_CAINFO, FCPATH.'cacert.pem');
if ( !($res = curl_exec($ch)) ) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
die('curl did not work<br>'.FCPATH.'cacert.pem');
}
curl_close($ch);
if (strcmp ($res, "VERIFIED") == 0) {
$this->verified = true;
}
$this->result = $res;
}
这是我的CI控制器:
function paypalipn(){
$this->load->model('subscription');
$this->load->library('paypal', array('sandbox' => true));;
$this->paypal->run();
$fields = $this->paypal->post_fields;
if($this->paypal->verified){
$data = array(
'user_id' => $fields['buyer_id'],
'txn_id' => $fields['txn_id'],
'payment_gross' => $fields['mc_gross'],
'currency_code' => $fields['mc_currency'],
'payer_email' => $fields['payer_email'],
'plan_id' => $fields['item_number'],
'payment_status' => $fields['payment_status']
);
$this->subscription->create_payment($data);
}
$this->load->library('email');
$this->email->to('*******@gmail.com');
$this->email->from('**************');
$this->email->subject('PayPal IPN');
$this->email->message($this->paypal->result."\nAmount: ".$fields['mc_gross']."\nCurrency: ".$fields['mc_currency']."\nUser ID: ".$fields['buyer_id']);
$this->email->send();
}
在电子邮件信息中,所有字段都是空的,并且 $this->paypal->result 总是返回“INVALID”。有人有什么想法吗?感谢您的时间。
$this->subscription->create_payment($data);
运行正常吗?由于if($this->paypal->verified){}
中的 verified 是 false,所以很可能所有代码都会失败。但是电子邮件已发送。您能发布一下curl_exec($ch)
的输出吗? - user2560539