From 97534ec292e02a2f09889824f81ca186c235f67c Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Tue, 21 Nov 2023 10:44:21 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=94=AF=E4=BB=98=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/logic/PaymentLogic.php | 14 +++++++ .../service/pay/WeChatPayMerchantService.php | 37 +++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app/common/logic/PaymentLogic.php b/app/common/logic/PaymentLogic.php index ed73572..2ca1faf 100644 --- a/app/common/logic/PaymentLogic.php +++ b/app/common/logic/PaymentLogic.php @@ -21,6 +21,7 @@ use app\common\model\pay\PayWay; use app\common\model\recharge\RechargeOrder; use app\common\model\user\User; use app\common\service\pay\WeChatPayService; +use Symfony\Component\HttpClient\HttpClient; /** @@ -231,4 +232,17 @@ class PaymentLogic extends BaseLogic return $orderSn . $terminal . $suffix; } + public static function callBusiness($notifyUrl,$param) + { + try { + $requestResponse = HttpClient::create()->request('POST', $notifyUrl, [ + 'body' => $param + ]); + return json_decode($requestResponse->getContent(), true); + } catch (Exception $e) { + self::setError($e->getMessage()); + return false; + } + } + } \ No newline at end of file diff --git a/app/common/service/pay/WeChatPayMerchantService.php b/app/common/service/pay/WeChatPayMerchantService.php index 153822f..e7997fb 100644 --- a/app/common/service/pay/WeChatPayMerchantService.php +++ b/app/common/service/pay/WeChatPayMerchantService.php @@ -19,6 +19,7 @@ namespace app\common\service\pay; use app\common\enum\PayEnum; use app\common\enum\PayOrderEnum; use app\common\enum\user\UserTerminalEnum; +use app\common\logic\PaymentLogic; use app\common\logic\PayNotifyLogic; use app\common\model\pay\PayConfig; use app\common\model\pay\PayOrder; @@ -28,6 +29,7 @@ use app\common\service\wechat\WeChatConfigService; use app\common\service\wechat\WeChatPayMerchantConfigService; use EasyWeChat\Pay\Application; use EasyWeChat\Pay\Message; +use think\Exception; use think\Log; @@ -199,16 +201,43 @@ class WeChatPayMerchantService extends BasePayService $server->handlePaid(function (Message $message) { Log::info(['支付回调信息', $message]); if ($message['trade_state'] === 'SUCCESS') { - $transaction_id = $message['transaction_id']; - $attach = $message['attach']; - $out_trade_no = $message['out_trade_no']; - + $this->handlePaid($message); + } else { + Log::info('支付失败', $message); } return true; }); return $server->serve(); } + /** + * @return void + * 修改支付状态,回写回调信息。 + * 通知业务系统 + */ + public function handlePaid($message) + { + try { + $transaction_id = $message['transaction_id']; + $out_trade_no = $message['out_trade_no']; + $order = PayOrder::where('order_no', $out_trade_no)->find(); + if (empty($order)) { + throw new Exception('回调订单不存在'); + } + $order->transaction_id = $transaction_id; + $order->pay_status = PayOrderEnum::PAY_STATUS_ISPAID; + $order->finish_time = time(); + $order->save(); + + // 回调业务系统 + PaymentLogic::callBusiness($order['business_callback_url'], ['out_trade_no'=>$order['business_order_no'], 'transaction_id'=>$transaction_id, 'pay_status'=>1]); + } catch (\Exception $e) { + Log::error(['支付回调信息处理失败', $e->getMessage(),$message]); + return; + } + + } + public function refundNotify() { $server = $this->app->getServer();