shopxo/extend/payment/Weixin.php

744 lines
26 KiB
PHP
Raw Normal View History

2019-01-07 13:52:42 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2019-02-18 13:52:07 +08:00
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
2019-01-07 13:52:42 +08:00
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace payment;
/**
* 微信支付
2019-01-07 13:52:42 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
class Weixin
2019-01-07 13:52:42 +08:00
{
// 插件配置参数
private $config;
/**
* 构造方法
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-17
* @desc description
* @param [array] $params [输入参数(支付配置参数)]
*/
public function __construct($params = [])
{
$this->config = $params;
}
/**
* 配置信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
public function Config()
{
// 基础信息
$base = [
'name' => '微信', // 插件名称
2019-10-29 17:18:19 +08:00
'version' => '1.1.1', // 插件版本
2019-01-07 13:52:42 +08:00
'apply_version' => '不限', // 适用系统版本描述
2020-09-19 20:55:23 +08:00
'apply_terminal'=> ['pc', 'h5', 'ios', 'android', 'weixin', 'toutiao', 'qq'], // 适用终端 默认全部 ['pc', 'h5', 'app', 'alipay', 'weixin', 'baidu']
2019-11-03 23:50:34 +08:00
'desc' => '适用公众号+PC+H5+APP+[微信|头条]小程序,即时到帐支付方式,买家的交易资金直接打入卖家账户,快速回笼交易资金。 <a href="https://pay.weixin.qq.com/" target="_blank">立即申请</a>', // 插件描述支持html
2019-01-07 13:52:42 +08:00
'author' => 'Devil', // 开发者
'author_url' => 'http://shopxo.net/', // 开发者主页
];
// 配置信息
$element = [
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'appid',
2019-05-25 14:33:45 +08:00
'placeholder' => '公众号/服务号AppID',
'title' => '公众号/服务号AppID',
2019-01-08 12:21:27 +08:00
'is_required' => 0,
2019-05-25 14:33:45 +08:00
'message' => '请填写微信分配的AppID',
2019-01-08 12:21:27 +08:00
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'mini_appid',
2019-01-07 13:52:42 +08:00
'placeholder' => '小程序ID',
'title' => '小程序ID',
'is_required' => 0,
'message' => '请填写微信分配的小程序ID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'mch_id',
'placeholder' => '微信支付商户号',
'title' => '微信支付商户号',
'is_required' => 0,
'message' => '请填写微信支付分配的商户号',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'key',
'placeholder' => '密钥',
'title' => '密钥',
2019-08-18 12:12:47 +08:00
'desc' => '微信支付商户平台API配置的密钥',
2019-01-07 13:52:42 +08:00
'is_required' => 0,
'message' => '请填写密钥',
],
2019-05-29 14:04:35 +08:00
[
'element' => 'textarea',
'name' => 'apiclient_cert',
'placeholder' => '证书(apiclient_cert.pem)',
2019-11-02 15:52:58 +08:00
'title' => '证书(apiclient_cert.pem)(退款操作必填项)',
2019-05-29 14:04:35 +08:00
'is_required' => 0,
'rows' => 6,
'message' => '请填写证书(apiclient_cert.pem)',
],
[
'element' => 'textarea',
'name' => 'apiclient_key',
'placeholder' => '证书密钥(apiclient_key.pem)',
2019-11-02 15:52:58 +08:00
'title' => '证书密钥(apiclient_key.pem)(退款操作必填项)',
2019-05-29 14:04:35 +08:00
'is_required' => 0,
'rows' => 6,
'message' => '请填写证书密钥(apiclient_key.pem)',
],
2019-05-25 00:36:33 +08:00
[
'element' => 'select',
'title' => '异步通知协议',
'message' => '请选择协议类型',
'name' => 'agreement',
'is_multiple' => 0,
'element_data' => [
2019-05-29 14:04:35 +08:00
['value'=>1, 'name'=>'默认当前协议'],
2019-05-25 00:36:33 +08:00
['value'=>2, 'name'=>'强制https转http协议'],
],
],
2019-01-07 13:52:42 +08:00
];
return [
'base' => $base,
'element' => $element,
];
}
/**
* 支付入口
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array] $params [输入参数]
*/
public function Pay($params = [])
{
// 参数
2019-01-07 13:52:42 +08:00
if(empty($params))
{
return DataReturn('参数不能为空', -1);
}
// 配置信息
if(empty($this->config))
{
return DataReturn('支付缺少配置', -1);
}
2020-09-19 23:21:26 +08:00
// 平台
$client_type = ApplicationClientType();
2019-05-25 13:23:18 +08:00
// 微信中打开
2020-09-19 23:21:26 +08:00
if($client_type == 'h5' && IsWeixinEnv())
2019-05-25 15:25:42 +08:00
{
2020-09-11 18:06:09 +08:00
exit(header('location:'.PluginsHomeUrl('weixinwebauthorization', 'pay', 'index', input())));
2019-05-25 15:25:42 +08:00
}
2019-05-25 13:23:18 +08:00
2019-01-07 13:52:42 +08:00
// 获取支付参数
2019-01-07 15:20:49 +08:00
$ret = $this->GetPayParams($params);
if($ret['code'] != 0)
2019-01-07 13:52:42 +08:00
{
2019-01-07 15:20:49 +08:00
return $ret;
2019-01-07 13:52:42 +08:00
}
2020-09-19 23:21:26 +08:00
// QQ小程序使用微信支付
if($client_type == 'qq')
{
// 获取QQ access_token
$qq_appid = MyC('common_app_mini_qq_appid');
$qq_appsecret = MyC('common_app_mini_qq_appsecret');
$access_token = (new \base\QQ($qq_appid, $qq_appsecret))->GetAccessToken();
if($access_token === false)
{
return DataReturn('QQ凭证AccessToken获取失败', -1);
}
// QQ小程序代理下单地址
$request_url = 'https://api.q.qq.com/wxpay/unifiedorder?appid='.$qq_appid.'&access_token='.$access_token.'&real_notify_url='.urlencode($this->GetNotifyUrl($params));
} else {
$request_url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
}
2019-05-29 16:57:10 +08:00
// 请求接口处理
2020-09-19 23:21:26 +08:00
$result = $this->XmlToArray($this->HttpRequest($request_url, $this->ArrayToXml($ret['data'])));
2019-01-07 13:52:42 +08:00
if(!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['prepay_id']))
{
2019-01-08 13:15:48 +08:00
return $this->PayHandleReturn($ret['data'], $result, $params);
2019-01-07 13:52:42 +08:00
}
2019-11-02 14:06:46 +08:00
$msg = is_string($result) ? $result : (empty($result['return_msg']) ? '支付接口异常' : $result['return_msg']);
2019-01-07 15:57:01 +08:00
if(!empty($result['err_code_des']))
{
$msg .= '-'.$result['err_code_des'];
}
return DataReturn($msg, -1);
2019-01-07 13:52:42 +08:00
}
2019-01-08 11:20:23 +08:00
/**
* 支付返回处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-08
* @desc description
* @param [array] $pay_params [支付参数]
* @param [array] $data [支付返回数据]
2019-01-08 13:15:48 +08:00
* @param [array] $params [输入参数]
2019-01-08 11:20:23 +08:00
*/
2019-01-08 13:15:48 +08:00
private function PayHandleReturn($pay_params = [], $data = [], $params = [])
2019-01-08 11:20:23 +08:00
{
2020-01-03 11:09:22 +08:00
$redirect_url = empty($params['redirect_url']) ? __MY_URL__ : $params['redirect_url'];
2019-01-08 11:20:23 +08:00
$result = DataReturn('支付接口异常', -1);
2019-01-08 11:32:05 +08:00
switch($pay_params['trade_type'])
2019-01-08 11:20:23 +08:00
{
// web支付
case 'NATIVE' :
2019-04-29 18:34:50 +08:00
if(empty($params['ajax_url']))
{
return DataReturn('支付状态校验地址不能为空', -50);
}
2019-01-08 13:20:21 +08:00
$pay_params = [
2019-01-14 13:56:20 +08:00
'url' => urlencode(base64_encode($data['code_url'])),
2019-01-08 13:20:21 +08:00
'order_no' => $params['order_no'],
2019-01-08 13:22:18 +08:00
'name' => urlencode('微信支付'),
'msg' => urlencode('打开微信APP扫一扫进行支付'),
2019-06-17 18:17:20 +08:00
'ajax_url' => urlencode(base64_encode($params['ajax_url'])),
2019-01-08 13:20:21 +08:00
];
2019-04-29 18:34:50 +08:00
$url = MyUrl('index/pay/qrcode', $pay_params);
2019-01-08 12:46:50 +08:00
$result = DataReturn('success', 0, $url);
2019-01-08 11:20:23 +08:00
break;
// h5支付
case 'MWEB' :
if(!empty($params['order_id']))
{
2019-05-24 18:41:06 +08:00
$data['mweb_url'] .= '&redirect_url='.$redirect_url;
}
2019-01-08 12:46:50 +08:00
$result = DataReturn('success', 0, $data['mweb_url']);
2019-01-08 11:20:23 +08:00
break;
// 微信中/小程序支付
case 'JSAPI' :
$pay_data = array(
2019-01-08 12:21:27 +08:00
'appId' => $pay_params['appid'],
2019-01-08 11:20:23 +08:00
'package' => 'prepay_id='.$data['prepay_id'],
'nonceStr' => md5(time().rand()),
'signType' => $pay_params['sign_type'],
'timeStamp' => (string) time(),
);
$pay_data['paySign'] = $this->GetSign($pay_data);
2019-05-24 18:25:57 +08:00
// 微信中
2020-09-11 18:06:09 +08:00
if(ApplicationClientType() == 'h5' && IsWeixinEnv())
2019-05-24 18:25:57 +08:00
{
2019-05-25 00:09:35 +08:00
$this->PayHtml($pay_data, $redirect_url);
2019-05-24 18:27:52 +08:00
} else {
$result = DataReturn('success', 0, $pay_data);
2019-05-24 18:25:57 +08:00
}
2019-01-08 11:20:23 +08:00
break;
// APP支付
case 'APP' :
2019-10-29 17:18:19 +08:00
$pay_data = array(
'appid' => $this->pay_params['appid'],
'partnerid' => $this->pay_params['mch_id'],
'prepayid' => $data['prepay_id'],
'package' => 'Sign=WXPay',
'noncestr' => md5(time().rand()),
'timestamp' => (string) time(),
);
$pay_data['sign'] = $this->GetSign($pay_data);
$result = DataReturn('success', 0, $pay_data);
2019-01-08 11:20:23 +08:00
break;
}
return $result;
}
2019-05-25 00:09:35 +08:00
/**
* 支付代码
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-05-25T00:07:52+0800
* @param [array] $pay_data [支付信息]
2020-01-03 11:09:22 +08:00
* @param [string] $redirect_url [支付结束后跳转url]
2019-05-25 00:09:35 +08:00
*/
private function PayHtml($pay_data, $redirect_url)
{
2019-05-25 20:22:57 +08:00
// 支付代码
2019-05-25 00:09:35 +08:00
exit('<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>微信安全支付</title>
<script type="text/javascript">
function onBridgeReady()
{
WeixinJSBridge.invoke(
2019-05-25 20:21:08 +08:00
\'getBrandWCPayRequest\', {
"appId":"'.$pay_data['appId'].'",
"timeStamp":"'.$pay_data['timeStamp'].'",
"nonceStr":"'.$pay_data['nonceStr'].'",
"package":"'.$pay_data['package'].'",
"signType":"'.$pay_data['signType'].'",
"paySign":"'.$pay_data['paySign'].'"
},
function(res) {
2020-01-03 11:09:22 +08:00
window.location.href = "'.$redirect_url.'";
2019-05-25 20:21:08 +08:00
}
);
2019-05-25 00:09:35 +08:00
}
if(typeof WeixinJSBridge == "undefined")
{
if( document.addEventListener )
{
document.addEventListener("WeixinJSBridgeReady", onBridgeReady, false);
} else if (document.attachEvent)
{
document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
}
} else {
onBridgeReady();
}
</script>
</head>
<body>
</html>');
}
2019-01-07 13:52:42 +08:00
/**
* 获取支付参数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [array] $params [输入参数]
*/
private function GetPayParams($params = [])
{
2019-01-08 14:22:41 +08:00
$trade_type = empty($params['trade_type']) ? $this->GetTradeType() : $params['trade_type'];
if(empty($trade_type))
{
return DataReturn('支付类型不匹配', -1);
}
2020-09-19 20:55:23 +08:00
// 平台
$client_type = ApplicationClientType();
2019-05-24 17:58:05 +08:00
// openid
2020-09-19 20:55:23 +08:00
if($client_type == 'weixin')
2019-05-24 17:58:05 +08:00
{
$openid = isset($params['user']['weixin_openid']) ? $params['user']['weixin_openid'] : '';
} else {
2019-05-25 14:14:59 +08:00
$openid = isset($params['user']['weixin_web_openid']) ? $params['user']['weixin_web_openid'] : '';
2019-05-24 17:58:05 +08:00
}
2019-01-08 14:22:41 +08:00
// appid
2020-09-19 20:55:23 +08:00
$appid = ($client_type == 'weixin') ? $this->config['mini_appid'] : $this->config['appid'];
2019-05-24 17:58:05 +08:00
2019-05-25 00:36:33 +08:00
// 异步地址处理
2020-09-19 23:21:26 +08:00
$notify_url = ($client_type == 'qq') ? 'https://api.q.qq.com/wxpay/notify' : $this->GetNotifyUrl($params);
2019-05-25 00:36:33 +08:00
2019-05-24 17:58:05 +08:00
// 请求参数
2019-01-07 15:18:11 +08:00
$data = [
2019-01-08 12:21:27 +08:00
'appid' => $appid,
2019-01-07 15:18:11 +08:00
'mch_id' => $this->config['mch_id'],
2019-01-07 15:52:06 +08:00
'body' => $params['site_name'].'-'.$params['name'],
2020-08-04 18:07:52 +08:00
'nonce_str' => md5(time().$params['order_no']),
2019-05-25 00:36:33 +08:00
'notify_url' => $notify_url,
2019-05-24 17:58:05 +08:00
'openid' => ($trade_type == 'JSAPI') ? $openid : '',
2020-08-04 18:07:52 +08:00
'out_trade_no' => $params['order_no'],
2019-01-07 15:18:11 +08:00
'spbill_create_ip' => GetClientIP(),
2020-02-22 12:22:22 +08:00
'total_fee' => (int) (($params['total_price']*1000)/10),
2019-01-08 14:22:41 +08:00
'trade_type' => $trade_type,
2019-01-08 01:53:37 +08:00
'attach' => empty($params['attach']) ? $params['site_name'].'-'.$params['name'] : $params['attach'],
2019-01-07 15:18:11 +08:00
'sign_type' => 'MD5',
];
2019-05-24 18:11:25 +08:00
$data['sign'] = $this->GetSign($data);
2019-01-07 13:52:42 +08:00
return DataReturn('success', 0, $data);
}
2020-09-19 23:21:26 +08:00
/**
* 获取异步通知地址
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-19
* @desc description
* @param [array] $params [输入参数]
*/
private function GetNotifyUrl($params)
{
return (__MY_HTTP__ == 'https' && isset($this->config['agreement']) && $this->config['agreement'] == 1) ? 'http'.mb_substr($params['notify_url'], 5, null, 'utf-8') : $params['notify_url'];
}
2019-01-08 11:20:23 +08:00
/**
* 获取支付交易类型
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-08
* @desc description
*/
private function GetTradeType()
{
2020-09-11 18:06:09 +08:00
// 平台
$client_type = ApplicationClientType();
// 平台类型定义
2019-01-08 11:20:23 +08:00
$type_all = [
'pc' => 'NATIVE',
'weixin' => 'JSAPI',
'h5' => 'MWEB',
2019-10-29 16:18:18 +08:00
'toutiao' => 'MWEB',
2020-09-19 20:55:23 +08:00
'qq' => 'MWEB',
2019-10-29 16:18:18 +08:00
'app' => 'APP',
'ios' => 'APP',
'android' => 'APP',
2019-01-08 11:20:23 +08:00
];
// 微信中打开
2020-09-11 18:06:09 +08:00
if($client_type == 'h5' && IsWeixinEnv())
2019-05-24 18:04:57 +08:00
{
2020-09-11 18:06:09 +08:00
$type_all['pc'] = $type_all['weixin'];
2019-05-24 18:04:57 +08:00
}
2019-05-24 18:09:56 +08:00
2020-09-11 18:06:09 +08:00
return isset($type_all[$client_type]) ? $type_all[$client_type] : '';
2019-01-08 11:20:23 +08:00
}
2019-01-07 13:53:27 +08:00
/**
* 支付回调处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array] $params [输入参数]
*/
public function Respond($params = [])
{
2019-01-08 01:43:42 +08:00
$result = empty($GLOBALS['HTTP_RAW_POST_DATA']) ? $this->XmlToArray(file_get_contents('php://input')) : $this->XmlToArray($GLOBALS['HTTP_RAW_POST_DATA']);
2019-01-07 16:29:59 +08:00
2019-01-08 02:07:14 +08:00
if(isset($result['result_code']) && $result['result_code'] == 'SUCCESS' && $result['sign'] == $this->GetSign($result))
2019-01-08 01:38:25 +08:00
{
2019-01-08 02:16:32 +08:00
return DataReturn('支付成功', 0, $this->ReturnData($result));
2019-01-08 01:38:25 +08:00
}
2019-01-08 01:53:37 +08:00
return DataReturn('处理异常错误', -100);
}
/**
* [ReturnData 返回数据统一格式]
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-10-06T16:54:24+0800
* @param [array] $data [返回数据]
*/
private function ReturnData($data)
{
// 返回数据固定基础参数
$data['trade_no'] = $data['transaction_id']; // 支付平台 - 订单号
$data['buyer_user'] = $data['openid']; // 支付平台 - 用户
2020-08-04 18:07:52 +08:00
$data['out_trade_no'] = $data['out_trade_no']; // 本系统发起支付的 - 订单号
2019-01-08 01:53:37 +08:00
$data['subject'] = $data['attach']; // 本系统发起支付的 - 商品名称
2019-01-08 02:14:36 +08:00
$data['pay_price'] = $data['total_fee']/100; // 本系统发起支付的 - 总价
2019-01-08 01:53:37 +08:00
return $data;
2019-01-07 13:53:27 +08:00
}
2019-05-29 14:04:35 +08:00
/**
* 退款处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-05-28
* @desc description
* @param [array] $params [输入参数]
*/
public function Refund($params = [])
{
// 参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_no',
'error_msg' => '订单号不能为空',
],
[
'checked_type' => 'empty',
'key_name' => 'trade_no',
'error_msg' => '交易平台订单号不能为空',
],
[
'checked_type' => 'empty',
'key_name' => 'pay_price',
'error_msg' => '支付金额不能为空',
],
[
'checked_type' => 'empty',
'key_name' => 'refund_price',
'error_msg' => '退款金额不能为空',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
2019-11-28 11:34:59 +08:00
// 证书是否配置
if(empty($this->config['apiclient_cert']) || empty($this->config['apiclient_key']))
{
return DataReturn('证书未配置', -1);
}
2019-05-29 14:04:35 +08:00
// 退款原因
$refund_reason = empty($params['refund_reason']) ? $params['order_no'].'订单退款'.$params['refund_price'].'元' : $params['refund_reason'];
2019-05-30 16:30:06 +08:00
// appid默认使用公众号appid
$appid = (!isset($params['client_type']) || in_array($params['client_type'], ['pc', 'h5'])) ? $this->config['appid'] : $this->config['mini_appid'];
2019-05-29 14:04:35 +08:00
// 请求参数
$data = [
'appid' => $appid,
'mch_id' => $this->config['mch_id'],
'nonce_str' => md5(time().rand().$params['order_no']),
'sign_type' => 'MD5',
'transaction_id' => $params['trade_no'],
'out_refund_no' => $params['order_no'].GetNumberCode(6),
2020-02-22 12:22:22 +08:00
'total_fee' => (int) (($params['pay_price']*1000)/10),
'refund_fee' => (int) (($params['refund_price']*1000)/10),
2019-05-29 15:27:27 +08:00
'refund_desc' => $refund_reason,
2019-05-29 14:04:35 +08:00
];
$data['sign'] = $this->GetSign($data);
2019-05-29 16:57:10 +08:00
// 请求接口处理
$result = $this->XmlToArray($this->HttpRequest('https://api.mch.weixin.qq.com/secapi/pay/refund', $this->ArrayToXml($data), true));
2019-12-03 17:16:25 +08:00
if(isset($result['result_code']) && $result['result_code'] == 'SUCCESS' && isset($result['return_code']) && $result['return_code'] == 'SUCCESS')
2019-05-29 14:04:35 +08:00
{
// 统一返回格式
$data = [
'out_trade_no' => isset($result['out_trade_no']) ? $result['out_trade_no'] : '',
2019-06-01 15:52:13 +08:00
'trade_no' => isset($result['transaction_id']) ? $result['transaction_id'] : (isset($result['err_code_des']) ? $result['err_code_des'] : ''),
2019-05-29 14:04:35 +08:00
'buyer_user' => isset($result['refund_id']) ? $result['refund_id'] : '',
2019-06-01 15:51:08 +08:00
'refund_price' => isset($result['refund_fee']) ? $result['refund_fee']/100 : 0.00,
2019-05-29 14:04:35 +08:00
'return_params' => $result,
];
return DataReturn('退款成功', 0, $data);
}
2019-12-03 17:16:25 +08:00
$msg = is_string($result) ? $result : (empty($result['err_code_des']) ? '退款接口异常' : $result['err_code_des']);
if(!empty($result['return_msg']))
2019-05-29 14:04:35 +08:00
{
2019-12-03 17:16:25 +08:00
$msg .= '-'.$result['return_msg'];
2019-05-29 14:04:35 +08:00
}
return DataReturn($msg, -1);
}
2019-01-07 13:52:42 +08:00
/**
* 签名生成
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [array] $params [输入参数]
*/
private function GetSign($params = [])
{
ksort($params);
$sign = '';
foreach($params as $k=>$v)
{
2019-01-08 12:41:51 +08:00
if($k != 'sign' && $v != '' && $v != null)
{
$sign .= "$k=$v&";
}
2019-01-07 13:52:42 +08:00
}
return strtoupper(md5($sign.'key='.$this->config['key']));
}
2019-01-07 16:29:59 +08:00
/**
* 数组转xml
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [array] $data [数组]
*/
private function ArrayToXml($data)
{
$xml = '<xml>';
foreach($data as $k=>$v)
{
$xml .= '<'.$k.'>'.$v.'</'.$k.'>';
}
$xml .= '</xml>';
return $xml;
}
2019-01-07 13:52:42 +08:00
/**
* xml转数组
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [string] $xml [xm数据]
*/
private function XmlToArray($xml)
{
2019-05-29 15:25:29 +08:00
if(!$this->XmlParser($xml))
{
return is_string($xml) ? $xml : '接口返回数据有误';
}
2019-01-07 13:52:42 +08:00
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
2019-01-07 14:23:54 +08:00
/**
* 判断字符串是否为xml格式
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [string] $string [字符串]
*/
function XmlParser($string)
{
$xml_parser = xml_parser_create();
if(!xml_parse($xml_parser, $string, true))
{
xml_parser_free($xml_parser);
return false;
} else {
return (json_decode(json_encode(simplexml_load_string($string)),true));
}
}
2019-01-07 13:52:42 +08:00
/**
* [HttpRequest 网络请求]
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2017-09-25T09:10:46+0800
* @param [string] $url [请求url]
* @param [array] $data [发送数据]
* @param [boolean] $use_cert [是否需要使用证书]
2019-01-16 18:49:25 +08:00
* @param [int] $second [超时]
2019-01-07 13:52:42 +08:00
* @return [mixed] [请求返回数据]
*/
2019-05-29 15:11:43 +08:00
private function HttpRequest($url, $data, $use_cert = false, $second = 30)
2019-01-07 13:52:42 +08:00
{
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
2019-05-29 17:15:56 +08:00
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
2019-01-07 13:52:42 +08:00
CURLOPT_POSTFIELDS => $data,
2019-01-16 18:49:25 +08:00
CURLOPT_TIMEOUT => $second,
2019-01-07 13:52:42 +08:00
);
if($use_cert == true)
{
2019-05-29 17:55:41 +08:00
//设置证书
//使用证书cert 与 key 分别属于两个.pem文件
2019-05-29 17:15:56 +08:00
$apiclient = $this->GetApiclientFile();
2019-05-29 16:57:10 +08:00
$options[CURLOPT_SSLCERTTYPE] = 'PEM';
2019-05-29 17:15:56 +08:00
$options[CURLOPT_SSLCERT] = $apiclient['cert'];
2019-05-29 16:57:10 +08:00
$options[CURLOPT_SSLKEYTYPE] = 'PEM';
2019-05-29 17:15:56 +08:00
$options[CURLOPT_SSLKEY] = $apiclient['key'];
2019-01-07 13:52:42 +08:00
}
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
2019-05-29 15:25:29 +08:00
//返回结果
if($result)
{
curl_close($ch);
return $result;
} else {
$error = curl_errno($ch);
curl_close($ch);
return "curl出错错误码:$error";
}
2019-01-07 13:52:42 +08:00
}
2019-05-29 17:15:56 +08:00
/**
* 获取证书文件路径
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-05-29
* @desc description
*/
private function GetApiclientFile()
{
// 证书位置
$apiclient_cert_file = ROOT.'runtime'.DS.'temp'.DS.'payment_weixin_pay_apiclient_cert.pem';
$apiclient_key_file = ROOT.'runtime'.DS.'temp'.DS.'payment_weixin_pay_apiclient_key.pem';
2019-11-28 11:32:02 +08:00
// 证书处理
2019-11-28 10:52:02 +08:00
if(stripos($this->config['apiclient_cert'], '-----') === false)
{
$apiclient_cert = "-----BEGIN CERTIFICATE-----\n";
$apiclient_cert .= wordwrap($this->config['apiclient_cert'], 64, "\n", true);
$apiclient_cert .= "\n-----END CERTIFICATE-----";
} else {
$apiclient_cert = $this->config['apiclient_cert'];
}
2019-11-02 15:16:32 +08:00
file_put_contents($apiclient_cert_file, $apiclient_cert);
2019-11-28 10:52:02 +08:00
if(stripos($this->config['apiclient_key'], '-----') === false)
{
$apiclient_key = "-----BEGIN PRIVATE KEY-----\n";
$apiclient_key .= wordwrap($this->config['apiclient_key'], 64, "\n", true);
$apiclient_key .= "\n-----END PRIVATE KEY-----";
} else {
$apiclient_key = $this->config['apiclient_key'];
}
2019-11-02 15:16:32 +08:00
file_put_contents($apiclient_key_file, $apiclient_key);
2019-05-29 17:15:56 +08:00
return ['cert' => $apiclient_cert_file, 'key' => $apiclient_key_file];
}
2019-01-07 13:52:42 +08:00
}
?>