wx_tx_class.php
11 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
<?php
/**
* tpshop 微信支付插件
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Author: IT宇宙人
* Date: 2015-09-09
*/
use think\Model;
/**
* 支付 逻辑定义
* Class
* @package Home\Payment
*/
class wx_tx extends Model
{
public $appid="";
public $mchid="";
public $key="";
public $appsecret="";
/**
* 析构流函数
*/
public function __construct() {
parent::__construct();
require_once("lib/WxPay.Api.php"); // 微信扫码支付demo 中的文件
require_once("example/WxPay.NativePay.php");
require_once("example/WxPay.JsApiPay.php");
}
/**
* 微信退款,$ordno,$openid,$money,$name,$path,$stoid
*/
function tixian($ordno,$openid,$money,$name,$path,$stoid,$source_type=0){
$config_value=M("wx_user")->where('store_id',$stoid)->field('appid,mchid,mchkey')->find();
if($source_type==1){
//---如果订单的来源是小程序的时候---
$path_wxapp= BASE_PATH . 'public/cert/' . $stoid . '/wxapp';
$conf_app_value = M("weapp")->where('store_id', $stoid)->field('appid,mchid,mchkey')->find();
if($config_value['mchid']!=$conf_app_value['mchid']){
$config_value=$conf_app_value;
$path=$path_wxapp;
}
}
$appid = $config_value['appid']; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
$mchid = $config_value['mchid']; // * MCHID:商户号(必须配置,开户邮件中可查看)
$key = $config_value['mchkey']; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
$out_trade_no = $ordno;
$input = new input_data();
$input->SetAppid($appid);//公众账号ID
$input->SetMch_id($mchid);//商户号
$nonce_str=$input->getNonceStr();
$input->SetNonce_str($nonce_str);//随机字符串
$input->SetData("partner_trade_no", $ordno);
$input->SetData("openid", $openid);
$input->SetData("check_name", 'FORCE_CHECK');
$input->SetData("re_user_name",$name);
$input->SetData("amount", $money*100);
$input->SetData("desc", "提现");
$input->SetData("spbill_create_ip", serverIP()); //传入服务器IP
$url="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
$input->SetSign($key);//签名
$xml = $input->ToXml();
$startTimeStamp = $input->getMillisecond();//请求开始时间
mlog('提现IP:'.serverIP().",支付商户号:".$mchid,'tixian/'.$stoid);
$response = $input->postXmlCurl($xml, $url, true, 12,$path);
$result =$input-> xmltoarray($response);
mlog('提现结果:'.$response,'tixian/'.$stoid);
return $result;
}
}
class input_data{
protected $values = array();
/**
* 设置参数
* @param string $key
* @param string $value
*/
public function SetData($key, $value)
{
$this->values[$key] = $value;
}
/**
* 设置微信分配的公众账号ID
* @param string $value
**/
public function SetAppid($value)
{
$this->values['mch_appid'] = $value;
}
/**
* 设置微信支付分配的商户号
* @param string $value
**/
public function SetMch_id($value)
{
$this->values['mchid'] = $value;
}
/**
* 设置随机字符串,不长于32位。推荐随机数生成算法
* @param string $value
**/
public function SetNonce_str($value)
{
$this->values['nonce_str'] = $value;
}
/**
*
* 产生随机字符串,不长于32位
* @param int $length
* @return 产生的随机字符串
*/
public static function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
/**
* 生成签名
* @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
*/
public function SetSign($key)
{
//签名步骤一:按字典序排序参数
ksort($this->values);
$string = $this->ToUrlParams();
//签名步骤二:在string后加入KEY
$string = $string . "&key=".$key;
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
$this->values['sign'] = $result;
return $result;
}
/**
* 获取毫秒级别的时间戳
*/
public static function getMillisecond()
{
//获取毫秒的时间戳
$time = explode ( " ", microtime () );
$time = $time[1] . ($time[0] * 1000);
$time2 = explode( ".", $time );
$time = $time2[0];
return $time;
}
/**
* 输出xml字符
* @throws WxPayException
**/
public function ToXml()
{
if(!is_array($this->values)
|| count($this->values) <= 0)
{
throw new WxPayException("数组数据异常!");
}
$xml = "<xml>";
foreach ($this->values as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
/**
* 格式化参数格式化成url参数
*/
public function ToUrlParams()
{
$buff = "";
foreach ($this->values as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/**
* 以post方式提交xml到对应的接口url
*
* @param string $xml 需要post的xml数据
* @param string $url url
* @param bool $useCert 是否需要证书,默认不需要
* @param int $second url执行超时时间,默认30s
* @throws WxPayException
*/
public static function postXmlCurl($xml, $url, $useCert = false, $second = 30,$path=null)
{
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
//如果有配置代理这里就设置代理
if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
&& WxPayConfig::CURL_PROXY_PORT != 0){
curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
}
curl_setopt($ch,CURLOPT_URL, $url);
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
// curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
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);
if($useCert == true){
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
if($path)
curl_setopt($ch,CURLOPT_SSLCERT, $path.'apiclient_cert.pem');
else
curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
if($path)
curl_setopt($ch,CURLOPT_SSLKEY, $path.'apiclient_key.pem');
else
curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);
}
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new WxPayException("curl出错,错误码:$error");
}
}
/**
*
* 上报数据, 上报的时候将屏蔽所有异常流程
* @param string $usrl
* @param int $startTimeStamp
* @param array $data
*/
public static function reportCostTime($url, $startTimeStamp, $data)
{
//如果不需要上报数据
if(WxPayConfig::REPORT_LEVENL == 0){
return;
}
//如果仅失败上报
if(WxPayConfig::REPORT_LEVENL == 1 &&
array_key_exists("return_code", $data) &&
$data["return_code"] == "SUCCESS" &&
array_key_exists("result_code", $data) &&
$data["result_code"] == "SUCCESS")
{
return;
}
//上报逻辑
$endTimeStamp = self::getMillisecond();
$objInput = new WxPayReport();
$objInput->SetInterface_url($url);
$objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
//返回状态码
if(array_key_exists("return_code", $data)){
$objInput->SetReturn_code($data["return_code"]);
}
//返回信息
if(array_key_exists("return_msg", $data)){
$objInput->SetReturn_msg($data["return_msg"]);
}
//业务结果
if(array_key_exists("result_code", $data)){
$objInput->SetResult_code($data["result_code"]);
}
//错误代码
if(array_key_exists("err_code", $data)){
$objInput->SetErr_code($data["err_code"]);
}
//错误代码描述
if(array_key_exists("err_code_des", $data)){
$objInput->SetErr_code_des($data["err_code_des"]);
}
//商户订单号
if(array_key_exists("out_trade_no", $data)){
$objInput->SetOut_trade_no($data["out_trade_no"]);
}
//设备号
if(array_key_exists("device_info", $data)){
$objInput->SetDevice_info($data["device_info"]);
}
try{
self::report($objInput);
} catch (WxPayException $e){
//不做任何处理
}
}
public function xmltoarray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
}