隐藏

Paypal支付接口回调IPN验证的PHP程序

发布:2016/12/20 0:32:10作者:管理员 来源:本站 浏览次数:909

这个网上也有不少代码,不过我看到很多采用fsockopen的方式调用的,但我在本地测试时发现返回:HTTP/1.0 302 Found,

HTTP/1.0 302 Found
Location: https://www.sandbox.paypal.com
Server: BigIP
Connection: close
Content-Length: 0

而我们预期的返回应该是:VERIFIED或者INVALID字符串,看返回结果是跳转到https://www.sandbox.paypal.com链接了,于是想着是不是用curl替换fscckopen方法。找了找curl的方法,这次能用了:

    #回调Paypal校验支付有效性 public function verified($data){ $req = 'cmd=_notify-validate'; if(function_exists('get_magic_quotes_gpc')) $get_magic_quotes_exists = true; foreach ($data 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"; } $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); 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')); $res = curl_exec($ch); if (strcmp ($res, "VERIFIED") == 0) { return true; } else if (strcmp ($res, "INVALID") == 0) { return false; } }
上面我用的是沙盒验证的地址,正式地址要去掉sandbox。验证通过表示这个订单是通过paypal发送过来的,通过之后还需要再对金额数、币种、支付状态、收款人邮箱进行验证。以防注入。