From bda0bae1174b12fe4b8635428f7fb0efb7737d5f Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Fri, 1 Dec 2023 09:34:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9B=E9=94=80=E6=8A=BC=E9=87=91=E5=85=85?= =?UTF-8?q?=E5=80=BC=E6=94=AF=E4=BB=98=E5=92=8C=E6=94=AF=E4=BB=98=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E5=AF=B9=E6=8E=A5=E6=94=AF=E4=BB=98=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/PayController.php | 39 ++++++++++++------- app/common/logic/PayNotifyLogic.php | 32 ++++++++-------- app/common/service/pay/WeChatPayService.php | 42 +++------------------ 3 files changed, 48 insertions(+), 65 deletions(-) diff --git a/app/api/controller/PayController.php b/app/api/controller/PayController.php index 5cf4e8e84..5827cf5da 100755 --- a/app/api/controller/PayController.php +++ b/app/api/controller/PayController.php @@ -16,8 +16,11 @@ namespace app\api\controller; use app\api\validate\PayValidate; +use app\common\enum\PayEnum; use app\common\enum\user\UserTerminalEnum; use app\common\logic\PaymentLogic; +use app\common\logic\PayRequestLogic; +use app\common\model\Company; use app\common\service\pay\WeChatPayService; use think\facade\Log; @@ -62,13 +65,23 @@ class PayController extends BaseApiController if (false === $order) { return $this->fail(PaymentLogic::getError(), $params); } - //支付流程 - $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; - $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl); + // 请求支付系统 + $companyInfo = Company::where(['id' => $this->userInfo['company_id']])->find(); + $requestData = [ + 'street' => $companyInfo['street'], + 'order_from' => 12, + 'order_type' => 101, + 'pay_user_role' => $this->userInfo['group_id'], + 'pay_user_info' => $this->userInfo, + 'business_order_no' => $order['sn'], + 'total_fee' => intval(bcmul($order['order_amount'], 100)), + 'business_callback_url' => (string)url('pay/notifyApp', [], false, true) + ]; + $result = PayRequestLogic::getPrePayId($requestData); if (false === $result) { return $this->fail(PaymentLogic::getError(), $params); } - return $this->success('', $result); + return $this->success('', $result['data']); } @@ -122,18 +135,18 @@ class PayController extends BaseApiController /** - * @notes app支付回调 - * @return \Psr\Http\Message\ResponseInterface - * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException - * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException - * @throws \ReflectionException - * @throws \Throwable + * @notes 支付系统回调 * @date 2023/2/28 14:21 */ public function notifyApp() { - return (new WeChatPayService(UserTerminalEnum::ANDROID))->notify(); + try { + $param = $this->request->param(); + Log::info(['支付系统回调', $param]); + (new WeChatPayService(UserTerminalEnum::ANDROID))->notify($param); + return $this->success('ok'); + } catch (\Exception $e) { + return $this->fail($e->getMessage()); + } } - - } diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 272986af4..59e6ccb8b 100755 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -77,7 +77,7 @@ class PayNotifyLogic extends BaseLogic if($order['change_type']==300){ $company=Company::where('user_id',$order['user_id'])->find(); - $left_amount= $company['deposit']+$order->order_amount; + $left_amount= $company['deposit']+$order->order_amount; $datas = [ 'sn' => $order->sn, 'user_id' => $order->user_id, @@ -92,28 +92,28 @@ class PayNotifyLogic extends BaseLogic CompanyAccountLog::create($datas); Company::where('id',$company['id'])->update(['deposit'=>$left_amount]); }else{ - // 增加用户累计充值金额及用户余额 - $user = User::findOrEmpty($order->user_id); - $user->total_recharge_amount += $order->order_amount; - $user->user_money += $order->order_amount; - $user->save(); + // 增加用户累计充值金额及用户余额 + $user = User::findOrEmpty($order->user_id); + $user->total_recharge_amount += $order->order_amount; + $user->user_money += $order->order_amount; + $user->save(); - // 记录账户流水 - AccountLogLogic::add( - $order->user_id, - AccountLogEnum::UM_INC_RECHARGE, - AccountLogEnum::INC, - $order->order_amount, - $order->sn, - '用户充值' - ); + // 记录账户流水 + AccountLogLogic::add( + $order->user_id, + AccountLogEnum::UM_INC_RECHARGE, + AccountLogEnum::INC, + $order->order_amount, + $order->sn, + '用户充值' + ); } } // 更新充值订单状态 $order->transaction_id = $extra['transaction_id']; - $order->pay_status = PayEnum::ISPAID; + $order->pay_status = $extra['pay_status'];; $order->pay_time = time(); $order->save(); } diff --git a/app/common/service/pay/WeChatPayService.php b/app/common/service/pay/WeChatPayService.php index 0a8ff788f..1a628c559 100755 --- a/app/common/service/pay/WeChatPayService.php +++ b/app/common/service/pay/WeChatPayService.php @@ -354,44 +354,14 @@ class WeChatPayService extends BasePayService /** * @notes 支付回调 - * @return \Psr\Http\Message\ResponseInterface - * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException - * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException - * @throws \ReflectionException - * @throws \Throwable - * @author 段誉 - * @date 2023/2/28 14:20 */ - public function notify() + public function notify($param=[]) { - $server = $this->app->getServer(); - // 支付通知 - $server->handlePaid(function (Message $message) { - $data = ['trade_state' => $message['trade_state'] ?? '', 'out_trade_no' => $message['out_trade_no'] ?? '', 'transaction_id' => $message['transaction_id'] ?? '', 'attach' => $message['attach'] ?? '']; - Log::info('wechat pay notify: ' . var_export($data, true)); - if ($message['trade_state'] === 'SUCCESS') { - $extra['transaction_id'] = $message['transaction_id']; - $attach = $message['attach']; - $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18); - switch ($attach) { - case 'recharge': - $order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty(); - Log::info('wechat pay notify: ' . var_export($order, true)); - if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) { - return true; - } - PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra); - break; - } - } - return true; - }); - - // 退款通知 - $server->handleRefunded(function (Message $message) { - return true; - }); - return $server->serve(); + Log::info(['支付回调', $param]); + $extra['transaction_id'] = $param['transaction_id']; + $extra['pay_status'] = $param['pay_status']; + PayNotifyLogic::handle('recharge', $param['out_trade_no'], $extra); + return true; } public function configForPayment($prepayId, $appId)