first commit
This commit is contained in:
@@ -0,0 +1,517 @@
|
||||
<?php
|
||||
|
||||
class adapay_plugin
|
||||
{
|
||||
static public $info = [
|
||||
'name' => 'adapay', //支付插件英文名称,需和目录名称一致,不能有重复
|
||||
'showname' => 'AdaPay聚合支付', //支付插件显示名称
|
||||
'author' => 'AdaPay', //支付插件作者
|
||||
'link' => 'https://www.adapay.tech/', //支付插件作者链接
|
||||
'types' => ['alipay','wxpay','bank'], //支付插件支持的支付方式,可选的有alipay,qqpay,wxpay,bank
|
||||
'inputs' => [ //支付插件要求传入的参数以及参数显示名称,可选的有appid,appkey,appsecret,appurl,appmchid
|
||||
'appid' => [
|
||||
'name' => '应用App_ID',
|
||||
'type' => 'input',
|
||||
'note' => '',
|
||||
],
|
||||
'appkey' => [
|
||||
'name' => 'prod模式API_KEY',
|
||||
'type' => 'input',
|
||||
'note' => '',
|
||||
],
|
||||
'appsecret' => [
|
||||
'name' => '商户RSA私钥',
|
||||
'type' => 'textarea',
|
||||
'note' => '',
|
||||
],
|
||||
],
|
||||
'select' => null,
|
||||
'select_alipay' => [
|
||||
'1' => '扫码支付',
|
||||
'2' => 'JS支付',
|
||||
'3' => '托管小程序支付',
|
||||
],
|
||||
'select_wxpay' => [
|
||||
'1' => '自有公众号/小程序支付',
|
||||
'2' => '动态二维码支付',
|
||||
'3' => '托管小程序支付',
|
||||
],
|
||||
'select_bank' => [
|
||||
'1' => '银联支付',
|
||||
'2' => '快捷支付',
|
||||
'3' => '网银支付',
|
||||
],
|
||||
'note' => '', //支付密钥填写说明
|
||||
'bindwxmp' => true, //是否支持绑定微信公众号
|
||||
'bindwxa' => true, //是否支持绑定微信小程序
|
||||
];
|
||||
|
||||
static public function submit(){
|
||||
global $siteurl, $channel, $order, $sitename;
|
||||
|
||||
if($order['typename']=='alipay'){
|
||||
if(checkalipay() && 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']) && 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.'/'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static public function mapi(){
|
||||
global $siteurl, $channel, $order, $device, $mdevice;
|
||||
|
||||
if($order['typename']=='alipay'){
|
||||
if($mdevice=='alipay' && 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']) && $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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//通用创建订单
|
||||
static private function addOrder($pay_channel, $openid = null){
|
||||
global $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
require PAY_ROOT . 'inc/Build.class.php';
|
||||
$pay_config = include PAY_ROOT . 'inc/config.php';
|
||||
$params = [
|
||||
'order_no' => TRADE_NO,
|
||||
'pay_channel' => $pay_channel,
|
||||
'pay_amt' => $order['realmoney'],
|
||||
'goods_title' => $ordername,
|
||||
'goods_desc' => $ordername,
|
||||
'currency' => 'cny',
|
||||
'notify_url' => $conf['localurl'].'pay/notify/'.TRADE_NO.'/',
|
||||
];
|
||||
if ($pay_channel === 'wx_pub' || $pay_channel === 'wx_lite') {
|
||||
$params['expend'] = [
|
||||
'openid' => $openid,
|
||||
];
|
||||
}elseif ($pay_channel === 'alipay_pub' || $pay_channel === 'alipay_lite') {
|
||||
$params['expend'] = [
|
||||
'buyer_id' => $openid,
|
||||
];
|
||||
}
|
||||
if($order['profits'] > 0){
|
||||
$params['pay_mode'] = 'delay';
|
||||
}
|
||||
/*if($order['profits'] > 0){
|
||||
$psreceiver = \lib\ProfitSharing\CommUtil::getReceiver($order['profits']);
|
||||
if($psreceiver){
|
||||
$psmoney = round(floor($order['realmoney'] * $psreceiver['rate']) / 100, 2);
|
||||
$psmoney2 = round($order['realmoney']-$psmoney, 2);
|
||||
$div_members = [];
|
||||
$div_members[] = ['member_id'=>$psreceiver['account'], 'amount' => sprintf('%.2f' , $psmoney), 'fee_flag'=>'N'];
|
||||
if($psmoney2 > 0){
|
||||
$div_members[] = ['member_id'=>'0', 'amount' => sprintf('%.2f' , $psmoney2), 'fee_flag'=>'Y'];
|
||||
}else{
|
||||
$div_members[0]['fee_flag'] = 'Y';
|
||||
}
|
||||
$params['div_members'] = $div_members;
|
||||
}
|
||||
}*/
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($pay_config, $params) {
|
||||
$result = AdaPay::config($pay_config)->createPayment($params);
|
||||
return $result['expend'];
|
||||
});
|
||||
}
|
||||
|
||||
//跳转支付创建订单
|
||||
static private function pagepay($func_code, $pay_channel){
|
||||
global $channel, $order, $ordername, $conf, $clientip, $siteurl;
|
||||
|
||||
require PAY_ROOT . 'inc/Build.class.php';
|
||||
$pay_config = include PAY_ROOT . 'inc/config.php';
|
||||
$params = [
|
||||
'adapay_func_code' => $func_code,
|
||||
'order_no' => TRADE_NO,
|
||||
'pay_channel' => $pay_channel,
|
||||
'pay_amt' => $order['realmoney'],
|
||||
'goods_title' => $ordername,
|
||||
'goods_desc' => $ordername,
|
||||
'currency' => 'cny',
|
||||
'notify_url' => $conf['localurl'].'pay/notify/'.TRADE_NO.'/',
|
||||
'callback_url' => $siteurl.'pay/return/'.TRADE_NO.'/',
|
||||
];
|
||||
if($order['profits'] > 0){
|
||||
$params['pay_mode'] = 'delay';
|
||||
}
|
||||
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($pay_config, $params) {
|
||||
$result = AdaPay::config($pay_config)->queryAdapay($params);
|
||||
return $result['expend'];
|
||||
});
|
||||
}
|
||||
|
||||
//收银台创建订单
|
||||
static private function checkout($pay_channel, $member_id = null){
|
||||
global $channel, $order, $ordername, $conf, $clientip, $siteurl;
|
||||
|
||||
require PAY_ROOT . 'inc/Build.class.php';
|
||||
$pay_config = include PAY_ROOT . 'inc/config.php';
|
||||
$params = [
|
||||
'adapay_func_code' => 'checkout',
|
||||
'order_no' => TRADE_NO,
|
||||
'pay_channel' => $pay_channel,
|
||||
'pay_amt' => $order['realmoney'],
|
||||
'goods_title' => $ordername,
|
||||
'goods_desc' => $ordername,
|
||||
'currency' => 'cny',
|
||||
'notify_url' => $conf['localurl'].'pay/notify/'.TRADE_NO.'/',
|
||||
'callback_url' => $siteurl.'pay/return/'.TRADE_NO.'/',
|
||||
];
|
||||
if($member_id){
|
||||
$params['member_id'] = $member_id;
|
||||
}
|
||||
|
||||
return \lib\Payment::lockPayData(TRADE_NO, function() use($pay_config, $params) {
|
||||
$result = AdaPay::config($pay_config)->queryAdapay($params);
|
||||
return $result['expend'];
|
||||
});
|
||||
}
|
||||
|
||||
//支付宝扫码支付
|
||||
static public function alipay(){
|
||||
global $channel, $device, $mdevice, $siteurl;
|
||||
if(in_array('1',$channel['apptype']) || empty($channel['apptype'][0])){
|
||||
try{
|
||||
$result = self::addOrder('alipay_qr');
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'支付宝下单失败!'.$e->getMessage()];
|
||||
}
|
||||
$code_url = $result['qrcode_url'];
|
||||
}elseif(in_array('2',$channel['apptype'])){
|
||||
$code_url = $siteurl.'pay/alipayjs/'.TRADE_NO.'/';
|
||||
}elseif(in_array('3',$channel['apptype'])){
|
||||
try{
|
||||
$result = self::pagepay('prePay.preOrder', 'alipay_lite');
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
$code_url = $result['ali_h5_pay_url'];
|
||||
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']];
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
$result = self::addOrder('alipay_pub', $user_id);
|
||||
$payinfo = json_decode($result['pay_info'], 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'=>$payinfo['tradeNO'], 'redirect_url'=>$redirect_url]];
|
||||
}
|
||||
|
||||
//微信扫码支付
|
||||
static public function wxpay(){
|
||||
global $siteurl, $channel, $device, $mdevice;
|
||||
|
||||
if(in_array('2',$channel['apptype'])){
|
||||
try{
|
||||
$result = self::pagepay('qrPrePay.qrPreOrder', '');
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
$code_url = $result['qr_pay_url'];
|
||||
}elseif(in_array('3',$channel['apptype']) && !in_array('1',$channel['apptype'])){
|
||||
$code_url = $siteurl.'pay/wxwappay/'.TRADE_NO.'/';
|
||||
}else{
|
||||
$code_url = $siteurl.'pay/wxjspay/'.TRADE_NO.'/';
|
||||
}
|
||||
|
||||
if(checkwechat() || $mdevice == 'wechat'){
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
} elseif (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;
|
||||
|
||||
//①、获取用户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{
|
||||
$result = self::addOrder('wx_pub', $openid);
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
|
||||
$jsApiParameters = $result['pay_info'];
|
||||
|
||||
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 wxwappay(){
|
||||
global $siteurl,$channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
if($channel['appwxa']>0 && in_array('1',$channel['apptype'])){
|
||||
$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];
|
||||
}elseif(in_array('3',$channel['apptype'])){ //托管小程序支付
|
||||
try{
|
||||
$result = self::pagepay('wxpay.createOrder', 'wx_lite');
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'微信支付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
$code_url = $result['scheme_code'];
|
||||
return ['type'=>'scheme','page'=>'wxpay_mini','url'=>$code_url];
|
||||
}else{
|
||||
return self::wxpay();
|
||||
}
|
||||
}
|
||||
|
||||
//微信小程序支付
|
||||
static public function wxminipay(){
|
||||
global $siteurl, $channel, $order, $ordername, $conf, $clientip;
|
||||
|
||||
$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{
|
||||
$result = self::addOrder('wx_lite', $openid);
|
||||
}catch (Exception $e) {
|
||||
exit('{"code":-1,"msg":"微信支付下单失败!'.$e->getMessage().'"}');
|
||||
}
|
||||
|
||||
$jsApiParameters = $result['pay_info'];
|
||||
exit(json_encode(['code'=>0, 'data'=>json_decode($jsApiParameters, true)]));
|
||||
}
|
||||
|
||||
//云闪付扫码支付
|
||||
static public function unionpay(){
|
||||
try{
|
||||
$result = self::addOrder('union_qr');
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'云闪付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
|
||||
$code_url = $result['qrcode_url'];
|
||||
|
||||
return ['type'=>'qrcode','page'=>'bank_qrcode','url'=>$code_url];
|
||||
}
|
||||
|
||||
//快捷支付
|
||||
static public function quickpay(){
|
||||
if(!empty($_COOKIE['adapay_user_id'])){
|
||||
$user_id = $_COOKIE['adapay_user_id'];
|
||||
}else{
|
||||
$user_id = substr(getSid(), 0, 10);
|
||||
setcookie('adapay_user_id', $user_id, time()+3600*24*365, '/');
|
||||
}
|
||||
try{
|
||||
$result = self::checkout('fast_pay', $user_id);
|
||||
$code_url = $result['pay_url'];
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'快捷支付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}
|
||||
|
||||
//网银支付
|
||||
static public function bank(){
|
||||
try{
|
||||
$result = self::checkout('online_pay');
|
||||
$code_url = $result['pay_url'];
|
||||
}catch (Exception $e) {
|
||||
return ['type'=>'error','msg'=>'网银支付下单失败!'.$e->getMessage()];
|
||||
}
|
||||
return ['type'=>'jump','url'=>$code_url];
|
||||
}
|
||||
|
||||
//异步回调
|
||||
static public function notify(){
|
||||
global $channel, $order;
|
||||
|
||||
//file_put_contents('logs.txt',http_build_query($_POST));
|
||||
|
||||
require_once PAY_ROOT . 'inc/Build.class.php';
|
||||
$app = AdaPay::config(include PAY_ROOT . 'inc/config.php');
|
||||
if ($app->ada_tools->verifySign($_POST['sign'] , $_POST['data'])) {
|
||||
$_data = json_decode($_POST['data'] , true);
|
||||
if ($_data['status'] == 'succeeded') {
|
||||
$api_trade_no = daddslashes($_data['id']);
|
||||
$trade_no = daddslashes($_data['order_no']);
|
||||
$orderAmount = sprintf('%.2f' , $_data['pay_amt']);
|
||||
if ($trade_no == TRADE_NO) {
|
||||
processNotify($order, $api_trade_no);
|
||||
}
|
||||
return ['type'=>'html','data'=>'Ok'];
|
||||
} else {
|
||||
return ['type'=>'html','data'=>'No'];
|
||||
}
|
||||
} else {
|
||||
return ['type'=>'html','data'=>'No'];
|
||||
}
|
||||
}
|
||||
|
||||
//支付成功页面
|
||||
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 PAY_ROOT . 'inc/Build.class.php';
|
||||
$params = [
|
||||
'payment_id' => $order['api_trade_no'],
|
||||
'refund_order_no' => $order['refund_no'],
|
||||
'refund_amt' => $order['realmoney']
|
||||
];
|
||||
if($order['profits'] > 0){
|
||||
$psorder = \lib\ProfitSharing\CommUtil::getOrder($order['trade_no']);
|
||||
if($psorder && ($psorder['status'] == 1 || $psorder['status'] == 2)){
|
||||
$div_members = [];
|
||||
if(strpos($psorder['account'], '|')){
|
||||
$accounts = explode('|', $account);
|
||||
$rates = explode('|', $psorder['rate']);
|
||||
$allmoney = 0;
|
||||
foreach($accounts as $i=>$account){
|
||||
$rate = isset($rates[$i]) ? $rates[$i] : $rates[0];
|
||||
$money = round($order_money * $rate / 100, 2);
|
||||
$div_members[] = ['member_id'=>$account, 'amount' => sprintf('%.2f' , $money)];
|
||||
$allmoney += $money;
|
||||
}
|
||||
if($order_money > $allmoney){
|
||||
$psmoney2 = round($order_money-$allmoney, 2);
|
||||
$div_members[] = ['member_id'=>'0', 'amount' => sprintf('%.2f' , $psmoney2)];
|
||||
}
|
||||
}else{
|
||||
$amount = $psorder['money'] > $order['refundmoney'] ? $order['refundmoney'] : $psorder['money'];
|
||||
$div_members[] = [
|
||||
'member_id' => $psorder['account'],
|
||||
'amount' => sprintf('%.2f' , $amount),
|
||||
];
|
||||
if($order['refundmoney'] > $psorder['money']){
|
||||
$amount = round($order['refundmoney'] - $psorder['money'], 2);
|
||||
$div_members[] = [
|
||||
'member_id' => '0',
|
||||
'amount' => sprintf('%.2f' , $amount),
|
||||
];
|
||||
}
|
||||
}
|
||||
$params['payment_id'] = $psorder['settle_no'];
|
||||
$params['div_members'] = $div_members;
|
||||
}
|
||||
}
|
||||
try{
|
||||
$res = AdaPay::config(include PAY_ROOT . 'inc/config.php')->createRefund($params);
|
||||
}catch(Exception $e){
|
||||
return ['code'=>-1, 'msg'=>$e->getMessage()];
|
||||
}
|
||||
|
||||
if($res['status']=='succeeded'||$res['status']=='pending'){
|
||||
$result = ['code'=>0, 'trade_no'=>$res['id'], 'refund_fee'=>$res['refund_amt']];
|
||||
}else{
|
||||
$result = ['code'=>-1, 'msg'=>'['.$res["error_code"].']'.$res["error_msg"]];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,369 @@
|
||||
<?php
|
||||
Class AdaTools
|
||||
{
|
||||
public $rsaPrivateKeyFilePath;
|
||||
public $rsaPublicKeyFilePath;
|
||||
public $rsaPrivateKey;
|
||||
public $rsaPublicKey;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function generateSignature($url , $params = []):string
|
||||
{
|
||||
$data = '';
|
||||
if (is_array($params)) {
|
||||
$data .= $url . json_encode($params);
|
||||
} else {
|
||||
$data .= $url . $params;
|
||||
}
|
||||
$sign = $this->SHA1withRSA($data);
|
||||
return $sign;
|
||||
}
|
||||
|
||||
public function SHA1withRSA($data)
|
||||
{
|
||||
if ($this->checkEmpty($this->rsaPrivateKeyFilePath)) {
|
||||
$privKey = trim($this->rsaPrivateKey);
|
||||
$key = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($privKey, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
|
||||
} else {
|
||||
$privKey = file_get_contents($this->rsaPrivateKeyFilePath);
|
||||
$key = openssl_get_privatekey($privKey);
|
||||
}
|
||||
openssl_sign($data , $signature , $key , OPENSSL_ALGO_SHA1);
|
||||
return base64_encode($signature);
|
||||
}
|
||||
|
||||
public function verifySign($signature , $data)
|
||||
{
|
||||
if ($this->checkEmpty($this->rsaPublicKeyFilePath)) {
|
||||
$pubKey = trim($this->rsaPublicKey);
|
||||
$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($pubKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
|
||||
} else {
|
||||
$pubKey = file_get_contents($this->rsaPublicKeyFilePath);
|
||||
$key = openssl_get_publickey($pubKey);
|
||||
}
|
||||
if (openssl_verify($data , base64_decode($signature) , $key , OPENSSL_ALGO_SHA1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function checkEmpty($value)
|
||||
{
|
||||
if (!isset($value) || ('' === trim($value)) || is_null($value)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
class AdaPay
|
||||
{
|
||||
const SDK_VERSION = 'v1.0.0';
|
||||
static $gateWayUrl = 'https://api.adapay.tech'; //网关地址
|
||||
static $header = ['Content-Type:application/json'];
|
||||
static $headerText = ['Content-Type:text/html'];
|
||||
static $rsaPrivateKey;
|
||||
static $rsaPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwN6xgd6Ad8v2hIIsQVnbt8a3JituR8o4Tc3B5WlcFR55bz4OMqrG/356Ur3cPbc2Fe8ArNd/0gZbC9q56Eb16JTkVNA/fye4SXznWxdyBPR7+guuJZHc/VW2fKH2lfZ2P3Tt0QkKZZoawYOGSMdIvO+WqK44updyax0ikK6JlNQIDAQAB";
|
||||
static $signType = 'RSA2';
|
||||
static $app_id;
|
||||
static $api_key;
|
||||
public $ada_tools;
|
||||
|
||||
public function __construct($config_info)
|
||||
{
|
||||
if (empty($config_info) || !is_array($config_info)) {
|
||||
throw new \Exception('缺少SDK配置信息');
|
||||
}
|
||||
if (empty($config_info['app_id'])) {
|
||||
throw new \Exception('应用AppID不能为空');
|
||||
}
|
||||
if (empty($config_info['api_key_live'])) {
|
||||
throw new \Exception('API_KEY不能为空');
|
||||
}
|
||||
if (empty($config_info['rsa_private_key'])) {
|
||||
throw new \Exception('商户RSA私钥不能为空');
|
||||
}
|
||||
|
||||
$sdk_version = self::SDK_VERSION;
|
||||
array_push(self::$header , "sdk_version:{$sdk_version}");
|
||||
array_push(self::$headerText , "sdk_version:{$sdk_version}");
|
||||
|
||||
self::$app_id = trim($config_info['app_id']);
|
||||
self::$api_key = trim($config_info['api_key_live']);
|
||||
self::$rsaPrivateKey = trim($config_info['rsa_private_key']);
|
||||
|
||||
$this->ada_tools = new AdaTools();
|
||||
$this->ada_tools->rsaPrivateKey = self::$rsaPrivateKey;
|
||||
$this->ada_tools->rsaPublicKey = self::$rsaPublicKey;
|
||||
}
|
||||
|
||||
static function config($config = [])
|
||||
{
|
||||
return new static($config);
|
||||
}
|
||||
|
||||
private function get_headers($req_url , $postData , array $header = [])
|
||||
{
|
||||
array_push($header , 'Authorization:' . self::$api_key);
|
||||
array_push($header , 'Signature:' . $this->ada_tools->generateSignature($req_url , $postData));
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function request($method, $endpoint, $params = null)
|
||||
{
|
||||
$req_url = self::$gateWayUrl . $endpoint;
|
||||
if($method == 'GET'){
|
||||
if($params){
|
||||
ksort($params);
|
||||
$headers = $this->get_headers($req_url , http_build_query($params) , self::$headerText);
|
||||
$req_url .= '?' . http_build_query($params);
|
||||
}else{
|
||||
$headers = $this->get_headers($req_url , "" , self::$headerText);
|
||||
}
|
||||
$response = $this->curl($req_url , null , $headers);
|
||||
}else{
|
||||
$headers = $this->get_headers($req_url , $params , self::$header);
|
||||
$response = $this->curl($req_url , $params , $headers);
|
||||
}
|
||||
|
||||
if (!$response || !($result = json_decode($response , true))) {
|
||||
throw new Exception('返回内容为空或解析失败');
|
||||
}
|
||||
if(!isset($result['data']) && isset($result['message'])){
|
||||
throw new Exception($result['message']);
|
||||
}
|
||||
$data = json_decode($result['data'], true);
|
||||
|
||||
if ($data['status'] !== 'succeeded' && $data['status'] !== 'pending' && empty($data['expend'])) {
|
||||
throw new Exception('['.$data['error_code'].']'.$data['error_msg']);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
//创建支付对象
|
||||
public function createPayment($params)
|
||||
{
|
||||
$endpoint = '/v1/payments';
|
||||
$public_params = [
|
||||
'app_id' => self::$app_id,
|
||||
'sign_type' => self::$signType,
|
||||
];
|
||||
$params = array_merge($params, $public_params);
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//通用请求
|
||||
public function queryAdapay($params)
|
||||
{
|
||||
self::$gateWayUrl = "https://page.adapay.tech";
|
||||
$adapayFuncCode = $params["adapay_func_code"];
|
||||
$endpoint = '/v1/'.str_replace(".", "/",$adapayFuncCode);
|
||||
$public_params = [
|
||||
'app_id' => self::$app_id,
|
||||
];
|
||||
$params = array_merge($public_params, $params);
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//通用请求
|
||||
public function requestAdapay($params)
|
||||
{
|
||||
$adapayFuncCode = $params["adapay_func_code"];
|
||||
$endpoint = '/v1/'.str_replace(".", "/",$adapayFuncCode);
|
||||
$public_params = [
|
||||
'app_id' => self::$app_id,
|
||||
];
|
||||
$params = array_merge($public_params, $params);
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询支付对象
|
||||
public function queryPayment($id)
|
||||
{
|
||||
$endpoint = '/v1/payments/'.$id;
|
||||
return $this->request('GET', $endpoint, null);
|
||||
}
|
||||
|
||||
//创建退款对象
|
||||
public function createRefund($params){
|
||||
$charge_id = isset($params['payment_id']) ? $params['payment_id'] : '';
|
||||
$endpoint = '/v1/payments/'.$charge_id.'/refunds';
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询退款对象
|
||||
public function queryRefund($params){
|
||||
$endpoint = '/v1/payments/refunds';
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//创建用户对象
|
||||
public function createMember($member_id){
|
||||
$params = [
|
||||
'app_id' => self::$app_id,
|
||||
'member_id' => $member_id,
|
||||
];
|
||||
$endpoint = '/v1/members';
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//创建结算账户对象
|
||||
public function createSettleAccount($member_id, $account_info){
|
||||
$params = [
|
||||
'app_id' => self::$app_id,
|
||||
'member_id' => $member_id,
|
||||
'channel' => 'bank_account',
|
||||
'account_info' => $account_info
|
||||
];
|
||||
$endpoint = '/v1/settle_accounts';
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询结算账户对象
|
||||
public function querySettleAccount($member_id, $settle_account_id){
|
||||
$params = [
|
||||
'app_id' => self::$app_id,
|
||||
'member_id' => $member_id,
|
||||
'settle_account_id' => $settle_account_id
|
||||
];
|
||||
$endpoint = '/v1/settle_accounts/'.$settle_account_id;
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//删除结算账户对象
|
||||
public function deleteSettleAccount($member_id, $settle_account_id){
|
||||
$params = [
|
||||
'app_id' => self::$app_id,
|
||||
'member_id' => $member_id,
|
||||
'settle_account_id' => $settle_account_id
|
||||
];
|
||||
$endpoint = '/v1/settle_accounts/delete';
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//创建支付确认对象
|
||||
public function createPaymentConfirm($params){
|
||||
$endpoint = '/v1/payments/confirm';
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询支付确认对象
|
||||
public function queryPaymentConfirm($payment_confirm_id){
|
||||
$params = [
|
||||
'payment_confirm_id' => $payment_confirm_id
|
||||
];
|
||||
$endpoint = '/v1/payments/confirm/'.$payment_confirm_id;
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//创建支付撤销对象
|
||||
public function createPaymentReverse($params){
|
||||
$endpoint = '/v1/payments/reverse';
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询支付确认对象
|
||||
public function queryPaymentReverse($reverse_id){
|
||||
$params = [
|
||||
'reverse_id' => $reverse_id
|
||||
];
|
||||
$endpoint = '/v1/payments/reverse/'.$reverse_id;
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//创建取现对象
|
||||
public function createDrawCash($params){
|
||||
$endpoint = '/v1/cashs';
|
||||
$public_params = [
|
||||
'app_id' => self::$app_id,
|
||||
];
|
||||
$params = array_merge($params, $public_params);
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询取现对象
|
||||
public function queryDrawCash($order_no){
|
||||
$endpoint = '/v1/cashs/stat';
|
||||
$params = [
|
||||
'order_no' => $order_no,
|
||||
];
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//查询账户余额
|
||||
public function queryBalance($member_id, $settle_account_id = null){
|
||||
$endpoint = '/v1/settle_accounts/balance';
|
||||
$params = [
|
||||
'app_id' => self::$app_id,
|
||||
'member_id' => $member_id
|
||||
];
|
||||
if($settle_account_id){
|
||||
$params['settle_account_id'] = $settle_account_id;
|
||||
}
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//钱包登录
|
||||
public function walletLogin($member_id, $ip){
|
||||
$endpoint = '/v1/walletLogin';
|
||||
$params = [
|
||||
'app_id' => self::$app_id,
|
||||
'member_id' => $member_id,
|
||||
'ip' => $ip
|
||||
];
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
//账户转账
|
||||
public function createTransfer($params){
|
||||
$endpoint = '/v1/settle_accounts/transfer';
|
||||
$public_params = [
|
||||
'app_id' => self::$app_id,
|
||||
];
|
||||
$params = array_merge($params, $public_params);
|
||||
return $this->request('POST', $endpoint, $params);
|
||||
}
|
||||
|
||||
//账户转账查询
|
||||
public function queryTransfer($params){
|
||||
$endpoint = '/v1/settle_accounts/transfer/list';
|
||||
$public_params = [
|
||||
'app_id' => self::$app_id,
|
||||
];
|
||||
$params = array_merge($public_params, $params);
|
||||
return $this->request('GET', $endpoint, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param null $post
|
||||
* @param null $cookie
|
||||
* @return bool|string
|
||||
*/
|
||||
protected function curl($url, $post = null, array $headers = [])
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
if (!is_null($post)) {
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
if (is_array($post)) {
|
||||
$postData = json_encode($post);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
||||
} else {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
|
||||
}
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||
$ret = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
return [
|
||||
'app_id' => trim($channel['appid']),
|
||||
'api_key_live' => trim($channel['appkey']),
|
||||
'rsa_private_key' => trim($channel['appsecret']),
|
||||
];
|
||||
?>
|
||||
Reference in New Issue
Block a user