AliwapService.php
4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Plugin\Pay\aliwap;
use Plugin\Pay\aliwap\AliwapConfig;
use Plugin\Pay\aliwap\AliwapReqparams;
use Plugin\Pay\aliwap\AliwapRspparams;
use Api\Yohobuy;
use Plugin\Helpers;
use Plugin\UdpLog;
/**
* 支付宝手机网页支付
*/
class AliwapService
{
var $config;
var $alipay_config;
public function __construct()
{
$this->logProjectPrefix = 'aliwap';
$this->config = new AliwapConfig();
}
/**
* 构造请求参数
* @see QPay_Utils_Interface::getPayRequestPars()
*/
public function getPayRequestPars(AliwapReqparams $params)
{
//构造要请求的参数数组,无需改动
$parameter = array(
'service' => $this->config->service,
'partner' => $this->config->partner,
'_input_charset' => $this->config->input_charset,
'notify_url' => SERVICE_NOTIFY . $this->config->notify_url,
'return_url' => Helpers::transHttpsUrl(Helpers::url($this->config->return_url)),
/* 业务参数 */
'payment_type' => $this->config->payment_type,
'seller_id' => $this->config->partner,
'it_b_pay' => date('Y-m-d H:i', $params->payExpire),
'payment_type' => $this->config->payment_type,
'out_trade_no' => $params->orderCode,
'subject' => $params->goodsName,
'total_fee' => $params->totalFee / 100, //单位为元
"show_url" => Helpers::transHttpsUrl(Helpers::url('/home/orderdetail', array('order_code' => $params->orderCode))),
);
ksort($parameter);
reset($parameter);
$param = '';
$sign = '';
$parameter = Helpers::paraFilter($parameter);
foreach ($parameter AS $key => $val) {
$param .= "$key=" . urlencode($val) . "&";
$sign .= "$key=$val&";
}
$param = substr($param, 0, -1);
$sign = substr($sign, 0, -1) . $this->config->alipay_key;
$result = array(
'pay_url' => $this->config->pay_url,
'pars' => $param . "&sign=" . md5($sign) . "&sign_type=" . $this->config->sign_type,
'reqType' => 'get'
);
UdpLog::info("【{$this->logProjectPrefix}-支付宝支付】,function:getPayRequestPars,请求参数", $result);
return $result;
}
/**
* 解析结果
*
*/
public function parseResponse(Array $arrResponse)
{
UdpLog::info("【{$this->logProjectPrefix}-支付宝支付】,function:parseResponse,回调参数", $arrResponse);
if (isset($arrResponse['q'])) {
unset($arrResponse['q']);
}
$rsp = new AliwapRspparams();
if (!$this->checkResponse($arrResponse)) {
//验证不成功
$rsp->payResult = -1;
} else {
$rsp->bankName = "";
$outTradeNo = $arrResponse["out_trade_no"];
$rsp->orderCode = $outTradeNo;
$rsp->payResult = $this->convertResult($arrResponse["trade_status"]);
$rsp->payTime = isset($arrResponse["gmt_payment"]) ? $arrResponse["gmt_payment"] : '';
$rsp->totalFee = $arrResponse["total_fee"];
$rsp->resultMsg = $arrResponse["notify_type"];
//添加支付订单号和交易号
$rsp->payOrderCode = $outTradeNo;
$rsp->tradeNo = $arrResponse['trade_no'];
$rsp->bankBillNo = "";
}
UdpLog::info("【{$this->logProjectPrefix}-支付宝支付】,function:parseResponse,回调参数的结果", $arrResponse);
return $rsp;
}
protected function convertResult($resultCode)
{
if ($resultCode == "TRADE_SUCCESS") {
return 200;
}
return 400;
}
/**
* 验证回复的正确性
* @see QPay_Utils_Abstract::verifResponse()
*/
protected function checkResponse(array $arrResponse)
{
if (empty($arrResponse['sign'])) {
return false;
}
ksort($arrResponse);
reset($arrResponse);
$sign = '';
foreach ($arrResponse AS $key => $val) {
if ($key === 'sign' || $key === 'sign_type' || $key === 'code' || $val === '') {
continue;
}
$sign .= "$key=$val&";
}
$sign = substr($sign, 0, -1) . $this->config->alipay_key;
return md5($sign) != $arrResponse['sign'] ? false : true;
}
}