初始化仓库
This commit is contained in:
76
app/common/logic/AccountLogLogic.php
Normal file
76
app/common/logic/AccountLogLogic.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\enum\user\AccountLogEnum;
|
||||
use app\common\model\user\UserAccountLog;
|
||||
use app\common\model\user\User;
|
||||
|
||||
/**
|
||||
* 账户流水记录逻辑层
|
||||
* Class AccountLogLogic
|
||||
* @package app\common\logic
|
||||
*/
|
||||
class AccountLogLogic extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 账户流水记录
|
||||
* @param $userId
|
||||
* @param $changeType
|
||||
* @param $action
|
||||
* @param $changeAmount
|
||||
* @param string $sourceSn
|
||||
* @param string $remark
|
||||
* @param array $extra
|
||||
* @return UserAccountLog|false|\think\Model
|
||||
* @author 段誉
|
||||
* @date 2023/2/23 12:03
|
||||
*/
|
||||
public static function add($userId, $changeType, $action, $changeAmount, string $sourceSn = '', string $remark = '', array $extra = [])
|
||||
{
|
||||
$user = User::findOrEmpty($userId);
|
||||
if($user->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$changeObject = AccountLogEnum::getChangeObject($changeType);
|
||||
if(!$changeObject) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($changeObject) {
|
||||
// 用户余额
|
||||
case AccountLogEnum::UM:
|
||||
$left_amount = $user->user_money;
|
||||
break;
|
||||
// 其他
|
||||
}
|
||||
|
||||
$data = [
|
||||
'sn' => generate_sn(UserAccountLog::class, 'sn', 20),
|
||||
'user_id' => $userId,
|
||||
'change_object' => $changeObject,
|
||||
'change_type' => $changeType,
|
||||
'action' => $action,
|
||||
'left_amount' => $left_amount,
|
||||
'change_amount' => $changeAmount,
|
||||
'source_sn' => $sourceSn,
|
||||
'remark' => $remark,
|
||||
'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
|
||||
];
|
||||
return UserAccountLog::create($data);
|
||||
}
|
||||
}
|
||||
114
app/common/logic/BaseLogic.php
Normal file
114
app/common/logic/BaseLogic.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
|
||||
/**
|
||||
* 逻辑基类
|
||||
* Class BaseLogic
|
||||
* @package app\common\logic
|
||||
*/
|
||||
class BaseLogic
|
||||
{
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected static $error;
|
||||
|
||||
/**
|
||||
* 返回状态码
|
||||
* @var int
|
||||
*/
|
||||
protected static $returnCode = 0;
|
||||
|
||||
|
||||
protected static $returnData;
|
||||
|
||||
/**
|
||||
* @notes 获取错误信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:23
|
||||
*/
|
||||
public static function getError() : string
|
||||
{
|
||||
if (false === self::hasError()) {
|
||||
return '系统错误';
|
||||
}
|
||||
return self::$error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置错误信息
|
||||
* @param $error
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:20
|
||||
*/
|
||||
public static function setError($error) : void
|
||||
{
|
||||
!empty($error) && self::$error = $error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 是否存在错误
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:32
|
||||
*/
|
||||
public static function hasError() : bool
|
||||
{
|
||||
return !empty(self::$error);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置状态码
|
||||
* @param $code
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:05
|
||||
*/
|
||||
public static function setReturnCode($code) : void
|
||||
{
|
||||
self::$returnCode = $code;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 特殊场景返回指定状态码,默认为0
|
||||
* @return int
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 15:14
|
||||
*/
|
||||
public static function getReturnCode() : int
|
||||
{
|
||||
return self::$returnCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取内容
|
||||
* @return mixed
|
||||
* @author cjhao
|
||||
* @date 2021/9/11 17:29
|
||||
*/
|
||||
public static function getReturnData()
|
||||
{
|
||||
return self::$returnData;
|
||||
}
|
||||
|
||||
}
|
||||
184
app/common/logic/NoticeLogic.php
Normal file
184
app/common/logic/NoticeLogic.php
Normal file
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\enum\notice\NoticeEnum;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\model\notice\NoticeRecord;
|
||||
use app\common\model\notice\NoticeSetting;
|
||||
use app\common\model\user\User;
|
||||
use app\common\service\sms\SmsMessageService;
|
||||
|
||||
|
||||
/**
|
||||
* 通知逻辑层
|
||||
* Class NoticeLogic
|
||||
* @package app\common\logic
|
||||
*/
|
||||
class NoticeLogic extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 根据场景发送短信
|
||||
* @param $params
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 15:28
|
||||
*/
|
||||
public static function noticeByScene($params)
|
||||
{
|
||||
try {
|
||||
$noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
|
||||
if (empty($noticeSetting)) {
|
||||
throw new \Exception('找不到对应场景的配置');
|
||||
}
|
||||
// 合并额外参数
|
||||
$params = self::mergeParams($params);
|
||||
$res = false;
|
||||
self::setError('发送通知失败');
|
||||
|
||||
// 短信通知
|
||||
if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES) {
|
||||
$res = (new SmsMessageService())->send($params);
|
||||
}
|
||||
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 整理参数
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 15:28
|
||||
*/
|
||||
public static function mergeParams($params)
|
||||
{
|
||||
// 用户相关
|
||||
if (!empty($params['params']['user_id'])) {
|
||||
$user = User::findOrEmpty($params['params']['user_id'])->toArray();
|
||||
$params['params']['nickname'] = $user['nickname'];
|
||||
$params['params']['user_name'] = $user['nickname'];
|
||||
$params['params']['user_sn'] = $user['sn'];
|
||||
$params['params']['mobile'] = $params['params']['mobile'] ?? $user['mobile'];
|
||||
}
|
||||
|
||||
// 跳转路径
|
||||
$jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
|
||||
$params['url'] = $jumpPath['url'];
|
||||
$params['page'] = $jumpPath['page'];
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 根据场景获取跳转链接
|
||||
* @param $sceneId
|
||||
* @param $extraId
|
||||
* @return string[]
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 15:29
|
||||
*/
|
||||
public static function getPathByScene($sceneId, $extraId)
|
||||
{
|
||||
// 小程序主页路径
|
||||
$page = '/pages/index/index';
|
||||
// 公众号主页路径
|
||||
$url = '/mobile/pages/index/index';
|
||||
return [
|
||||
'url' => $url,
|
||||
'page' => $page,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 替换消息内容中的变量占位符
|
||||
* @param $content
|
||||
* @param $params
|
||||
* @return array|mixed|string|string[]
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 15:29
|
||||
*/
|
||||
public static function contentFormat($content, $params)
|
||||
{
|
||||
foreach ($params['params'] as $k => $v) {
|
||||
$search = '{' . $k . '}';
|
||||
$content = str_replace($search, $v, $content);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加通知记录
|
||||
* @param $params
|
||||
* @param $noticeSetting
|
||||
* @param $sendType
|
||||
* @param $content
|
||||
* @param string $extra
|
||||
* @return NoticeRecord|\think\Model
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 15:29
|
||||
*/
|
||||
public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
|
||||
{
|
||||
return NoticeRecord::create([
|
||||
'user_id' => $params['params']['user_id'] ?? 0,
|
||||
'title' => self::getTitleByScene($sendType, $noticeSetting),
|
||||
'content' => $content,
|
||||
'scene_id' => $noticeSetting['scene_id'],
|
||||
'read' => YesNoEnum::NO,
|
||||
'recipient' => $noticeSetting['recipient'],
|
||||
'send_type' => $sendType,
|
||||
'notice_type' => $noticeSetting['type'],
|
||||
'extra' => $extra,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 通知记录标题
|
||||
* @param $sendType
|
||||
* @param $noticeSetting
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 15:30
|
||||
*/
|
||||
public static function getTitleByScene($sendType, $noticeSetting)
|
||||
{
|
||||
switch ($sendType) {
|
||||
case NoticeEnum::SMS:
|
||||
$title = '';
|
||||
break;
|
||||
case NoticeEnum::OA:
|
||||
$title = $noticeSetting['oa_notice']['name'] ?? '';
|
||||
break;
|
||||
case NoticeEnum::MNP:
|
||||
$title = $noticeSetting['mnp_notice']['name'] ?? '';
|
||||
break;
|
||||
default:
|
||||
$title = '';
|
||||
}
|
||||
return $title;
|
||||
}
|
||||
|
||||
}
|
||||
88
app/common/logic/PayNotifyLogic.php
Normal file
88
app/common/logic/PayNotifyLogic.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\AccountLogEnum;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use app\common\model\user\User;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 支付成功后处理订单状态
|
||||
* Class PayNotifyLogic
|
||||
* @package app\api\logic
|
||||
*/
|
||||
class PayNotifyLogic extends BaseLogic
|
||||
{
|
||||
|
||||
public static function handle($action, $orderSn, $extra = [])
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
self::$action($orderSn, $extra);
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
Log::write(implode('-', [
|
||||
__CLASS__,
|
||||
__FUNCTION__,
|
||||
$e->getFile(),
|
||||
$e->getLine(),
|
||||
$e->getMessage()
|
||||
]));
|
||||
self::setError($e->getMessage());
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 充值回调
|
||||
* @param $orderSn
|
||||
* @param array $extra
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 15:28
|
||||
*/
|
||||
public static function recharge($orderSn, array $extra = [])
|
||||
{
|
||||
$order = RechargeOrder::where('sn', $orderSn)->findOrEmpty();
|
||||
// 增加用户累计充值金额及用户余额
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
$user->total_recharge_amount += $order->order_amount;
|
||||
$user->user_money += $order->order_amount;
|
||||
$user->save();
|
||||
|
||||
// 记录账户流水
|
||||
AccountLogLogic::add(
|
||||
$order->user_id,
|
||||
AccountLogEnum::UM_INC_RECHARGE,
|
||||
AccountLogEnum::INC,
|
||||
$order->order_amount,
|
||||
$order->sn,
|
||||
'用户充值'
|
||||
);
|
||||
|
||||
// 更新充值订单状态
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->pay_time = time();
|
||||
$order->save();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
238
app/common/logic/PaymentLogic.php
Normal file
238
app/common/logic/PaymentLogic.php
Normal file
@ -0,0 +1,238 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\model\pay\PayWay;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use app\common\model\user\User;
|
||||
use app\common\service\pay\AliPayService;
|
||||
use app\common\service\pay\WeChatPayService;
|
||||
|
||||
|
||||
/**
|
||||
* 支付逻辑
|
||||
* Class PaymentLogic
|
||||
* @package app\common\logic
|
||||
*/
|
||||
class PaymentLogic extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 支付方式
|
||||
* @param $userId
|
||||
* @param $terminal
|
||||
* @param $params
|
||||
* @return array|false
|
||||
* @author 段誉
|
||||
* @date 2023/2/24 17:53
|
||||
*/
|
||||
public static function getPayWay($userId, $terminal, $params)
|
||||
{
|
||||
try {
|
||||
if ($params['from'] == 'recharge') {
|
||||
// 充值
|
||||
$order = RechargeOrder::findOrEmpty($params['order_id'])->toArray();
|
||||
}
|
||||
|
||||
if (empty($order)) {
|
||||
throw new \Exception('待支付订单不存在');
|
||||
}
|
||||
|
||||
//获取支付场景
|
||||
$pay_way = PayWay::alias('pw')
|
||||
->join('dev_pay_config dp', 'pw.pay_config_id = dp.id')
|
||||
->where(['pw.scene' => $terminal, 'pw.status' => YesNoEnum::YES])
|
||||
->field('dp.id,dp.name,dp.pay_way,dp.icon,dp.sort,dp.remark,pw.is_default')
|
||||
->order('pw.is_default desc,dp.sort desc,id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
foreach ($pay_way as $k => &$item) {
|
||||
if ($item['pay_way'] == PayEnum::WECHAT_PAY) {
|
||||
$item['extra'] = '微信快捷支付';
|
||||
}
|
||||
if ($item['pay_way'] == PayEnum::ALI_PAY) {
|
||||
$item['extra'] = '支付宝快捷支付';
|
||||
}
|
||||
if ($item['pay_way'] == PayEnum::BALANCE_PAY) {
|
||||
$user_money = User::where(['id' => $userId])->value('user_money');
|
||||
$item['extra'] = '可用余额:' . $user_money;
|
||||
}
|
||||
// 充值时去除余额支付
|
||||
if ($params['from'] == 'recharge' && $item['pay_way'] == PayEnum::BALANCE_PAY) {
|
||||
unset($pay_way[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'lists' => array_values($pay_way),
|
||||
'order_amount' => $order['order_amount'],
|
||||
];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取支付状态
|
||||
* @param $params
|
||||
* @return array|false
|
||||
* @author 段誉
|
||||
* @date 2023/3/1 16:23
|
||||
*/
|
||||
public static function getPayStatus($params)
|
||||
{
|
||||
try {
|
||||
$order = [];
|
||||
$orderInfo = [];
|
||||
switch ($params['from']) {
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::where(['user_id' => $params['user_id'], 'id' => $params['order_id']])
|
||||
->findOrEmpty();
|
||||
$payTime = empty($order['pay_time']) ? '' : date('Y-m-d H:i:s', $order['pay_time']);
|
||||
$orderInfo = [
|
||||
'order_id' => $order['id'],
|
||||
'order_sn' => $order['sn'],
|
||||
'order_amount' => $order['order_amount'],
|
||||
'pay_way' => PayEnum::getPayDesc($order['pay_way']),
|
||||
'pay_status' => PayEnum::getPayStatusDesc($order['pay_status']),
|
||||
'pay_time' => $payTime,
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($order)) {
|
||||
throw new \Exception('订单不存在');
|
||||
}
|
||||
|
||||
return [
|
||||
'pay_status' => $order['pay_status'],
|
||||
'pay_way' => $order['pay_way'],
|
||||
'order' => $orderInfo
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取预支付订单信息
|
||||
* @param $params
|
||||
* @return RechargeOrder|array|false|\think\Model
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 15:19
|
||||
*/
|
||||
public static function getPayOrderInfo($params)
|
||||
{
|
||||
try {
|
||||
switch ($params['from']) {
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::findOrEmpty($params['order_id']);
|
||||
if ($order->isEmpty()) {
|
||||
throw new \Exception('充值订单不存在');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if ($order['pay_status'] == PayEnum::ISPAID) {
|
||||
throw new \Exception('订单已支付');
|
||||
}
|
||||
return $order;
|
||||
} catch (\Exception $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 支付
|
||||
* @param $payWay
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $terminal
|
||||
* @param $redirectUrl
|
||||
* @return array|false|mixed|string|string[]
|
||||
* @throws \Exception
|
||||
* @author mjf
|
||||
* @date 2024/3/18 16:49
|
||||
*/
|
||||
public static function pay($payWay, $from, $order, $terminal, $redirectUrl)
|
||||
{
|
||||
// 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
|
||||
$paySn = $order['sn'];
|
||||
if ($payWay == PayEnum::WECHAT_PAY) {
|
||||
$paySn = self::formatOrderSn($order['sn'], $terminal);
|
||||
}
|
||||
|
||||
//更新支付方式
|
||||
switch ($from) {
|
||||
case 'recharge':
|
||||
RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($order['order_amount'] == 0) {
|
||||
PayNotifyLogic::handle($from, $order['sn']);
|
||||
return ['pay_way' => PayEnum::BALANCE_PAY];
|
||||
}
|
||||
|
||||
$payService = null;
|
||||
switch ($payWay) {
|
||||
case PayEnum::WECHAT_PAY:
|
||||
$payService = (new WeChatPayService($terminal, $order['user_id'] ?? null));
|
||||
$order['pay_sn'] = $paySn;
|
||||
$order['redirect_url'] = $redirectUrl;
|
||||
$result = $payService->pay($from, $order);
|
||||
break;
|
||||
case PayEnum::ALI_PAY:
|
||||
$payService = (new AliPayService($terminal));
|
||||
$order['redirect_url'] = $redirectUrl;
|
||||
$result = $payService->pay($from, $order);
|
||||
break;
|
||||
default:
|
||||
self::$error = '订单异常';
|
||||
$result = false;
|
||||
}
|
||||
|
||||
if (false === $result && !self::hasError()) {
|
||||
self::setError($payService->getError());
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 设置订单号 支付回调时截取前面的单号 18个
|
||||
* @param $orderSn
|
||||
* @param $terminal
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2023/3/1 16:31
|
||||
* @remark 回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误
|
||||
*/
|
||||
public static function formatOrderSn($orderSn, $terminal)
|
||||
{
|
||||
$suffix = mb_substr(time(), -4);
|
||||
return $orderSn . $terminal . $suffix;
|
||||
}
|
||||
|
||||
}
|
||||
209
app/common/logic/RefundLogic.php
Normal file
209
app/common/logic/RefundLogic.php
Normal file
@ -0,0 +1,209 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\RefundEnum;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use app\common\model\refund\RefundLog;
|
||||
use app\common\model\refund\RefundRecord;
|
||||
use app\common\service\pay\AliPayService;
|
||||
use app\common\service\pay\WeChatPayService;
|
||||
|
||||
|
||||
/**
|
||||
* 订单退款逻辑
|
||||
* Class OrderRefundLogic
|
||||
* @package app\common\logic
|
||||
*/
|
||||
class RefundLogic extends BaseLogic
|
||||
{
|
||||
|
||||
protected static $refundLog;
|
||||
|
||||
|
||||
/**
|
||||
* @notes 发起退款
|
||||
* @param $order
|
||||
* @param $refundRecordId
|
||||
* @param $refundAmount
|
||||
* @param $handleId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 17:24
|
||||
*/
|
||||
public static function refund($order, $refundRecordId, $refundAmount, $handleId)
|
||||
{
|
||||
// 退款前校验
|
||||
self::refundBeforeCheck($refundAmount);
|
||||
|
||||
// 添加退款日志
|
||||
self::log($order, $refundRecordId, $refundAmount, $handleId);
|
||||
|
||||
// 根据不同支付方式退款
|
||||
try {
|
||||
switch ($order['pay_way']) {
|
||||
//微信退款
|
||||
case PayEnum::WECHAT_PAY:
|
||||
self::wechatPayRefund($order, $refundAmount);
|
||||
break;
|
||||
// 支付宝退款
|
||||
case PayEnum::ALI_PAY:
|
||||
self::aliPayRefund($refundRecordId, $refundAmount);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('支付方式异常');
|
||||
}
|
||||
|
||||
// 此处true并不表示退款成功,仅表示退款请求成功,具体成功与否由定时任务查询或通过退款回调得知
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
// 退款请求失败,标记退款记录及日志为失败.在退款记录处重新退款
|
||||
self::$error = $e->getMessage();
|
||||
self::refundFailHandle($refundRecordId, $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 退款前校验
|
||||
* @param $refundAmount
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 16:27
|
||||
*/
|
||||
public static function refundBeforeCheck($refundAmount)
|
||||
{
|
||||
if ($refundAmount <= 0) {
|
||||
throw new \Exception('订单金额异常');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 微信支付退款
|
||||
* @param $order
|
||||
* @param $refundAmount
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 17:19
|
||||
*/
|
||||
public static function wechatPayRefund($order, $refundAmount)
|
||||
{
|
||||
// 发起退款。 若发起退款请求返回明确错误,退款日志和记录标记状态为退款失败
|
||||
// 退款日志及记录的成功状态目前统一由定时任务查询退款结果为退款成功后才标记成功
|
||||
// 也可通过设置退款回调,在退款回调时处理退款记录状态为成功
|
||||
(new WeChatPayService($order['order_terminal']))->refund([
|
||||
'transaction_id' => $order['transaction_id'],
|
||||
'refund_sn' => self::$refundLog['sn'],
|
||||
'refund_amount' => $refundAmount,// 退款金额
|
||||
'total_amount' => $order['order_amount'],// 订单金额
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 支付宝退款
|
||||
* @param $refundRecordId
|
||||
* @param $refundAmount
|
||||
* @throws \Exception
|
||||
* @author mjf
|
||||
* @date 2024/3/18 18:54
|
||||
*/
|
||||
public static function aliPayRefund($refundRecordId, $refundAmount)
|
||||
{
|
||||
$refundRecord = RefundRecord::where('id', $refundRecordId)->findOrEmpty()->toArray();
|
||||
|
||||
$result = (new AliPayService())->refund($refundRecord['order_sn'], $refundAmount, self::$refundLog['sn']);
|
||||
$result = (array)$result;
|
||||
|
||||
if ($result['code'] == '10000' && $result['msg'] == 'Success' && $result['fundChange'] == 'Y') {
|
||||
// 更新日志
|
||||
RefundLog::update([
|
||||
'refund_status' => RefundEnum::REFUND_SUCCESS,
|
||||
'refund_msg' => json_encode($result, JSON_UNESCAPED_UNICODE),
|
||||
], ['id'=>self::$refundLog['id']]);
|
||||
|
||||
// 更新记录
|
||||
RefundRecord::update([
|
||||
'refund_status' => RefundEnum::REFUND_SUCCESS,
|
||||
], ['id'=>$refundRecordId]);
|
||||
|
||||
// 更新订单信息
|
||||
if ($refundRecord['order_type'] == 'recharge') {
|
||||
RechargeOrder::update([
|
||||
'id' => $refundRecord['order_id'],
|
||||
'refund_transaction_id' => $result['tradeNo'] ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 退款请求失败处理
|
||||
* @param $refundRecordId
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 16:00
|
||||
* @remark 【微信,支付宝】退款接口请求失败时,更新退款记录及日志为失败,在退款记录重新发起
|
||||
*/
|
||||
public static function refundFailHandle($refundRecordId, $msg)
|
||||
{
|
||||
// 更新退款日志记录
|
||||
RefundLog::update([
|
||||
'id' => self::$refundLog['id'],
|
||||
'refund_status' => RefundEnum::REFUND_ERROR,
|
||||
'refund_msg' => $msg,
|
||||
]);
|
||||
|
||||
// 更新退款记录状态为退款失败
|
||||
RefundRecord::update([
|
||||
'id' => $refundRecordId,
|
||||
'refund_status' => RefundEnum::REFUND_ERROR,
|
||||
'refund_msg' => $msg,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 退款日志
|
||||
* @param $order
|
||||
* @param $refundRecordId
|
||||
* @param $refundAmount
|
||||
* @param $handleId
|
||||
* @param int $refundStatus
|
||||
* @author 段誉
|
||||
* @date 2023/2/28 15:29
|
||||
*/
|
||||
public static function log($order, $refundRecordId, $refundAmount, $handleId, $refundStatus = RefundEnum::REFUND_ING)
|
||||
{
|
||||
$sn = generate_sn(RefundLog::class, 'sn');
|
||||
|
||||
self::$refundLog = RefundLog::create([
|
||||
'sn' => $sn,
|
||||
'record_id' => $refundRecordId,
|
||||
'user_id' => $order['user_id'],
|
||||
'handle_id' => $handleId,
|
||||
'order_amount' => $order['order_amount'],
|
||||
'refund_amount' => $refundAmount,
|
||||
'refund_status' => $refundStatus
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user