Files
sentcms/core/extend/com/pay/driver/Alipay.php
2016-06-30 16:53:58 +08:00

141 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace com\pay\driver;
class Alipay extends \com\pay\Pay {
protected $gateway = 'https://mapi.alipay.com/gateway.do';
protected $verify_url = 'http://notify.alipay.com/trade/notify_query.do';
protected $config = array(
'email' => '',
'key' => '',
'partner' => '',
);
public function check() {
if (!$this->config['email'] || !$this->config['key'] || !$this->config['partner']) {
E("支付宝账号未开通!");
}
return true;
}
public function pay(\com\pay\Input $input) {
$param = array(
'service' => 'create_direct_pay_by_user',
'payment_type' => '1',
'_input_charset' => 'utf-8',
'seller_email' => $this->config['email'],
'partner' => $this->config['partner'],
'notify_url' => $this->config['notify_url'],
'return_url' => $this->config['return_url'],
'out_trade_no' => $input->getOrderNo(),
'subject' => $input->getTitle(),
'body' => $input->getBody(),
'total_fee' => $input->getFee()
);
ksort($param);
reset($param);
$arg = '';
foreach ($param as $key => $value) {
if ($value) {
$arg .= "$key=$value&";
}
}
//echo substr($arg, 0, -1) . $this->config['key'];exit();
$param['sign'] = md5(substr($arg, 0, -1) . $this->config['key']);
$param['sign_type'] = 'MD5';
$sHtml = $this->_buildForm($param, $this->gateway,true, 'get');
return $sHtml;
}
/**
* 获取返回时的签名验证结果
* @param $para_temp 通知返回来的参数数组
* @param $sign 返回的签名结果
* @return 签名验证结果
*/
protected function getSignVeryfy($param, $sign) {
//除去待签名参数数组中的空值和签名参数
$param_filter = array();
while (list ($key, $val) = each($param)) {
if ($key == "sign" || $key == "sign_type" || $val == "") {
continue;
} else {
$param_filter[$key] = $param[$key];
}
}
ksort($param_filter);
reset($param_filter);
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
$prestr = "";
while (list ($key, $val) = each($param_filter)) {
$prestr.=$key . "=" . $val . "&";
}
//去掉最后一个&字符
$prestr = substr($prestr, 0, -1);
$prestr = $prestr . $this->config['key'];
$mysgin = md5($prestr);
if ($mysgin == $sign) {
return true;
} else {
return false;
}
}
/**
* 针对notify_url验证消息是否是支付宝发出的合法消息
* @return 验证结果
*/
public function verifyNotify($notify) {
//生成签名结果
$isSign = $this->getSignVeryfy($notify, $notify["sign"]);
//获取支付宝远程服务器ATN结果验证是否是支付宝发来的消息
$responseTxt = 'true';
if (!empty($notify["notify_id"])) {
$responseTxt = $this->getResponse($notify["notify_id"]);
}
if (preg_match("/true$/i", $responseTxt) && $isSign) {
$this->setInfo($notify);
return $this->info;
} else {
return false;
}
}
protected function setInfo($notify) {
$info = array();
//支付状态
$info['status'] = ($notify['trade_status'] == 'TRADE_FINISHED' || $notify['trade_status'] == 'TRADE_SUCCESS') ? true : false;
$info['total_fee'] = $notify['total_fee'];
$info['out_trade_no'] = $notify['out_trade_no'];
$this->info = $info;
}
/**
* 获取远程服务器ATN结果,验证返回URL
* @param $notify_id 通知校验ID
* @return 服务器ATN结果
* 验证结果集:
* invalid命令参数不对 出现这个错误请检测返回处理中partner和key是否为空
* true 返回正确信息
* false 请检查防火墙或者是服务器阻止端口问题以及验证时间是否超过一分钟
*/
protected function getResponse($notify_id) {
$partner = $this->config['partner'];
$veryfy_url = $this->verify_url . "?partner=" . $partner . "&notify_id=" . $notify_id;
$responseTxt = $this->fsockOpen($veryfy_url);
return $responseTxt;
}
}