feat(PayController): 修改支付控制器以处理微信通用回调

This commit is contained in:
mkm 2024-06-03 16:09:06 +08:00
parent 8976b59d1f
commit 5d24bbe2a0
4 changed files with 141 additions and 107 deletions

View File

@ -31,8 +31,8 @@ class PayController extends BaseApiController
$extra['transaction_id'] = $ciphertext['transaction_id'];
$attach = $ciphertext['attach'];
switch ($attach) {
case 'cashierclass':
PayNotifyLogic::handle('cashierclass', $ciphertext['out_trade_no'], $ciphertext);
case 'wechat_common':
PayNotifyLogic::handle('wechat_common', $ciphertext['out_trade_no'], $ciphertext);
$app->wechat->success();
break;
}
@ -53,7 +53,7 @@ class PayController extends BaseApiController
$res = $app->wechat->query($order);
if ($res['trade_state'] == 'SUCCESS' && $res['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('cashierclass', $res['out_trade_no'], $res);
PayNotifyLogic::handle('wechat_common', $res['out_trade_no'], $res);
return $this->success('支付成功');
} else {
return $this->fail('订单支付中');

View File

@ -0,0 +1,84 @@
<?php
namespace app\common\enum;
class OrderEnum
{
/**
* 流水类型
* @USER_ORDER_PAY 用户订单支付
* @CASHIER_ORDER_PAY 收银台支付
* @CASHIER_CASH_ORDER_PAY 收银台现金支付
* @MERCHANT_ORDER_OBTAINS 商户订单获得
* @ORDER_HANDLING_FEES 订单手续费
* @MERCHANT_ORDER_PAY 商户订单支付
* @PLATFORM_ORDER_OBTAINS 平台订单获得
* @SUPPLIER_ORDER_OBTAINS 供应链订单获得
* @PLATFORM_ORDER_PAY 平台订单支付
* @SYSTEM_SET 系统设置
*/
const USER_ORDER_PAY = 1;
const MERCHANT_ORDER_OBTAINS = 2;
const ORDER_HANDLING_FEES = 3;
const MERCHANT_ORDER_PAY = 4;
const PLATFORM_ORDER_OBTAINS = 5;
const SUPPLIER_ORDER_OBTAINS = 6;
const PLATFORM_ORDER_PAY = 7;
const SYSTEM_SET = 8;
const CASHIER_ORDER_PAY = 9;
const CASHIER_CASH_ORDER_PAY = 10;
/**
* 收入支出类型
* @EXPENDITURE 支出
* @INCOME 收入
*/
const EXPENDITURE =0;
const INCOME =1;
/**
* 账户类型
* @USER 用户
* @MERCHANT 商户
* @PLATFORM 平台
* @SUPPLIER 供应链
* @SYSTEM 系统
*/
const USER =0;
const MERCHANT =1;
const PLATFORM =2;
const SUPPLIER =3;
const SYSTEM=4;
/**
* @notes 获取支付类型
* @param bool $value
* @return string|string[]
* @author 段誉
* @date 2023/2/23 15:36
*/
public static function getFinancialType($value = true)
{
$data = [
self::USER_ORDER_PAY=>'用户订单支付',
self::MERCHANT_ORDER_PAY=>'商户订单支付',
self::PLATFORM_ORDER_PAY=>'平台订单支付',
self::MERCHANT_ORDER_OBTAINS=>'商户订单获得',
self::ORDER_HANDLING_FEES=>'订单手续费',
self::PLATFORM_ORDER_OBTAINS=>'平台订单获得',
self::SUPPLIER_ORDER_OBTAINS=>'供应商订单获得',
self::SYSTEM_SET=>'平台设置',
];
if ($value === true) {
return $data;
}
return $data[$value] ?? '';
}
}

View File

@ -7,9 +7,11 @@ 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\financial_record\FinancialRecord;
use app\common\model\recharge\RechargeOrder;
use app\common\model\retail\Cashierclass;
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 app\common\service\wechat\WeChatMnpService;
@ -48,15 +50,14 @@ class PayNotifyLogic extends BaseLogic
/**
* @notes 零售回调
* @notes 微信通用回调
* @param $orderSn
* @param array $extra
* @author 段誉
* @date 2023/2/27 15:28
*/
public static function cashierclass($orderSn, $extra = [])
public static function wechat_common($orderSn, $extra = [])
{
$order = Cashierclass::where('number', $orderSn)->findOrEmpty();
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
@ -64,7 +65,7 @@ class PayNotifyLogic extends BaseLogic
$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->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->paid = 1;
$order->status = 1;
$order->save();
@ -81,128 +82,54 @@ class PayNotifyLogic extends BaseLogic
$record[] = [
'financial_record_sn' => $extra['transaction_id'],
'order_id' => $order['id'],
'number_sn' => $order['number'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type,
'financial_pm' => OrderEnum::EXPENDITURE,
'number' => $order['actual'],
'number' => $order['pay_price'],
'status' => 1,
'type' => OrderEnum::USER,
'mer_id' => $order['merchant'],
'store_id' => $order['store_id'],
'staff_id' => $order['staff_id'],
];
}
//商户获得流水
$record[] = [
'financial_record_sn' => $extra['transaction_id'],
'order_id' => $order['id'],
'number_sn' => $order['number'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type2,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['actual'],
'number' => $order['pay_price'],
'status' => 0,
'type' => OrderEnum::MERCHANT,
'mer_id' => $order['merchant'],
'store_id' => $order['store_id'],
'staff_id' => $order['staff_id'],
];
(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]);
PushService::push('store_merchant_' . $order['id'], $order['id'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]);
} else {
PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']);
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']]);
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);
Redis::send('push-delivery', ['order_sn' => $order['order_id'], '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();
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
@ -214,19 +141,20 @@ class PayNotifyLogic extends BaseLogic
$record[] = [
'financial_record_sn' => time(),
'order_id' => $order['id'],
'number_sn' => $order['number'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => OrderEnum::CASHIER_CASH_ORDER_PAY,
'financial_pm' => OrderEnum::INCOME,
'number' => $order['actual'],
'number' => $order['pay_price'],
'status' => 1,
'type' => OrderEnum::MERCHANT,
'mer_id' => $order['merchant'],
'store_id' => $order['store_id'],
'staff_id' => $order['staff_id'],
];
(new FinancialRecord())->saveAll($record);
}
/**
/**
* @notes 零售回调
* @param $orderSn
* @param array $extra
@ -235,7 +163,7 @@ class PayNotifyLogic extends BaseLogic
*/
public static function alipay_cashier($orderSn, $extra = [])
{
$order = Cashierclass::where('number', $orderSn)->findOrEmpty();
$order = StoreOrder::where('order_id', $orderSn)->findOrEmpty();
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
return true;
@ -260,7 +188,7 @@ class PayNotifyLogic extends BaseLogic
$record[] = [
'financial_record_sn' => $extra['trade_no'],
'order_id' => $order['id'],
'number_sn' => $order['number'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type,
'financial_pm' => OrderEnum::EXPENDITURE,
@ -274,7 +202,7 @@ class PayNotifyLogic extends BaseLogic
$record[] = [
'financial_record_sn' => $extra['trade_no'],
'order_id' => $order['id'],
'number_sn' => $order['number'],
'order_sn' => $order['order_id'],
'user_id' => $order['uid'],
'financial_type' => $financial_type2,
'financial_pm' => OrderEnum::INCOME,
@ -288,10 +216,10 @@ class PayNotifyLogic extends BaseLogic
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]);
PushService::push('store_merchant_' . $order['id'], $order['id'], ['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);
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;
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\common\model\financial_record;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 财务流水模型
* Class FinancialRecord
* @package app\common\model\financial_record
*/
class FinancialRecord extends BaseModel
{
use SoftDelete;
protected $name = 'financial_record';
protected $deleteTime = 'delete_time';
}