multi-store/app/common/logic/PayNotifyLogic.php

170 lines
5.8 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\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') {
$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();
}
}
/**
* @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->status = 1;
$order->save();
} else {
$extra['transaction_id'] = time();
}
if ($order->pay_type == 9) {
$order->status = 2;
}
$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();
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->status = 2;
$order->save();
//商户获得流水
$record[] = [
'financial_record_sn' => time(),
'order_id' => $order['id'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => OrderEnum::CASHIER_CASH_ORDER_PAY,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['pay_price'],
'status' => 1,
'type' => OrderEnum::MERCHANT,
'store_id' => $order['store_id'],
'staff_id' => $order['staff_id'],
];
(new StoreFinanceFlow())->saveAll($record);
}
/**
* @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->status = 1;
$order->save();
} else {
$extra['transaction_id'] = time();
}
if ($order->pay_type == 9) {
$order->status = 2;
}
$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();
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;
}
}