first commit
This commit is contained in:
@@ -0,0 +1,746 @@
|
||||
<?php
|
||||
class huifu_plugin
|
||||
{
|
||||
static public $info = [
|
||||
'name' => 'huifu', //支付插件英文名称,需和目录名称一致,不能有重复
|
||||
'showname' => '汇付斗拱平台', //支付插件显示名称
|
||||
'author' => '汇付天下', //支付插件作者
|
||||
'link' => 'https://paas.huifu.com/', //支付插件作者链接
|
||||
'types' => ['alipay','wxpay','bank','ecny'], //支付插件支持的支付方式,可选的有alipay,qqpay,wxpay,bank
|
||||
'inputs' => [ //支付插件要求传入的参数以及参数显示名称,可选的有appid,appkey,appsecret,appurl,appmchid
|
||||
'appid' => [
|
||||
'name' => '汇付系统号',
|
||||
'type' => 'input',
|
||||
'note' => '当主体为渠道商时填写渠道商ID,主体为直连商户时填写商户ID',
|
||||
],
|
||||
'appurl' => [
|
||||
'name' => '汇付产品号',
|
||||
'type' => 'input',
|
||||
'note' => '',
|
||||
],
|
||||
'appsecret' => [
|
||||
'name' => '商户私钥',
|
||||
'type' => 'textarea',
|
||||
'note' => '',
|
||||
],
|
||||
'appkey' => [
|
||||
'name' => '汇付公钥',
|
||||
'type' => 'textarea',
|
||||
'note' => '',
|
||||
],
|
||||
'appmchid' => [
|
||||
'name' => '汇付子商户号',
|
||||
'type' => 'input',
|
||||
'note' => '当主体为渠道商时需要填写,主体为直连商户时不需要填写',
|
||||
],
|
||||
'project_id' => [
|
||||
'name' => '半支付托管项目号',
|
||||
'type' => 'input',
|
||||
'note' => '仅托管支付需要填写',
|
||||
],
|
||||
],
|
||||
'select_alipay' => [
|
||||
'1' => '扫码支付',
|
||||
'2' => '托管H5/PC支付',
|
||||
'3' => '托管小程序支付',
|
||||
'4' => 'JS支付',
|
||||
],
|
||||
'select_wxpay' => [
|
||||
'1' => '自有公众号/小程序支付',
|
||||
'2' => '托管H5/PC支付',
|
||||
'3' => '托管小程序支付',
|
||||
],
|
||||
'select_bank' => [
|
||||
'1' => '银联支付',
|
||||
'2' => '快捷支付',
|
||||
'3' => '网银支付',
|
||||
],
|
||||
'select' => null,
|
||||
'note' => null, //支付密钥填写说明
|
||||
'bindwxmp' => true, //是否支持绑定微信公众号
|
||||
'bindwxa' => true, //是否支持绑定微信小程序
|
||||
];
|
||||
|
||||
static public function submit(){
|
||||
global $siteurl, $channel, $order, $sitename;
|
||||
|
||||
if($order['typename']=='alipay'){
|
||||
if(checkalipay() && in_array('4',$channel['apptype']) && !in_array('2',$channel['apptype'])){
|
||||
return ['type'=>'jump','url'=>'/pay/alipayjs/'.TRADE_NO.'/?d=1'];
|
||||
}else{
|
||||
return ['type'=>'jump','url'=>'/pay/alipay/'.TRADE_NO.'/'];
|
||||
}
|
||||
}elseif($order['typename']=='wxpay'){
|
||||
if((in_array('1',$channel['apptype']) || in_array('2',$channel['apptype'])) && checkwechat()){
|
||||
return ['type'=>'jump','url'=>'/pay/wxjspay/'.TRADE_NO.'/?d=1'];
|
||||
}elseif(checkmobile()){
|
||||
return ['type'=>'jump','url'=>'/pay/wxwappay/'.TRADE_NO.'/'];
|
||||
}else{
|
||||
return ['type'=>'jump','url'=>'/pay/wxpay/'.TRADE_NO.'/'];
|
||||
}
|
||||
}elseif($order['typename']=='bank'){
|
||||
if(in_array('3',$channel['apptype'])){
|
||||
return ['type'=>'jump','url'=>'/pay/bank/'.TRADE_NO.'/'];
|
||||
}elseif(in_array('2',$channel['apptype'])){
|
||||
return ['type'=>'jump','url'=>'/pay/quickpay/'.TRADE_NO.'/'];
|
||||
}else{
|
||||
return ['type'=>'jump','url'=>'/pay/unionpay/'.TRADE_NO.'/'];
|
||||
}
|
||||
}elseif($order['typename']=='ecny'){
|
||||
return ['type'=>'jump','url'=>'/pay/ecny/'.TRADE_NO.'/'];
|
||||
}
|
||||
}
|
||||
|
||||
static public function mapi(){
|
||||
global $siteurl, $channel, $order, $conf, $device, $mdevice, $method;
|
||||
|
||||
if($method == 'applet'){
|
||||
return self::wxplugin();
|
||||
}
|
||||
elseif($method == 'app'){
|
||||
if($order['typename']=='alipay'){
|
||||
return self::aliapppay();
|
||||
}else{
|
||||
return self::wxapppay();
|
||||
}
|
||||
}
|
||||
elseif($order['typename']=='alipay'){
|
||||
if($mdevice=='alipay' && in_array('4',$channel['apptype']) && !in_array('2',$channel['apptype'])){
|
||||
return ['type'=>'jump','url'=>$siteurl.'pay/alipayjs/'.TRADE_NO.'/?d=1'];
|
||||
}else{
|
||||
return self::alipay();
|
||||
}
|
||||
}elseif($order['typename']=='wxpay'){
|
||||
if((in_array('1',$channel['apptype']) || in_array('2',$channel['apptype'])) && $mdevice=='wechat'){
|
||||
return ['type'=>'jump','url'=>$siteurl.'pay/wxjspay/'.TRADE_NO.'/?d=1'];
|
||||
}elseif($device=='mobile'){
|
||||
return self::wxwappay();
|
||||
}else{
|
||||
return self::wxpay();
|
||||
}
|
||||
}elseif($order['typename']=='bank'){
|
||||
if(in_array('3',$channel['apptype'])){
|
||||
return self::bank();
|
||||
}elseif(in_array('2',$channel['apptype'])){
|
||||
return self::quickpay();
|
||||
}else{
|
||||
return self::unionpay();
|
||||
}
|
||||
}elseif($order['typename']=='ecny'){
|
||||
return self::ecny();
|
||||
}
|
||||
}
|
||||
|
||||
//统一下单
|
||||
static private function addOrder($trade_type, $sub_appid=null, $sub_openid=null){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'req_date' => substr(TRADE_NO,0,8),
|
||||
'req_seq_id' => TRADE_NO,
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'trade_type' => $trade_type,
|
||||
'trans_amt' => $order['realmoney'],
|
||||
'goods_desc' => $ordername,
|
||||
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
|
||||
'risk_check_data' => json_encode(['ip_addr' => $clientip]),
|
||||
];
|
||||
if($trade_type == 'T_JSAPI' || $trade_type == 'T_MINIAPP'){
|
||||
$param['wx_data'] = json_encode(['sub_openid' => $sub_openid, 'openid' => $sub_openid, 'device_info' => '4', 'spbill_create_ip' => $clientip]);
|
||||
}elseif($trade_type == 'A_JSAPI'){
|
||||
$param['alipay_data'] = json_encode(['buyer_id' => $sub_openid]);
|
||||
}elseif($trade_type == 'T_NATIVE'){
|
||||
$param['wx_data'] = json_encode(['product_id' => '01001']);
|
||||
}
|
||||
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($client, $param, $trade_type) {
|
||||
$result = $client->requestApi('/v2/trade/payment/jspay', $param);
|
||||
if(isset($result['resp_code']) && $result['resp_code']=='00000100') {
|
||||
if($trade_type == 'T_JSAPI' || $trade_type == 'T_MINIAPP' || $trade_type == 'A_JSAPI'){
|
||||
return $result['pay_info'];
|
||||
}else{
|
||||
return $result['qr_code'];
|
||||
}
|
||||
}elseif(isset($result['resp_desc'])){
|
||||
throw new Exception($result['resp_desc'].($result['bank_message']?' '.$result['bank_message']:''));
|
||||
}else{
|
||||
throw new Exception('返回数据解析失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//支付宝扫码支付
|
||||
static public function alipay(){
|
||||
global $channel, $device, $mdevice, $siteurl;
|
||||
if(in_array('1',$channel['apptype']) || empty($channel['apptype'][0])){
|
||||
try{
|
||||
$code_url = self::addOrder('A_NATIVE');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'支付宝支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
}elseif(in_array('2',$channel['apptype'])){
|
||||
try{
|
||||
$code_url = self::hostingOrder('A_JSAPI', 'M');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'支付宝支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
}elseif(in_array('3',$channel['apptype'])){
|
||||
try{
|
||||
$code_url = self::aliapphosting();
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'支付宝支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
if(checkalipay() || $mdevice=='alipay'){
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}elseif(checkmobile() || $device=='mobile'){
|
||||
return ['type'=>'page','page'=>'alipay_h5','data'=>['code_url'=>$code_url, 'redirect_url'=>'data.backurl']];
|
||||
}
|
||||
}elseif(in_array('4',$channel['apptype'])){
|
||||
$code_url = $siteurl.'pay/alipayjs/'.TRADE_NO.'/';
|
||||
}
|
||||
|
||||
if(checkalipay() || $mdevice=='alipay'){
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}else{
|
||||
return ['type'=>'qrcode','page'=>'alipay_qrcode','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
static public function alipayjs(){
|
||||
[$user_type, $user_id] = alipay_oauth();
|
||||
|
||||
$blocks = checkBlockUser($user_id, TRADE_NO);
|
||||
if($blocks) return $blocks;
|
||||
|
||||
if($user_type == 'openid'){
|
||||
return ['type'=>'error','msg'=>'支付宝快捷登录获取uid失败,需将用户标识切换到uid模式'];
|
||||
}
|
||||
|
||||
try{
|
||||
$payinfo = self::addOrder('A_JSAPI', null, $user_id);
|
||||
$result = json_decode($payinfo, true);
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'支付宝支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
if($_GET['d']=='1'){
|
||||
$redirect_url='data.backurl';
|
||||
}else{
|
||||
$redirect_url='\'/pay/ok/'.TRADE_NO.'/\'';
|
||||
}
|
||||
return ['type'=>'page','page'=>'alipay_jspay','data'=>['alipay_trade_no'=>$result['tradeNO'], 'redirect_url'=>$redirect_url]];
|
||||
}
|
||||
|
||||
//微信扫码支付
|
||||
static public function wxpay(){
|
||||
global $siteurl, $channel, $device;
|
||||
if(in_array('3',$channel['apptype']) && !in_array('2',$channel['apptype']) || in_array('1',$channel['apptype']) && $channel['appwxa']>0 && !$channel['appwxmp']){
|
||||
$code_url = $siteurl.'pay/wxwappay/'.TRADE_NO.'/';
|
||||
}else{
|
||||
$code_url = $siteurl.'pay/wxjspay/'.TRADE_NO.'/';
|
||||
}
|
||||
|
||||
if (checkmobile() || $device == 'mobile') {
|
||||
return ['type'=>'qrcode','page'=>'wxpay_wap','url'=>$code_url];
|
||||
} else {
|
||||
return ['type'=>'qrcode','page'=>'wxpay_qrcode','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//微信公众号支付
|
||||
static public function wxjspay(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf;
|
||||
|
||||
if(in_array('2',$channel['apptype']) || !$channel['appwxmp']){
|
||||
try{
|
||||
$jump_url = self::hostingOrder('T_JSAPI', 'M');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
return ['type'=>'jump','url'=>$jump_url];
|
||||
}
|
||||
|
||||
//①、获取用户openid
|
||||
$wxinfo = \lib\Channel::getWeixin($channel['appwxmp']);
|
||||
if(!$wxinfo) return ['type'=>'error','msg'=>'支付通道绑定的微信公众号不存在'];
|
||||
try{
|
||||
$tools = new \WeChatPay\JsApiTool($wxinfo['appid'], $wxinfo['appsecret']);
|
||||
$openid = $tools->GetOpenid();
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
$blocks = checkBlockUser($openid, TRADE_NO);
|
||||
if($blocks) return $blocks;
|
||||
|
||||
//②、统一下单
|
||||
try{
|
||||
$jsApiParameters = self::addOrder('T_JSAPI', $wxinfo['appid'], $openid);
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
if($_GET['d']==1){
|
||||
$redirect_url='data.backurl';
|
||||
}else{
|
||||
$redirect_url='\'/pay/ok/'.TRADE_NO.'/\'';
|
||||
}
|
||||
return ['type'=>'page','page'=>'wxpay_jspay','data'=>['jsApiParameters'=>$jsApiParameters, 'redirect_url'=>$redirect_url]];
|
||||
}
|
||||
|
||||
//微信小程序支付
|
||||
static public function wxminipay(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf;
|
||||
|
||||
$code = isset($_GET['code'])?trim($_GET['code']):exit('{"code":-1,"msg":"code不能为空"}');
|
||||
|
||||
//①、获取用户openid
|
||||
$wxinfo = \lib\Channel::getWeixin($channel['appwxa']);
|
||||
if(!$wxinfo)exit('{"code":-1,"msg":"支付通道绑定的微信小程序不存在"}');
|
||||
try{
|
||||
$tools = new \WeChatPay\JsApiTool($wxinfo['appid'], $wxinfo['appsecret']);
|
||||
$openid = $tools->AppGetOpenid($code);
|
||||
}catch(Exception $e){
|
||||
exit('{"code":-1,"msg":"'.$e->getMessage().'"}');
|
||||
}
|
||||
$blocks = checkBlockUser($openid, TRADE_NO);
|
||||
if($blocks)exit('{"code":-1,"msg":"'.$blocks['msg'].'"}');
|
||||
|
||||
//②、统一下单
|
||||
try{
|
||||
$jsApiParameters = self::addOrder('T_MINIAPP', $wxinfo['appid'], $openid);
|
||||
}catch(Exception $ex){
|
||||
exit('{"code":-1,"msg":"'.$ex->getMessage().'"}');
|
||||
}
|
||||
|
||||
exit(json_encode(['code'=>0, 'data'=>json_decode($jsApiParameters, true)]));
|
||||
}
|
||||
|
||||
//微信手机支付
|
||||
static public function wxwappay(){
|
||||
global $siteurl,$channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
if(in_array('3',$channel['apptype'])){
|
||||
try{
|
||||
$result = self::wxapphosting();
|
||||
$code_url = $result['scheme_code'];
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
return ['type'=>'scheme','page'=>'wxpay_mini','url'=>$code_url];
|
||||
}elseif(in_array('1',$channel['apptype']) && $channel['appwxa']>0){
|
||||
$wxinfo = \lib\Channel::getWeixin($channel['appwxa']);
|
||||
if(!$wxinfo) return ['type'=>'error','msg'=>'支付通道绑定的微信小程序不存在'];
|
||||
try{
|
||||
$code_url = wxminipay_jump_scheme($wxinfo['id'], TRADE_NO);
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'scheme','page'=>'wxpay_mini','url'=>$code_url];
|
||||
}else{
|
||||
$code_url = $siteurl.'pay/wxjspay/'.TRADE_NO.'/';
|
||||
return ['type'=>'qrcode','page'=>'wxpay_wap','url'=>$code_url];
|
||||
}
|
||||
}
|
||||
|
||||
//微信托管小程序下单
|
||||
static private function wxapphosting($need_scheme = 'Y', $return_type = false){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'pre_order_type' => '3',
|
||||
'req_date' => substr(TRADE_NO,0,8),
|
||||
'req_seq_id' => TRADE_NO,
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'trans_amt' => $order['realmoney'],
|
||||
'goods_desc' => $ordername,
|
||||
'miniapp_data' => json_encode(['need_scheme'=>$need_scheme]),
|
||||
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
|
||||
];
|
||||
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($client, $param, $return_type) {
|
||||
$result = $client->requestApi('/v2/trade/hosting/payment/preorder', $param);
|
||||
|
||||
if(isset($result['resp_code']) && $result['resp_code']=='00000000') {
|
||||
\lib\Payment::updateOrderCombine(TRADE_NO);
|
||||
return $return_type ? $result['pre_order_id'] : json_decode($result['miniapp_data'], true);
|
||||
}elseif(isset($result['resp_desc'])){
|
||||
throw new Exception($result['resp_desc'].($result['bank_message']?' '.$result['bank_message']:''));
|
||||
}else{
|
||||
throw new Exception('返回数据解析失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//支付宝托管小程序下单
|
||||
static private function aliapphosting(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'pre_order_type' => '2',
|
||||
'req_date' => substr(TRADE_NO,0,8),
|
||||
'req_seq_id' => TRADE_NO,
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'trans_amt' => $order['realmoney'],
|
||||
'goods_desc' => $ordername,
|
||||
'app_data' => json_encode(['app_schema'=>$siteurl. 'pay/return/' . TRADE_NO . '/']),
|
||||
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
|
||||
];
|
||||
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($client, $param) {
|
||||
$result = $client->requestApi('/v2/trade/hosting/payment/preorder', $param);
|
||||
|
||||
if(isset($result['resp_code']) && $result['resp_code']=='00000000') {
|
||||
\lib\Payment::updateOrderCombine(TRADE_NO);
|
||||
return $result['jump_url'];
|
||||
}elseif(isset($result['resp_desc'])){
|
||||
throw new Exception($result['resp_desc'].($result['bank_message']?' '.$result['bank_message']:''));
|
||||
}else{
|
||||
throw new Exception('返回数据解析失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//H5、PC预下单
|
||||
static private function hostingOrder($trans_type, $request_type){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'req_date' => substr(TRADE_NO,0,8),
|
||||
'req_seq_id' => TRADE_NO,
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'trans_amt' => $order['realmoney'],
|
||||
'goods_desc' => $ordername,
|
||||
'pre_order_type' => '1',
|
||||
'hosting_data' => json_encode(['project_title'=>$conf['sitename'], 'project_id'=>$channel['project_id'], 'callback_url'=>$siteurl. 'pay/return/' . TRADE_NO . '/', 'request_type'=>$request_type]),
|
||||
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
|
||||
'trans_type' => $trans_type
|
||||
];
|
||||
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($client, $param) {
|
||||
$result = $client->requestApi('/v2/trade/hosting/payment/preorder', $param);
|
||||
|
||||
if(isset($result['resp_code']) && $result['resp_code']=='00000000') {
|
||||
\lib\Payment::updateOrderCombine(TRADE_NO);
|
||||
return $result['jump_url'];
|
||||
}elseif(isset($result['resp_desc'])){
|
||||
throw new Exception($result['resp_desc'].($result['bank_message']?' '.$result['bank_message']:''));
|
||||
}else{
|
||||
throw new Exception('返回数据解析失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//微信小程序插件支付
|
||||
static public function wxplugin(){
|
||||
try{
|
||||
$pre_order_id = self::wxapphosting('N', true);
|
||||
$payinfo = ['appId'=>'wx11361ccf7f47b948', 'pre_order_id'=>$pre_order_id];
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'wxplugin','data'=>$payinfo];
|
||||
}
|
||||
|
||||
//支付宝APP支付
|
||||
static public function aliapppay(){
|
||||
try{
|
||||
$code_url = self::aliapphosting();
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'scheme','page'=>'alipay_qrcode','url'=>$code_url];
|
||||
}
|
||||
|
||||
//微信APP支付
|
||||
static public function wxapppay(){
|
||||
try{
|
||||
$result = self::wxapphosting('N');
|
||||
}catch(Exception $e){
|
||||
return ['type'=>'error','msg'=>$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'wxapp','data'=>['appId'=>'', 'miniProgramId'=>$result['gh_id'], 'path'=>$result['path']]];
|
||||
}
|
||||
|
||||
//云闪付扫码支付
|
||||
static public function unionpay(){
|
||||
try{
|
||||
$code_url = self::addOrder('U_NATIVE');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'云闪付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
return ['type'=>'qrcode','page'=>'bank_qrcode','url'=>$code_url];
|
||||
}
|
||||
|
||||
//快捷支付
|
||||
static public function quickpay(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip, $device;
|
||||
|
||||
if(checkmobile() || $device == 'mobile'){
|
||||
$request_type = 'M';
|
||||
$gw_chnnl_tp = '02';
|
||||
$device_type = '1';
|
||||
}else{
|
||||
$request_type = 'P';
|
||||
$gw_chnnl_tp = '01';
|
||||
$device_type = '4';
|
||||
}
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'req_seq_id' => TRADE_NO,
|
||||
'req_date' => substr(TRADE_NO,0,8),
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'trans_amt' => $order['realmoney'],
|
||||
'goods_desc' => $ordername,
|
||||
'request_type' => $request_type,
|
||||
'extend_pay_data' => json_encode(['goods_short_name'=>$order['name'], 'gw_chnnl_tp'=>$gw_chnnl_tp, 'biz_tp'=>'100099']),
|
||||
'terminal_device_data' => json_encode(['device_type'=>$device_type, 'device_ip'=>$clientip]),
|
||||
'risk_check_data' => json_encode(['ip_addr' => $clientip]),
|
||||
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
|
||||
'front_url' => $siteurl . 'pay/return/' . TRADE_NO . '/',
|
||||
];
|
||||
|
||||
try{
|
||||
$jump_url = \lib\Payment::lockPayData(TRADE_NO, function() use($client, $param) {
|
||||
$result = $client->requestApi('/v2/trade/onlinepayment/quickpay/frontpay', $param);
|
||||
|
||||
if(isset($result['resp_code']) && ($result['resp_code']=='00000000' || $result['resp_code']=='00000100')) {
|
||||
return $result['form_url'];
|
||||
}elseif(isset($result['resp_desc'])){
|
||||
throw new Exception($result['resp_desc'].($result['bank_message']?' '.$result['bank_message']:''));
|
||||
}else{
|
||||
throw new Exception('返回数据解析失败');
|
||||
}
|
||||
});
|
||||
return ['type'=>'jump','url'=>$jump_url];
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'快捷支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
//网银支付
|
||||
static public function bank(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip, $device;
|
||||
|
||||
if(checkmobile() || $device == 'mobile'){
|
||||
$gw_chnnl_tp = '02';
|
||||
$device_type = '1';
|
||||
}else{
|
||||
$gw_chnnl_tp = '01';
|
||||
$device_type = '4';
|
||||
}
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'req_seq_id' => TRADE_NO,
|
||||
'req_date' => substr(TRADE_NO,0,8),
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'trans_amt' => $order['realmoney'],
|
||||
'goods_desc' => $ordername,
|
||||
'extend_pay_data' => json_encode(['goods_short_name'=>$order['name'], 'gw_chnnl_tp'=>$gw_chnnl_tp, 'biz_tp'=>'100099']),
|
||||
'terminal_device_data' => json_encode(['device_type'=>$device_type, 'device_ip'=>$clientip]),
|
||||
'risk_check_data' => json_encode(['ip_addr' => $clientip]),
|
||||
'notify_url' => $conf['localurl'] . 'pay/notify/' . TRADE_NO . '/',
|
||||
'front_url' => $siteurl . 'pay/return/' . TRADE_NO . '/',
|
||||
];
|
||||
|
||||
try{
|
||||
$jump_url = \lib\Payment::lockPayData(TRADE_NO, function() use($client, $param) {
|
||||
$result = $client->requestApi('/v2/trade/onlinepayment/banking/frontpay', $param);
|
||||
|
||||
if(isset($result['resp_code']) && ($result['resp_code']=='00000000' || $result['resp_code']=='00000100')) {
|
||||
return $result['form_url'];
|
||||
}elseif(isset($result['resp_desc'])){
|
||||
throw new Exception($result['resp_desc'].($result['bank_message']?' '.$result['bank_message']:''));
|
||||
}else{
|
||||
throw new Exception('返回数据解析失败');
|
||||
}
|
||||
});
|
||||
return ['type'=>'jump','url'=>$jump_url];
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'网银支付下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
//数字人民币支付
|
||||
static public function ecny(){
|
||||
try{
|
||||
$code_url = self::addOrder('D_NATIVE');
|
||||
}catch(Exception $ex){
|
||||
return ['type'=>'error','msg'=>'数字人民币下单失败!'.$ex->getMessage()];
|
||||
}
|
||||
|
||||
return ['type'=>'qrcode','page'=>'bank_qrcode','url'=>$code_url];
|
||||
}
|
||||
|
||||
//异步回调
|
||||
static public function notify(){
|
||||
global $channel, $order;
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$data = json_decode($_POST['resp_data'], true);
|
||||
if(!$data)return ['type'=>'html','data'=>'no data'];
|
||||
|
||||
if($client->checkNotifySign($_POST['resp_data'], $_POST['sign'])){
|
||||
if ($data['trans_stat'] == 'S') {
|
||||
if($data['req_seq_id'] == TRADE_NO){
|
||||
$api_trade_no = $data['hf_seq_id'];
|
||||
$bill_trade_no = $data['out_trans_id'];
|
||||
if(isset($data['alipay_response'])){
|
||||
$buyer = $data['alipay_response']['buyer_id'];
|
||||
}elseif(isset($data['wx_response'])){
|
||||
$buyer = $data['wx_response']['sub_openid'];
|
||||
}
|
||||
processNotify($order, $api_trade_no, $buyer, $bill_trade_no);
|
||||
}
|
||||
return ['type'=>'html','data'=>'RECV_ORD_ID_'.TRADE_NO];
|
||||
}
|
||||
return ['type'=>'html','data'=>'resp_code fail'];
|
||||
}
|
||||
else {
|
||||
return ['type'=>'html','data'=>'sign fail'];
|
||||
}
|
||||
}
|
||||
|
||||
//支付成功页面
|
||||
static public function ok(){
|
||||
return ['type'=>'page','page'=>'ok'];
|
||||
}
|
||||
|
||||
//支付返回页面
|
||||
static public function return(){
|
||||
return ['type'=>'page','page'=>'return'];
|
||||
}
|
||||
|
||||
//退款
|
||||
static public function refund($order){
|
||||
global $channel;
|
||||
if(empty($order))exit();
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'req_date' => date("Ymd"),
|
||||
'req_seq_id' => $order['refund_no'],
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'ord_amt' => $order['refundmoney'],
|
||||
'org_req_date' => substr($order['trade_no'], 0, 8),
|
||||
'org_req_seq_id' => $order['trade_no']
|
||||
];
|
||||
try{
|
||||
$result = $client->requestApi('/v2/trade/payment/scanpay/refund', $param);
|
||||
}catch(Exception $e){
|
||||
return ['code'=>-1, 'msg'=>$e->getMessage()];
|
||||
}
|
||||
|
||||
if($result['resp_code'] == '00000000' || $result['resp_code'] == '00000100'){
|
||||
return ['code'=>0, 'trade_no'=>$result['hf_seq_id'], 'refund_fee'=>$result['ord_amt']];
|
||||
}else{
|
||||
return ['code'=>-1, 'msg'=>$result['resp_desc']];
|
||||
}
|
||||
}
|
||||
|
||||
//托管支付退款
|
||||
static public function refund_combine($order){
|
||||
global $channel, $conf;
|
||||
if(empty($order))exit();
|
||||
|
||||
require_once PAY_ROOT."inc/HuifuClient.php";
|
||||
$config_info = [
|
||||
'sys_id' => $channel['appid'],
|
||||
'product_id' => $channel['appurl'],
|
||||
'merchant_private_key' => $channel['appsecret'],
|
||||
'huifu_public_key' => $channel['appkey'],
|
||||
];
|
||||
$client = new HuifuClient($config_info);
|
||||
|
||||
$param = [
|
||||
'req_date' => date("Ymd"),
|
||||
'req_seq_id' => $order['refund_no'],
|
||||
'huifu_id' => $channel['appmchid']?$channel['appmchid']:$channel['appid'],
|
||||
'ord_amt' => $order['refundmoney'],
|
||||
'org_req_date' => substr($order['trade_no'], 0, 8),
|
||||
'org_req_seq_id' => $order['trade_no']
|
||||
];
|
||||
try{
|
||||
$result = $client->requestApi('/v2/trade/hosting/payment/htRefund', $param);
|
||||
}catch(Exception $e){
|
||||
return ['code'=>-1, 'msg'=>$e->getMessage()];
|
||||
}
|
||||
|
||||
if($result['resp_code'] == '00000000' || $result['resp_code'] == '00000100'){
|
||||
return ['code'=>0, 'trade_no'=>$result['hf_seq_id'], 'refund_fee'=>$result['ord_amt']];
|
||||
}else{
|
||||
return ['code'=>-1, 'msg'=>$result['resp_desc']];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
|
||||
class HuifuClient
|
||||
{
|
||||
const BASE_API_URL = "https://api.huifu.com";
|
||||
|
||||
//汇付系统号
|
||||
private $sys_id;
|
||||
|
||||
//汇付产品号
|
||||
private $product_id;
|
||||
|
||||
//商户私钥
|
||||
private $merchant_private_key;
|
||||
|
||||
//汇付公钥
|
||||
private $huifu_public_key;
|
||||
|
||||
/**
|
||||
* @param $config 商户配置信息
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (empty($config['sys_id'])) {
|
||||
throw new \InvalidArgumentException('汇付系统号不能为空');
|
||||
}
|
||||
if (empty($config['product_id'])) {
|
||||
throw new \InvalidArgumentException("汇付产品号不能为空");
|
||||
}
|
||||
if (empty($config['merchant_private_key'])) {
|
||||
throw new \InvalidArgumentException("商户私钥不能为空");
|
||||
}
|
||||
if (empty($config['huifu_public_key'])) {
|
||||
throw new \InvalidArgumentException("汇付公钥不能为空");
|
||||
}
|
||||
$this->sys_id = $config['sys_id'];
|
||||
$this->product_id = $config['product_id'];
|
||||
$this->merchant_private_key = $this->loadPrivateKey($config['merchant_private_key']);
|
||||
$this->huifu_public_key = $this->loadPublicKey($config['huifu_public_key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求API接口并解析返回数据
|
||||
*/
|
||||
public function requestApi($path, $data)
|
||||
{
|
||||
$url = self::BASE_API_URL . $path;
|
||||
$body = [
|
||||
'sys_id' => $this->sys_id,
|
||||
'product_id' => $this->product_id,
|
||||
'data' => $data
|
||||
];
|
||||
$body['sign'] = $this->makeSign($data);
|
||||
$response = $this->curlPost($url, $body);
|
||||
$result = json_decode($response, true);
|
||||
if (!$result || empty($result['data']) || empty($result['sign'])) {
|
||||
throw new \Exception("接口返回数据解析失败");
|
||||
}
|
||||
//print_r($result);
|
||||
if (!$this->checkResponseSign($result['data'], $result['sign'])) {
|
||||
throw new \Exception("接口返回数据验签失败");
|
||||
}
|
||||
return $result['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
public function upload($path, $data, $file_path, $file_name)
|
||||
{
|
||||
$url = self::BASE_API_URL . $path;
|
||||
$body = [
|
||||
'sys_id' => $this->sys_id,
|
||||
'product_id' => $this->product_id,
|
||||
'data' => $data
|
||||
];
|
||||
$file = new \CURLFile($file_path, '', $file_name);
|
||||
$body['sign'] = $this->makeSign($data);
|
||||
$response = $this->curlPost($url, $body, $file);
|
||||
$result = json_decode($response, true);
|
||||
if (!$result || empty($result['data'])) {
|
||||
throw new \Exception("接口返回数据解析失败");
|
||||
}
|
||||
return $result['data'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起POST请求
|
||||
* @param string $url 请求URL
|
||||
* @param array $body POST数据
|
||||
* @param \CURLFile $file 上传文件
|
||||
* @return string
|
||||
*/
|
||||
private function curlPost($url, $body, $file = null)
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
if ($file) {
|
||||
$body['data'] = json_encode($body['data'], JSON_UNESCAPED_UNICODE);
|
||||
$body['file'] = $file;
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||
} else {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
|
||||
}
|
||||
$response = curl_exec($ch);
|
||||
if (curl_errno($ch) > 0) {
|
||||
$errmsg = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new \Exception($errmsg, 0);
|
||||
}
|
||||
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
if ($httpStatusCode != 200) {
|
||||
curl_close($ch);
|
||||
throw new \Exception($response ? $response : 'http_code=' . $httpStatusCode, $httpStatusCode);
|
||||
}
|
||||
curl_close($ch);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求签名
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
private function makeSign($params)
|
||||
{
|
||||
$params = array_filter($params, function ($value) {
|
||||
return $value !== null;
|
||||
});
|
||||
ksort($params);
|
||||
$content = json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
return $this->rsaPrivateSign($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验通知数据签名
|
||||
* @param string $data
|
||||
* @param string $sign
|
||||
* @return bool
|
||||
*/
|
||||
public function checkNotifySign($data, $sign)
|
||||
{
|
||||
if (empty($sign)) return false;
|
||||
return $this->rsaPublicVerify($data, $sign);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验返回数据签名
|
||||
* @param array $params
|
||||
* @param string $sign
|
||||
* @return bool
|
||||
*/
|
||||
private function checkResponseSign($params, $sign)
|
||||
{
|
||||
if (empty($sign)) return false;
|
||||
ksort($params);
|
||||
$content = json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
return $this->rsaPublicVerify($content, $sign);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商户私钥签名
|
||||
* @param string $data 待签名字符串
|
||||
* @return string
|
||||
*/
|
||||
private function rsaPrivateSign($data)
|
||||
{
|
||||
openssl_sign($data, $signature, $this->merchant_private_key, OPENSSL_ALGO_SHA256);
|
||||
return base64_encode($signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台公钥验签
|
||||
* @param string $data 待验签字符串
|
||||
* @return bool
|
||||
*/
|
||||
private function rsaPublicVerify($data, $signature)
|
||||
{
|
||||
$result = openssl_verify($data, base64_decode($signature), $this->huifu_public_key, OPENSSL_ALGO_SHA256);
|
||||
return $result === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台公钥加密
|
||||
* @param string $data 待加密字符串
|
||||
* @return string
|
||||
*/
|
||||
private function rsaPublicEncrypt($data)
|
||||
{
|
||||
openssl_public_encrypt($data, $encryptResult, $this->huifu_public_key, OPENSSL_PKCS1_PADDING);
|
||||
return base64_encode($encryptResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载汇付公钥
|
||||
*/
|
||||
private function loadPublicKey($public_key)
|
||||
{
|
||||
$res = "-----BEGIN PUBLIC KEY-----\n" .
|
||||
wordwrap($public_key, 64, "\n", true) .
|
||||
"\n-----END PUBLIC KEY-----";
|
||||
$pubkeyid = openssl_get_publickey($res);
|
||||
if (!$pubkeyid) {
|
||||
throw new Exception('汇付公钥不正确');
|
||||
}
|
||||
return $pubkeyid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载商户私钥
|
||||
*/
|
||||
private function loadPrivateKey($private_key)
|
||||
{
|
||||
$res = "-----BEGIN PRIVATE KEY-----\n" .
|
||||
wordwrap($private_key, 64, "\n", true) .
|
||||
"\n-----END PRIVATE KEY-----";
|
||||
$prikeyid = openssl_get_privatekey($res);
|
||||
if (!$prikeyid) {
|
||||
throw new Exception('商户私钥不正确');
|
||||
}
|
||||
return $prikeyid;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user