优化支付逻辑,增加现金支付方式,修复支付状态判断

This commit is contained in:
mkm 2024-05-24 11:31:54 +08:00
parent f9fa105be6
commit adab9b2702
4 changed files with 82 additions and 36 deletions

View File

@ -41,23 +41,23 @@ class RetailOrderController extends BaseApiController
$number = $this->request->get('number'); $number = $this->request->get('number');
$page_no = $this->request->get('page_no', 1); $page_no = $this->request->get('page_no', 1);
$date = $this->request->get('date', date('Y-m-d')); $date = $this->request->get('date', date('Y-m-d'));
if(!$this->userInfo['merchant']['mer_id']){ if (!$this->userInfo['merchant']['mer_id']) {
return $this->fail('没有权限'); return $this->fail('没有权限');
} }
$where=[]; $where = [];
if($number){ if ($number) {
$where[] = ['number', 'like', '%' . $number . '%']; $where[] = ['number', 'like', '%' . $number . '%'];
} }
$where[] = ['merchant', '=',$this->userInfo['merchant']['mer_id']]; $where[] = ['merchant', '=', $this->userInfo['merchant']['mer_id']];
$where[] = ['paid', '=',1]; $where[] = ['paid', '=', 1];
$where[] = ['pay_type', '<>',9]; $where[] = ['pay_type', '<>', 9];
$where[] = ['is_opurchase', '=',0]; $where[] = ['is_opurchase', '=', 0];
$res = Cashierclass::where($where)->page($page_no, 25)->whereDay('create_time', $date) $res = Cashierclass::where($where)->page($page_no, 25)->whereDay('create_time', $date)
->order('address_id asc,id desc') ->order('address_id asc,id desc')
->select()->each(function($item){ ->select()->each(function ($item) {
$item['goods_list']=Cashierinfo::where('pid',$item['id'])->with('goodsName')->field('goods,nums,price sell')->limit(5)->select(); $item['goods_list'] = Cashierinfo::where('pid', $item['id'])->with('goodsName')->field('goods,nums,price sell')->limit(5)->select();
$item['goods_count']=count(explode(',',$item['cart_id'])); $item['goods_count'] = count(explode(',', $item['cart_id']));
}); });
$data['count'] = Cashierclass::where($where)->whereDay('create_time', $date)->count(); $data['count'] = Cashierclass::where($where)->whereDay('create_time', $date)->count();
$data['lists'] = $res?->toArray(); $data['lists'] = $res?->toArray();
$data['page_no'] = $page_no; $data['page_no'] = $page_no;
@ -70,10 +70,10 @@ class RetailOrderController extends BaseApiController
public function merchant_order_count() public function merchant_order_count()
{ {
$date = $this->request->get('date', date('Y-m-d')); $date = $this->request->get('date', date('Y-m-d'));
$where[] = ['merchant', '=',$this->userInfo['merchant']['mer_id']]; $where[] = ['merchant', '=', $this->userInfo['merchant']['mer_id']];
$where[] = ['paid', '=',1]; $where[] = ['paid', '=', 1];
$where[] = ['pay_type', '<>',9]; $where[] = ['pay_type', '<>', 9];
$where[] = ['is_opurchase', '=',0]; $where[] = ['is_opurchase', '=', 0];
$res = Cashierclass::where($where)->whereDay('create_time', $date)->count(); $res = Cashierclass::where($where)->whereDay('create_time', $date)->count();
return $this->success('ok', ['order_count' => $res]); return $this->success('ok', ['order_count' => $res]);
} }
@ -124,19 +124,19 @@ class RetailOrderController extends BaseApiController
$addressId = (int)$this->request->post('address_id'); $addressId = (int)$this->request->post('address_id');
$auth_code = $this->request->post('auth_code'); //微信支付条码 $auth_code = $this->request->post('auth_code'); //微信支付条码
$params = $this->request->post(); $params = $this->request->post();
if ($mer_id <= 0 && $pay_type!=9) { if ($mer_id <= 0 && $pay_type != 9) {
return $this->fail('自提点不能为空'); return $this->fail('自提点不能为空');
} }
if (count($cartId) > 100) { if (count($cartId) > 100) {
return $this->fail('购物车商品不能超过100个'); return $this->fail('购物车商品不能超过100个');
} }
if($pay_type==9){ if ($pay_type == 9) {
if(empty($this->request->userInfo['merchant'])){ if (empty($this->request->userInfo['merchant'])) {
return $this->fail('请先绑定商户'); return $this->fail('请先绑定商户');
} }
$mer_id=$this->request->userInfo['merchant']['mer_id']; $mer_id = $this->request->userInfo['merchant']['mer_id'];
$params['mer_id']=$mer_id; $params['mer_id'] = $mer_id;
} }
$order = OrderLogic::createOrder($cartId, $addressId, null, $params); $order = OrderLogic::createOrder($cartId, $addressId, null, $params);
if ($order != false) { if ($order != false) {
@ -146,7 +146,7 @@ class RetailOrderController extends BaseApiController
$user = User::where('id', $this->request->userId)->find(); $user = User::where('id', $this->request->userId)->find();
$res = RetailOrderLogic::payBalance($user, $order); $res = RetailOrderLogic::payBalance($user, $order);
if (RetailOrderLogic::hasError()) { if (RetailOrderLogic::hasError()) {
return $this->fail(RetailOrderLogic::getError()); return $this->fail(RetailOrderLogic::getError());
} else { } else {
$res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]); $res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]);
if (RetailOrderLogic::hasError()) { if (RetailOrderLogic::hasError()) {
@ -155,6 +155,11 @@ class RetailOrderController extends BaseApiController
return $this->success('余额支付成功'); return $this->success('余额支付成功');
} }
break; break;
case PayEnum::CASH_PAY:
//现金支付
PayNotifyLogic::handle('cash_pay', $order['number']);
return $this->success('余额支付成功');
break;
case PayEnum::WECHAT_PAY: case PayEnum::WECHAT_PAY:
//微信支付 //微信支付
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
@ -170,13 +175,13 @@ class RetailOrderController extends BaseApiController
if (PaymentLogic::hasError()) { if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params); return $this->fail(PaymentLogic::getError(), $params);
} }
if(isset($result['trade_state_desc']) && $result['trade_state_desc']=='支付成功'){ if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('cashierclass', $result['out_trade_no'], $result); PayNotifyLogic::handle('cashierclass', $result['out_trade_no'], $result);
}else{ } else {
Redis::send('send-code-pay', ['number'=>$order['number']]); Redis::send('send-code-pay', ['number' => $order['number']]);
return $this->success('用户支付中'); return $this->success('用户支付中');
} }
$result['create_time']=$order['create_time']; $result['create_time'] = $order['create_time'];
return $this->success('', $result); return $this->success('', $result);
break; break;
default: default:
@ -204,7 +209,7 @@ class RetailOrderController extends BaseApiController
'paid' => 0, 'paid' => 0,
]; ];
$order = Cashierclass::where($where)->find(); $order = Cashierclass::where($where)->find();
if(!$order) return $this->fail('订单不存在或已支付'); if (!$order) return $this->fail('订单不存在或已支付');
switch ($pay_type) { switch ($pay_type) {
case PayEnum::BALANCE_PAY: case PayEnum::BALANCE_PAY:
@ -213,11 +218,11 @@ class RetailOrderController extends BaseApiController
$res = RetailOrderLogic::payBalance($user, $order); $res = RetailOrderLogic::payBalance($user, $order);
if (!RetailOrderLogic::hasError()) { if (!RetailOrderLogic::hasError()) {
$res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]); $res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]);
if(RetailOrderLogic::hasError()){ if (RetailOrderLogic::hasError()) {
return $this->fail(RetailOrderLogic::getError()); return $this->fail(RetailOrderLogic::getError());
} }
return $this->success('余额支付成功'); return $this->success('余额支付成功');
}else{ } else {
return $this->fail(RetailOrderLogic::getError()); return $this->fail(RetailOrderLogic::getError());
} }
break; break;
@ -246,13 +251,13 @@ class RetailOrderController extends BaseApiController
if (PaymentLogic::hasError()) { if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params); return $this->fail(PaymentLogic::getError(), $params);
} }
if(isset($result['trade_state_desc']) && $result['trade_state_desc']=='支付成功'){ if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('cashierclass', $result['out_trade_no'], $result); PayNotifyLogic::handle('cashierclass', $result['out_trade_no'], $result);
}else{ } else {
Redis::send('send-code-pay', ['number'=>$order['number']]); Redis::send('send-code-pay', ['number' => $order['number']]);
return $this->success('用户支付中'); return $this->success('用户支付中');
} }
$result['create_time']=$order['create_time']; $result['create_time'] = $order['create_time'];
return $this->success('', $result); return $this->success('', $result);
break; break;
default: default:

View File

@ -102,6 +102,9 @@ class OrderLogic extends BaseLogic
if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){ if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){
$_order['source']=1; $_order['source']=1;
} }
if($params['pay_type']==PayEnum::CASH_PAY){
$_order['money']=$_order['total'];
}
Db::startTrans(); Db::startTrans();
try { try {
$order = Cashierclass::create($_order); $order = Cashierclass::create($_order);

View File

@ -30,6 +30,7 @@ class PayEnum
* @ALIPAY_QRCODE 支付宝二维码 * @ALIPAY_QRCODE 支付宝二维码
* @GOODS_FIRST_PAYMENT_LATER 先货后款 * @GOODS_FIRST_PAYMENT_LATER 先货后款
* @CORPORATE_TRANSFER 对公转账 * @CORPORATE_TRANSFER 对公转账
* @CASH_PAY 现金支付
*/ */
const BALANCE_PAY = 3; const BALANCE_PAY = 3;
const WECHAT_PAY = 1; const WECHAT_PAY = 1;
@ -47,6 +48,7 @@ class PayEnum
const ALIPAY_QRCODE = 14; const ALIPAY_QRCODE = 14;
const GOODS_FIRST_PAYMENT_LATER = 15; const GOODS_FIRST_PAYMENT_LATER = 15;
const CORPORATE_TRANSFER = 16; const CORPORATE_TRANSFER = 16;
const CASH_PAY = 17;
//支付状态 //支付状态
const UNPAID = 0; //未支付 const UNPAID = 0; //未支付

View File

@ -63,14 +63,14 @@ class PayNotifyLogic extends BaseLogic
} }
$financial_type = OrderEnum::USER_ORDER_PAY; $financial_type = OrderEnum::USER_ORDER_PAY;
$financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS; $financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS;
if($order->pay_type !=10){ if ($order->pay_type != 10) {
$order->money = bcdiv($extra['amount']['payer_total'], 100, 2); $order->money = bcdiv($extra['amount']['payer_total'], 100, 2);
$order->paid = 1; $order->paid = 1;
$order->status = 1; $order->status = 1;
$order->save(); $order->save();
}else{ } else {
$financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY; $financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY;
$extra['transaction_id']=time(); $extra['transaction_id'] = time();
} }
if ($order->pay_type == 9) { if ($order->pay_type == 9) {
$order->status = 2; $order->status = 2;
@ -202,4 +202,40 @@ class PayNotifyLogic extends BaseLogic
// ]; // ];
(new FinancialRecord())->saveAll($record); (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();
if ($order['cart_id']) {
if (!is_array($order['cart_id'])) {
$cart_arr = explode(',', $order['cart_id']);
Cart::whereIn('cart_id', $cart_arr)->update(['is_pay' => 1]);
} else {
Cart::whereIn('cart_id', $order['cart_id'])->update(['is_pay' => 1]);
}
}
//商户获得流水
$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);
}
} }