提交其他文件
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;
|
||||
}
|
||||
|
||||
}
|
||||
780
app/common/logic/PayNotifyLogic.php
Normal file
780
app/common/logic/PayNotifyLogic.php
Normal file
@ -0,0 +1,780 @@
|
||||
<?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\api\logic\CommonLogic;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\AccountLogEnum;
|
||||
use app\common\model\order\OrderAll;
|
||||
use app\common\model\order\OrderGroup;
|
||||
use app\common\model\order\OrderMember;
|
||||
use app\common\model\order\OrderStore;
|
||||
use app\common\model\order\OrderStoreRenew;
|
||||
use app\common\model\order\OrderTeamaster;
|
||||
use app\common\model\order\OrderTeamasterRenew;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use app\common\model\refund\RefundLog;
|
||||
use app\common\model\refund\RefundRecord;
|
||||
use app\common\model\store\StoreUserAccountLog;
|
||||
use app\common\model\order\OrderStoreRecharge;
|
||||
use app\common\model\teamaster\TeamasterAccountLog;
|
||||
use app\common\model\teamaster\TeamasterUser;
|
||||
use app\common\model\teastore\TeaStore;
|
||||
use app\common\model\user\UserStoreMoney;
|
||||
use app\common\model\store\StoreMember;
|
||||
use app\common\model\teastore\TeaStoreGroup;
|
||||
use app\common\model\teastore\TeaStoreRoom;
|
||||
use app\common\model\user\User;
|
||||
use app\common\model\user\UserAccountLog;
|
||||
use app\common\model\user\UserCoupon;
|
||||
use app\common\model\user\UserGroup;
|
||||
use app\common\model\user\UserMember;
|
||||
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 = [])
|
||||
{
|
||||
// Log::write("测试2",$orderSn."-".$extra['transaction_id']);
|
||||
$order = RechargeOrder::where('order_sn', $orderSn)->findOrEmpty();
|
||||
// 增加用户累计充值金额及用户余额
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
$before_amount = $user->user_money;
|
||||
$after_amount = $user->user_money + $order->order_amount;
|
||||
$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->order_sn,
|
||||
// '用户充值'
|
||||
// );
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>1,
|
||||
"change_type"=>4,
|
||||
"action"=>1,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>$before_amount,
|
||||
"after_amount"=>$after_amount,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order->store_id,
|
||||
"remark"=>'用户充值',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
OrderAll::where("source_sn",$order->order_sn)->where("type",1)->update([
|
||||
"pay_way"=>2,
|
||||
"order_status"=>1,
|
||||
"pay_status"=>1,
|
||||
"pay_time"=>time(),
|
||||
"update_time"=>time()
|
||||
]);
|
||||
// 判断是否为第一次充值
|
||||
$result = RechargeOrder::where('user_id', $order->user_id)->where("pay_status",1)->count();
|
||||
if($result == 1){
|
||||
// 判断是否为会员
|
||||
$member = UserMember::where("user_id",$order->user_id)->where("status",1)->find();
|
||||
if($member==null){
|
||||
$time = time();
|
||||
UserMember::create([
|
||||
"user_id"=>$order->user_id,
|
||||
"expiration_time"=>strtotime('+1 year', $time),
|
||||
"dtime"=>time()
|
||||
]);
|
||||
User::where("id",$order->user_id)->update(['member'=>1,'update_time'=>time()]);
|
||||
}
|
||||
}
|
||||
// 更新充值订单状态
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->pay_time = time();
|
||||
$order->save();
|
||||
}
|
||||
public static function orderStore($orderSn, array $extra = [])
|
||||
{
|
||||
$order = OrderStore::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
// 查询用户
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
// 记录账户流水
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>1,
|
||||
"action"=>2,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>$user->user_money,
|
||||
"after_amount"=>$user->user_money,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order->store_id,
|
||||
"remark"=>'包间预定',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
$tea_store = TeaStore::where("id",$order->store_id)->find();
|
||||
// 添加商户流水信息
|
||||
$order_amount = $order->order_amount;
|
||||
TeaStore::where('id', $order['store_id'])->inc('total_amount', $order_amount)->update();
|
||||
TeaStore::where('id', $order['store_id'])->inc('balance', $order_amount)->update();
|
||||
if($order->pay_way == 3){
|
||||
$order_amount = 0;
|
||||
}
|
||||
$r = StoreUserAccountLog::create([
|
||||
'sn'=>createSn("store_user_account_log","sn"),
|
||||
'user_id'=>$order->user_id,
|
||||
'change_object'=>2,
|
||||
'change_type'=>1,
|
||||
'action'=>1,
|
||||
'amount' => $order_amount,
|
||||
'before_amount'=>$tea_store['balance'],
|
||||
'after_amount' => round($tea_store['balance']+$order_amount,2),
|
||||
'source_sn' => $orderSn,
|
||||
'store_id'=>$order->store_id,
|
||||
'room_id'=>$order->room_id,
|
||||
'remark'=>"包间预定",
|
||||
'create_time' => time()
|
||||
]);
|
||||
//结算团购套餐
|
||||
if($order['group_coupon_id'] != 0){
|
||||
UserGroup::where('id',$order['group_coupon_id'])->update(["status"=>1]);
|
||||
$group = UserGroup::where("id",$order['group_coupon_id'])->find();
|
||||
$store_group = TeaStoreGroup::where("id",$group['group_id'])->find();
|
||||
$change_object = 4;
|
||||
if($group['type'] == 2){
|
||||
$store_group['discount_price'] = 0;
|
||||
$change_object = 5;
|
||||
}
|
||||
StoreUserAccountLog::create([
|
||||
'sn'=>createSn("store_user_account_log","sn"),
|
||||
'change_object'=>$change_object,
|
||||
'change_type'=>1,
|
||||
'user_id'=>$order->user_id,
|
||||
'action'=>1,
|
||||
'amount' => $store_group['discount_price'],
|
||||
'before_amount'=>round($tea_store['balance']+$order_amount,2),
|
||||
'after_amount' => round($tea_store['balance']+$store_group['discount_price']+$order_amount,2),
|
||||
'source_sn' => $orderSn,
|
||||
'store_id'=>$order->store_id,
|
||||
'room_id'=>$order->room_id,
|
||||
'remark'=>"团购套餐",
|
||||
'create_time' => time()
|
||||
]);
|
||||
TeaStore::where('id', $order['store_id'])->inc('total_amount', $store_group['discount_price'])->update();
|
||||
TeaStore::where('id', $order['store_id'])->inc('balance', $store_group['discount_price'])->update();
|
||||
}
|
||||
// 更新预定订单状态
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->order_status = 1;
|
||||
$order->update_dtime = date("Y-m-d H:i:s");
|
||||
$order->save();
|
||||
if(isset($order['room_id'])){
|
||||
if($order['room_id']!=0&&$order['room_id']!=""&&$order['room_id']!=null){
|
||||
TeaStoreRoom::where('id', $order['room_id'])->inc('sold', 1)->update();
|
||||
}
|
||||
}
|
||||
OrderAll::where("source_sn",$orderSn)->where("type",1)->update([
|
||||
"pay_way"=>2,
|
||||
"order_status"=>1,
|
||||
"pay_status"=>1,
|
||||
"pay_time"=>time(),
|
||||
"update_time"=>time()
|
||||
]);
|
||||
}
|
||||
public static function OrderGroup($orderSn, array $extra = [])
|
||||
{
|
||||
|
||||
$order = OrderGroup::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
// 查询用户
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
// 记录账户流水
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>2,
|
||||
"action"=>2,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>$user->user_money,
|
||||
"after_amount"=>$user->user_money,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order->store_id,
|
||||
"remark"=>'团购套餐',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
|
||||
// 更新预定订单状态
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->order_status = 1;
|
||||
$order->update_dtime = date("Y-m-d H:i:s");
|
||||
$order->save();
|
||||
|
||||
OrderAll::where("source_sn",$orderSn)->where("type",6)->update([
|
||||
"pay_way"=>2,
|
||||
"order_status"=>1,
|
||||
"pay_status"=>1,
|
||||
"pay_time"=>time(),
|
||||
"update_time"=>time()
|
||||
]);
|
||||
$qr_sn = createSn("user_group","qr_sn");
|
||||
$qr_url = CommonLogic::qrcode($qr_sn);
|
||||
$group = TeaStoreGroup::where('id', $order->group_id)->find();
|
||||
UserGroup::create([
|
||||
"user_id"=>$order->user_id,
|
||||
"group_id"=>$order->group_id,
|
||||
"qr_sn"=>$qr_sn,
|
||||
"qr_url"=>$qr_url['file_url'],
|
||||
"type"=>$group['type'],
|
||||
"order_id"=>$order->id,
|
||||
"store_id"=>$order->store_id,
|
||||
"dtime"=>date("Y-m-d H:i:s")
|
||||
]);
|
||||
$tea_store = TeaStore::where("id",$order->store_id)->find();
|
||||
// 添加商户流水信息
|
||||
$order_amount = $order->order_amount;
|
||||
// TeaStore::where('id', $order['store_id'])->inc('total_amount', $order_amount)->update();
|
||||
// TeaStore::where('id', $order['store_id'])->inc('balance', $order_amount)->update();
|
||||
if(isset($order['group_id'])){
|
||||
if($order['group_id']!=0&&$order['group_id']!=""&&$order['group_id']!=null){
|
||||
TeaStoreGroup::where('id', $order['group_id'])->inc('sold', 1)->update();
|
||||
}
|
||||
}
|
||||
// $r = StoreUserAccountLog::create([
|
||||
// 'sn'=>createSn("store_user_account_log","sn"),
|
||||
// 'change_object'=>1,
|
||||
// 'change_type'=>1,
|
||||
// 'action'=>1,
|
||||
// 'amount' => $order_amount,
|
||||
// 'before_amount'=>$tea_store['balance'],
|
||||
// 'after_amount' => round($tea_store['balance']+$order_amount,2),
|
||||
// 'source_sn' => $orderSn,
|
||||
// 'store_id'=>$order->store_id,
|
||||
// 'room_id'=>$order->room_id,
|
||||
// 'remark'=>"团购套餐",
|
||||
// 'create_time' => time()
|
||||
// ]);
|
||||
|
||||
}
|
||||
|
||||
public static function OrderMember($orderSn, array $extra = [])
|
||||
{
|
||||
$order = OrderMember::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
// 查询用户
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
// 记录账户流水
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>3,
|
||||
"action"=>2,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>$user->user_money,
|
||||
"after_amount"=>$user->user_money,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>0,
|
||||
"remark"=>'开通会员',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
|
||||
// 更新预定订单状态
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->order_status = 1;
|
||||
$order->update_dtime = date("Y-m-d H:i:s");
|
||||
$order->save();
|
||||
|
||||
OrderAll::where("source_sn",$orderSn)->where("type",2)->update([
|
||||
"pay_way"=>2,
|
||||
"order_status"=>1,
|
||||
"pay_status"=>1,
|
||||
"pay_time"=>time(),
|
||||
"update_time"=>time()
|
||||
]);
|
||||
$member = UserMember::where("user_id",$order->user_id)->where("status",1)->find();
|
||||
if($member!=null){
|
||||
$expiration_time = strtotime('+1 year', $member['expiration_time']);
|
||||
UserMember::where("user_id",$order->user_id)
|
||||
->update(['expiration_time',$expiration_time,
|
||||
'update_dtime'=>time()]);
|
||||
}else{
|
||||
$time = time();
|
||||
UserMember::create([
|
||||
"user_id"=>$order->user_id,
|
||||
"expiration_time"=>strtotime('+1 year', $time),
|
||||
"dtime"=>time()
|
||||
]);
|
||||
User::where("id",$order->user_id)->update(['member'=>1,'update_time'=>time()]);
|
||||
}
|
||||
}
|
||||
public static function orderStoreRenew($orderSn, array $extra = [])
|
||||
{
|
||||
|
||||
$order = OrderStoreRenew::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
Log::write("测试2",$orderSn);
|
||||
$order_store = OrderStore::where("id",$order['source_id'])->find();
|
||||
// 查询用户
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
// 记录账户流水
|
||||
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>7,
|
||||
"action"=>2,
|
||||
"amount"=>$order->price,
|
||||
"before_amount"=>$user->user_money,
|
||||
"after_amount"=>$user->user_money,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order_store['store_id'],
|
||||
"remark"=>'包间续订',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
Log::write("测试4",$orderSn);
|
||||
$tea_store = TeaStore::where("id",$order_store['store_id'])->find();
|
||||
// 添加商户流水信息
|
||||
$order_amount = $order->price;
|
||||
|
||||
TeaStore::where('id', $order_store['store_id'])->inc('total_amount', $order_amount)->update();
|
||||
TeaStore::where('id', $order_store['store_id'])->inc('balance', $order_amount)->update();
|
||||
if($order->pay_way == 3){
|
||||
$order_amount = 0;
|
||||
}
|
||||
$r = StoreUserAccountLog::create([
|
||||
'sn'=>createSn("store_user_account_log","sn"),
|
||||
'change_object'=>2,
|
||||
'change_type'=>2,
|
||||
'user_id'=>$order->user_id,
|
||||
'action'=>1,
|
||||
'amount' => $order_amount,
|
||||
'before_amount'=>$tea_store['balance'],
|
||||
'after_amount' => round($tea_store['balance']+$order_amount,2),
|
||||
'source_sn' => $orderSn,
|
||||
'store_id'=>$order_store['store_id'],
|
||||
'room_id'=>$order_store['room_id'],
|
||||
'remark'=>"包间续订",
|
||||
'create_time' => time()
|
||||
]);
|
||||
// 更新预定订单状态
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->pay_dtime = time();
|
||||
$order->save();
|
||||
OrderAll::where("source_sn",$orderSn)->where("type",7)->update([
|
||||
"pay_way"=>2,
|
||||
"order_status"=>1,
|
||||
"pay_status"=>1,
|
||||
"pay_time"=>time(),
|
||||
"update_time"=>time()
|
||||
]);
|
||||
$renew_timeslot = explode(',', $order->timeslot);
|
||||
$store_timeslot = explode(',', $order_store['timeslot']);
|
||||
$timeList = array_merge($store_timeslot, $renew_timeslot);
|
||||
$end = end($timeList);
|
||||
$da['end_time'] = $end;
|
||||
$da['timeslot'] = implode(',', $timeList);
|
||||
$da['renew_hour'] = $order->hour + $order_store['renew_hour'];
|
||||
$da['renew_price'] = $order_amount+$order_store['renew_price'];
|
||||
$da['store_income_price'] = $order_store['store_income_price'] + $order_amount;
|
||||
$transfer_order = OrderStore::where('transfer_order_id',$order['source_id'])->select();
|
||||
$ids = explode(',',$order['source_id']);
|
||||
if($transfer_order->count() > 0){
|
||||
$orderIds = $transfer_order->column('id');
|
||||
$ids = array_merge($ids,$orderIds);
|
||||
}
|
||||
|
||||
OrderStore::whereIn("id",$ids)->update($da);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function OrderStoreRecharge($orderSn, array $extra = []){
|
||||
|
||||
$order = OrderStoreRecharge::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
$StoreMember = StoreMember::where([
|
||||
'user_id'=>$order->user_id,
|
||||
'store_id'=>$order->store_id
|
||||
])->find();
|
||||
if(!$StoreMember){
|
||||
StoreMember::create([
|
||||
'store_id'=>$order->store_id,
|
||||
'user_id'=>$order->user_id,
|
||||
'dtime'=>time()
|
||||
]);
|
||||
}
|
||||
|
||||
$UserStoreMoney = UserStoreMoney::where([
|
||||
'user_id'=>$order->user_id,
|
||||
'store_id'=>$order->store_id
|
||||
])->find();
|
||||
if($UserStoreMoney){
|
||||
$money = $UserStoreMoney->money+$order->recharge_price+$order->gift_price;
|
||||
$UserStoreMoney->money = $money;
|
||||
$UserStoreMoney->update_dtime = date('Y-m-d H:i:s');
|
||||
$UserStoreMoney->save();
|
||||
}else{
|
||||
$money = $order->recharge_price+$order->gift_price;
|
||||
UserStoreMoney::create([
|
||||
'store_id'=>$order->store_id,
|
||||
'user_id'=>$order->user_id,
|
||||
'money' =>$money,
|
||||
'dtime'=> date('Y-m-d H:i:s')
|
||||
]);
|
||||
}
|
||||
$tea_store = TeaStore::where("id",$order['store_id'])->find();
|
||||
$order_amount = $order->order_amount;
|
||||
$r = StoreUserAccountLog::create([
|
||||
'sn'=>createSn("store_user_account_log","sn"),
|
||||
'change_object'=>2,
|
||||
'change_type'=>9,
|
||||
'user_id'=>$order->user_id,
|
||||
'action'=>1,
|
||||
'amount' => $order_amount,
|
||||
'before_amount'=>$tea_store['balance'],
|
||||
'after_amount' => round($tea_store['balance']+$order_amount,2),
|
||||
'source_sn' => $orderSn,
|
||||
'store_id'=>$order['store_id'],
|
||||
'room_id'=>'',
|
||||
'remark'=>"门店余额充值",
|
||||
'create_time' => time()
|
||||
]);
|
||||
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>20,
|
||||
"action"=>2,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>0,
|
||||
"after_amount"=>0,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order->store_id,
|
||||
"remark"=>'门店余额充值',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
|
||||
$order->transaction_id = $extra['transaction_id'] ?? '';
|
||||
$order->pay_status = PayEnum::ISPAID;
|
||||
$order->pay_time = time();
|
||||
$order->save();
|
||||
|
||||
TeaStore::where('id', $order['store_id'])->inc('total_amount', $order_amount)->update();
|
||||
TeaStore::where('id', $order['store_id'])->inc('balance', $order_amount)->update();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static function orderTeamaster($orderSn, array $extra = []){
|
||||
|
||||
$order = OrderTeamaster::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
if($order->user_coupon_id !=0){
|
||||
$user_coupon=UserCoupon::where([
|
||||
'id'=>$order->user_coupon_id,
|
||||
'status'=>0
|
||||
])->find();
|
||||
$user_coupon->status = 1;
|
||||
$user_coupon->save();
|
||||
}
|
||||
$data = [
|
||||
'pay_status'=>1,
|
||||
'order_status'=>28,
|
||||
'transaction_id'=>$extra['transaction_id'],
|
||||
'pay_way'=>2,
|
||||
'pay_time'=>time()
|
||||
];
|
||||
$order = OrderTeamaster::where('id',$order->id)->update($data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function orderTeamaRenw($orderSn, array $extra = []){
|
||||
Log::write("支付日志",'444444444444444444444444');
|
||||
$order = OrderTeamasterRenew::where('order_sn', $orderSn)->findOrEmpty();
|
||||
if($order->pay_status != 0){
|
||||
return true;
|
||||
}
|
||||
$is_renewal = 0;
|
||||
$hours = $order->hours;
|
||||
$is_tea = 0;
|
||||
$renew_price = 0;
|
||||
$renew_tea_price =0;
|
||||
$change_type = 2;
|
||||
$source_order = OrderTeamaster::where('id',$order->source_id)->find();
|
||||
$renew_income_price =$order->price*0.35;
|
||||
$team_income_price = $source_order->team_income_price+$renew_income_price;
|
||||
$end_time = $source_order->end_time+($hours * 3600);
|
||||
Log::write("支付日志",'555555555555555555555');
|
||||
Log::write("测试1",$orderSn);
|
||||
$remark = '续时';
|
||||
if($order->type == 1){
|
||||
$is_renewal =1;
|
||||
$renew_price =$source_order->server_price*$hours;
|
||||
}elseif ($order->type == 2){
|
||||
$is_tea =1;
|
||||
$renew_tea_price =$order->tea_price;
|
||||
$change_type = 3;
|
||||
$remark = '需茶';
|
||||
}elseif ($order->type == 3){
|
||||
$is_tea =1;
|
||||
$renew_tea_price =$order->tea_price;
|
||||
$is_renewal =1;
|
||||
$renew_price =$source_order->server_price*$hours;
|
||||
$remark = '续时+续茶';
|
||||
}
|
||||
$renw_data = [
|
||||
'pay_status'=>1,
|
||||
'pay_dtime'=>time()
|
||||
];
|
||||
Log::write("测试2",$orderSn);
|
||||
$data = [
|
||||
'is_renewal'=>$is_renewal,
|
||||
'is_tea'=>$is_tea,
|
||||
'renew_tea_price'=>$renew_tea_price+$source_order->renew_tea_price,
|
||||
'renew_price'=>$renew_price+$source_order->renew_price,
|
||||
'renew_hour'=>$hours+$source_order->renew_hour,
|
||||
'team_income_price'=>$team_income_price,
|
||||
'end_time'=>$end_time,
|
||||
'pay_way'=>2,
|
||||
];
|
||||
$team_user = TeamasterUser::where('id',$source_order->team_user_id)->find();
|
||||
$account_log = [
|
||||
'team_user_id'=>$source_order->team_user_id,
|
||||
'user_id'=>$order->user_id,
|
||||
'change_object'=>1,
|
||||
'change_type'=>$change_type,
|
||||
'action'=>1,
|
||||
'amount'=>$order->price,
|
||||
'before_amount'=>$team_user->user_money,
|
||||
'after_amount'=>$team_user->user_money+$renew_income_price,
|
||||
'source_sn'=>$source_order->order_sn,
|
||||
'sub_sn'=>$order->order_sn,
|
||||
'remark'=>$remark,
|
||||
'create_time'=>time()
|
||||
];
|
||||
|
||||
$renw_order = OrderTeamasterRenew::where('id',$order->id)->update($renw_data);
|
||||
$team_order = OrderTeamaster::where('id',$source_order->id)->update($data);
|
||||
self::getTeamasterAccountLog($account_log);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function getTeamasterAccountLog($account_log){
|
||||
TeamasterAccountLog::create($account_log);
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static function refundOrderGroup($orderSn, array $extra = []){
|
||||
Log::write("退款1",$orderSn);
|
||||
$order = OrderGroup::where('order_sn', $orderSn)->findOrEmpty();
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
$before_amount = $user->user_money;
|
||||
$after_amount = $user->user_money; Log::write("退款2",$orderSn);
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>5,
|
||||
"action"=>1,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>$before_amount,
|
||||
"after_amount"=>$after_amount,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order->store_id,
|
||||
"remark"=>'用户团购套餐退款',
|
||||
"create_time"=>time()
|
||||
]); Log::write("退款3",$orderSn);
|
||||
OrderAll::where("source_sn",$order->order_sn)->where("type",5)->update([
|
||||
"order_status"=>5,
|
||||
"update_time"=>time()
|
||||
]); Log::write("退款4",$orderSn);
|
||||
// 更新充值订单状态
|
||||
$order->order_status =5;
|
||||
$order->update_dtime = date("Y-m-d H:i:s");
|
||||
$order->save(); Log::write("退款5",$orderSn);
|
||||
UserGroup::where("order_id",$order->id)->update(['status'=>3]);
|
||||
RefundRecord::where("source_sn",$orderSn)->update(['refund_way'=>3,'refund_status'=>1,'update_time'=>time(),'transaction_id'=>$extra['transaction_id'] ?? '']); Log::write("退款6",$orderSn);
|
||||
}
|
||||
|
||||
public static function refundOrderStore($orderSn, array $extra = []){
|
||||
$order = OrderStore::where('order_sn', $orderSn)->findOrEmpty();
|
||||
$user = User::findOrEmpty($order->user_id);
|
||||
$before_amount = $user->user_money;
|
||||
$after_amount = $user->user_money;
|
||||
UserAccountLog::create([
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>2,
|
||||
"change_type"=>5,
|
||||
"action"=>1,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>$before_amount,
|
||||
"after_amount"=>$after_amount,
|
||||
"source_sn"=>$orderSn,
|
||||
"store_id"=>$order->store_id,
|
||||
"remark"=>'用户包间预定退款',
|
||||
"create_time"=>time()
|
||||
]);
|
||||
$tes_store = TeaStore::where('id', $order['store_id'])->find();
|
||||
$order_amount = $order->store_income_price;
|
||||
$total_amount = round($tes_store['total_amount'] - $order_amount,2);
|
||||
$balance = round($tes_store['balance'] - $order_amount,2);
|
||||
TeaStore::where('id', $order['store_id'])->update(['total_amount'=>$total_amount,'balance'=>$balance]);
|
||||
StoreUserAccountLog::create([
|
||||
'sn'=>createSn("store_user_account_log","sn"),
|
||||
'change_object'=>1,
|
||||
'change_type'=>1,
|
||||
'action'=>2,
|
||||
'amount' => $order->store_income_price,
|
||||
'before_amount'=>$tes_store['balance'],
|
||||
'after_amount' => $balance,
|
||||
'source_sn' => $orderSn,
|
||||
'store_id'=>$order->store_id,
|
||||
'room_id'=>$order->room_id,
|
||||
'remark'=>"包间预定退款",
|
||||
'create_time' => time()
|
||||
]);
|
||||
|
||||
OrderAll::where("source_sn",$order->order_sn)->where("type",$order['order_type'])->update([
|
||||
"order_status"=>5,
|
||||
"update_time"=>time()
|
||||
]);
|
||||
// 更新包间订单状态
|
||||
$order->order_status =5;
|
||||
$order->update_dtime = date("Y-m-d H:i:s");
|
||||
$order->save();
|
||||
//查询是否使用优惠券
|
||||
if($order->user_coupon_id != 0){
|
||||
UserCoupon::where("id",$order->user_coupon_id)->update(['status'=>0]);
|
||||
}
|
||||
//查询是否使用团购券
|
||||
if($order->group_coupon_id != 0){
|
||||
UserGroup::where("id",$order->group_coupon_id)->update(['status'=>0]);
|
||||
}
|
||||
RefundRecord::where("source_sn",$orderSn)->update(['refund_way'=>3,'refund_status'=>1,'update_time'=>time(),'transaction_id'=>$extra['transaction_id'] ?? '']);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function teamasterOrderRefund($orderSn, array $extra = []){
|
||||
$order = OrderTeamaster::where('order_sn', $orderSn)->findOrEmpty();
|
||||
$data['pay_way'] = 3;
|
||||
if($order->order_status ==28||$order->order_status ==29){
|
||||
$data['order_status'] = 40;
|
||||
$data['refund_time'] = time();
|
||||
$data['refund_price'] = $order->order_amount;
|
||||
}elseif($order->order_status ==30){
|
||||
$data['order_status'] = 42;
|
||||
$data['refund_time'] = time();
|
||||
$refund_pricet = $order->order_amount - $order->mileage_server_price;
|
||||
$refund_pricet = $refund_pricet-($order->server_all_price*0.3);
|
||||
$data['refund_price'] = $refund_pricet;
|
||||
$data['team_income_price'] = (($order->server_all_price*0.3)*0.65)+$order->mileage_server_price;
|
||||
$user_team =TeamasterUser::where('id',$order->team_user_id)->find();
|
||||
$user_money = $user_team['user_money']+$data['team_income_price'];
|
||||
$user_team->user_money = $user_money;
|
||||
$user_team->save();
|
||||
|
||||
$account_log = [
|
||||
'team_user_id'=>$order->team_user_id,
|
||||
'user_id'=>$order->user_id,
|
||||
'change_object'=>2,
|
||||
'change_type'=>1,
|
||||
'action'=>1,
|
||||
'amount'=>$data['team_income_price'],
|
||||
'before_amount'=>$user_team->user_money,
|
||||
'after_amount'=>$user_team->user_money+$data['team_income_price'],
|
||||
'source_sn'=>$order->order_sn,
|
||||
'sub_sn'=>$order->order_sn,
|
||||
'remark'=>'出发后用户退款——收取30%服务费和全额车马费',
|
||||
'create_time'=>time()
|
||||
];
|
||||
self::getTeamasterAccountLog($account_log);
|
||||
}
|
||||
|
||||
OrderTeamaster::where('id',$order->id)->update($data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
603
app/common/logic/PaymentLogic.php
Normal file
603
app/common/logic/PaymentLogic.php
Normal file
@ -0,0 +1,603 @@
|
||||
<?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\order\Order;
|
||||
use app\common\model\order\OrderGroup;
|
||||
use app\common\model\order\OrderMember;
|
||||
use app\common\model\order\OrderStore;
|
||||
use app\common\model\pay\Pay;
|
||||
use app\common\model\pay\PayWay;
|
||||
use app\common\model\recharge\RechargeOrder;
|
||||
use app\common\model\store\StoreUserAccountLog;
|
||||
use app\common\model\teamaster\TeamasterAccountLog;
|
||||
use app\common\model\teastore\TeaStore;
|
||||
use app\common\model\teastore\TeaStoreGroup;
|
||||
use app\common\model\user\User;
|
||||
use app\common\model\user\UserAccountLog;
|
||||
use app\common\model\user\UserStoreMoney;
|
||||
use app\common\service\ConfigService;
|
||||
use app\common\service\pay\AliPayService;
|
||||
use app\common\service\pay\WeChatPayService;
|
||||
use app\common\model\order\OrderStoreRenew;
|
||||
use app\common\model\user\UserGroup;
|
||||
use app\common\model\user\UserCoupon;
|
||||
use app\common\model\order\OrderTeamaster;
|
||||
use app\common\model\order\OrderTeamasterRenew;
|
||||
use app\common\model\teamaster\TeamasterUser;
|
||||
use app\common\model\order\OrderStoreRecharge;
|
||||
use think\facade\Db;
|
||||
use app\common\logic\Exception;
|
||||
|
||||
/**
|
||||
* 支付逻辑
|
||||
* 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,$user_id)
|
||||
{
|
||||
// order_type 0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值 7.续费
|
||||
try {
|
||||
switch ($params['from']) {
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::findOrEmpty($params['order_id']);
|
||||
if ($order->isEmpty()) {
|
||||
throw new \Exception('充值订单不存在');
|
||||
}
|
||||
break;
|
||||
case 'balance':
|
||||
if($params['order_type'] == 1){
|
||||
$order = OrderStore::where("id",$params['order_id'])->find();
|
||||
}elseif($params['order_type'] == 2){
|
||||
$order = OrderGroup::where("id",$params['order_id'])->find();
|
||||
}elseif($params['order_type'] == 7){
|
||||
$order = OrderStoreRenew::where("id",$params['order_id'])->find();
|
||||
$order['order_amount'] =$order->price;
|
||||
|
||||
}
|
||||
$user = User::where("id",$user_id)->find();
|
||||
if($params['pay_way'] == 3){
|
||||
$user_store_money = UserStoreMoney::where("user_id",$user_id)->where("store_id",$params['store_id'])->find();
|
||||
if($user_store_money==null){
|
||||
throw new \Exception('余额不足');
|
||||
}
|
||||
$user['user_money'] = $user_store_money['money'];
|
||||
}
|
||||
if ($order['order_amount'] > $user['user_money']) {
|
||||
throw new \Exception('余额不足');
|
||||
}
|
||||
break;
|
||||
case 'wx':
|
||||
if($params['order_type'] == 0){
|
||||
$order = Order::where("id",$params['order_id'])->find();
|
||||
}elseif($params['order_type'] == 1){
|
||||
$order = OrderStore::where("id",$params['order_id'])->find();
|
||||
}elseif($params['order_type'] == 2) {
|
||||
$order = OrderGroup::where("id", $params['order_id'])->find();
|
||||
}elseif($params['order_type'] == 3){
|
||||
$order_sn = createSn("order_store","order_sn");
|
||||
$price = ConfigService::get("member","price");
|
||||
$r = OrderMember::create([
|
||||
'order_sn'=>$order_sn,
|
||||
'pay_way'=>$params['pay_way'],
|
||||
'order_amount'=> $price,
|
||||
'user_id'=>$user_id,
|
||||
'dtime'=>date("Y-m-d H:i:s")
|
||||
]);
|
||||
|
||||
//0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值
|
||||
OrderStore::orderAll("order_member",3,$order_sn,0,$price,$user_id);
|
||||
$order = OrderMember::where("id",$r->id)->find();
|
||||
}elseif($params['order_type'] == 7){
|
||||
$order = OrderStoreRenew::where("id", $params['order_id'])->find();
|
||||
$order['order_amount'] = $order['price'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
return $order;
|
||||
} catch (\Exception $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取预支付订单信息
|
||||
* @param $params
|
||||
* @return RechargeOrder|array|false|\think\Model
|
||||
* @author Yzt
|
||||
* @date 2026/1/02 15:19
|
||||
*/
|
||||
public static function getPayInfo($params,$user_id)
|
||||
{
|
||||
// order_type 0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值 7.续费 10预定茶艺师 11茶艺师续订
|
||||
try {
|
||||
switch ($params['order_type']) {
|
||||
case '9':
|
||||
$order = OrderStoreRecharge::where([
|
||||
'id'=>$params['order_id'],
|
||||
])->find();
|
||||
if($order->pay_status == 1){
|
||||
throw new \Exception("订单已支付");
|
||||
}
|
||||
break;
|
||||
case '10':
|
||||
$order = OrderTeamaster::where([
|
||||
'id'=>$params['order_id'],
|
||||
])->find();
|
||||
if($order->pay_status == 1){
|
||||
throw new \Exception("订单已支付");
|
||||
}
|
||||
if($order->order_status !==10){
|
||||
throw new \Exception("订单已取消");
|
||||
}
|
||||
break;
|
||||
case '11':
|
||||
$order = OrderTeamasterRenew::where([
|
||||
'id'=>$params['order_id'],
|
||||
])->find();
|
||||
if(!$order){
|
||||
throw new \Exception("订单错误订单不存在");
|
||||
}
|
||||
if($order->pay_status == 1){
|
||||
throw new \Exception("订单已支付");
|
||||
}
|
||||
$order['order_amount'] =$order['price'];
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
}
|
||||
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($order_type,$payWay, $from, $order, $terminal, $redirectUrl)
|
||||
{
|
||||
// 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
|
||||
$paySn = $order['order_sn'];
|
||||
if ($payWay == PayEnum::WECHAT_PAY) {
|
||||
$paySn = self::formatOrderSn($order['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($order_type,$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;
|
||||
}
|
||||
|
||||
|
||||
public static function balancePay($order,$params){
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = User::where('id',$order['user_id'])->find();
|
||||
if(!$user){
|
||||
throw new \Exception("用户不存在");
|
||||
}
|
||||
if($order->order_amount > $user->user_money){
|
||||
throw new \Exception("余额不足");
|
||||
}
|
||||
if($order->pay_status == 1){
|
||||
throw new \Exception("订单已支付");
|
||||
}
|
||||
$user_money = $user->user_money-$order->order_amount;
|
||||
$user->user_money =$user_money;
|
||||
$user->save();
|
||||
|
||||
switch ($params['order_type']) {
|
||||
case '10':
|
||||
$result = self::orderTeamaster($order);
|
||||
break;
|
||||
|
||||
case '11':
|
||||
$result = self::orderTeamaRenw($order);
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return [];
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function orderTeamaster($order){
|
||||
|
||||
if($order->user_coupon_id !=0){
|
||||
$user_coupon=UserCoupon::where([
|
||||
'id'=>$order->user_coupon_id,
|
||||
'type_id'=>1,
|
||||
'status'=>0
|
||||
])->find();
|
||||
|
||||
if(!$user_coupon ){
|
||||
throw new \Exception("优惠券已被核销");
|
||||
}
|
||||
$user_coupon->status = 1;
|
||||
$user_coupon->save();
|
||||
}
|
||||
$data = [
|
||||
'pay_status'=>1,
|
||||
'order_status'=>28,
|
||||
'pay_time'=>time()
|
||||
];
|
||||
$user_account_log = [
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>1,
|
||||
"change_type"=>11,
|
||||
"action"=>2,
|
||||
"amount"=>$order->order_amount,
|
||||
"before_amount"=>0,
|
||||
"after_amount"=>0,
|
||||
"source_sn"=>$order->order_sn,
|
||||
"store_id"=>0,
|
||||
"remark"=>'茶艺师订单',
|
||||
"create_time"=>time()
|
||||
];
|
||||
self::getUserAccountLog($user_account_log);
|
||||
$order = OrderTeamaster::where('id',$order->id)->update($data);
|
||||
return [];
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function orderTeamaRenw($order){
|
||||
$is_renewal = 0;
|
||||
$hours = $order->hours;
|
||||
$is_tea = 0;
|
||||
$renew_price = 0;
|
||||
$renew_tea_price =0;
|
||||
$change_type = 2;
|
||||
$source_order = OrderTeamaster::where('id',$order->source_id)->find();
|
||||
$renew_income_price =$order->price*0.35;
|
||||
$team_income_price = $source_order->team_income_price+$renew_income_price;
|
||||
$end_time = $source_order->end_time+($hours * 3600);
|
||||
|
||||
|
||||
$remark = '茶艺师续时';
|
||||
if($order->type == 1){
|
||||
$is_renewal =1;
|
||||
$renew_price =$source_order->server_price*$hours;
|
||||
}elseif ($order->type == 2){
|
||||
$is_tea =1;
|
||||
$renew_tea_price =$order->tea_price;
|
||||
$change_type = 3;
|
||||
$remark = '需茶';
|
||||
}elseif ($order->type == 3){
|
||||
$is_tea =1;
|
||||
$renew_tea_price =$order->tea_price;
|
||||
$is_renewal =1;
|
||||
$renew_price =$source_order->server_price*$hours;
|
||||
$remark = '茶艺师续时+续茶';
|
||||
}
|
||||
$renw_data = [
|
||||
'pay_status'=>1,
|
||||
'pay_dtime'=>time()
|
||||
];
|
||||
|
||||
$data = [
|
||||
'is_renewal'=>$is_renewal,
|
||||
'is_tea'=>$is_tea,
|
||||
'renew_tea_price'=>$renew_tea_price+$source_order->renew_tea_price,
|
||||
'renew_price'=>$renew_price+$source_order->renew_price,
|
||||
'renew_hour'=>$hours+$source_order->renew_hour,
|
||||
'team_income_price'=>$team_income_price,
|
||||
'end_time'=>$end_time
|
||||
];
|
||||
$team_user = TeamasterUser::where('id',$source_order->team_user_id)->find();
|
||||
$account_log = [
|
||||
'team_user_id'=>$source_order->team_user_id,
|
||||
'user_id'=>$order->user_id,
|
||||
'change_object'=>1,
|
||||
'change_type'=>$change_type,
|
||||
'action'=>1,
|
||||
'amount'=>$order->price,
|
||||
'before_amount'=>$team_user->user_money,
|
||||
'after_amount'=>$team_user->user_money+$renew_income_price,
|
||||
'source_sn'=>$source_order->order_sn,
|
||||
'sub_sn'=>$order->order_sn,
|
||||
'remark'=>$remark,
|
||||
'create_time'=>time()
|
||||
];
|
||||
$user_account_log = [
|
||||
"sn"=>createSn("user_account_log","sn"),
|
||||
"user_id"=>$order->user_id,
|
||||
"change_object"=>1,
|
||||
"change_type"=>11,
|
||||
"action"=>2,
|
||||
"amount"=>$order->price,
|
||||
"before_amount"=>0,
|
||||
"after_amount"=>0,
|
||||
"source_sn"=>$order->order_sn,
|
||||
"store_id"=>0,
|
||||
"remark"=>$remark,
|
||||
"create_time"=>time()
|
||||
];
|
||||
|
||||
$renw_order = OrderTeamasterRenew::where('id',$order->id)->update($renw_data);
|
||||
$team_order = OrderTeamaster::where('id',$source_order->id)->update($data);
|
||||
self::getTeamasterAccountLog($account_log);
|
||||
self::getUserAccountLog($user_account_log);
|
||||
return [];
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function getTeamasterAccountLog($account_log){
|
||||
TeamasterAccountLog::create($account_log);
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function getUserAccountLog($user_account_log){
|
||||
UserAccountLog::create($user_account_log);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 创建支付数据
|
||||
* @param $orderSn
|
||||
* @param $terminal
|
||||
* @return string
|
||||
* @author 胥聪
|
||||
* @date 2025/11/3 11:51
|
||||
* @remark
|
||||
*/
|
||||
public static function createPay($params, $order, $user_id)
|
||||
{
|
||||
// 0为茶艺师 1为茶室支付 2购买套餐 3购买会员 4充值 7续费
|
||||
if(!isset($params['order_type'])){
|
||||
$params['order_type'] = 0;
|
||||
}
|
||||
if($params['order_type'] == 1){
|
||||
if($order['order_amount'] == 0&&$params['pay_way'] == 2){
|
||||
$d['order_status'] = 1;
|
||||
$d['pay_status'] = 1;
|
||||
$d['update_dtime'] = date("Y-m-d H:i:s");
|
||||
if($order['group_coupon_id']!=0){
|
||||
UserGroup::where("id",$order['group_coupon_id'])->update(['status'=>1]);
|
||||
$tea_store = TeaStore::where("id",$order['store_id'])->find();
|
||||
$group = UserGroup::where("id",$order['group_coupon_id'])->find();
|
||||
$store_group = TeaStoreGroup::where("id",$group['group_id'])->find();
|
||||
if($group['type'] == 2){
|
||||
$store_group['discount_price'] = 0;
|
||||
}
|
||||
StoreUserAccountLog::create([
|
||||
'sn'=>createSn("store_user_account_log","sn"),
|
||||
'change_object'=>4,
|
||||
'change_type'=>1,
|
||||
'user_id'=>$user_id,
|
||||
'action'=>1,
|
||||
'amount' => $store_group['discount_price'],
|
||||
'before_amount'=>round($tea_store['balance'],2),
|
||||
'after_amount' => round($tea_store['balance']+$store_group['discount_price'],2),
|
||||
'source_sn' => $order['order_sn'],
|
||||
'store_id'=>$order['store_id'],
|
||||
'room_id'=>$order['room_id'],
|
||||
'remark'=>"团购套餐",
|
||||
'create_time' => time()
|
||||
]);
|
||||
TeaStore::where('id', $order['store_id'])->inc('total_amount', $store_group['discount_price'])->update();
|
||||
TeaStore::where('id', $order['store_id'])->inc('balance', $store_group['discount_price'])->update();
|
||||
}
|
||||
|
||||
}
|
||||
$d['pay_way'] = $params['pay_way'];
|
||||
OrderStore::where("id",$params['order_id'])->update($d);
|
||||
}elseif($params['order_type'] == 2){
|
||||
OrderGroup::where("id",$params['order_id'])->update(['pay_way'=>$params['pay_way']]);
|
||||
}elseif($params['order_type'] == 4){
|
||||
RechargeOrder::where("id",$params['order_id'])->update(['pay_way'=>$params['pay_way']]);
|
||||
}elseif ($params['order_type'] == 7){
|
||||
OrderStoreRenew::where('id',$params['order_id'])->update(['pay_way'=>$params['pay_way']]);
|
||||
}
|
||||
$result = Pay::insertGetId([
|
||||
'order_id'=>$params['order_id'],
|
||||
'pay_way'=>$params['pay_way'],
|
||||
'order_amount'=>$order['order_amount'],
|
||||
'user_id'=>$user_id,
|
||||
'order_source'=>$params['order_source'],
|
||||
'order_type'=>$params['order_type']
|
||||
]);
|
||||
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