Files
2026-03-14 16:20:49 +08:00

603 lines
22 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
}
}