WechatPay.php
10.4 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* Created by PhpStorm.
* User: billy
* Date: 09/03/2017
* Time: 8:33 PM
*/
namespace App\Common;
/**
* Class WechatPay
* @package App\Common
*/
use App\Common\Enums\PaySource;
/**
* Class WechatPay
* @package App\Common
*/
class WechatPay
{
/**
* order Id
* @var
*/
private $order_id;
/**
* Order price
* @var
*/
private $price;
/**
* pay description
* @var
*/
private $body;
/**
* @var
*/
private $pay_type;
/**
* Callback Url
* @var
*/
private $call_back_url;
/**
* Used for js and mini program
* @var
*/
private $open_id;
/**
* @var
*/
private $refund_fee;
/**
* @var
*/
private $refund_id;
/**
* @var
*/
private $op_user_id;
/**
* WechatPay constructor.
* @param $order_id
* @param $price
* @param $body
* @param $pay_type
* @param $call_back_url
*/
public function __construct($order_id="", $price=0, $body="", $call_back_url="", $pay_type=PaySource::APP)
{
$this->order_id = $order_id;
$this->price = $price;
$this->body = $body;
$this->call_back_url = $call_back_url;
$this->pay_type = $pay_type;
}
/**
* @param mixed $pay_type
*/
public function setPayType($pay_type)
{
$this->pay_type = $pay_type;
}
/**
* @param mixed $open_id
*/
public function setOpenId($open_id)
{
$this->open_id = $open_id;
}
/**
* @param mixed $refund_fee
*/
public function setRefundFee($refund_fee)
{
$this->refund_fee = $refund_fee;
}
/**
* @param mixed $op_user_id
*/
public function setOpUserId($op_user_id)
{
$this->op_user_id = $op_user_id;
}
/**
* @param mixed $refund_id
*/
public function setRefundId($refund_id)
{
$this->refund_id = $refund_id;
}
/**
* @param $success
* @param $error_messge
* @param string $payInfo
* @return array
*/
private function response($success, $error_messge, $payInfo="")
{
$result = array('success'=>$success, 'error_message'=>$error_messge, 'payInfo'=>$payInfo);
return $result;
}
/**
* 请求微信API进行微信统一下单
* URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder
*/
public function getUnifiedOrderInfo()
{
$check_result = $this->checkConfigParam();
if (!empty($check_result))
{
return $this->response(false, $check_result);
}
$param = $this->getUnifiedOrderParam();
//wechat unified order request
$resultXmlStr = Http::WechatPostWithSecurity($param, config('constants.wechat.unified_order_url'));
$result = Utils::xmlToArray($resultXmlStr);
if (!$this->checkRespSign($result))
{
return $this->response(false, "统一下单失败");
}
switch ($this->pay_type)
{
case PaySource::APP:
$unifiedOrderInfo = $this->getAppUnifiedOrderInfo($result['prepay_id']);
break;
case PaySource::JS:
case PaySource::MINI_PROGRAM:
$unifiedOrderInfo = $this->getJSUnifiedOrderInfo($result['prepay_id']);
break;
case PaySource::NATIVE:
$unifiedOrderInfo = ['code_url' => $result['code_url']];
break;
default:
$unifiedOrderInfo = $this->getAppUnifiedOrderInfo($result['prepay_id']);
break;
}
$unifiedOrderInfo["sign"] = $this->sign($unifiedOrderInfo);//生成签名
return $this->response(true, "", $unifiedOrderInfo);
}
/**
* @return mixed|string
*/
private function getAppId()
{
switch ($this->pay_type)
{
case PaySource::APP:
case PaySource::NATIVE:
return config('constants.wechat.app_id');
break;
case PaySource::JS:
return config('constants.wechat.js_id');
break;
case PaySource::MINI_PROGRAM:
return config('constants.wechat.mini_program_id');
break;
}
return "";
}
/**
* @return string
*/
private function getTradeType()
{
switch ($this->pay_type)
{
case PaySource::APP:
return "APP";
break;
case PaySource::JS:
case PaySource::MINI_PROGRAM:
return "JSAPI";
break;
case PaySource::NATIVE:
return "NATIVE";
break;
default:
return "APP";
break;
}
}
/**
* 获取同意下单参数
* @return string
*/
private function getUnifiedOrderParam()
{
$price=sprintf("%.2f",$this->price);
$param = array(
"appid" => $this->getAppId(),
"body" => $this->body,
"mch_id" => config('constants.wechat.mch_id'),
"nonce_str" => $this->getNonceStr(),
"notify_url" => $this->call_back_url,
"out_trade_no" => $this->order_id,
"total_fee" => $price * 100,
"trade_type" => $this->getTradeType(),
);
if ($this->pay_type == PaySource::JS || $this->pay_type == PaySource::MINI_PROGRAM)
{
$param['openid'] = $this->open_id;
}
$sign = $this->sign($param);//生成签名
$param['sign'] = $sign;
$paramXml = "<xml>";
foreach ($param as $k => $v) {
$paramXml .= "<" . $k . ">" . $v . "</" . $k . ">";
}
$paramXml .= "</xml>";
return $paramXml;
}
/**
* @param $prepay_id
* @return array
*/
private function getAppUnifiedOrderInfo($prepay_id)
{
$unifiedOrderInfo = [
"appid" => $this->getAppId(),
"noncestr" => $this->getNonceStr(),
"package" => "Sign=WXPay",
"partnerid" => config('constants.wechat.mch_id'),
"prepayid" => $prepay_id,
"timestamp" => substr(time().'',0,10)
];
return $unifiedOrderInfo;
}
/**
* @param $prepay_id
* @return array
*/
private function getJSUnifiedOrderInfo($prepay_id)
{
$unifiedOrderInfo = [
"appId" => $this->getAppId(),
"nonceStr" => $this->getNonceStr(),
"package" => "prepay_id=".$prepay_id,
"signType" => "MD5",
"timeStamp" => substr(time().'',0,10)
];
return $unifiedOrderInfo;
}
/**
* 获取随机字符串
* @return string
*/
private function getNonceStr()
{
$nonceStr = md5(rand(100, 1000) . time());
return $nonceStr;
}
/**
* 检测配置信息是否完整
*/
public function checkConfigParam()
{
if ($this->getAppId() == "") {
return "微信APPID未配置";
} elseif (config('constants.wechat.mch_id') == "") {
return "微信商户号MCHID未配置";
} elseif (config('constants.wechat.api_key') == "") {
return "微信API密钥KEY未配置";
}
return "";
}
public function refund()
{
$check_result = $this->checkConfigParam();
if (!empty($check_result))
{
return $this->response(false, $check_result);
}
$param = $this->getRefundInfo();
//wechat unified order request
$resultXmlStr = Http::WechatPostWithSecurity($param, config('constants.wechat.refund_url'), true);
$result = Utils::xmlToArray($resultXmlStr);
if (!$this->checkRespSign($result))
{
return $this->response(false, "退款失败");
}
return $this->response(true, "", $result);
}
private function getRefundInfo()
{
$price=sprintf("%.2f",$this->price);
$refund_fee = sprintf("%.2f", $this->refund_fee);
$param = array(
"appid" => $this->getAppId(),
"mch_id" => config('constants.wechat.mch_id'),
"nonce_str" => $this->getNonceStr(),
"out_trade_no" => $this->order_id,
"out_refund_no" => $this->refund_id,
"total_fee" => $price * 100,
"refund_fee" => $refund_fee * 100,
"op_user_id" => config('constants.wechat.mch_id'),
);
$sign = $this->sign($param);//生成签名
$param['sign'] = $sign;
$paramXml = "<xml>";
foreach ($param as $k => $v) {
$paramXml .= "<" . $k . ">" . $v . "</" . $k . ">";
}
$paramXml .= "</xml>";
return $paramXml;
}
/**
* sign拼装获取
* @param Array $param
* @return String
*/
private function sign($param)
{
ksort($param);
$sign = "";
foreach ($param as $k => $v) {
if ($v != "" && !is_array($v))
{
$sign .= $k . "=" . $v . "&";
}
}
$sign .= "key=" . config('constants.wechat.api_key');
$sign = strtoupper(md5($sign));
return $sign;
}
/**
* 检查微信回调签名
* @param $param
* @return bool
*/
public function checkRespSign($param)
{
if ($param['return_code'] == "SUCCESS") {
$wxSign = $param['sign'];
unset($param['sign']);
$sign = $this->sign($param);//生成签名
if ($this->checkSign($wxSign, $sign)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* @param $type
* @param $msg
* @return string
*/
public static function returnInfo($type, $msg)
{
if ($type == "SUCCESS") {
return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code></xml>";
} else {
return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code><return_msg><![CDATA[{$msg}]]></return_msg></xml>";
}
}
/**
* 签名验证
* @param $sign1
* @param $sign2
* @return bool
*/
private function checkSign($sign1, $sign2)
{
return trim($sign1) == trim($sign2);
}
}