-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKuaiLaiFu.php
371 lines (322 loc) · 9.95 KB
/
KuaiLaiFu.php
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
<?php
header( "Content-Type:text/html; charset=utf-8" );
echo "<pre>";
// * 2.1 无卡支付 [ 下單接口 ] 使用範例
$obj = new KuaiLaiFuPayGeteWay();
print_r( $obj->getKuaiLaiFuPayResult( '20.00', 'pay2019032700001', 'testRMB20', '可為空' ) );
// * 2.2 无卡支付查询 使用範例
$obj = new KuaiLaiFuPayQuery();
print_r( $obj->getKuaiLaiFuPayQuery( '2019032700001' ) );
// * 2.4 代付
$obj = new KuaiLaiFuProxyPay();
$obj->bankAccType = 'PRIVATE_DEBIT_ACCOUNT';
$obj->bankAccName = '許功蓋';
$obj->bankAccNo = '1234567890123456';
$obj->bankBranchNo = '123456789012';
$obj->bankCode = 'SPDB';
$obj->bankName = '上海浦东发展银行XXXXXX支行';
$obj->province = 'XX省';
$obj->city = 'XX市';
$obj->amount = '1.00';
$obj->proxyPayNo = 'proxypay201903280001';
print_r( $obj->getKuaiLaiFuProxyPayResult() );
// * 2.5 代付查询
$obj = new KuaiLaiFuProxyQuery();
print_r( $obj->getKuaiLaiFuProxyQuery( 'proxypay201903270001' ) );
// * 2.6 余额查询
$obj = new KuaiLaiFuQueryBalance();
print_r( $obj->getKuaiLaiFuBalance() );
/**
* 快來付 第三方金流類 Class KuaiLaiFu
* @version 1.0.0
* @author NashYang
* @example KuaiLaiFu.php 使用範例
* @see 快来付商户信息.txt
* @see 交易接口文档_1.0.3(3).docx
*/
class KuaiLaiFu {
public $isShowSignStr = FALSE;
/**
* @var array 處理完成回傳內容,包含結果與資料。
*/
public $returnData = Array(
'result' => FALSE,
'data' => Array(),
'message' => '',
);
/**
* @var string 商店編號
* @todo 請填入商店編號
*/
protected $merchantNo = 'DEVV10000000';
/**
* @var string 密鑰,固定值
* @todo 請填入支付密鑰
*/
protected $privateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
/**
* @var string 頁面通知連結位置 (文件內說 暂不可用)
* @todo 需填入通知連結
*/
protected $returnURL = 'http://www.url.com/KuaiLaiFu/return.php';
/**
* @var string 後台異步通知連結位置
* @todo 需填入後台異步通知連結位置
*/
protected $notifyURL = 'http://www.url.com/KuaiLaiFu/notify.php';
/**
* @var string 快來付商店特定 host 位置
* @todo 需填入商店 host
*/
protected $urlHost = '192.192.192.192:8888';
/**
* 快來付第三方簽名法。
* @param array $payArr 簽名字段
* @return string MD5簽名字串
*/
protected function getSignStr( $payArr ) {
$sign = '';
ksort( $payArr );
foreach ( $payArr AS $key => $value ) {
if ( $key === 'key' ) {
continue;
}
if ( !empty( $sign ) ) {
$sign .= '&';
}
$sign .= $key . '=' . $value;
if ( $this->isShowSignStr ) {
echo $key . ':' . $value . '<br/>';
}
}
$sign .= '&key=' . $this->privateKey;
$sign = iconv( mb_detect_encoding( $sign ), 'UTF-8', $sign );
if ( $this->isShowSignStr ) {
echo 'sign:' . md5( $sign ) . '<br/><br/>';
echo $sign . '<br/><br/>';
}
return md5( $sign );
}
/**
* HTTP 請求並取得第三方結果。
* @param array $postArray 請求數據陣列
* @param string $url 請求對象 URL
* @return bool|array 請求失敗回傳 FALSE,成功則回傳 json 返回陣列。
*/
protected function sendHttp( $postArray, $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArray ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$output = curl_exec( $ch );
curl_close( $ch );
if ( $output === FALSE ) {
return FALSE;
} else {
return json_decode( $output, TRUE );
}
}
}
/**
* 快來付 無卡支付 Class KuaiLaiFuPayGeteWay
*/
class KuaiLaiFuPayGeteWay extends KuaiLaiFu {
/**
* @var string 請求 URL
* @example $url = 'http://host:port/pay/cnp/gateway';
*/
private $url;
/**
* @var string 币种 固定填入 156。
*/
protected $currency = '156';
/**
* @var string 支付方式 20000:支付宝扫码,30000:微信扫码
*/
protected $payCode = '20000';
/**
* @var string 下单时间
*/
protected $requestTime;
/**
* 2.1 无卡支付 [ 下單接口 ]
* @param $amout string|float 訂單金額,必須保留小數點後兩位,最低 20
* @param $orderNo string 訂單編號
* @param $productName string 商品名稱
* @param string $remark 備註
* @return array|bool
*/
public function getKuaiLaiFuPayResult( $amout, $orderNo, $productName, $remark = ' ' ) {
$this->requestTime = date( 'Y-m-d H:i:s' );
$payArr = Array(
'merchant_no' => $this->merchantNo,
'amount' => $amout,
'currency' => $this->currency,
'order_no' => $orderNo,
'pay_code' => $this->payCode,
'request_time' => $this->requestTime,
'product_name' => $productName,
'return_url' => $this->returnURL,
'pay_ip' => $_SERVER['REMOTE_ADDR'],
'remark' => $remark,
'notify_url' => $this->notifyURL,
);
$payArr['sign'] = $this->getSignStr( $payArr );
$this->url = 'http://' . $this->urlHost . '/pay/cnp/gateway';
return $this->sendHttp( $payArr, $this->url );
}
}
/**
* 快來付 代付 Class KuaiLaiFuProxyPay
*/
class KuaiLaiFuProxyPay extends KuaiLaiFu {
/**
* @var string 銀行卡類型,對公輸入 'PUBLIC_ACCOUNT',對私則輸入 'PRIVATE_DEBIT_ACCOUNT'
*/
public $bankAccType;
/**
* @var string 代付訂單號
*/
public $proxyPayNo;
/**
* @var float|string 代付訂單金額 保留小數點後兩位。
*/
public $amount;
/**
* @var string 代付 收款人姓名
*/
public $bankAccName;
/**
* @var string 代付 银行卡号
*/
public $bankAccNo;
/**
* @var string 代付 开户行支行行号
*/
public $bankBranchNo;
/**
* @var string 代付 银行编码
* @see 交易接口文档_1.0.3(3).docx -> 3.3 代付支持银行
*/
public $bankCode;
/**
* @var string 代付 开户行支行名称
*/
public $bankName;
/**
* @var string 代付 银行卡开户省份
*/
public $province;
/**
* @var string 代付 银行卡开户城市
*/
public $city;
/**
* @var string 代付 接入方式,填:SELFHELP
*/
protected $accessType = 'SELFHELP';
/**
* @var string 請求 URL
* @example $url = 'http://host:port/proxypay/cnp/gateway';
*/
private $url;
/**
* @return array|bool
*/
public function getKuaiLaiFuProxyPayResult() {
$payArr = Array(
'merchant_no' => $this->merchantNo,
'sett_no' => $this->proxyPayNo,
'sett_amount' => $this->amount,
'access_type' => $this->accessType,
'account_type' => $this->bankAccType,
'bank_account_name' => $this->bankAccName,
'bank_account_no' => $this->bankAccNo,
'bank_branch_no' => $this->bankBranchNo,
'bank_code' => $this->bankCode,
'bank_name' => $this->bankName,
'province' => $this->province,
'city' => $this->city,
);
$payArr['sign'] = $this->getSignStr( $payArr );
$this->url = 'http://' . $this->urlHost . '/proxypay/cnp/gateway';
return $this->sendHttp( $payArr, $this->url );
}
}
/**
* 快來付 無卡支付查詢 Class KuaiLaiFuPayQuery
*/
class KuaiLaiFuPayQuery extends KuaiLaiFu {
/**
* @var string 請求 URL
* @example $url = 'http://host:port/pay/query';
*/
private $url;
/**
* 2.2 无卡支付查询
* @param $orderNo string 查詢的訂單號
* @return array|bool
*/
public function getKuaiLaiFuPayQuery( $orderNo ) {
$queryArr = Array(
'merchant_no' => $this->merchantNo,
'order_no' => $orderNo,
);
$queryArr['sign'] = $this->getSignStr( $queryArr );
$this->url = 'http://' . $this->urlHost . '/pay/query';
return $this->sendHttp( $queryArr, $this->url );
}
}
/**
* 快來付 代付查詢類別 Class KuaiLaiFuProxyQuery
*/
class KuaiLaiFuProxyQuery extends KuaiLaiFu {
/**
* @var string 請求 URL
* @example $url = 'http://host:port/proxypay/cnp/query';
*/
private $url;
/**
* 2.5 代付查询
* @param $orderNo string 欲查詢的代付訂單號
* @return array|bool
*/
public function getKuaiLaiFuProxyQuery( $orderNo ) {
$queryArr = Array(
'merchant_no' => $this->merchantNo,
'sett_no' => $orderNo,
);
$queryArr['sign'] = $this->getSignStr( $queryArr );
$this->url = 'http://' . $this->urlHost . '/proxypay/cnp/query';
return $this->sendHttp( $queryArr, $this->url );
}
}
/**
* 快來付 餘額查詢類別 Class KuaiLaiFuQueryBalance
* @version 1.0.0
* @author NashYang
* @example KuaiLaiFu.php 使用範例
* @see 快来付商户信息.txt
* @see 交易接口文档_1.0.3(3).docx
*/
class KuaiLaiFuQueryBalance extends KuaiLaiFu {
/**
* @var string 請求 URL
* @example $url = 'http://host:port/pay/queryBalance';
*/
private $url;
/**
* 2.6 余额查询 [ 餘額查詢 ]
* @return bool|string
*/
public function getKuaiLaiFuBalance() {
$queryBalanceArr = Array(
'merchant_no' => $this->merchantNo,
);
$queryBalanceArr['sign'] = $this->getSignStr( $queryBalanceArr );
$this->url = 'http://' . $this->urlHost . '/pay/queryBalance';
return $this->sendHttp( $queryBalanceArr, $this->url );
}
}
?>