Files
anmo/server/app/coachapi/logic/PayLogic.php
2025-08-19 14:16:51 +08:00

245 lines
9.2 KiB
PHP
Executable File
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
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\coachapi\logic;
use app\common\enum\PayEnum;
use app\common\enum\user\UserTerminalEnum;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\logic\CoachPayNotifyLogic;
use app\common\model\deposit\DepositOrder;
use app\common\model\pay\PayWay;
use app\common\model\user\User;
use app\common\service\AliPayService;
use app\common\service\BalancePayService;
use app\common\service\CoachBalancePayService;
use app\common\service\ConfigService;
use app\common\service\WeChatPayService;
use app\common\service\WeChatService;
class PayLogic extends BaseLogic
{
/**
* @notes 支付方式
* @param $params
* @return array|false
* @author ljj
* @date 2022/2/28 2:56 下午
*/
public static function payWay($params)
{
try {
$order = [];
// 获取待支付金额
// if ($params['from'] == 'deposit') {
// // 订单
// $order = DepositOrder::findOrEmpty($params['order_id'])->toArray();
// }
//
// if (empty($order)) {
// throw new \Exception('订单不存在');
// }
// 获取订单剩余支付时间
// $cancelUnpaidOrders = ConfigService::get('transaction', 'cancel_unpaid_orders',1);
// $cancelUnpaidOrdersTimes = ConfigService::get('transaction', 'cancel_unpaid_orders_times',30);
// $cancelTime = 0;
// if(!in_array($params['from'],['deposit'])){
// if (empty($cancelUnpaidOrders)) {
// // 不自动取消待支付订单
// $cancelTime = 0;
// } else {
// // 指定时间内取消待支付订单
// $cancelTime = strtotime($order['create_time']) + intval($cancelUnpaidOrdersTimes) * 60;
// }
// }
$pay_way = PayWay::alias('pw')
->join('dev_pay dp', 'pw.pay_id = dp.id')
->where(['pw.scene'=>$params['scene'],'pw.status'=>YesNoEnum::YES])
->field('dp.id,dp.name,dp.pay_way,dp.image,pw.is_default')
->order(['sort'=>'asc','id'=>'desc'])
->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' => $params['user_id']])->value('user_money');
// $item['extra'] = '可用余额:'.$user_money;
// }
// 充值时去除余额支付
if ( $item['pay_way'] == PayEnum::BALANCE_PAY) {
unset($pay_way[$k]);
}
// 充值时去除微信支付
if ( $item['pay_way'] == PayEnum::WECHAT_PAY) {
unset($pay_way[$k]);
}
}
return [
'lists' => array_values($pay_way),
// 'order_amount' => $order['order_amount'],
// 'cancel_time' => $cancelTime,
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 支付
* @param $payWay // 支付方式
* @param $from //订单来源(商品订单?充值订单?其他订单?)
* @param $order_id //订单id
* @param $terminal //终端
* @param $wechatCode //终端
* @return array|bool|string|void
* @throws \Exception
* @author 段誉
* @date 2021/7/29 14:49
*/
public static function pay($payWay, $from, $order_id, $terminal,$wechatCode = '')
{
$order = [];
//更新支付方式
switch ($from) {
case 'deposit':
DepositOrder::update(['pay_way' => $payWay], ['id' => $order_id]);
$order = DepositOrder::where('id',$order_id)->findOrEmpty()->toArray();
break;
}
if (empty($order)) {
self::setError('订单错误');
}
if($order['order_amount'] == 0) {
CoachPayNotifyLogic::handle($from, $order['sn']);
return ['pay_way'=>$payWay];
}
switch ($payWay) {
case PayEnum::WECHAT_PAY:
if (isset($wechatCode) && $wechatCode != '') {
switch ($terminal) {
case UserTerminalEnum::WECHAT_MMP:
$response = (new WeChatService())->getMnpResByCode($wechatCode);
$order['openid'] = $response['openid'];
break;
case UserTerminalEnum::WECHAT_OA:
$response = (new WeChatService())->getOaResByCode($wechatCode);
$order['openid'] = $response['openid'];
break;
}
DepositOrder::update(['openid' => $order['openid']], ['id' => $order_id]);
}
$payService = (new WeChatPayService($terminal, null));
$result = $payService->pay($from, $order);
break;
case PayEnum::BALANCE_PAY:
//余额支付
$payService = (new CoachBalancePayService());
$result = $payService->pay($from, $order);
if (false !== $result) {
CoachPayNotifyLogic::handle($from, $order['sn']);
}
break;
case PayEnum::ALI_PAY:
$payService = (new AliPayService($terminal));
$result = $payService->pay($from, $order);
break;
default:
self::$error = '订单异常';
$result = false;
}
//支付成功, 执行支付回调
if (false === $result && !self::hasError()) {
self::setError($payService->getError());
}
return $result;
}
/**
* @notes 获取支付结果
* @param $params
* @return array
* @author ljj
* @date 2024/3/21 5:48 下午
*/
public static function getPayResult($params)
{
switch ($params['from']) {
case 'deposit' :
$result = DepositOrder::where(['id' => $params['order_id']])
->field(['id', 'sn', 'pay_time', 'pay_way', 'order_amount', 'pay_status'])
->findOrEmpty()
->toArray();
$result['total_amount'] = '¥' . $result['order_amount'];
break;
default :
$result = [];
}
if (empty($result)) {
self::$error = '订单信息不存在';
}
$result['pay_way_desc'] = PayEnum::getPayTypeDesc($result['pay_way']);
$result['pay_time'] = empty($result['pay_time']) ? '-' : date('Y-m-d H:i:s', $result['pay_time']);
return $result;
}
/**
* @notes 获取支付方式
* @param int $scene
* @param int $userId
* @return array
* @author cjhao
* @date 2024/10/11 22:49
*/
public static function getPayWayList(int $scene,int $userId){
$pay_way = PayWay::alias('pw')
->join('dev_pay dp', 'pw.pay_id = dp.id')
->where(['pw.scene'=>$scene,'pw.status'=>YesNoEnum::YES])
->field('dp.id,dp.name,dp.pay_way,dp.image,pw.is_default')
->order(['sort'=>'asc','id'=>'desc'])
->select()
->toArray();
foreach ($pay_way as $k=>&$item) {
if ($item['pay_way'] == PayEnum::WECHAT_PAY) {
$item['extra'] = '微信快捷支付';
}
// if ($item['pay_way'] == PayEnum::BALANCE_PAY) {
// $user_money = User::where(['id' => $userId])->value('user_money');
// $item['extra'] = '可用余额:'.$user_money;
// }
}
return $pay_way;
}
}