From cbc52952ae569fb8d35a1aa59b8b8067dfdfbb14 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Sat, 25 May 2024 11:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84Bug=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/IndexController.php | 23 +++--- app/api/controller/PayController.php | 68 +++++++++------- .../order/RetailOrderController.php | 24 ++++++ app/common/logic/PayNotifyLogic.php | 78 +++++++++++++++++++ app/common/logic/PaymentLogic.php | 38 ++++++++- 5 files changed, 191 insertions(+), 40 deletions(-) diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index 0480e38..687f861 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -29,17 +29,20 @@ class IndexController extends BaseApiController public function index() { - $auth_code=$this->request->get('auth_code'); - $config = Config::get('payment'); - Pay::config($config); + Redis::send('push-platform-print', ['order_id' => 39],5); - $result = Pay::alipay()->pos([ - 'out_trade_no' => time(), - 'auth_code' => $auth_code, - 'total_amount' => '0.01', - 'subject' => 'yansongda 测试 - 01', - ]); - d($result); + // $auth_code=$this->request->get('auth_code'); + // $config = Config::get('payment'); + // Pay::config($config); + + // $result = Pay::alipay()->pos([ + // 'out_trade_no' => time(), + // 'auth_code' => $auth_code, + // 'total_amount' => '0.01', + // 'subject' => 'yansongda 测试 - 01', + // 'extend_params'=>['attach'=>'cashierclass'] + // ]); + d(1); // $arr = []; // foreach ($a as $k => $v) { // $pid = Goodsclass::where('id', $v)->value('pid'); diff --git a/app/api/controller/PayController.php b/app/api/controller/PayController.php index ed32a0b..1422a10 100644 --- a/app/api/controller/PayController.php +++ b/app/api/controller/PayController.php @@ -16,17 +16,17 @@ use support\Log; class PayController extends BaseApiController { - public $notNeedLogin = ['notifyMnp','alipay_return','alipay_notify']; + public $notNeedLogin = ['notifyMnp', 'alipay_return', 'alipay_notify']; /** * @notes 小程序支付回调 */ public function notifyMnp() { - $app=new PayService(1); + $app = new PayService(1); $result = $app->wechat->callback(Request()->post()); - if($result && $result->event_type=='TRANSACTION.SUCCESS'){ - $ciphertext=$result->resource['ciphertext']; + if ($result && $result->event_type == 'TRANSACTION.SUCCESS') { + $ciphertext = $result->resource['ciphertext']; if ($ciphertext['trade_state'] === 'SUCCESS') { $extra['transaction_id'] = $ciphertext['transaction_id']; $attach = $ciphertext['attach']; @@ -43,18 +43,19 @@ class PayController extends BaseApiController /** * @notes 查询订单支付状态 */ - public function wechatQuery(){ - $order_no=$this->request->get('order_no'); + public function wechatQuery() + { + $order_no = $this->request->get('order_no'); $order = [ 'out_trade_no' => $order_no, ]; - $app=new PayService(0); + $app = new PayService(0); $res = $app->wechat->query($order); if ($res['trade_state'] == 'SUCCESS' && $res['trade_state_desc'] == '支付成功') { PayNotifyLogic::handle('cashierclass', $res['out_trade_no'], $res); return $this->success('支付成功'); - }else{ + } else { return $this->fail('订单支付中'); } } @@ -62,30 +63,43 @@ class PayController extends BaseApiController /** * 支付宝同步回调地址 */ - public function alipay_return(){ - $app=new PayService(); + public function alipay_return() + { + $app = new PayService(); $result = $app->alipay->callback(Request()->post()); - Log::error('支付宝同步回调',$result->toArray()); - // if($result && $result->event_type=='TRANSACTION.SUCCESS'){ - // $ciphertext=$result->resource['ciphertext']; - // if ($ciphertext['trade_state'] === 'SUCCESS') { - // $extra['transaction_id'] = $ciphertext['transaction_id']; - // $attach = $ciphertext['attach']; - // switch ($attach) { - // case 'cashierclass': - // PayNotifyLogic::handle('cashierclass', $ciphertext['out_trade_no'], $ciphertext); - // $app->wechat->success(); - // break; - // } - // } - // } + if ($result) { + $data = $result->toArray(); + if ($data['trade_status'] === 'TRADE_SUCCESS') { + $attach = $data['attach']; + switch ($attach) { + case 'alipay_cashier': + PayNotifyLogic::handle('alipay_cashier', $data['out_trade_no'], $data); + $app->alipay->success(); + break; + } + } + } } /** * 支付宝异步回调地址 */ - public function alipay_notify(){ - $app=new PayService(); + public function alipay_notify() + { + $app = new PayService(); $result = $app->alipay->callback(Request()->post()); - Log::error('支付宝异步回调',$result->toArray()); + $app = new PayService(); + $result = $app->alipay->callback(Request()->post()); + if ($result) { + $data = $result->toArray(); + if ($data['trade_status'] === 'TRADE_SUCCESS') { + $attach = $data['attach']; + switch ($attach) { + case 'alipay_cashier': + PayNotifyLogic::handle('alipay_cashier', $data['out_trade_no'], $data); + $app->alipay->success(); + break; + } + } + } } } diff --git a/app/api/controller/order/RetailOrderController.php b/app/api/controller/order/RetailOrderController.php index c13ed92..d89bf80 100644 --- a/app/api/controller/order/RetailOrderController.php +++ b/app/api/controller/order/RetailOrderController.php @@ -184,6 +184,18 @@ class RetailOrderController extends BaseApiController $result['create_time'] = $order['create_time']; return $this->success('', $result); break; + case PayEnum::ALIPAY_BARCODE: + //支付宝条码支付 + $result = PaymentLogic::ali_auth_code($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + if ($result['msg'] !== 'Success') { + return $this->success('用户支付中'); + } + $result['create_time'] = $order['create_time']; + return $this->success('', $result); + break; default: return $this->fail('支付方式错误'); } @@ -265,6 +277,18 @@ class RetailOrderController extends BaseApiController $result['create_time'] = $order['create_time']; return $this->success('', $result); break; + case PayEnum::ALIPAY_BARCODE: + //支付宝条码支付 + $result = PaymentLogic::ali_auth_code($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + if ($result['msg'] !== 'Success') { + return $this->success('用户支付中'); + } + $result['create_time'] = $order['create_time']; + return $this->success('', $result); + break; default: return $this->fail('支付方式错误'); } diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 256f644..c140e67 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -238,4 +238,82 @@ class PayNotifyLogic extends BaseLogic ]; (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['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]); + } + } + 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; + } } diff --git a/app/common/logic/PaymentLogic.php b/app/common/logic/PaymentLogic.php index 4cfdd71..0bbd268 100644 --- a/app/common/logic/PaymentLogic.php +++ b/app/common/logic/PaymentLogic.php @@ -94,9 +94,41 @@ class PaymentLogic extends BaseLogic try { $result = $wechat->wechat->pos($order)->toArray(); } catch (ExceptionException $e) { - if(getenv('APP_DEBUG')==true){ - self::$error=$e->extra['message']??$e->getMessage(); - }else{ + if (getenv('APP_DEBUG') == true) { + self::$error = $e->extra['message'] ?? $e->getMessage(); + } else { + self::$error = $e->getMessage(); + } + return false; + } + return $result; + } + + /** + * 支付宝条码支付 + */ + public static function ali_auth_code($auth_code, $order) + { + $pattern = '/^([25-30]{2})(\d{14,22})$/'; + + if (!preg_match($pattern, (string)$auth_code)) { + self::$error = '请使用正确的支付宝收付款条码'; + return false; + } + $order = [ + 'subject' => '条码商品', + 'out_trade_no' => $order['number'], + 'auth_code' => (string)$auth_code, + 'total_amount' => $order['actual'], + 'extend_params'=>['attach'=>'alipay_cashier'] + ]; + $wechat = new PayService(); + try { + $result = $wechat->alipay->pos($order)->toArray(); + } catch (ExceptionException $e) { + if (getenv('APP_DEBUG') == true) { + self::$error = $e->extra['message'] ?? $e->getMessage(); + } else { self::$error = $e->getMessage(); } return false;