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

219 lines
7.6 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 {
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;
}
$financial_type = OrderEnum::USER_ORDER_PAY;
$financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS;
if ($order->pay_type != 10) {
$order->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->paid = 1;
$order->status = 1;
$order->save();
} else {
$financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY;
$extra['transaction_id'] = time();
}
if ($order->pay_type == 9) {
$order->status = 2;
$financial_type2 = OrderEnum::CASHIER_ORDER_PAY;
}
if ($order->pay_type != 9 || $order->pay_type != 10) {
//用户支出流水
$record[] = [
'financial_record_sn' => $extra['transaction_id'],
'order_id' => $order['id'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type,
'financial_pm' => OrderEnum::EXPENDITURE,
'number' => $order['pay_price'],
'status' => 1,
'type' => OrderEnum::USER,
'store_id' => $order['store_id'],
'staff_id' => $order['staff_id'],
];
}
//商户获得流水
$record[] = [
'financial_record_sn' => $extra['transaction_id'],
'order_id' => $order['id'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type2,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['pay_price'],
'status' => 0,
'type' => OrderEnum::MERCHANT,
'store_id' => $order['store_id'],
'staff_id' => $order['staff_id'],
];
(new StoreFinanceFlow())->saveAll($record);
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;
}
$financial_type = OrderEnum::USER_ORDER_PAY;
$financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS;
if ($order->pay_type != 10) {
$order->money = $extra['buyer_pay_amount'];
$order->paid = 1;
$order->status = 1;
$order->save();
} else {
$financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY;
$extra['transaction_id'] = time();
}
if ($order->pay_type == 9) {
$order->status = 2;
$financial_type2 = OrderEnum::CASHIER_ORDER_PAY;
}
if ($order->pay_type != 9 || $order->pay_type != 10) {
//用户支出流水
$record[] = [
'financial_record_sn' => $extra['trade_no'],
'order_id' => $order['id'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type,
'financial_pm' => OrderEnum::EXPENDITURE,
'number' => $order['actual'],
'status' => 1,
'type' => OrderEnum::USER,
'mer_id' => $order['merchant'],
];
}
//商户获得流水
$record[] = [
'financial_record_sn' => $extra['trade_no'],
'order_id' => $order['id'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type2,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['actual'],
'status' => 0,
'type' => OrderEnum::MERCHANT,
'mer_id' => $order['merchant'],
];
(new StoreFinanceFlow())->saveAll($record);
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;
}
}