vr-shopxo-plugin/shopxo/extend/payment/Weixin.php

896 lines
32 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/mit-license.php )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace payment;
use app\service\AppMiniUserService;
/**
* 微信支付
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
*/
class Weixin
{
// 插件配置参数
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' => '微信', // 插件名称
'version' => '1.1.7', // 插件版本
'apply_version' => '不限', // 适用系统版本描述
'apply_terminal'=> ['pc', 'h5', 'ios', 'android', 'weixin', 'qq'], // 适用终端 默认全部 ['pc', 'h5', 'app', 'alipay', 'weixin', 'baidu']
'desc' => '适用公众号+PC+H5+APP+微信小程序,即时到帐支付方式,买家的交易资金直接打入卖家账户,快速回笼交易资金。 <a href="https://pay.weixin.qq.com/" target="_blank">立即申请</a>', // 插件描述支持html
'author' => 'Devil', // 开发者
'author_url' => 'http://shopxo.net/', // 开发者主页
];
// 配置信息
$element = [
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'app_appid',
'placeholder' => '开放平台AppID',
'title' => '开放平台AppID',
'is_required' => 0,
'message' => '请填写微信开放平台APP支付分配的AppID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'appid',
'placeholder' => '公众号/服务号AppID',
'title' => '公众号/服务号AppID',
'is_required' => 0,
'message' => '请填写微信分配的AppID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'mini_appid',
'placeholder' => '小程序AppID',
'title' => '小程序AppID',
'is_required' => 0,
'message' => '请填写微信分配的小程序AppID',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'mch_id',
'placeholder' => '微信支付商户号',
'title' => '微信支付商户号',
'is_required' => 0,
'message' => '请填写微信支付分配的商户号',
],
[
'element' => 'input',
'type' => 'text',
'default' => '',
'name' => 'key',
'placeholder' => '密钥',
'title' => '密钥',
'desc' => '微信支付商户平台API配置的密钥',
'is_required' => 0,
'message' => '请填写密钥',
],
[
'element' => 'textarea',
'name' => 'apiclient_cert',
'placeholder' => '证书(apiclient_cert.pem)',
'title' => '证书(apiclient_cert.pem)(退款操作必填项)',
'is_required' => 0,
'rows' => 6,
'message' => '请填写证书(apiclient_cert.pem)',
],
[
'element' => 'textarea',
'name' => 'apiclient_key',
'placeholder' => '证书密钥(apiclient_key.pem)',
'title' => '证书密钥(apiclient_key.pem)(退款操作必填项)',
'is_required' => 0,
'rows' => 6,
'message' => '请填写证书密钥(apiclient_key.pem)',
],
[
'element' => 'select',
'title' => '异步通知协议',
'message' => '请选择协议类型',
'name' => 'agreement',
'is_multiple' => 0,
'element_data' => [
['value'=>1, 'name'=>'默认当前协议'],
['value'=>2, 'name'=>'强制https转http协议'],
],
],
[
'element' => 'select',
'title' => 'h5跳转地址urlencode',
'message' => '请选择h5跳转地址urlencode',
'name' => 'is_h5_url_encode',
'is_multiple' => 0,
'element_data' => [
['value'=>1, 'name'=>'是'],
['value'=>2, 'name'=>'否'],
],
],
[
'element' => 'select',
'title' => 'H5走NATIVE模式',
'message' => '请选择是否H5走NATIVE模式',
'desc' => '账户没有取得h5支付权限的情况下可以开启',
'name' => 'is_h5_pay_native_mode',
'is_multiple' => 0,
'element_data' => [
['value'=>0, 'name'=>'否'],
['value'=>1, 'name'=>'是'],
],
],
];
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 = [])
{
// 配置参数验证
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_no',
'error_msg' => '支付单号为空',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 配置信息
if(empty($this->config))
{
return DataReturn('支付缺少配置', -1);
}
// 平台
$client_type = $this->GetApplicationClientType($params);
// 微信中打开
if(APPLICATION_CLIENT_TYPE == 'pc' && IsWeixinEnv() && (empty($params['user']) || empty($params['user']['weixin_web_openid'])))
{
exit(header('location:'.PluginsHomeUrl('weixinwebauthorization', 'pay', 'index', input())));
}
// 获取支付参数
$ret = $this->GetPayParams($params);
if($ret['code'] != 0)
{
return $ret;
}
// 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';
}
// 请求接口处理
$result = $this->XmlToArray($this->HttpRequest($request_url, $this->ArrayToXml($ret['data'])));
if(!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['prepay_id']))
{
return $this->PayHandleReturn($ret['data'], $result, $params);
}
$msg = is_string($result) ? $result : (empty($result['return_msg']) ? '支付接口异常' : $result['return_msg']);
if(!empty($result['err_code_des']))
{
$msg .= '-'.$result['err_code_des'];
}
return DataReturn($msg, -1);
}
/**
* 终端
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-12-07
* @desc description
* @param [array] $params [输入参数]
*/
private function GetApplicationClientType($params = [])
{
// 平台
$client_type = APPLICATION_CLIENT_TYPE;
if($client_type == 'pc' && IsMobile())
{
$client_type = 'h5';
}
// 是否收银台支付
if(isset($params['is_cashier']) && $params['is_cashier'] == 1)
{
$client_type = 'weixin';
}
return $client_type;
}
/**
* 支付返回处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-08
* @desc description
* @param [array] $pay_params [支付参数]
* @param [array] $data [支付返回数据]
* @param [array] $params [输入参数]
*/
private function PayHandleReturn($pay_params = [], $data = [], $params = [])
{
$redirect_url = empty($params['redirect_url']) ? __MY_URL__ : $params['redirect_url'];
$result = DataReturn('支付接口异常', -1);
switch($pay_params['trade_type'])
{
// web支付
case 'NATIVE' :
if(empty($params['check_url']))
{
return DataReturn('支付状态校验地址不能为空', -50);
}
if(APPLICATION == 'app')
{
$data = [
'qrcode_url' => $data['code_url'],
'order_no' => $params['order_no'],
'name' => '微信支付',
'msg' => '打开微信APP扫一扫进行支付',
'check_url' => $params['check_url'],
];
} else {
$pay_params = [
'url' => $data['code_url'],
'order_no' => $params['order_no'],
'name' => '微信支付',
'msg' => '打开微信APP扫一扫进行支付',
'check_url' => $params['check_url'],
];
MySession('payment_qrcode_data', $pay_params);
$data = MyUrl('index/pay/qrcode');
}
$result = DataReturn('success', 0, $data);
break;
// h5支付
case 'MWEB' :
if(!empty($params['order_id']))
{
// 是否需要urlencode
$redirect_url = (isset($this->config['is_h5_url_encode']) && $this->config['is_h5_url_encode'] == 1) ? urlencode($redirect_url) : $redirect_url;
$data['mweb_url'] .= '&redirect_url='.$redirect_url;
}
$result = DataReturn('success', 0, $data['mweb_url']);
break;
// 微信中/小程序支付
case 'JSAPI' :
$pay_data = [
'appId' => $pay_params['appid'],
'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);
// 是否收银台支付
if(isset($params['is_cashier']) && $params['is_cashier'] == 1)
{
$result = DataReturn('success', 0, [
'pay_price' => $params['total_price'],
'pay_data' => $pay_data,
]);
} else {
// 微信中
if(APPLICATION == 'web' && IsWeixinEnv())
{
$html = $this->PayHtml($pay_data, $redirect_url);
die($html);
} else {
$result = DataReturn('success', 0, $pay_data);
}
}
break;
// APP支付
case 'APP' :
$pay_data = array(
'appid' => $pay_params['appid'],
'partnerid' => $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);
break;
}
return $result;
}
/**
* 支付代码
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-05-25T00:07:52+0800
* @param [array] $pay_data [支付信息]
* @param [string] $redirect_url [支付结束后跳转url]
*/
private function PayHtml($pay_data, $redirect_url)
{
// 支付代码
return '<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>微信安全支付</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1, maximum-scale=1">
<body></body>
<script type="text/javascript">
function onBridgeReady()
{
WeixinJSBridge.invoke(
\'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) {
window.location.href = "'.$redirect_url.'";
}
);
}
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>
</html>';
}
/**
* 获取支付参数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-07
* @desc description
* @param [array] $params [输入参数]
*/
private function GetPayParams($params = [])
{
$trade_type = empty($params['trade_type']) ? $this->GetTradeType($params) : $params['trade_type'];
if(empty($trade_type))
{
return DataReturn('支付类型不匹配', -1);
}
// 平台
$client_type = $this->GetApplicationClientType($params);
// openid
if(empty($params['weixin_openid']))
{
if($client_type == 'weixin')
{
$openid = isset($params['user']['weixin_openid']) ? $params['user']['weixin_openid'] : '';
} else {
$openid = isset($params['user']['weixin_web_openid']) ? $params['user']['weixin_web_openid'] : '';
}
} else {
$openid = $params['weixin_openid'];
}
// appid
$appid = $this->PayAppID($client_type);
// 异步地址处理
$notify_url = ($client_type == 'qq') ? 'https://api.q.qq.com/wxpay/notify' : $this->GetNotifyUrl($params);
// 请求参数
$data = [
'appid' => $appid,
'mch_id' => $this->config['mch_id'],
'body' => $params['site_name'].'-'.$params['name'],
'nonce_str' => md5(time().$params['order_no']),
'notify_url' => $notify_url,
'openid' => ($trade_type == 'JSAPI') ? $openid : '',
'out_trade_no' => $params['order_no'],
'spbill_create_ip' => GetClientIP(),
'total_fee' => (int) (($params['total_price']*1000)/10),
'trade_type' => $trade_type,
'attach' => empty($params['attach']) ? $params['site_name'].'-'.$params['name'] : $params['attach'],
'sign_type' => 'MD5',
'time_expire' => $this->OrderAutoCloseTime(),
];
$data['sign'] = $this->GetSign($data);
return DataReturn('success', 0, $data);
}