erp/app/common/logic/PayNotifyLogic.php

299 lines
11 KiB
PHP

<?php
namespace app\common\logic;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\enum\user\AccountLogEnum;
use app\common\model\operation\Opurchaseclass;
use app\common\model\order\Cart;
use app\common\model\order\FinancialRecord;
use app\common\model\recharge\RechargeOrder;
use app\common\model\retail\Cashierclass;
use app\common\model\user\User;
use app\common\service\PushService;
use app\common\service\wechat\WeChatMnpService;
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
* @author 段誉
* @date 2023/2/27 15:28
*/
public static function cashierclass($orderSn, $extra = [])
{
$order = Cashierclass::where('number', $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 = 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'],
'number_sn' => $order['number'],
'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['transaction_id'],
'order_id' => $order['id'],
'number_sn' => $order['number'],
'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 FinancialRecord())->saveAll($record);
if ($order->pay_type == 9) {
$extra['create_time'] = $order['create_time'];
PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]);
} else {
PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['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_id' => $orderSn, 'openid' => $extra['payer']['openid']], 5);
}
return true;
}
/**
* 采购订单支付成功
*/
public static function opurchaseclass($orderSn, $extra = [])
{
$order = Opurchaseclass::where('number', $orderSn)->findOrEmpty();
$order_arr = explode(',', $order['order_arr']);
(new FinancialRecord())->where('order_id', 'in', $order_arr)->update(['status' => 1]);
Db::name('order_middle')->where('c_order_id', 'in', $order_arr)->update(['b_order_id' => $order['id']]);
$time = time();
//商户支出流水
$record[] = [
'financial_record_sn' => $time,
'order_id' => $order['id'],
'number_sn' => $order['number'],
'user_id' => $order['uid'],
'financial_type' => OrderEnum::MERCHANT_ORDER_PAY,
'financial_pm' => OrderEnum::EXPENDITURE,
'number' => $order['actual'],
'status' => 1,
'type' => OrderEnum::MERCHANT,
'mer_id' => $order['merchant'],
];
//平台获得流水
$record[] = [
'financial_record_sn' => $time,
'order_id' => $order['id'],
'number_sn' => $order['number'],
'user_id' => $order['uid'],
'financial_type' => OrderEnum::PLATFORM_ORDER_OBTAINS,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['actual'],
'status' => 1,
'type' => OrderEnum::PLATFORM,
'mer_id' => $order['merchant'],
];
(new FinancialRecord())->saveAll($record);
}
/**
* 平台采购订单支付成功
*/
public static function operated($orderSn, $extra = [])
{
$order = Opurchaseclass::where('number', $orderSn)->findOrEmpty();
$order_arr = explode(',', $order['order_arr']);
(new FinancialRecord())->where('order_id', 'in', $order_arr)->update(['status' => 1]);
Db::name('order_middle')->where('b_order_id', 'in', $order_arr)->update(['p_order_id' => $order['id']]);
$time = time();
//平台支出流水
$record[] = [
'financial_record_sn' => $time,
'order_id' => $order['id'],
'number_sn' => $order['number'],
'user_id' => $order['uid'],
'financial_type' => OrderEnum::PLATFORM_ORDER_PAY,
'financial_pm' => OrderEnum::EXPENDITURE,
'number' => $order['actual'],
'status' => 1,
'type' => OrderEnum::PLATFORM,
'mer_id' => getenv('OPERATED'),
];
// //平台获得流水
// $record[] = [
// 'financial_record_sn' => $time,
// 'order_id' => $order['id'],
// 'number_sn' => $order['number'],
// 'user_id' => $order['uid'],
// 'financial_type' => OrderEnum::PLATFORM_ORDER_OBTAINS,
// 'financial_pm' => OrderEnum::INCOME,
// 'number' => $order['actual'],
// 'status' => 1,
// 'type' => OrderEnum::PLATFORM,
// 'mer_id' => $order['merchant'],
// ];
(new FinancialRecord())->saveAll($record);
}
/**
* 现金支付
*/
public static function cash_pay($orderSn)
{
$order = Cashierclass::where('number', $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'],
'number_sn' => $order['number'],
'user_id' => $order['uid'],
'financial_type' => OrderEnum::CASHIER_CASH_ORDER_PAY,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['actual'],
'status' => 1,
'type' => OrderEnum::MERCHANT,
'mer_id' => $order['merchant'],
];
(new FinancialRecord())->saveAll($record);
}
/**
* @notes 零售回调
* @param $orderSn
* @param array $extra
* @author 段誉
* @date 2023/2/27 15:28
*/
public static function alipay_cashier($orderSn, $extra = [])
{
$order = Cashierclass::where('number', $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'],
'number_sn' => $order['number'],
'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'],
'number_sn' => $order['number'],
'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 FinancialRecord())->saveAll($record);
if ($order->pay_type == 9) {
$extra['create_time'] = $order['create_time'];
PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]);
} else {
PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']);
Redis::send('push-platform-print', ['order_id' => $order['id']], 60);
}
return true;
}
}