multi-store/app/common/logic/PayNotifyLogic.php
2024-06-04 17:21:57 +08:00

197 lines
6.4 KiB
PHP

<?php
namespace app\common\logic;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_order\StoreOrder;
use app\common\model\user\User;
use app\common\service\PushService;
use support\Log;
use think\facade\Db;
use Webman\RedisQueue\Redis;
/**
* 支付成功后处理订单状态
* Class PayNotifyLogic
* @package app\api\logic
*/
class PayNotifyLogic extends BaseLogic
{
public static function handle($action, $orderSn, $extra = [])
{
Db::startTrans();
try {
if ($action != 'cash_pay' && $action != 'balancePay') {
$payNotifyLogLogic = new PayNotifyLogLogic();
$payNotifyLogLogic->insert($action, $extra);
}
self::$action($orderSn, $extra);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
Log::error(implode('-', [
__CLASS__,
__FUNCTION__,
$e->getFile(),
$e->getLine(),
$e->getMessage()
]));
self::setError($e->getMessage());
return $e->getMessage();
}
}
/**
* 余额支付
* @param $orderSn
* @param $extra
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
static function balancePay($orderSn, $extra = [])
{
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
$user = User::where('id', $order['uid'])->find();
if ($user['now_money'] < $order['pay_price']) {
throw new \Exception('余额不足');
}
Db::startTrans();
try {
$order->money = $order['pay_price'];
$order->paid = 1;
$order->pay_time = time();
$order->save();
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order['pay_price']);
self::afterPay($order);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine());
self::setError('支付失败' . $e->getMessage());
return false;
}
}
/**
* @notes 微信通用回调
* @param $orderSn
* @param array $extra
* @date 2023/2/27 15:28
*/
public static function wechat_common($orderSn, $extra = [])
{
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
}
if ($order->pay_type != 10) {
$order->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->paid = 1;
$order->pay_time = time();
$order->status = 1;
$order->save();
} else {
$extra['transaction_id'] = time();
}
if ($order->pay_type == 9) {
$order->status = 2;
}
self::afterPay($order);
if ($order->pay_type == 9) {
$extra['create_time'] = $order['create_time'];
PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]);
} else {
PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']);
Redis::send('push-platform-print', ['order_id' => $order['id']], 60);
Db::name('order_middle')->insert(['c_order_id' => $order['id']]);
}
if (!empty($extra['payer']['openid']) && $order->pay_type != 9) {
Redis::send('push-delivery', ['order_sn' => $order['order_id'], 'openid' => $extra['payer']['openid']], 5);
}
return true;
}
/**
* 现金支付
*/
public static function cash_pay($orderSn)
{
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
}
$order->paid = 1;
$order->pay_time = time();
$order->status = 2;
$order->save();
self::afterPay($order);
}
/**
* @notes 零售回调
* @param $orderSn
* @param array $extra
* @author 段誉
* @date 2023/2/27 15:28
*/
public static function alipay_cashier($orderSn, $extra = [])
{
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
}
if ($order->pay_type != 10) {
$order->money = $extra['buyer_pay_amount'];
$order->paid = 1;
$order->pay_time = time();
$order->status = 1;
$order->save();
} else {
$extra['transaction_id'] = time();
}
if ($order->pay_type == 9) {
$order->status = 2;
}
self::afterPay($order);
if ($order->pay_type == 9) {
$extra['create_time'] = $order['create_time'];
PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]);
} else {
PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']);
Redis::send('push-platform-print', ['order_sn' => $order['order_id']], 60);
}
return true;
}
/**
* 支付后逻辑
* @param $order
* @return void
*/
public static function afterPay($order)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user = ['uid' => $order['uid']];
if ($order->pay_type != 9 || $order->pay_type != 10) {
$financeLogic->in($order['pay_price'], OrderEnum::USER_ORDER_PAY);
}
$financeLogic->out($order['pay_price'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0);
$financeLogic->save();
}
}