Paybank.php
5.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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use addons\epay\library\Service;
use app\admin\model\Porder;
use think\Db;
use Yansongda\Pay\Pay;
use app\admin\model\Account;
use app\admin\model\User;
use app\admin\model\Message;
use app\admin\model\Car;
use app\admin\model\Product;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
/**
* 企业付款接口**
*/
class Paybank extends Api
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $KEY = 'cccccccccccccccccccccccccccccc00'; //商户秘钥
private $params;
public function _initialize()
{
parent::_initialize();
$this->user_id = $this->auth->getUserId();
}
//获取公钥
public function getPuyKey(){
$this->params = [
'mch_id' => '1515355581',//商户ID
'nonce_str' => md5(time()),
'sign_type' => 'MD5',
];
//将数据发送到接口地址
$res = $this->send("https://fraud.mch.weixin.qq.com/risk/getpublickey");
var_dump($res);exit;
}
// public function payBank(){
// if($this->request->isPost()){
// $rsa = new Rsa(file_get_contents(url('/addons/epay/certs/apiclient_cert.pem','','',true)), '');
// $data = [
// 'enc_bank_no' => $rsa->public_encrypt('1234342343234234'),//收款方银行卡号RSA加密
// 'enc_true_name' => $rsa->public_encrypt('李明'),//收款方姓名RSA加密
// 'bank_code' => '1002',//收款方开户行
// 'amount' => '1',//付款金额
// ];
// $res = $this->bankPay($data);
// var_dump($res);exit;
// }else{
// $this->error('请求方式错误');
// }
// }
//
// private function bankPay($data){
// //构建原始数据
// $this->params = [
// 'mch_id' => '1515355581',//商户号
// 'partner_trade_no' => $this->auth->genPayOrderSn(),//商户付款单号
// 'nonce_str' => md5(time()), //随机串
// 'enc_bank_no' => $data['enc_bank_no'],//收款方银行卡号RSA加密
// 'enc_true_name' => $data['enc_true_name'],//收款方姓名RSA加密
// 'bank_code' => $data['bank_code'],//收款方开户行
// 'amount' => $data['amount'],//付款金额
// ];
// //将数据发送到接口地址
// return $this->send('https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank');
// }
public function send($url){
$res = $this->sign();
$xml = $this->ArrToXml($res);
$returnData = $this->postData($url, $xml);
var_dump($returnData);exit;
return $this->XmlToArr($returnData);
}
public function sign(){
return $this->setSign($this->params);
}
//数组转xml
public function ArrToXml($arr)
{
if(!is_array($arr) || count($arr) == 0) return '';
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
/**
* 获取签名
* @param array $arr
* @return string
*/
public function getSign($arr){
//去除空值
$arr = array_filter($arr);
if(isset($arr['sign'])){
unset($arr['sign']);
}
//按照键名字典排序
ksort($arr);
//生成url格式的字符串
$str = $this->arrToUrl($arr) . '&key=' .$this->KEY;
return strtoupper(md5($str));
}
/**
* 获取带签名的数组
* @param array $arr
* @return array
*/
public function setSign($arr){
$arr['sign'] = $this->getSign($arr);
return $arr;
}
/**
* 数组转URL格式的字符串
* @param array $arr
* @return string
*/
public function arrToUrl($arr){
return urldecode(http_build_query($arr));
}
//Xml转数组
public function XmlToArr($xml){
if($xml == '') return '';
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arr;
}
//发送数据
public function postData($url,$postfields){
$ch = curl_init();
$params[CURLOPT_URL] = $url; //请求url地址
$params[CURLOPT_HEADER] = false; //是否返回响应头信息
$params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
$params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
$params[CURLOPT_POST] = true;
$params[CURLOPT_POSTFIELDS] = $postfields;
$params[CURLOPT_SSL_VERIFYPEER] = false;
$params[CURLOPT_SSL_VERIFYHOST] = false;
//以下是证书相关代码
$params[CURLOPT_SSLCERTTYPE] = 'PEM';
$params[CURLOPT_SSLCERT] = '../certs/apiclient_cert.pem';
$params[CURLOPT_SSLKEYTYPE] = 'PEM';
$params[CURLOPT_SSLKEY] = '../certs/apiclient_key.pem';
curl_setopt_array($ch, $params); //传入curl参数
$content = curl_exec($ch); //执行
curl_close($ch); //关闭连接
return $content;
}
}