Service.php
7.78 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace WebPlugin\Pay\Alibank;
use DOMDocument;
use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\PhpLog;
class Service extends PayAbstract
{
public $config;
private $log;
public function __construct(array $paymentParams)
{
$this->logProjectPrefix = 'alibank';
$this->config = new Config();
$myConfig = json_decode($paymentParams["pay_params"]);
$this->config->partner = $myConfig->merchant_id;
$this->config->alipay_key = $myConfig->merchant_key;
$this->config->sellerMail = $myConfig->merchant_other_code;
$this->log = new PhpLog($this->config->logDir, "PRC", $this->config->logLevel);
}
/**
* 获取时间戳
*/
private function getTimestamp()
{
$url = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" . trim(strtolower($this->config->partner)) . "&_input_charset=" . trim(strtolower($this->config->input_charset));
$doc = new DOMDocument();
$doc->load($url);
$itemEncrypt_key = $doc->getElementsByTagName("encrypt_key");
$encrypt_key = $itemEncrypt_key->item(0)->nodeValue;
return $encrypt_key;
}
public function getPayRequestPars(Reqparams $params)
{
$baseUrl = $this->getBaseNoticeUrl($params->isTest);
$loseTime = intval(($params->orderTime + 7200 - time()) / 60);
// $loseTime = intval((strtotime(date("Y-m-d 10:00:00", strtotime("+1 day")))-time())/60);
$parameter = array(
'service' => $this->config->service,
'partner' => $this->config->partner,
'_input_charset' => $this->config->input_charset,
'notify_url' => $baseUrl . $this->config->notify_url,
'return_url' => $baseUrl . $this->config->return_url,
/* 业务参数 */
'subject' => $params->goodsName,
'out_trade_no' => $params->orderCode . '_' . $params->paymentParameter,
'total_fee' => $params->totalFee / 100, //单位为元
'payment_type' => $this->config->payment_type,
'defaultbank' => $params->paymentParameter,
'seller_email' => $this->config->sellerMail,
// 'it_b_pay' => $loseTime . 'm'
);
if ($this->config->anti_fishing['timestamp_enable']) {
$anti_phishing_key = $this->getTimestamp();
if (!empty($anti_phishing_key)) {
$parameter['anti_phishing_key'] = $anti_phishing_key;
}
}
if ($this->config->anti_fishing['ip_enable']) {
$parameter['exter_invoke_ip'] = $params->spbill_create_ip;
}
// 除去数组中的空值和签名参数
$para_filter = array();
foreach ($parameter AS $k => $v) {
if ($k == "sign" || $k == "sign_type" || $v == "") {
continue;
} else {
$para_filter[$k] = $parameter[$k];
}
}
ksort($para_filter);
reset($para_filter);
$param = '';
$sign = '';
foreach ($para_filter AS $key => $val) {
$param .= "$key=" . urlencode($val) . "&";
$sign .= "$key=$val&";
}
$param = substr($param, 0, -1);
$sign = substr($sign, 0, -1);
//如果存在转义字符,那么去掉转义
if (get_magic_quotes_gpc()) {
$sign = stripslashes($sign);
}
$sign .= $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'
);
return $result;
}
public function parseResponse(array $arrResponse)
{
/* 返回示例
* http://www.yohobuy.com/pay/notice/alipayreturn?buyer_email=tds%40smartunite.com&buyer_id=2088302294447308&exterface=create_direct_pay_by_user&is_success=T¬ify_id=RqPnCoPT3K9%252Fvwbh3I7xtEV5W65QRToFQ5fPrXsVxt12e%252FExCtC1XNiKnuRwupLaVLAR¬ify_time=2011-06-11+07%3A48%3A10¬ify_type=trade_status_sync&out_trade_no=1061003000&payment_type=1&seller_email=shop%40yoho.cn&seller_id=2088001550230585&subject=YOHO%E5%95%86%E5%93%81&total_fee=0.01&trade_no=2011061199833830&trade_status=TRADE_SUCCESS&sign=ca1c49f58d17eaa57aac308d0ac64434&sign_type=MD5
*/
$this->log->LogInfo("===开始解析支付宝银行的回调参数===");
$this->log->LogInfo("-----回调参数为---");
$this->log->LogInfo(var_export($arrResponse, true));
if (isset($arrResponse['q'])) {
unset($arrResponse['q']);
}
$rsp = new Rspparams();
if (!$this->checkResponse($arrResponse)) {
$this->log->LogInfo("---验证支付宝银行的回调参数失败---");
//验证不成功
$rsp->payResult = -1;
} else {
$this->log->LogInfo("---验证支付宝银行的回调参数成功---");
$rsp->bankName = "";
$outTradeNo = $arrResponse["out_trade_no"];
// if(strlen($outTradeNo) > 9) {
// $outTradeNo = substr($outTradeNo, 2);
// }
//支付订单处理
$orderCodeArr = explode('_', $outTradeNo);
if (count($orderCodeArr) == 2) {
$rsp->orderCode = $orderCodeArr[0];
$rsp->bankName = $orderCodeArr[1];
} else {
$rsp->orderCode = $outTradeNo;
$rsp->bankName = "";
}
$rsp->payResult = $this->convertResult($arrResponse["trade_status"]);
$rsp->payTime = isset($arrResponse["gmt_payment"]) ? $arrResponse["gmt_payment"] : time();
$rsp->totalFee = $arrResponse["total_fee"];
$rsp->resultMsg = $arrResponse["notify_type"];
//添加支付订单号和交易号和银行的流水号
$rsp->payOrderCode = $outTradeNo;
$rsp->tradeNo = $arrResponse['trade_no'];
$rsp->bankBillNo = $arrResponse['bank_seq_no'] ? $arrResponse['bank_seq_no'] : "";
$this->log->LogInfo('----支付宝银行回调处理结果为----');
$this->log->LogInfo(var_export($rsp, true));
}
return $rsp;
}
protected function convertResult($resultCode)
{
if ($resultCode == "TRADE_SUCCESS") {
return 200;
}
return 400;
}
/**
* 除去数组中的空值和签名参数
* @param $para 签名参数组
* return 去掉空值与签名参数后的新签名参数组
*/
private function paraFilter($para)
{
$para_filter = array();
foreach ($para as $key => $val) {
if ($key == "sign" || $key == "sign_type" || $val == "") continue;
else $para_filter[$key] = $para[$key];
}
return $para_filter;
}
/**
* 对数组排序
* @param $para 排序前的数组
* return 排序后的数组
*/
private function argSort($para)
{
ksort($para);
reset($para);
return $para;
}
/**
* 验证回复的正确性
* @see QPay_Utils_Abstract::verifResponse()
*/
protected function checkResponse(array $arrResponse)
{
ksort($arrResponse);
reset($arrResponse);
$sign = '';
foreach ($arrResponse AS $key => $val) {
if ($key != 'sign' && $key != 'sign_type' && $key != 'code') {
$sign .= "$key=$val&";
}
}
$sign = substr($sign, 0, -1) . $this->config->alipay_key;
return md5($sign) != $arrResponse['sign'] ? false : true;
}
}