WxPay.php
7.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
namespace app\common\library\wechat;
use app\common\model\Wxapp as WxappModel;
use app\task\model\Setting as SettingModel;
use app\common\library\sms\Driver as SmsDriver;
use app\common\exception\BaseException;
/**
* 微信支付
* Class WxPay
* @package app\common\library\wechat
*/
class WxPay
{
private $config; // 微信支付配置
private $error;
/**
* 构造方法
* WxPay constructor.
* @param $config
*/
public function __construct($config)
{
$this->config = $config;
}
/**
* 统一下单API
* @param $order_no
* @param $openid
* @param $total_fee
* @return array
* @throws BaseException
*/
public function unifiedorder($order_no, $openid, $total_fee)
{
// 当前时间
$time = time();
// 生成随机字符串
$nonceStr = md5($time . $openid);
// API参数
$params = [
'appid' => $this->config['app_id'],
'attach' => 'test',
'body' => $order_no,
'mch_id' => $this->config['mchid'],
'nonce_str' => $nonceStr,
'notify_url' => base_url() . 'notice.php', // 异步通知地址
'openid' => $openid,
'out_trade_no' => $order_no,
'spbill_create_ip' => \request()->ip(),
'total_fee' => $total_fee * 100, // 价格:单位分
'trade_type' => 'JSAPI',
];
// 生成签名
$params['sign'] = $this->makeSign($params);
// 请求API
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$result = $this->postXmlCurl($this->toXml($params), $url);
$prepay = $this->fromXml($result);
// 请求失败
if ($prepay['return_code'] === 'FAIL') {
$this->error = $prepay['return_msg'];
return false;
}
if ($prepay['result_code'] === 'FAIL') {
$this->error = $prepay['return_msg'];
return false;
}
// 生成 nonce_str 供前端使用
$paySign = $this->makePaySign($params['nonce_str'], $prepay['prepay_id'], $time);
return [
'prepay_id' => $prepay['prepay_id'],
'nonceStr' => $nonceStr,
'timeStamp' => (string)$time,
'paySign' => $paySign
];
}
/**
* 支付成功异步通知
* @param \app\task\model\Order $OrderModel
* @throws BaseException
* @throws \Exception
* @throws \think\exception\DbException
*/
public function notify($OrderModel)
{
if (!$xml = file_get_contents('php://input')) {
$this->returnCode(false, 'Not found DATA');
}
// 将服务器返回的XML数据转化为数组
$data = $this->fromXml($xml);
// 记录日志
$this->doLogs($xml);
$this->doLogs($data);
// 订单信息
$order = $OrderModel->payDetail($data['out_trade_no']);
empty($order) && $this->returnCode(true, '订单不存在');
// 保存微信服务器返回的签名sign
$dataSign = $data['sign'];
// sign不参与签名算法
unset($data['sign']);
// 生成签名
$sign = $this->makeSign($data);
// 判断签名是否正确 判断支付状态
if (($sign === $dataSign)
&& ($data['return_code'] == 'SUCCESS')
&& ($data['result_code'] == 'SUCCESS')) {
// 更新订单状态
$order->updatePayStatus($data['transaction_id']);
// 返回状态
$this->returnCode(true, 'OK');
}
// 返回状态
$this->returnCode(false, '签名失败');
}
/**
* 返回状态给微信服务器
* @param bool $is_success
* @param string $msg
*/
private function returnCode($is_success = true, $msg = null)
{
$xml_post = $this->toXml([
'return_code' => $is_success ? $msg ?: 'SUCCESS' : 'FAIL',
'return_msg' => $is_success ? 'OK' : $msg,
]);
die($xml_post);
}
/**
* 写入日志记录
* @param $values
* @return bool|int
*/
private function doLogs($values)
{
return write_log($values, __DIR__);
}
/**
* 生成paySign
* @param $nonceStr
* @param $prepay_id
* @param $timeStamp
* @return string
*/
private function makePaySign($nonceStr, $prepay_id, $timeStamp)
{
$data = [
'appId' => $this->config['app_id'],
'nonceStr' => $nonceStr,
'package' => 'prepay_id=' . $prepay_id,
'signType' => 'MD5',
'timeStamp' => $timeStamp,
];
//签名步骤一:按字典序排序参数
ksort($data);
$string = $this->toUrlParams($data);
//签名步骤二:在string后加入KEY
$string = $string . '&key=' . $this->config['apikey'];
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
/**
* 将xml转为array
* @param $xml
* @return mixed
*/
private function fromXml($xml)
{
// 禁止引用外部xml实体
libxml_disable_entity_loader(true);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
/**
* 以post方式提交xml到对应的接口url
* @param $xml
* @param $url
* @param int $second
* @return mixed
*/
private function postXmlCurl($xml, $url, $second = 30)
{
$ch = curl_init();
// 设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//严格校验
// 设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
// 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
// 运行curl
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* 生成签名
* @param $values
* @return string 本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
*/
private function makeSign($values)
{
//签名步骤一:按字典序排序参数
ksort($values);
$string = $this->toUrlParams($values);
//签名步骤二:在string后加入KEY
$string = $string . '&key=' . $this->config['apikey'];
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
/**
* 格式化参数格式化成url参数
* @param $values
* @return string
*/
private function toUrlParams($values)
{
$buff = '';
foreach ($values as $k => $v) {
if ($k != 'sign' && $v != '' && !is_array($v)) {
$buff .= $k . '=' . $v . '&';
}
}
return trim($buff, '&');
}
/**
* 输出xml字符
* @param $values
* @return bool|string
*/
private function toXml($values)
{
if (!is_array($values)
|| count($values) <= 0
) {
return false;
}
$xml = "<xml>";
foreach ($values as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
public function getError()
{
return $this->error;
}
}