chore(src): 所有代码上传

This commit is contained in:
2025-12-02 15:36:42 +08:00
parent ce8e59902c
commit eb79ad260c
669 changed files with 86838 additions and 87639 deletions

View File

@@ -1,38 +1,30 @@
<?php
/**
*/
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
<?php
return [
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据json格式' ]
'template' => [],
// 后台自定义组件——装修
'util' => [],
// 自定义页面路径
'link' => [],
// 自定义图标库
'icon_library' => [],
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ]多个逗号隔开自定义组件名称前缀必须是diy-,也可以引用第三方组件
'component' => [],
// uni-app 页面,多个逗号隔开
'pages' => [],
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
'info' => [],
// 主题风格配色格式可以自由定义扩展【在uni-app中通过this.themeStyle... 获取定义的颜色字段例如this.themeStyle.main_color】
'theme' => [],
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据json格式] ]
'data' => []
];

View File

@@ -1,50 +1,42 @@
<?php
/**
*/
return [
'bind' => [
],
'listen' => [
//支付异步回调
'PayNotify' => [
'addon\alipay\event\PayNotify'
],
//支付方式,后台查询
'PayType' => [
'addon\alipay\event\PayType'
],
//支付,前台应用
'Pay' => [
'addon\alipay\event\Pay'
],
'PayClose' => [
'addon\alipay\event\PayClose'
],
'PayRefund' => [
'addon\alipay\event\PayRefund'
],
'PayTransfer' => [
'addon\alipay\event\PayTransfer'
],
'TransferType' => [
'addon\alipay\event\TransferType'
],
'AuthcodePay' => [
'addon\alipay\event\AuthcodePay'
],
'PayOrderQuery' => [
'addon\alipay\event\PayOrderQuery'
],
],
'subscribe' => [
],
];
<?php
return [
'bind' => [
],
'listen' => [
//支付异步回调
'PayNotify' => [
'addon\alipay\event\PayNotify'
],
//支付方式,后台查询
'PayType' => [
'addon\alipay\event\PayType'
],
//支付,前台应用
'Pay' => [
'addon\alipay\event\Pay'
],
'PayClose' => [
'addon\alipay\event\PayClose'
],
'PayRefund' => [
'addon\alipay\event\PayRefund'
],
'PayTransfer' => [
'addon\alipay\event\PayTransfer'
],
'TransferType' => [
'addon\alipay\event\TransferType'
],
'AuthcodePay' => [
'addon\alipay\event\AuthcodePay'
],
'PayOrderQuery' => [
'addon\alipay\event\PayOrderQuery'
],
],
'subscribe' => [
],
];

View File

@@ -1,21 +1,12 @@
<?php
/**
*/
return [
'name' => 'alipay',
'title' => '支付宝支付',
'description' => '支付宝支付功能',
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
<?php
return [
'name' => 'alipay',
'title' => '支付宝支付',
'description' => '支付宝支付功能',
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:营销插件 tool:工具插件
'status' => 1,
'author' => '',
'version' => '5.3.1',
'version_no' => '525231212001',
'content' => '',
];

View File

@@ -1,23 +1,15 @@
<?php
/**
*/
return [
[
'name' => 'ALI_PAY_CONFIG',
'title' => '支付宝支付编辑',
'url' => 'alipay://shop/pay/config',
'parent' => 'CONFIG_PAY',
'is_show' => 0,
'is_control' => 1,
'is_icon' => 0,
'picture' => '',
'picture_select' => '',
'sort' => 1,
],
];
<?php
return [
[
'name' => 'ALI_PAY_CONFIG',
'title' => '支付宝支付编辑',
'url' => 'alipay://shop/pay/config',
'parent' => 'CONFIG_PAY',
'is_show' => 0,
'is_control' => 1,
'is_icon' => 0,
'picture' => '',
'picture_select' => '',
'sort' => 1,
],
];

View File

@@ -1,27 +1,18 @@
<?php
/**
*/
namespace addon\alipay\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
<?php
namespace addon\alipay\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
return success();
}
}

View File

@@ -1,33 +1,25 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 生成支付
*/
class Pay
{
/**
* 支付方式及配置
*/
public function handle($param)
{
if ($param[ "pay_type" ] == "alipay") {
if (in_array($param[ "app_type" ], [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
$pay_model = new PayModel($param[ 'site_id' ], $param[ "app_type" ] == 'aliapp');
$res = $pay_model->pay($param);
return $res;
}
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 生成支付
*/
class Pay
{
/**
* 支付方式及配置
*/
public function handle($param)
{
if ($param[ "pay_type" ] == "alipay") {
if (in_array($param[ "app_type" ], [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
$pay_model = new PayModel($param[ 'site_id' ], $param[ "app_type" ] == 'aliapp');
$res = $pay_model->pay($param);
return $res;
}
}
}
}

View File

@@ -1,39 +1,31 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 关闭支付
*/
class PayClose
{
/**
* 关闭支付
* @param $params
* @return \addon\alipay\model\multitype|array
*/
public function handle($params)
{
// if ($params["pay_type"] == "alipay") {
try {
$pay_model = new PayModel($params[ 'site_id' ]);
$result = $pay_model->close($params);
return $result;
} catch (\Exception $e) {
return error(-1, $e->getMessage());
} catch (\Throwable $e) {
return error(-1, $e->getMessage());
}
// }
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 关闭支付
*/
class PayClose
{
/**
* 关闭支付
* @param $params
* @return \addon\alipay\model\multitype|array
*/
public function handle($params)
{
// if ($params["pay_type"] == "alipay") {
try {
$pay_model = new PayModel($params[ 'site_id' ]);
$result = $pay_model->close($params);
return $result;
} catch (\Exception $e) {
return error(-1, $e->getMessage());
} catch (\Throwable $e) {
return error(-1, $e->getMessage());
}
// }
}
}

View File

@@ -1,41 +1,33 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay as PayCommon;
/**
* 支付回调
*/
class PayNotify
{
/**
* 支付方式及配置
*/
public function handle()
{
if (isset($_POST[ 'out_trade_no' ])) {
$out_trade_no = $_POST[ 'out_trade_no' ];
$pay = new PayCommon();
$pay_info = $pay->getPayInfo($out_trade_no)[ 'data' ];
if (empty($pay_info)) return false;
if ($_POST[ 'total_amount' ] != $pay_info[ 'pay_money' ]) {
return false;
}
$mch_info = empty($pay_info[ 'mch_info' ]) ? [] : json_decode($pay_info[ 'mch_info' ], true);
$pay_model = new PayModel($pay_info[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$pay_model->payNotify();
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay as PayCommon;
/**
* 支付回调
*/
class PayNotify
{
/**
* 支付方式及配置
*/
public function handle()
{
if (isset($_POST[ 'out_trade_no' ])) {
$out_trade_no = $_POST[ 'out_trade_no' ];
$pay = new PayCommon();
$pay_info = $pay->getPayInfo($out_trade_no)[ 'data' ];
if (empty($pay_info)) return false;
if ($_POST[ 'total_amount' ] != $pay_info[ 'pay_money' ]) {
return false;
}
$mch_info = empty($pay_info[ 'mch_info' ]) ? [] : json_decode($pay_info[ 'mch_info' ], true);
$pay_model = new PayModel($pay_info[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$pay_model->payNotify();
}
}
}

View File

@@ -1,34 +1,26 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Config as ConfigModel;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay;
/**
* 查询支付结果
*/
class PayOrderQuery
{
public function handle(array $params)
{
$pay_info = ( new Pay() )->getInfo([ [ 'id', '=', $params[ 'relate_id' ] ] ])[ 'data' ];
if (!empty($pay_info)) {
$config_model = new ConfigModel();
$pay_config = $config_model->getPayConfig($pay_info[ 'site_id' ])[ 'data' ][ 'value' ];
if (!empty($pay_config) && $pay_config[ 'pay_status' ] != 2) {
$pay_common = new PayModel($pay_info[ 'site_id' ]);
$pay_common->orderQuery($pay_info);
}
}
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Config as ConfigModel;
use addon\alipay\model\Pay as PayModel;
use app\model\system\Pay;
/**
* 查询支付结果
*/
class PayOrderQuery
{
public function handle(array $params)
{
$pay_info = ( new Pay() )->getInfo([ [ 'id', '=', $params[ 'relate_id' ] ] ])[ 'data' ];
if (!empty($pay_info)) {
$config_model = new ConfigModel();
$pay_config = $config_model->getPayConfig($pay_info[ 'site_id' ])[ 'data' ][ 'value' ];
if (!empty($pay_config) && $pay_config[ 'pay_status' ] != 2) {
$pay_common = new PayModel($pay_info[ 'site_id' ]);
$pay_common->orderQuery($pay_info);
}
}
}
}

View File

@@ -1,33 +1,25 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 原路退款
*/
class PayRefund
{
/**
* 关闭支付
*/
public function handle($params)
{
if ($params[ "pay_info" ][ "pay_type" ] == "alipay") {
$mch_info = empty($params[ 'pay_info' ][ 'mch_info' ]) ? [] : json_decode($params[ 'pay_info' ][ 'mch_info' ], true);
$pay_model = new PayModel($params[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$result = $pay_model->refund($params);
return $result;
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay as PayModel;
/**
* 原路退款
*/
class PayRefund
{
/**
* 关闭支付
*/
public function handle($params)
{
if ($params[ "pay_info" ][ "pay_type" ] == "alipay") {
$mch_info = empty($params[ 'pay_info' ][ 'mch_info' ]) ? [] : json_decode($params[ 'pay_info' ][ 'mch_info' ], true);
$pay_model = new PayModel($params[ 'site_id' ], $mch_info[ 'is_aliapp' ] ?? 0);
$result = $pay_model->refund($params);
return $result;
}
}
}

View File

@@ -1,42 +1,34 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Pay;
use addon\alipay\model\Config;
class PayTransfer
{
public function handle(array $params)
{
if ($params[ 'transfer_type' ] == 'alipay') {
$pay = new Pay($params[ 'site_id' ]);
$config_model = new Config();
$config_result = $config_model->getPayConfig($params[ 'site_id' ]);
$config = $config_result[ "data" ];
if (!empty($config[ 'value' ])) {
$config_info = $config[ "value" ];
$countersign_type = $config_info['countersign_type'] ?? 0;
if ($countersign_type == 0) {
$res = $pay->payTransfer($params);
return $res;
} else {
$res = $pay->payNewTransfer($params);
return $res;
}
} else {
$res = $pay->payTransfer($params);
return $res;
}
}
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Pay;
use addon\alipay\model\Config;
class PayTransfer
{
public function handle(array $params)
{
if ($params[ 'transfer_type' ] == 'alipay') {
$pay = new Pay($params[ 'site_id' ]);
$config_model = new Config();
$config_result = $config_model->getPayConfig($params[ 'site_id' ]);
$config = $config_result[ "data" ];
if (!empty($config[ 'value' ])) {
$config_info = $config[ "value" ];
$countersign_type = $config_info['countersign_type'] ?? 0;
if ($countersign_type == 0) {
$res = $pay->payTransfer($params);
return $res;
} else {
$res = $pay->payNewTransfer($params);
return $res;
}
} else {
$res = $pay->payTransfer($params);
return $res;
}
}
}
}

View File

@@ -1,51 +1,43 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Config;
/**
* 支付方式 (后台调用)
*/
class PayType
{
/**
* 支付方式及配置
*/
public function handle($param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
return '';
}
if ($app_type != 'aliapp') {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$pay_status = $config[ "pay_status" ] ?? 0;
if ($pay_status == 0) {
return '';
}
}
}
$info = array (
"pay_type" => "alipay",
"pay_type_name" => "支付宝支付",
"edit_url" => "alipay://shop/pay/config",
"shop_url" => "alipay://shop/pay/config",
"logo" => "addon/alipay/icon.png",
"desc" => "支付宝网站(www.alipay.com) 是国内先进的网上支付平台。"
);
return $info;
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Config;
/**
* 支付方式 (后台调用)
*/
class PayType
{
/**
* 支付方式及配置
*/
public function handle($param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp", 'wechat' ])) {
return '';
}
if ($app_type != 'aliapp') {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$pay_status = $config[ "pay_status" ] ?? 0;
if ($pay_status == 0) {
return '';
}
}
}
$info = array (
"pay_type" => "alipay",
"pay_type_name" => "支付宝支付",
"edit_url" => "alipay://shop/pay/config",
"shop_url" => "alipay://shop/pay/config",
"logo" => "addon/alipay/icon.png",
"desc" => "支付宝网站(www.alipay.com) 是国内先进的网上支付平台。"
);
return $info;
}
}

View File

@@ -1,39 +1,31 @@
<?php
/**
*/
namespace addon\alipay\event;
use addon\alipay\model\Config;
class TransferType
{
public function handle(array $param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp" ])) {
return '';
}
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$transfer_status = $config[ "transfer_status" ] ?? 0;
if ($transfer_status == 0) {
return '';
}
}
$info = array (
"type" => "alipay",
"type_name" => "支付宝",
);
return $info;
}
<?php
namespace addon\alipay\event;
use addon\alipay\model\Config;
class TransferType
{
public function handle(array $param)
{
$app_type = $param['app_type'] ?? '';
if (!empty($app_type)) {
if (!in_array($app_type, [ "h5", "app", "pc", "aliapp" ])) {
return '';
}
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
$config = $config_result[ "data" ][ "value" ] ?? [];
$transfer_status = $config[ "transfer_status" ] ?? 0;
if ($transfer_status == 0) {
return '';
}
}
$info = array (
"type" => "alipay",
"type_name" => "支付宝",
);
return $info;
}
}

View File

@@ -1,26 +1,17 @@
<?php
/**
*/
namespace addon\alipay\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
return error(-1, "系统插件不得删除");
}
<?php
namespace addon\alipay\event;
/**
* 应用卸载
*/
class UnInstall
{
/**
* 执行卸载
*/
public function handle()
{
return error(-1, "系统插件不得删除");
}
}

View File

@@ -1,96 +1,88 @@
<?php
/**
*/
namespace addon\alipay\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 支付宝支付配置
*/
class Config extends BaseModel
{
private $encrypt = '******';
/**
* 设置支付配置
* @param $data
* @param int $site_id
* @param string $app_module
* @return array
*/
public function setPayConfig($data, $site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
// 未加密前的数据
$original_config = $this->getPayConfig($site_id)[ 'data' ][ 'value' ];
// 检测数据是否发生变化,如果没有变化,则保持未加密前的数据
if (!empty($data[ 'private_key' ]) && $data[ 'private_key' ] == $this->encrypt) {
$data[ 'private_key' ] = $original_config[ 'private_key' ]; // 应用私钥
}
if (!empty($data[ 'public_key' ]) && $data[ 'public_key' ] == $this->encrypt) {
$data[ 'public_key' ] = $original_config[ 'public_key' ]; // 应用公钥
}
if (!empty($data[ 'alipay_public_key' ]) && $data[ 'alipay_public_key' ] == $this->encrypt) {
$data[ 'alipay_public_key' ] = $original_config[ 'alipay_public_key' ]; // 支付宝公钥
}
if (!empty($data[ 'public_key_crt' ]) && $data[ 'public_key_crt' ] == $this->encrypt) {
$data[ 'public_key_crt' ] = $original_config[ 'public_key_crt' ]; // 应用公钥证书
}
if (!empty($data[ 'alipay_public_key_crt' ]) && $data[ 'alipay_public_key_crt' ] == $this->encrypt) {
$data[ 'alipay_public_key_crt' ] = $original_config[ 'alipay_public_key_crt' ]; // 支付宝公钥证书
}
if (!empty($data[ 'alipay_with_crt' ]) && $data[ 'alipay_with_crt' ] == $this->encrypt) {
$data[ 'alipay_with_crt' ] = $original_config[ 'alipay_with_crt' ]; // 支付宝根证书
}
$res = $config->setConfig($data, '支付宝支付配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
return $res;
}
/**
* 获取支付配置
* @param int $site_id
* @param string $app_module
* @param bool $need_encrypt 是否需要加密数据true加密、false不加密
* @return array
*/
public function getPayConfig($site_id = 0, $app_module = 'shop', $need_encrypt = false)
{
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
if ($need_encrypt) {
// 加密敏感信息
if (!empty($res[ 'data' ][ 'value' ][ 'private_key' ])) {
$res[ 'data' ][ 'value' ][ 'private_key' ] = $this->encrypt; // 应用私钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key' ])) {
$res[ 'data' ][ 'value' ][ 'public_key' ] = $this->encrypt; // 应用公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key' ] = $this->encrypt; // 支付宝公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'public_key_crt' ] = $this->encrypt; // 应用公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ] = $this->encrypt; // 支付宝公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_with_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_with_crt' ] = $this->encrypt; // 支付宝根证书
}
}
return $res;
}
<?php
namespace addon\alipay\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 支付宝支付配置
*/
class Config extends BaseModel
{
private $encrypt = '******';
/**
* 设置支付配置
* @param $data
* @param int $site_id
* @param string $app_module
* @return array
*/
public function setPayConfig($data, $site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
// 未加密前的数据
$original_config = $this->getPayConfig($site_id)[ 'data' ][ 'value' ];
// 检测数据是否发生变化,如果没有变化,则保持未加密前的数据
if (!empty($data[ 'private_key' ]) && $data[ 'private_key' ] == $this->encrypt) {
$data[ 'private_key' ] = $original_config[ 'private_key' ]; // 应用私钥
}
if (!empty($data[ 'public_key' ]) && $data[ 'public_key' ] == $this->encrypt) {
$data[ 'public_key' ] = $original_config[ 'public_key' ]; // 应用公钥
}
if (!empty($data[ 'alipay_public_key' ]) && $data[ 'alipay_public_key' ] == $this->encrypt) {
$data[ 'alipay_public_key' ] = $original_config[ 'alipay_public_key' ]; // 支付宝公钥
}
if (!empty($data[ 'public_key_crt' ]) && $data[ 'public_key_crt' ] == $this->encrypt) {
$data[ 'public_key_crt' ] = $original_config[ 'public_key_crt' ]; // 应用公钥证书
}
if (!empty($data[ 'alipay_public_key_crt' ]) && $data[ 'alipay_public_key_crt' ] == $this->encrypt) {
$data[ 'alipay_public_key_crt' ] = $original_config[ 'alipay_public_key_crt' ]; // 支付宝公钥证书
}
if (!empty($data[ 'alipay_with_crt' ]) && $data[ 'alipay_with_crt' ] == $this->encrypt) {
$data[ 'alipay_with_crt' ] = $original_config[ 'alipay_with_crt' ]; // 支付宝根证书
}
$res = $config->setConfig($data, '支付宝支付配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
return $res;
}
/**
* 获取支付配置
* @param int $site_id
* @param string $app_module
* @param bool $need_encrypt 是否需要加密数据true加密、false不加密
* @return array
*/
public function getPayConfig($site_id = 0, $app_module = 'shop', $need_encrypt = false)
{
$config = new ConfigModel();
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'ALI_PAY_CONFIG' ] ]);
if ($need_encrypt) {
// 加密敏感信息
if (!empty($res[ 'data' ][ 'value' ][ 'private_key' ])) {
$res[ 'data' ][ 'value' ][ 'private_key' ] = $this->encrypt; // 应用私钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key' ])) {
$res[ 'data' ][ 'value' ][ 'public_key' ] = $this->encrypt; // 应用公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key' ] = $this->encrypt; // 支付宝公钥
}
if (!empty($res[ 'data' ][ 'value' ][ 'public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'public_key_crt' ] = $this->encrypt; // 应用公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_public_key_crt' ] = $this->encrypt; // 支付宝公钥证书
}
if (!empty($res[ 'data' ][ 'value' ][ 'alipay_with_crt' ])) {
$res[ 'data' ][ 'value' ][ 'alipay_with_crt' ] = $this->encrypt; // 支付宝根证书
}
}
return $res;
}
}

View File

@@ -1,486 +1,478 @@
<?php
/**
*/
namespace addon\alipay\model;
use addon\alipay\data\sdk\AopClient;
use addon\alipay\data\sdk\request\AlipayFundTransToaccountTransferRequest;
use addon\alipay\data\sdk\request\AlipayTradeAppPayRequest;
use addon\alipay\data\sdk\request\AlipayTradeCloseRequest;
use addon\alipay\data\sdk\request\AlipayTradeCreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePagePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeRefundRequest;
use addon\alipay\data\sdk\request\AlipayTradeWapPayRequest;
use addon\alipay\data\sdk\request\AlipayTradePrecreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeQueryRequest;
use app\model\BaseModel;
use app\model\system\Cron;
use app\model\system\Pay as PayCommon;
use addon\alipay\data\sdk\request\AlipayFundTransUniTransferRequest;
use addon\alipay\data\sdk\AopCertClient;
use app\model\system\Pay as PayModel;
use addon\aliapp\model\Config as AliappConfig;
use think\facade\Log;
/**
* 支付宝支付配置
*/
class Pay extends BaseModel
{
public $aop;
private $is_aliapp = 0;
/**
*
* @param $site_id
* @param int $is_aliapp 是否是小程序
*/
function __construct($site_id, $is_aliapp = 0)
{
$this->is_aliapp = $is_aliapp;
try {
// 获取支付宝支付参数(统一支付到平台账户)
if ($is_aliapp) {
$config_info = ( new AliappConfig() )->getAliappConfig($site_id)[ 'data' ][ 'value' ];
} else {
$config_info = ( new Config() )->getPayConfig($site_id)[ 'data' ][ 'value' ];
}
if (!empty($config_info)) {
$countersign_type = $config_info[ 'countersign_type' ] ?? 0;
if ($countersign_type == 1) {
$appCertPath = $config_info[ "public_key_crt" ] ?? "";
$alipayCertPath = $config_info[ "alipay_public_key_crt" ] ?? "";
$rootCertPath = $config_info[ "alipay_with_crt" ] ?? "";
$this->aop = new AopCertClient();
//调用getPublicKey从支付宝公钥证书中提取公钥
$this->aop->alipayrsaPublicKey = $this->aop->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$this->aop->isCheckAlipayPublicCert = false;
//调用getCertSN获取证书序列号
$this->aop->appCertSN = $this->aop->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$this->aop->alipayRootCertSN = $this->aop->getRootCertSN($rootCertPath);
} else {
// 获取支付宝支付参数(统一支付到平台账户)
$this->aop = new AopClient();
$this->aop->alipayrsaPublicKey = $config_info[ 'public_key' ] ?? "";
$this->aop->alipayPublicKey = $config_info[ 'alipay_public_key' ] ?? "";
}
$this->aop->appId = $config_info[ "app_id" ] ?? "";
$this->aop->rsaPrivateKey = $config_info[ 'private_key' ] ?? "";
$this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$this->aop->apiVersion = '1.0';
$this->aop->signType = 'RSA2';
$this->aop->postCharset = 'UTF-8';
$this->aop->format = 'json';
}
// else{
// return $this->error('', '支付宝支付未配置');
// }
} catch (\Exception $e) {
return $this->error('', '支付宝配置错误');
}
}
/**
* 生成支付
* @param $param
* @return array
*/
public function pay($param)
{
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"body" => str_sub($param[ "pay_body" ], 60),
"product_code" => 'FAST_INSTANT_TRADE_PAY',
);
switch ( $param[ "app_type" ] ) {
case "h5":
$request = new AlipayTradeWapPayRequest();
break;
case "pc":
$request = new AlipayTradePagePayRequest();
break;
case "app":
$request = new AlipayTradeAppPayRequest();
break;
case 'wechat':
$request = new AlipayTradeWapPayRequest();
break;
case 'cashier':
$request = new AlipayTradePrecreateRequest();
break;
case 'aliapp':
$parameter[ 'product_code' ] = 'FACE_TO_FACE_PAYMENT';
$member_info = model('member')->getInfo([ [ "member_id", "=", $param[ "member_id" ] ] ], 'ali_openid');
if (empty($member_info)) return $this->error(-1, '未获取到会员信息');
$parameter[ 'buyer_id' ] = $member_info[ 'ali_openid' ];
$request = new AlipayTradeCreateRequest();
break;
}
$parameter = json_encode($parameter);
$request->setBizContent($parameter);
$request->SetReturnUrl($param[ "return_url" ]);
$request->SetNotifyUrl($param[ "notify_url" ]);
///绑定商户数据
$pay_model = new PayModel();
$pay_model->bindMchPay($param[ "out_trade_no" ], [ "is_aliapp" => $this->is_aliapp ]);
try {
if ($param[ "app_type" ] == 'h5' || $param[ "app_type" ] == 'wechat' || $param[ "app_type" ] == 'pc') {
$result = $this->aop->pageExecute($request, 'get');
return $this->success([
'type' => 'url',
'data' => $result
]);
} elseif ($param[ "app_type" ] == 'app') {
$result = $this->aop->sdkExecute($request);
if (strpos(get_class($this->aop), 'AopClient') !== false) {
return $this->success([
'type' => 'url',
'data' => $result
]);
}
} else {
$result = $this->aop->execute($request);
}
if ($result === false) return $this->error('', '支付宝发起支付失败');
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
switch ( $param[ "app_type" ] ) {
case 'cashier':
return $this->success([
'type' => 'qrcode',
'data' => [
'qrcode' => $result->$responseNode->qr_code
]
]);
break;
case 'aliapp':
return $this->success([
'type' => 'data',
'data' => [
'orderInfo' => $result->$responseNode->trade_no
]
]);
break;
default:
return $this->success();
}
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付关闭
* @param $param
* @return array
* @throws \think\Exception
*/
public function close($param)
{
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ]
);
// 建立请求
$request = new AlipayTradeCloseRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
$sub_code = $result->$responseNode->sub_code;
$data = [];
if(in_array($sub_code, ['ACQ.TRADE_STATUS_ERROR', 'ACQ.REASON_TRADE_STATUS_INVALID', 'ACQ.REASON_ILLEGAL_STATUS'])){
$pay_order_result = $this->get($param[ "out_trade_no" ]);
if(!empty($pay_order_result) && $pay_order_result['code'] >= 0){
if($pay_order_result['data']['trade_status'] == 'TRADE_SUCCESS' || $pay_order_result['data']['trade_status'] == 'TRADE_FINISHED'){
$data['is_paid'] = 1;
}
}
}
return $this->error($data, $result->$responseNode->sub_msg);
}
}
/**
* 支付宝支付原路返回
* @param array $param 支付参数
* @return array
* @throws \think\Exception
*/
public function refund($param)
{
$pay_info = $param[ "pay_info" ];
$refund_no = $param[ "refund_no" ];
$out_trade_no = $pay_info[ "trade_no" ] ?? '';
$refund_fee = $param[ "refund_fee" ];
$parameter = array (
'trade_no' => $out_trade_no,
'refund_amount' => sprintf("%.2f", $refund_fee),
'out_request_no' => $refund_no
);
// 建立请求
$request = new AlipayTradeRefundRequest ();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付宝转账
* @param $param
* @return array
*/
public function payTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'payee_type' => 'ALIPAY_LOGONID',
'payee_account' => $param[ "account_number" ],
'amount' => sprintf("%.2f", $param[ 'amount' ]),
'payee_real_name' => $param[ "real_name" ],
'remark' => $param[ "desc" ]
];
// 建立请求
$request = new AlipayFundTransToaccountTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->pay_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 异步完成支付
* @param $param
*/
public function payNotify()
{
// Log::write('pay_notifiy_log:alipay:'.json_encode(input()), 'notice');
try {
$res = $this->aop->rsaCheckV1($_POST, $this->aop->alipayrsaPublicKey, $this->aop->signType);
if ($res) { // 验证成功
$out_trade_no = $_POST[ 'out_trade_no' ];
// 支付宝交易号
$trade_no = $_POST[ 'trade_no' ];
// 交易状态
$trade_status = $_POST[ 'trade_status' ];
$pay_common = new PayCommon();
if ($trade_status == "TRADE_SUCCESS") {
$retval = $pay_common->onlinePay($out_trade_no, "alipay", $trade_no, "alipay");
}
echo "success";
} else {
// 验证失败
echo "fail";
}
} catch (\Exception $e) {
echo "fail";
}
}
public function payNewTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'trans_amount' => sprintf("%.2f", $param[ 'amount' ]),
'product_code' => 'TRANS_ACCOUNT_NO_PWD',
'biz_scene' => 'DIRECT_TRANSFER',
'order_title' => '支付宝转账',
'remark' => $param[ "desc" ],
'payee_info' => [
'identity' => $param[ "account_number" ],
'identity_type' => "ALIPAY_LOGON_ID",
'name' => $param[ "real_name" ]
]
];
// 建立请求
$request = new AlipayFundTransUniTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->trans_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 付款码支付
* @param $param
* @return array|mixed|void
*/
public function micropay($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"scene" => "bar_code",
"auth_code" => $param[ 'auth_code' ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradePayRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('支付宝—付款码支付result' . json_encode($result));
Log::write('支付宝—付款码支付resultCode' . json_encode($resultCode));
if (!empty($resultCode)) {
if ($resultCode == 10000) {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else if ($resultCode == 10003) {
// 等待用户付款
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
// todo 查询交易信息【AlipayTradeQueryRequest】 https://opendocs.alipay.com/open/194/106039?pathHash=5b8cf9e6
public function orderQuery($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradeQueryRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('alipay_orderQuery' . json_encode($result));
Log::write('alipay_orderQuery_$resultCode' . json_encode($resultCode));
if (!empty($resultCode) && $resultCode == 10000) {
if ($result->$responseNode->trade_status == 'TRADE_SUCCESS') {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else {
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 查询订单信息
* @param $out_trade_no
* @return array
* @throws \think\Exception
*/
public function get($out_trade_no)
{
$parameter = array (
"out_trade_no" => $out_trade_no
);
// 建立请求
$request = new AlipayTradeQueryRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success(json_decode(json_encode($result->$responseNode), true));
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
}
<?php
namespace addon\alipay\model;
use addon\alipay\data\sdk\AopClient;
use addon\alipay\data\sdk\request\AlipayFundTransToaccountTransferRequest;
use addon\alipay\data\sdk\request\AlipayTradeAppPayRequest;
use addon\alipay\data\sdk\request\AlipayTradeCloseRequest;
use addon\alipay\data\sdk\request\AlipayTradeCreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePagePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeRefundRequest;
use addon\alipay\data\sdk\request\AlipayTradeWapPayRequest;
use addon\alipay\data\sdk\request\AlipayTradePrecreateRequest;
use addon\alipay\data\sdk\request\AlipayTradePayRequest;
use addon\alipay\data\sdk\request\AlipayTradeQueryRequest;
use app\model\BaseModel;
use app\model\system\Cron;
use app\model\system\Pay as PayCommon;
use addon\alipay\data\sdk\request\AlipayFundTransUniTransferRequest;
use addon\alipay\data\sdk\AopCertClient;
use app\model\system\Pay as PayModel;
use addon\aliapp\model\Config as AliappConfig;
use think\facade\Log;
/**
* 支付宝支付配置
*/
class Pay extends BaseModel
{
public $aop;
private $is_aliapp = 0;
/**
*
* @param $site_id
* @param int $is_aliapp 是否是小程序
*/
function __construct($site_id, $is_aliapp = 0)
{
$this->is_aliapp = $is_aliapp;
try {
// 获取支付宝支付参数(统一支付到平台账户)
if ($is_aliapp) {
$config_info = ( new AliappConfig() )->getAliappConfig($site_id)[ 'data' ][ 'value' ];
} else {
$config_info = ( new Config() )->getPayConfig($site_id)[ 'data' ][ 'value' ];
}
if (!empty($config_info)) {
$countersign_type = $config_info[ 'countersign_type' ] ?? 0;
if ($countersign_type == 1) {
$appCertPath = $config_info[ "public_key_crt" ] ?? "";
$alipayCertPath = $config_info[ "alipay_public_key_crt" ] ?? "";
$rootCertPath = $config_info[ "alipay_with_crt" ] ?? "";
$this->aop = new AopCertClient();
//调用getPublicKey从支付宝公钥证书中提取公钥
$this->aop->alipayrsaPublicKey = $this->aop->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$this->aop->isCheckAlipayPublicCert = false;
//调用getCertSN获取证书序列号
$this->aop->appCertSN = $this->aop->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$this->aop->alipayRootCertSN = $this->aop->getRootCertSN($rootCertPath);
} else {
// 获取支付宝支付参数(统一支付到平台账户)
$this->aop = new AopClient();
$this->aop->alipayrsaPublicKey = $config_info[ 'public_key' ] ?? "";
$this->aop->alipayPublicKey = $config_info[ 'alipay_public_key' ] ?? "";
}
$this->aop->appId = $config_info[ "app_id" ] ?? "";
$this->aop->rsaPrivateKey = $config_info[ 'private_key' ] ?? "";
$this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$this->aop->apiVersion = '1.0';
$this->aop->signType = 'RSA2';
$this->aop->postCharset = 'UTF-8';
$this->aop->format = 'json';
}
// else{
// return $this->error('', '支付宝支付未配置');
// }
} catch (\Exception $e) {
return $this->error('', '支付宝配置错误');
}
}
/**
* 生成支付
* @param $param
* @return array
*/
public function pay($param)
{
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"body" => str_sub($param[ "pay_body" ], 60),
"product_code" => 'FAST_INSTANT_TRADE_PAY',
);
switch ( $param[ "app_type" ] ) {
case "h5":
$request = new AlipayTradeWapPayRequest();
break;
case "pc":
$request = new AlipayTradePagePayRequest();
break;
case "app":
$request = new AlipayTradeAppPayRequest();
break;
case 'wechat':
$request = new AlipayTradeWapPayRequest();
break;
case 'cashier':
$request = new AlipayTradePrecreateRequest();
break;
case 'aliapp':
$parameter[ 'product_code' ] = 'FACE_TO_FACE_PAYMENT';
$member_info = model('member')->getInfo([ [ "member_id", "=", $param[ "member_id" ] ] ], 'ali_openid');
if (empty($member_info)) return $this->error(-1, '未获取到会员信息');
$parameter[ 'buyer_id' ] = $member_info[ 'ali_openid' ];
$request = new AlipayTradeCreateRequest();
break;
}
$parameter = json_encode($parameter);
$request->setBizContent($parameter);
$request->SetReturnUrl($param[ "return_url" ]);
$request->SetNotifyUrl($param[ "notify_url" ]);
///绑定商户数据
$pay_model = new PayModel();
$pay_model->bindMchPay($param[ "out_trade_no" ], [ "is_aliapp" => $this->is_aliapp ]);
try {
if ($param[ "app_type" ] == 'h5' || $param[ "app_type" ] == 'wechat' || $param[ "app_type" ] == 'pc') {
$result = $this->aop->pageExecute($request, 'get');
return $this->success([
'type' => 'url',
'data' => $result
]);
} elseif ($param[ "app_type" ] == 'app') {
$result = $this->aop->sdkExecute($request);
if (strpos(get_class($this->aop), 'AopClient') !== false) {
return $this->success([
'type' => 'url',
'data' => $result
]);
}
} else {
$result = $this->aop->execute($request);
}
if ($result === false) return $this->error('', '支付宝发起支付失败');
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
switch ( $param[ "app_type" ] ) {
case 'cashier':
return $this->success([
'type' => 'qrcode',
'data' => [
'qrcode' => $result->$responseNode->qr_code
]
]);
break;
case 'aliapp':
return $this->success([
'type' => 'data',
'data' => [
'orderInfo' => $result->$responseNode->trade_no
]
]);
break;
default:
return $this->success();
}
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付关闭
* @param $param
* @return array
* @throws \think\Exception
*/
public function close($param)
{
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ]
);
// 建立请求
$request = new AlipayTradeCloseRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
$sub_code = $result->$responseNode->sub_code;
$data = [];
if(in_array($sub_code, ['ACQ.TRADE_STATUS_ERROR', 'ACQ.REASON_TRADE_STATUS_INVALID', 'ACQ.REASON_ILLEGAL_STATUS'])){
$pay_order_result = $this->get($param[ "out_trade_no" ]);
if(!empty($pay_order_result) && $pay_order_result['code'] >= 0){
if($pay_order_result['data']['trade_status'] == 'TRADE_SUCCESS' || $pay_order_result['data']['trade_status'] == 'TRADE_FINISHED'){
$data['is_paid'] = 1;
}
}
}
return $this->error($data, $result->$responseNode->sub_msg);
}
}
/**
* 支付宝支付原路返回
* @param array $param 支付参数
* @return array
* @throws \think\Exception
*/
public function refund($param)
{
$pay_info = $param[ "pay_info" ];
$refund_no = $param[ "refund_no" ];
$out_trade_no = $pay_info[ "trade_no" ] ?? '';
$refund_fee = $param[ "refund_fee" ];
$parameter = array (
'trade_no' => $out_trade_no,
'refund_amount' => sprintf("%.2f", $refund_fee),
'out_request_no' => $refund_no
);
// 建立请求
$request = new AlipayTradeRefundRequest ();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success();
} else {
return $this->error("", $result->$responseNode->sub_msg);
}
}
/**
* 支付宝转账
* @param $param
* @return array
*/
public function payTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'payee_type' => 'ALIPAY_LOGONID',
'payee_account' => $param[ "account_number" ],
'amount' => sprintf("%.2f", $param[ 'amount' ]),
'payee_real_name' => $param[ "real_name" ],
'remark' => $param[ "desc" ]
];
// 建立请求
$request = new AlipayFundTransToaccountTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->pay_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 异步完成支付
* @param $param
*/
public function payNotify()
{
// Log::write('pay_notifiy_log:alipay:'.json_encode(input()), 'notice');
try {
$res = $this->aop->rsaCheckV1($_POST, $this->aop->alipayrsaPublicKey, $this->aop->signType);
if ($res) { // 验证成功
$out_trade_no = $_POST[ 'out_trade_no' ];
// 支付宝交易号
$trade_no = $_POST[ 'trade_no' ];
// 交易状态
$trade_status = $_POST[ 'trade_status' ];
$pay_common = new PayCommon();
if ($trade_status == "TRADE_SUCCESS") {
$retval = $pay_common->onlinePay($out_trade_no, "alipay", $trade_no, "alipay");
}
echo "success";
} else {
// 验证失败
echo "fail";
}
} catch (\Exception $e) {
echo "fail";
}
}
public function payNewTransfer($param)
{
try {
$config_model = new Config();
$config_result = $config_model->getPayConfig($param[ 'site_id' ]);
if ($config_result[ 'code' ] < 0) return $config_result;
$config = $config_result[ 'data' ][ 'value' ];
if (empty($config)) return $this->error([], '未配置支付宝支付');
if (!$config[ 'transfer_status' ]) return $this->error([], '未启用支付宝转账');
$parameter = [
'out_biz_no' => $param[ 'out_trade_no' ],
'trans_amount' => sprintf("%.2f", $param[ 'amount' ]),
'product_code' => 'TRANS_ACCOUNT_NO_PWD',
'biz_scene' => 'DIRECT_TRANSFER',
'order_title' => '支付宝转账',
'remark' => $param[ "desc" ],
'payee_info' => [
'identity' => $param[ "account_number" ],
'identity_type' => "ALIPAY_LOGON_ID",
'name' => $param[ "real_name" ]
]
];
// 建立请求
$request = new AlipayFundTransUniTransferRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success([
'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
'payment_no' => $result->$responseNode->order_id, // 微信付款单号
'payment_time' => date_to_time($result->$responseNode->trans_date) // 付款成功时间
]);
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 付款码支付
* @param $param
* @return array|mixed|void
*/
public function micropay($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
"subject" => str_sub($param[ "pay_body" ], 15),
"total_amount" => (float) $param[ "pay_money" ],
"scene" => "bar_code",
"auth_code" => $param[ 'auth_code' ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradePayRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('支付宝—付款码支付result' . json_encode($result));
Log::write('支付宝—付款码支付resultCode' . json_encode($resultCode));
if (!empty($resultCode)) {
if ($resultCode == 10000) {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else if ($resultCode == 10003) {
// 等待用户付款
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
// todo 查询交易信息【AlipayTradeQueryRequest】 https://opendocs.alipay.com/open/194/106039?pathHash=5b8cf9e6
public function orderQuery($param)
{
try {
//构造要请求的参数数组,无需改动
$parameter = array (
"out_trade_no" => $param[ "out_trade_no" ],
);
$parameter = json_encode($parameter);
$request = new AlipayTradeQueryRequest();
$request->setBizContent($parameter);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
Log::write('alipay_orderQuery' . json_encode($result));
Log::write('alipay_orderQuery_$resultCode' . json_encode($resultCode));
if (!empty($resultCode) && $resultCode == 10000) {
if ($result->$responseNode->trade_status == 'TRADE_SUCCESS') {
$pay_common = new PayModel();
return $res = $pay_common->onlinePay($param[ 'out_trade_no' ], 'alipay', $result->$responseNode->trade_no, 'alipay');
} else {
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
} catch (\Exception $e) {
return $this->error([], $e->getMessage());
}
}
/**
* 查询订单信息
* @param $out_trade_no
* @return array
* @throws \think\Exception
*/
public function get($out_trade_no)
{
$parameter = array (
"out_trade_no" => $out_trade_no
);
// 建立请求
$request = new AlipayTradeQueryRequest();
$request->setBizContent(json_encode($parameter));
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $this->success(json_decode(json_encode($result->$responseNode), true));
} else {
return $this->error([], $result->$responseNode->sub_msg);
}
}
}

View File

@@ -1,94 +1,86 @@
<?php
/**
*/
namespace addon\alipay\shop\controller;
use addon\alipay\model\Config as ConfigModel;
use app\shop\controller\BaseShop;
use think\facade\Config;
use app\model\upload\Upload;
/**
* 支付宝 控制器
*/
class Pay extends BaseShop
{
public function config()
{
$config_model = new ConfigModel();
if (request()->isJson()) {
$app_id = input("app_id", "");//支付宝应用ID (支付宝分配给开发者的应用ID)
$private_key = input("private_key", "");//应用私钥
$public_key = input("public_key", "");//应用公钥
$alipay_public_key = input("alipay_public_key", "");//支付宝公钥
$app_type = input("app_type", "");//支持端口 如web app
$pay_status = input("pay_status", 0);//支付启用状态
$refund_status = input("refund_status", 0);//退款启用状态
$transfer_status = input("transfer_status", 0);//转账启用状态
$public_key_crt = input("public_key_crt", "");
$alipay_public_key_crt = input("alipay_public_key_crt", "");
$alipay_with_crt = input("alipay_with_crt", "");
$countersign_type = input("countersign_type", 0);//加签模式
$data = array (
"app_id" => $app_id,
"private_key" => $private_key,
"public_key" => $public_key,
"alipay_public_key" => $alipay_public_key,
"refund_status" => $refund_status,
"pay_status" => $pay_status,
"transfer_status" => $transfer_status,
"app_type" => $app_type,
"public_key_crt" => $public_key_crt,
"alipay_public_key_crt" => $alipay_public_key_crt,
"alipay_with_crt" => $alipay_with_crt,
"countersign_type" => $countersign_type
);
$result = $config_model->setPayConfig($data, $this->site_id, $this->app_module);
return $result;
} else {
$info = $config_model->getPayConfig($this->site_id, $this->app_module, true)[ 'data' ][ 'value' ];
if (!empty($info)) {
$app_type_arr = [];
if (!empty($info[ 'app_type' ])) {
$app_type_arr = explode(',', $info[ 'app_type' ]);
}
$info[ 'app_type_arr' ] = $app_type_arr;
if (empty($info[ 'countersign_type' ])) {
$info[ 'countersign_type' ] = 0;
}
}
$this->assign("info", $info);
$this->assign("app_type", Config::get("app_type"));
return $this->fetch("pay/config");
}
}
/**
* 上传微信支付证书
*/
public function uploadAlipayCrt()
{
$upload_model = new Upload();
$site_id = request()->siteid();
$name = input("name", "");
$extend_type = [ 'crt' ];
$param = array (
"name" => "file",
"extend_type" => $extend_type
);
$site_id = max($site_id, 0);
$result = $upload_model->setPath("common/alipay/crt/" . $site_id . "/")->file($param);
return $result;
}
<?php
namespace addon\alipay\shop\controller;
use addon\alipay\model\Config as ConfigModel;
use app\shop\controller\BaseShop;
use think\facade\Config;
use app\model\upload\Upload;
/**
* 支付宝 控制器
*/
class Pay extends BaseShop
{
public function config()
{
$config_model = new ConfigModel();
if (request()->isJson()) {
$app_id = input("app_id", "");//支付宝应用ID (支付宝分配给开发者的应用ID)
$private_key = input("private_key", "");//应用私钥
$public_key = input("public_key", "");//应用公钥
$alipay_public_key = input("alipay_public_key", "");//支付宝公钥
$app_type = input("app_type", "");//支持端口 如web app
$pay_status = input("pay_status", 0);//支付启用状态
$refund_status = input("refund_status", 0);//退款启用状态
$transfer_status = input("transfer_status", 0);//转账启用状态
$public_key_crt = input("public_key_crt", "");
$alipay_public_key_crt = input("alipay_public_key_crt", "");
$alipay_with_crt = input("alipay_with_crt", "");
$countersign_type = input("countersign_type", 0);//加签模式
$data = array (
"app_id" => $app_id,
"private_key" => $private_key,
"public_key" => $public_key,
"alipay_public_key" => $alipay_public_key,
"refund_status" => $refund_status,
"pay_status" => $pay_status,
"transfer_status" => $transfer_status,
"app_type" => $app_type,
"public_key_crt" => $public_key_crt,
"alipay_public_key_crt" => $alipay_public_key_crt,
"alipay_with_crt" => $alipay_with_crt,
"countersign_type" => $countersign_type
);
$result = $config_model->setPayConfig($data, $this->site_id, $this->app_module);
return $result;
} else {
$info = $config_model->getPayConfig($this->site_id, $this->app_module, true)[ 'data' ][ 'value' ];
if (!empty($info)) {
$app_type_arr = [];
if (!empty($info[ 'app_type' ])) {
$app_type_arr = explode(',', $info[ 'app_type' ]);
}
$info[ 'app_type_arr' ] = $app_type_arr;
if (empty($info[ 'countersign_type' ])) {
$info[ 'countersign_type' ] = 0;
}
}
$this->assign("info", $info);
$this->assign("app_type", Config::get("app_type"));
return $this->fetch("pay/config");
}
}
/**
* 上传微信支付证书
*/
public function uploadAlipayCrt()
{
$upload_model = new Upload();
$site_id = request()->siteid();
$name = input("name", "");
$extend_type = [ 'crt' ];
$param = array (
"name" => "file",
"extend_type" => $extend_type
);
$site_id = max($site_id, 0);
$result = $upload_model->setPath("common/alipay/crt/" . $site_id . "/")->file($param);
return $result;
}
}