1 line
20 KiB
PHP
Executable File
1 line
20 KiB
PHP
Executable File
<?php
|
||
namespace hema\alipay;
|
||
|
||
use hema\alipay\engine\AopClient;
|
||
use app\common\model\Setting;
|
||
use think\facade\Cache;
|
||
|
||
class Driver
|
||
{
|
||
private $config;
|
||
private $applet_id;
|
||
private $error;
|
||
|
||
/**
|
||
* 构造函数
|
||
*/
|
||
public function __construct($applet_id='')
|
||
{
|
||
$this->applet_id = $applet_id;
|
||
if(empty($applet_id)){
|
||
$this->config = null;
|
||
}else{
|
||
$this->config = Setting::getItem('alipay',$applet_id);
|
||
}
|
||
$this->aop = new AopClient($this->config);
|
||
}
|
||
|
||
/**
|
||
* 换取授权访问令牌
|
||
* $code 授权码
|
||
*/
|
||
public function systemOauthToken($code = '')
|
||
{
|
||
$queryarr = [
|
||
'grant_type' => 'authorization_code',
|
||
'code' => $code
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.system.oauth.token'));
|
||
}
|
||
|
||
//######################## 小程序 - 开始 ##############################
|
||
|
||
//******** 小程序码 ************************************************
|
||
/**
|
||
* 小程序生成推广二维码接口
|
||
* $query_param 二维码参数
|
||
* $file_path 生成二维码图片存放位置
|
||
*/
|
||
public function openAppQrcodeCreate($query_param,$file_path,$url_param = 'pages/index/index',$describe='推广小程序码')
|
||
{
|
||
$queryarr = [
|
||
'url_param' => $url_param,//访问到的页面路径
|
||
'query_param' => $query_param,//启动参数
|
||
'describe' => $describe,//二维码描述
|
||
//'color' => '0x00BFFF',//圆形二维码颜色
|
||
//'size' => 's',//图片的大小 s -- 8cm, m -- 12cm, l -- 30cm
|
||
];
|
||
$result = $this->result($this->aop->execute($queryarr,'alipay.open.app.qrcode.create'));
|
||
if($result){
|
||
$temp_file = file_get_contents($result['qr_code_url_circle_blue']); //获取网络图片
|
||
file_put_contents(public_path().$file_path,$temp_file); //存放临时图片
|
||
return $result;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//******** 基础信息 ************************************************
|
||
|
||
/**
|
||
* 查询小程序基础信息
|
||
*/
|
||
public function openMiniBaseinfoQuery()
|
||
{
|
||
$queryarr = [];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.baseinfo.query'));
|
||
}
|
||
|
||
/**
|
||
* 小程序修改基础信息
|
||
*/
|
||
public function openMiniBaseinfoModify($queryarr = [])
|
||
{
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.baseinfo.modify'));
|
||
}
|
||
|
||
/**
|
||
* 小程序设置客服方式
|
||
*/
|
||
public function openMiniMiniappServiceconfigModify($home_open = true)
|
||
{
|
||
$queryarr = [
|
||
'service_config' => 'ANTCLOUD',//客服方式,目前支持ANTCLOUD-云客服
|
||
'home_open' => $home_open,//云客服是否在小程序首页透出
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.miniapp.serviceconfig.modify'));
|
||
}
|
||
|
||
//******** 成员管理 ************************************************
|
||
|
||
/**
|
||
* 应用查询成员列表
|
||
*/
|
||
public function openAppMembersQuery($role='DEVELOPER')
|
||
{
|
||
$queryarr = [
|
||
'role' => $role,//角色类型 DEVELOPER-开发者 EXPERIENCER-体验者
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.app.members.query'));
|
||
}
|
||
|
||
/**
|
||
* 应用添加成员
|
||
*/
|
||
public function openAppMembersCreate($logon_id,$role)
|
||
{
|
||
$queryarr = [
|
||
'logon_id' => $logon_id,//支付宝登录账号
|
||
'role' => $role,//角色类型 DEVELOPER-开发者 EXPERIENCER-体验者
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.app.members.create'));
|
||
}
|
||
|
||
/**
|
||
* 应用删除成员
|
||
*/
|
||
public function openAppMembersDelete($user_id,$role)
|
||
{
|
||
$queryarr = [
|
||
'user_id' => $user_id,//被删除成员的支付宝账户唯一标识
|
||
'role' => $role,//角色类型DEVELOPER-开发者 EXPERIENCER-体验者
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.app.members.delete'));
|
||
}
|
||
|
||
//******** 类目管理 ************************************************
|
||
|
||
/**
|
||
* 小程序类目树查询
|
||
*/
|
||
public function openMiniCategoryQuery($is_filter=true)
|
||
{
|
||
$queryarr = [
|
||
'is_filter' => $is_filter,//是否过滤小程序不可用类目
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.category.query'));
|
||
}
|
||
|
||
/**
|
||
* 查询类目所需资质信息
|
||
* $category_code_list 类目列表,不超过五个。为数组,如:[ "a","b"]
|
||
*/
|
||
public function openMiniCategoryRequireQuery($category_code_list)
|
||
{
|
||
$queryarr = [
|
||
'category_code_list' => $category_code_list,//类目列表,不超过五个
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.category.require.query'));
|
||
}
|
||
|
||
//******** 版本管理 ************************************************
|
||
|
||
/**
|
||
* 小程序退回开发
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionAuditedCancel($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version,//版本号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.audited.cancel'));
|
||
}
|
||
|
||
/**
|
||
* 小程序灰度上架
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionGrayOnline($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version,//版本号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.gray.online'));
|
||
}
|
||
|
||
/**
|
||
* 小程序结束灰度
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionGrayCancel($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version,//版本号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.gray.cancel'));
|
||
}
|
||
|
||
/**
|
||
* 小程序下架
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionOffline($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version,//版本号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.offline'));
|
||
}
|
||
|
||
/**
|
||
* 小程序上架
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionOnline($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version,//版本号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.online'));
|
||
}
|
||
|
||
/**
|
||
* 小程序回滚
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionRollback($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.rollback'));
|
||
}
|
||
|
||
/**
|
||
* 小程序删除版本
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionDelete($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.delete'));
|
||
}
|
||
|
||
/**
|
||
* 小程序取消体验版
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniExperienceCancel($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.experience.cancel'));
|
||
}
|
||
|
||
/**
|
||
* 小程序提交审核
|
||
* $data 模板参数
|
||
*/
|
||
public function openMiniVersionAuditApply($data,$auto_online='true',$speed_up='false')
|
||
{
|
||
$queryarr = [
|
||
'version_desc' => $data['user_desc'],//版本描述
|
||
'app_version' => $data['user_version'],//版本号
|
||
'auto_online' => $auto_online,//审核通过后是否自动上架 默认false
|
||
'speed_up' => $speed_up,//是否加急 默认true
|
||
'region_type' => 'CHINA',//服务区域类型,GLOBAL-全球,CHINA=中国,LOCATION-指定区域
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.audit.apply'));
|
||
}
|
||
|
||
/**
|
||
* 小程序撤销审核
|
||
* $app_version = 小程序版本号,为空时撤消正在审核中的版本
|
||
*/
|
||
public function openMiniVersionAuditCancel($app_version='')
|
||
{
|
||
$queryarr = [];
|
||
!empty($app_version) && $queryarr['app_version'] = $app_version;
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.audit.cancel'));
|
||
}
|
||
|
||
/**
|
||
* 小程序基于模板上传版本
|
||
* $data 模板参数
|
||
*/
|
||
public function openMiniVersionUpload($data)
|
||
{
|
||
$isv = Setting::getItem('alipayopen');
|
||
|
||
$ext = [
|
||
'extEnable' => true,
|
||
'ext' => [
|
||
'applet_id' => $this->applet_id,
|
||
'api_url' => 'https://' . $isv['domain']
|
||
],
|
||
];
|
||
$queryarr = [
|
||
'template_id' => $data['id'],
|
||
'template_version' => $data['user_version'],
|
||
'app_version' => $data['user_version'],
|
||
'ext' => json_encode($ext,JSON_UNESCAPED_UNICODE),
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.upload'));
|
||
}
|
||
|
||
/**
|
||
* 查询使用模板的小程序列表
|
||
* $template_id=模板APPID
|
||
*/
|
||
public function openMiniTemplateUsageQuery($template_id)
|
||
{
|
||
$queryarr = [
|
||
'template_id' => $template_id
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.template.usage.query'));
|
||
}
|
||
|
||
/**
|
||
* 小程序查询版本构建状态
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionBuildQuery($app_version='1.0.0')
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.build.query'));
|
||
}
|
||
|
||
/**
|
||
* 小程序版本详情查询
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniVersionDetailQuery($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.detail.query'));
|
||
}
|
||
|
||
/**
|
||
* 小程序版本列表查询
|
||
*/
|
||
public function openMiniVersionListQuery()
|
||
{
|
||
$queryarr = [
|
||
'version_status' => 'INIT,AUDITING,AUDIT_REJECT,WAIT_RELEASE,BASE_AUDIT_PASS,GRAY,RELEASE,OFFLINE,AUDIT_OFFLINE'
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.version.list.query'));
|
||
}
|
||
|
||
/**
|
||
* 小程序体验版状态查询接口
|
||
* $app_version = 小程序版本号
|
||
*/
|
||
public function openMiniExperienceQuery($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.experience.query'));
|
||
}
|
||
|
||
/**
|
||
* 小程序生成体验版
|
||
* $app_version 版本号
|
||
*/
|
||
public function openMiniExperienceCreate($app_version)
|
||
{
|
||
$queryarr = [
|
||
'app_version' => $app_version
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.mini.experience.create'));
|
||
}
|
||
|
||
//######################## 小程序 - 结束 ##############################
|
||
|
||
|
||
|
||
//######################## 第三方应用 - 开始 ##############################
|
||
|
||
//******** 第三方应用授权 ************************************************
|
||
/**
|
||
* 查询某个应用授权AppAuthToken的授权信息
|
||
* $app_auth_code 授权码
|
||
*/
|
||
public function openAuthTokenAppQuery($app_auth_code)
|
||
{
|
||
$queryarr = [
|
||
'app_auth_token' => $app_auth_code
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.auth.token.app.query'));
|
||
}
|
||
/**
|
||
* 换取应用授权令牌
|
||
* $app_auth_code 授权码
|
||
*/
|
||
public function openAuthTokenApp($app_auth_code)
|
||
{
|
||
$queryarr = [
|
||
'grant_type' => 'authorization_code',
|
||
'code' => $app_auth_code
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.auth.token.app'));
|
||
}
|
||
//******** AES密钥管理 ************************************************
|
||
/**
|
||
* 授权应用aes密钥查询
|
||
* $merchant_app_id 已授权小程序APP_ID
|
||
*/
|
||
public function openAuthAppAesGet($merchant_app_id)
|
||
{
|
||
$queryarr = [
|
||
'merchant_app_id' => $merchant_app_id
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.auth.app.aes.get',true));
|
||
}
|
||
|
||
/**
|
||
* 授权应用aes密钥设置
|
||
* $merchant_app_id 已授权小程序APP_ID
|
||
*/
|
||
public function openAuthAppAesSet($merchant_app_id)
|
||
{
|
||
$queryarr = [
|
||
'merchant_app_id' => $merchant_app_id
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.open.auth.app.aes.set',true));
|
||
}
|
||
//######################## 第三方应用 - 结束 ##############################
|
||
|
||
//######################## 小程序支付 - 开始 ##############################
|
||
|
||
|
||
/**
|
||
* 统一收单交易创建接口
|
||
* $notify_ur=回调地址
|
||
*/
|
||
public function tradeCreate($out_trade_no,$total_amount,$buyer_id,$notify_url='',$subject='订单支付')
|
||
{
|
||
if(!empty($notify_url)){
|
||
$this->aop->setNotifyUrl(base_url() . $notify_url);
|
||
}
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
'total_amount' => $total_amount,//订单总金额。单位为元,精确到小数点后两位
|
||
'subject' => $subject,//订单标题
|
||
'buyer_id' => $buyer_id,//买家支付宝用户ID
|
||
'product_code' => 'FACE_TO_FACE_PAYMENT',//产品码-当面付
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.create'));
|
||
}
|
||
|
||
/**
|
||
* 统一收单交易查询
|
||
*/
|
||
public function tradeQuery($out_trade_no)
|
||
{
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.query'));
|
||
}
|
||
|
||
/**
|
||
* 统一收单交易退款接口
|
||
* $refund_reason=退款原因
|
||
*/
|
||
public function tradeRefund($out_trade_no,$refund_amount,$refund_reason='')
|
||
{
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
'refund_amount' => $refund_amount,//退款金额
|
||
];
|
||
if(!empty($refund_reason)){
|
||
$queryarr['refund_reason'] = $refund_reason;
|
||
}
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.refund'));
|
||
}
|
||
|
||
/**
|
||
* 统一收单交易撤销接口
|
||
*/
|
||
public function tradeCancel($out_trade_no)
|
||
{
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
];
|
||
if(!empty($refund_reason)){
|
||
$queryarr['refund_reason'] = $refund_reason;
|
||
}
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.cancel'));
|
||
}
|
||
|
||
/**
|
||
* 统一收单交易关闭接口
|
||
* $notify_ur=回调地址
|
||
*/
|
||
public function tradeClose($out_trade_no,$notify_url='')
|
||
{
|
||
if(!empty($notify_url)){
|
||
$this->aop->setNotifyUrl(base_url() . $notify_url);
|
||
}
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.close'));
|
||
}
|
||
|
||
/**
|
||
* 统一收单交易退款查询
|
||
*/
|
||
public function tradeFastpayRefundQuery($out_trade_no)
|
||
{
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
'out_request_no' => $out_trade_no,//退款请求号
|
||
];
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.fastpay.refund.query'));
|
||
}
|
||
|
||
//######################## 小程序支付 - 结束 ##############################
|
||
|
||
|
||
//######################## 手机网站支付 - 开始 ##############################
|
||
|
||
/**
|
||
* 手机网站支付接口2.0
|
||
* $notify_ur=回调地址
|
||
*/
|
||
public function tradeWapPay($out_trade_no,$total_amount,$notify_url='',$return_url='',$subject='订单支付')
|
||
{
|
||
$queryarr = [
|
||
'out_trade_no' => $out_trade_no,//商户订单号
|
||
'total_amount' => $total_amount,//订单总金额。单位为元,精确到小数点后两位
|
||
'subject' => $subject,//订单标题
|
||
'product_code' => 'QUICK_WAP_WAY',//产品码-手机网站支付
|
||
];
|
||
if(!empty($notify_url)){
|
||
$this->aop->setNotifyUrl(base_url() . $notify_url);
|
||
}
|
||
if(!empty($return_url)){
|
||
$queryarr['quit_url'] = base_url() . $return_url;
|
||
$this->aop->setReturnUrl($queryarr['quit_url']);
|
||
}
|
||
return $this->result($this->aop->execute($queryarr,'alipay.trade.wap.pay'));
|
||
}
|
||
|
||
//######################## 手机网站支付 - 结束 ##############################
|
||
|
||
|
||
//######################## 支付回调 - 开始 ##############################
|
||
|
||
/**
|
||
* 支付成功异步通知
|
||
*/
|
||
public function notify($Model,$method='edit')
|
||
{
|
||
//接收支付宝服务器回调的数据流
|
||
if (!$result = file_get_contents('php://input')) {
|
||
write_log('支付成功异步通知 - 通知为空',__DIR__);
|
||
return false;
|
||
}
|
||
$str = urldecode($result);
|
||
parse_str($str,$data);
|
||
// 订单信息
|
||
if(!$order = $Model->payDetail($data['out_trade_no'])){
|
||
write_log('支付成功异步通知 - 订单不存在',__DIR__);
|
||
return false;
|
||
}
|
||
//支付成功
|
||
if($data['trade_status'] == 'TRADE_SUCCESS') {
|
||
if($method == 'add'){
|
||
$Model->updatePayStatus($data['trade_no'],$order);
|
||
Cache::delete($data['out_trade_no']);
|
||
}else{
|
||
// 更新订单状态
|
||
$order->updatePayStatus($data['trade_no']);
|
||
}
|
||
}
|
||
//交易创建
|
||
if($data['trade_status'] == 'WAIT_BUYER_PAY') {
|
||
write_log('支付成功异步通知 - 交易创建',__DIR__);
|
||
}
|
||
//交易完成
|
||
if($data['trade_status'] == 'TRADE_FINISHED') {
|
||
write_log('支付成功异步通知 - 交易完成',__DIR__);
|
||
}
|
||
//交易关闭
|
||
if($data['trade_status'] == 'TRADE_CLOSED') {
|
||
write_log('支付成功异步通知 - 交易关闭',__DIR__);
|
||
}
|
||
echo "success";
|
||
}
|
||
|
||
|
||
|
||
//######################## 支付回调 - 结束 ##############################
|
||
/**
|
||
* 请求数据验证
|
||
**/
|
||
private function result($result)
|
||
{
|
||
//write_log($result,__DIR__);
|
||
if(!$result){
|
||
$this->error = $this->aop->getError();
|
||
return false;
|
||
}
|
||
if(isset($result['code']) and $result['code'] != '10000'){
|
||
$this->error = '错误代码:' . $result['sub_code'] . ',错误信息:' . $result['sub_msg'];
|
||
return false;
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
public function getError()
|
||
{
|
||
return $this->error;
|
||
}
|
||
|
||
} |