cpcn.php
21.8 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
// +----------------------------------------------------------------------
// | 中金支付
// +----------------------------------------------------------------------
// | Author: yyc
// +----------------------------------------------------------------------
// | Date: 2018.11.7
// +----------------------------------------------------------------------
// | Copyright:
// +----------------------------------------------------------------------
namespace Payment;
include_once('sdk/market_core.php');
include_once('sdk/business_core.php');
class Cpcn
{
const MARKET_MODE = 2;
const BUSINESS_MODE = 1;
const WX = 'wxpay';
const ALI = 'alipay';
const UNION = 'unionpay';
const UNIONB2B = 'unionpay_b2b';
const WX_QR_PAY_TYPE = 10;//微信扫码支付
const ALI_QR_PAY_TYPE = 20;//支付宝扫码支付
const UNION_QR_PAY_TYPE = 30;//网银扫码支付
const WX_OFFI_PAY_TYPE = 12;//微信公众号支付
const ALI_OFFI_PAY_TYPE = 22;//支付宝公众号支付
const PAY_WAY = 1;//支付行为
const REFUND_WAY = 2;//退款行为
const SETTLE_WAY = 3;//结算行为
private $pay_way_to_code = array(//中金支付方式转pay_code
10 => 'wxpay',//'微信扫码支付',
11 => 'wxpay',//'微信反扫支付',
12 => 'wxpay',//'微信公众号支付',
13 => 'wxpay',//'微信app支付',
20 => 'alipay',//'支付宝扫码支付',
21 => 'alipay',//'支付宝反扫支付',
22 => 'alipay',//'支付宝公众号支付',
23 => 'alipay',//'支付宝app支付',
30 => 'unionpay',//'银联扫码支付',
31 => 'unionpay',//'银联反扫支付'
);
public $error = '';
private $mode;//市场模式 或 商户模式
public function __construct($mode = BUSINESS_MODE)
{
$this->setMode($mode);
}
/**
* 设置模式
* @param [type] $mode [description]
*/
public function setMode($mode)
{
if (in_array($mode, array(self::MARKET_MODE, self::BUSINESS_MODE))) {
$this->mode = $mode;
} else {
$this->mode = self::BUSINESS_MODE;
}
return $this;
}
/**
* 获取当前模式的实例
* @return [type] [description]
*/
private function getModeClass()
{
if ($this->mode == self::BUSINESS_MODE) {
$config = C('CPCN_BUSINESS_CONFIG');
$class = 'CpcnBusinessCore';
} else {
$config = C('CPCN_MARKET_CONFIG');
$class = 'CpcnMarketCore';
}
$this->config = $config;
$cpcn = new $class($config);
return $cpcn;
}
/**
* 生成支付代码
* @param $order<array> 订单信息
* @param $config_value<array> 支付方式信息
*/
public function get_code($order, $config_value)
{
$cpcn = $this->getModeClass();
$data = array(
'OrderNo' => $order['wallet_id'],
'PaymentNo' => $order['serial_number'],
'Amount' => $order['order_amount'] * 100,
'BankID' => $order['bank_id'],
'AccountType' => $order['account_type'],
'Goods' => $order['goods_items'],
'PayerID' => !empty($order['user_id']) ? $order['user_id'] : '',
'PayerName' => !empty($order['user_account']) ? $order['user_account'] : '',
'Usage' => !empty($order['usage']) ? $order['usage'] : '',
'Remark' => !empty($order['remark']) ? $order['remark'] : '',
'Note' => !empty($order['note']) ? $order['note'] : '',
'CardType' => !empty($order['card_type']) ? $order['card_type'] : '',
'LimitPay' => !empty($order['limit_pay']) ? $order['limit_pay'] : '',
'NotificationURL' => $this->config['notification_url'] . $config_value['pay_code'],
'SettlementFlag' => '0001',
);
if (in_array($config_value['pay_code'], array(self::ALI, self::WX))) {//聚合支付
if ($config_value['pay_code'] == self::ALI) {
$UserID = 'buyer_user_id';
$Qr_PaymentWay = self::ALI_QR_PAY_TYPE;
$Office_PaymentWay = self::ALI_OFFI_PAY_TYPE;
} else {
$UserID = 'open_id';
$Qr_PaymentWay = self::WX_QR_PAY_TYPE;
$Office_PaymentWay = self::WX_OFFI_PAY_TYPE;
}
$data['ExpirePeriod'] = isset($order['expire']) ? $order['expire'] : 120;
$data['Subject'] = $order['order_sn'] .'('. $order['topay_name'] . ')';
if (in_array($config_value['pay_mode'], array(2,3))) {//公众号\小程序
$MiniAppFlag = $config_value['pay_mode'] == 2 ? 10 : 20;
$data['UserID'] = $config_value[$UserID];
$data['MiniAppFlag'] = $MiniAppFlag;
$data['PaymentWay'] = $Office_PaymentWay;
$data['DiscountAmount'] = 0;
$res = $cpcn->buildOfficialPay($data);
if ($res->Head->Code != '2000' || !in_array($res->Body->ResponseCode, ['0000', 'SUCCESS']) || $res->Body->Status != '10') {
return false;
}
$AuthCode = strval($res->Body->AuthCode);
if ($config_value['pay_mode'] == 3) {//小程序
$html = json_decode($AuthCode, true);//最终ajax返回数据所以先解析
} else {
$data['AuthCode'] = $AuthCode;
$html = $this->bulidJsPay($data, $config_value['return_url']);
}
} else {
$data['PaymentWay'] = $Qr_PaymentWay;
$res = $cpcn->buildQrPay($data);
if ($res->Head->Code == '2000') {
$html = strval($res->Body->ImageUrl);
} else {
$html = false;
}
}
} else {//网银支付
// if ($this->mode == self::BUSINESS_MODE) {
$html = $cpcn->buildOrder($data);
// } else {
// $html = $cpcn->buildDirectOrder($data);
// }
}
return $html;
}
/**
* 查询支付订单接口
* @param [type] $serial_number 流水号
* @return [type] [description]
*/
public function query($serial_number, $ext = array())
{
$cpcn = $this->getModeClass();
$data = array(
'PaymentNo' => $serial_number
);
if ($ext['is_aggregate_pay']) {//是否聚合支付
$res = $cpcn->queryQrPay($data);
} else {
$res = $cpcn->queryPay($data);
}
if (empty($res) || $res->Head->Code != '2000') {
return false;
}
list($order_sn) = explode('_', $res->Body->PaymentNo, 2);
$result = array(
'out_trade_no' => $order_sn,//订单号
);
if ($res->Body->Status == 20) {
$result['status'] = 1;
// $result['transaction_id'] = $res['transaction_id'];//微信支付单号
$result['order_amount'] = $res->Body->Amount / 100;//订单金额
} else {
$result['status'] = 0;//未付款
}
$result['origin'] = json_encode($res);//接口原数据
return $result;
}
/**
* 查询订单
* @param string $order_sn [description]
* @param string $start_date [description]
* @param string $end_date [description]
* @return [type] [description]
*/
public function queryOrder($order_sn = '', $start_date = '', $end_date = '')
{
$cpcn = $this->getModeClass();
$data = array(
'OrderNo' => $order_sn,
'startDate' => $start_date,
'endDate' => $end_date
);
$res = $cpcn->queryOrder($data);
if (empty($res) || $res->Head->Code != '2000') {
return false;
}
return $res->Body;
}
/**
* 响应服务返回付款成功与否结果信息
* @return [type] [description]
*/
public function response()
{
$pay_code = $_GET['pay_code'];//机构ID
$message = $_POST['message'];
$signature = $_POST['signature'];
$cpcn = $this->getModeClass();
$res = $cpcn->responseVerify($message, $signature);
if ($res) {
//后台通知
$txCode=$res->Head->TxCode;
switch ($txCode) {
case '1118'://商户网银支付
case '1318'://市场网银支付
$pay_code = self::UNION;
return $this->responsePay($pay_code, $res);
break;
case '1408'://市场聚合支付
case '2818'://商户聚合支付
$pay_code = $this->pay_way_to_code[strval($res->Body->PaymentWay)];
return $this->responsePay($pay_code, $res);
break;
case '1138'://商户网银退款
case '2838'://商户聚合退款
return $this->responseRefund($res);
break;
case '1348'://市场结算结果通知
return $this->responseSettle($res);
break;
default:
return false;
break;
}
} else {
return false;
}
}
/**
* 响应中金内容
* @return [type] [description]
*/
public function responseOut($content)
{
$responseXML = new \SimpleXMLElement(\xmltmpl::$xmlNotification);
if ($content == 'success') {
$responseXML->Head->Code = "2000";
$responseXML->Head->Message ="OK.";
} else {
$responseXML->Head->Code = "2001";
$responseXML->Head->Message = '验签失败';
}
$responseXMLStr = $responseXML->asXML();
$base64Str = base64_encode(trim($responseXMLStr));
echo $base64Str;
}
/**
* 响应支付的数据整合
*/
private function responsePay($pay_code, $res)
{
if ($res->Body->Status == '20') {
$queryId = strval($res->Body->PaymentNo); //支付流水号
$gmt_payment = $res->Body->BankNotificationTime;//交易付款时间
$txnAmt = $res->Body->Amount / 100; //交易金额
if (strpos($queryId, '_') !== false) {//正常解析
$info = explode('_', $queryId);
$order_sn = $info[0];
$pay_type = $info[1];
} elseif(strpos($queryId, 'X') !== false) {//兼容可能的错误传输了银联格式
$info = explode('X', $queryId);
$order_sn = $info[0];
$pay_type = $info[1];
} else {//暂时没用了
$order_sn = substr($queryId, 0, strlen($queryId)-2);
$pay_type = substr($queryId, -2, 1);
}
$data['notify_way'] = self::PAY_WAY;//支付行为
$data['order_sn'] = $order_sn;
$data['pay_order_sn'] = $queryId;//组合的支付单号
$data['serial_number'] = $queryId;
$data['order_amount'] = $txnAmt;
$data['pay_type'] = $pay_type;
$data['pay_code'] = $pay_code;
$data['create_time'] = strtotime($gmt_payment);
$data['pay_time'] = strtotime($gmt_payment);
} else {
$data = false;
}
return $data;
}
/**
* 响应退款的数据整合
* @param [type] $pay_code [description]
* @param [type] $res [description]
* @return [type] [description]
*/
private function responseRefund($res)
{
$serial_number = strval($res->Body->SerialNumber);
$arr = explode('_', $serial_number, 2);
$order_sn = $arr[0];
if ($res->Body->Status == '40') {
$data['notify_way'] = self::REFUND_WAY;//退款行为
$data['status'] = 1;//退款成功
$data['order_sn'] = $order_sn;//退款单号
$data['out_refund_no'] = $serial_number; //退款单流水号
$data['serial_number'] = strval($res->Body->PaymentNo); //支付流水号
$data['refund_amount'] = $res->Body->Amount * 100; //退款金额
$data['refund_time'] = strtotime($res->Body->RefundTime); //退款时间
$data['message'] = '';
} else {
$data['status'] = -1;//退款失败
$data['order_sn'] = $order_sn;//退款单号
$data['out_refund_no'] = $serial_number; //退款单流水号
$data['serial_number'] = strval($res->Body->PaymentNo); //支付流水号
$data['refund_amount'] = $res->Body->Amount * 100; //退款金额
$data['message'] = '结算金额小于退款金额';
}
return $data;
}
/**
* 响应结算的数据整合
* @param [type] $res [description]
* @return [type] [description]
*/
private function responseSettle($res)
{
list($order_sn, $log_type) = explode('_', $res->Body->SerialNumber);
$data['notify_way'] = self::SETTLE_WAY;//结算行为
$data['order_sn'] = $order_sn;//结算单号
$data['serial_number'] = strval($res->Body->SerialNumber); // 结算流水号
$data['user_id'] = intval($res->Body->OrderNo); //结算主体
if ($res->Body->Status == '40') {
$data['settle_amount'] = $res->Body->Amount / 100; //结算金额
$data['transfer_time'] = strtotime($res->Body->TransferTime); //转账时间
} else {
$this->error = strval($res->Body->ErrorMessage);
$data = false;
}
return $data;
}
/**
* 退款业务(微信一年内可退)
* @return [type] [description]
*/
public function refund($order)
{
$cpcn = $this->getModeClass();
$data = array(
'SerialNumber' => $order['out_refund_no'],//退款流水号
'PaymentNo' => $order['serial_number'],//原支付流水号
'Amount' => $order['refund_amount'] * 100,
'Remark' => $order['remark'],
);
if ($order['is_aggregate_pay']) {
$res = $cpcn->refundQr($data);
} else {
$res = $cpcn->refund($data);
}
if (empty($res) || $res->Head->Code != '2000') {
return false;
}
$result = array(
'status' => 1,
'out_refund_no' => $order['out_refund_no'],//退款单号
'out_trade_no' => $order['serial_number'],//订单号
'transaction_id' => '',//支付单号
'refund_id' => $order['out_refund_no'],//微信退款单号
'origin' => json_encode($res),//接口原数据
);
return $result;
}
/**
* 查询退款状态
* @param [type] $serial_number 退款流水号
* @param boolean $is_aggregate_pay 是否聚合支付
* @return [type] [description]
*/
public function queryRefund($serial_number, $is_aggregate_pay = false)
{
$cpcn = $this->getModeClass();
$data = array(
'SerialNumber' => $serial_number,//退款流水号
);
if ($is_aggregate_pay) {
$res = $cpcn->queryRefundQr($data);
} else {
$res = $cpcn->queryRefund($data);
}
if (empty($res) || $res->Head->Code != '2000') {
return false;
}
$message = '';
$status = strval($res->Body->Status);
switch($status) {
case '10':
case '30':
$status = 0;//正在处理
break;
case '20':
$status = -1;//失败
$message = '结算金额小于退款金额';
break;
case '40':
$status = 1;//成功
break;
}
$result = array(
'status' => $status,
'refund_amount' => $res->Body->Amount / 100,//退款金额
'payment_no' => strval($res->Body->PaymentNo),//支付流水单
'serial_number' => strval($res->Body->SerialNumber),//退款流水单
'message' => $message,
);
return $result;
}
/**
* 结算业务
* @return [type] [description]
*/
public function sellet($order)
{
$cpcn = $this->getModeClass();
$data = array(
'SerialNumber' => $order['serial_number'],
'OrderNo' => $order['order_sn'],
'Amount' => abs($order['refund_amount']) * 100,
'Remark' => $order['remark'],
'AccountType' => $order['account_type'],
'PaymentAccountNumber' => $order['user_id'],
'BankID' => $order['bank_id'],
'AccountName' => $order['bank_user'],
'AccountNumber' => $order['bank_account'],
'BranchName' => $order['bank_address'],
'Province' => $order['bank_province'],
'City' => $order['bank_city'],
);
$res = $cpcn->sellet($data);
if (empty($res) || $res->Head->Code != '2000') {
$this->error = strval($res->Head->Message);
return false;
}
return true;
}
/**
* 验证银行卡有效性
* @return [type] [description]
*/
public function verifyCard($order)
{
$cpcn = $this->getModeClass();
$data = array(
'TxSNBinding' => $order['serial_number'],
'BankID' => $order['bank_id'],
'AccountName' => $order['bank_user'],
'AccountNumber' => $order['bank_account'],
'IdentificationType' => $order['id_type'],
'IdentificationNumber' => $order['id_number'],
'PhoneNumber' => $order['mobile'],
'CardType' => $order['card_type'],
'ValidDate' => $order['valid_date'],
'CVN2' => $order['cvn2'],
);
$res = $cpcn->buildRelevance($data);
if (empty($res) || $res->Head->Code != '2000') {
$this->error = strval($res->Head->Message);
return false;
}
return true;
}
/**
* 验证绑卡手机短信
* @param [type] $order [description]
* @return [type] [description]
*/
public function verifyBindCard($order)
{
$cpcn = $this->getModeClass();
$data = array(
'TxSNBinding' => $order['serial_number'],
'CVN2' => $order['cvn2'],
'SMSValidationCode' => $order['sms_code'],
'Remark' => $order['remark'],
);
$res = $cpcn->verifyRelevance($data);
//dump($res,1,'',0);
if (empty($res) || $res->Head->Code != '2000') {
$this->error = strval($res->Head->Message);
return false;
}
$result = array(
'verify_status' => $res->Body->VerifyStatus,//验证码结果
'status' => $res->Body->Status,//绑定结果
'bank_time' => $res->Body->BankTxTime,//银行处理时间
'bank_id' => strval($res->Body->IssueBankID),//银行
'card_type' => strval($res->Body->IssueCardType),//银行卡类型
'origin' => json_encode($res),//接口原数据
);
return $result;
}
/**
* 移动端构建JS跳转唤起支付
* @param [type] $data [description]
* @return [type] [description]
*/
private function bulidJsPay($data, $backurl = null)
{
$back = !empty($backurl) ? 'window.location.href="'.$backurl.'";return;' : '';
if ($data['PaymentWay'] == self::WX_OFFI_PAY_TYPE) {//微信
$html ='
<html>
<head><title>猎芯网</title></head>
<body>跳转中...
<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
"getBrandWCPayRequest",
'.$data['AuthCode'].',
function(res){
'.$back.'
window.history.back();
// WeixinJSBridge.log(res.err_msg);
// alert(res.err_code+res.err_desc+res.err_msg);
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent("WeixinJSBridgeReady", jsApiCall);
document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
}
}else{
jsApiCall();
}
</script>
</body>
</html>';
return $html;
} elseif ($data['PaymentWay'] == self::ALI_OFFI_PAY_TYPE) {//支付宝
$html = '
<html>
<head><title>猎芯网</title></head>
<body>跳转中...
<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script>
<script>
function ready(callback) {
// 如果jsbridge已经注入则直接调用
if (window.AlipayJSBridge) {
callback && callback();
} else {
// 如果没有注入则监听注入的事件
document.addEventListener("AlipayJSBridgeReady", callback, false);
}
}
ready(function () {
ap.tradePay({
tradeNO: "'.$data['AuthCode'].'"
}, function(res){
ap.alert(res.resultCode);
});
});
// });
</script>
</body>
</html>';
return $html;
}
}
}