From 8e0ab3f46ce5c6c81b5bffcfa50e137f9636f420 Mon Sep 17 00:00:00 2001 From: ljl Date: Fri, 28 Apr 2023 11:42:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Payment/PageController.php | 3 ++ app/Helper/Platform/Notification.php | 27 +++++++++++++++++ app/Helper/Platform/Signer.php | 22 ++++++++++++++ app/Request/JsapiPayRequest.php | 2 +- app/Service/PaymentService.php | 37 +++++++++++++++++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/Helper/Platform/Notification.php create mode 100644 app/Helper/Platform/Signer.php diff --git a/app/Controller/Payment/PageController.php b/app/Controller/Payment/PageController.php index ae84f01..7f3d704 100644 --- a/app/Controller/Payment/PageController.php +++ b/app/Controller/Payment/PageController.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Controller\Payment; +use App\Helper\Log; use App\Model\Order; use Hyperf\HttpServer\Contract\ResponseInterface; use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface; @@ -14,6 +15,7 @@ class PageController extends AbstractController public function index(RequestInterface $request, ResponseInterface $response): Psr7ResponseInterface { $orderNo = $request->input('order_no', ''); + Log::info('redirectToOut orderNo:' . $orderNo, [], 'omipay'); if (!$orderNo) { return '订单号错误'; } @@ -21,6 +23,7 @@ class PageController extends AbstractController if (!$order) { return '订单号错误'; } + Log::info('redirectToOut url:' . $order->redirect_url, [], 'omipay'); return $response->redirect($order->redirect_url); } } diff --git a/app/Helper/Platform/Notification.php b/app/Helper/Platform/Notification.php new file mode 100644 index 0000000..2bf06e5 --- /dev/null +++ b/app/Helper/Platform/Notification.php @@ -0,0 +1,27 @@ + HandlerStack::create(new CoroutineHandler()), + 'timeout' => 5, + 'swoole' => [ + 'timeout' => 10, + 'socket_buffer_size' => 1024 * 1024 * 2, + ], + ]); + $response = $client->request('POST', $url, [ + 'json' => $params + ]); + return (string)$response->getBody(); + } +} \ No newline at end of file diff --git a/app/Helper/Platform/Signer.php b/app/Helper/Platform/Signer.php new file mode 100644 index 0000000..00e291e --- /dev/null +++ b/app/Helper/Platform/Signer.php @@ -0,0 +1,22 @@ + ['required', 'max:64'], 'out_order_no' => ['required', 'max:32'], diff --git a/app/Service/PaymentService.php b/app/Service/PaymentService.php index 86f30bd..b54229f 100644 --- a/app/Service/PaymentService.php +++ b/app/Service/PaymentService.php @@ -5,11 +5,15 @@ declare(strict_types=1); namespace App\Service; use App\Exception\BusinessException; +use App\Helper\Log; use App\Helper\OmiPay\Api; use App\Helper\OmiPay\Result; use App\Helper\OmiPay\Signer; +use App\Helper\Platform\Notification; +use App\Helper\Platform\Signer as PlatformSigner; use App\Helper\Redis; use App\Helper\RedisKey; +use App\Helper\StringHelper; use App\Model\App; use App\Model\Order; @@ -83,21 +87,54 @@ class PaymentService extends AbstractService } public function notify($params) { + Log::info('notify:', $params, 'omipay'); if (Signer::verify($params)) { return 'SIGN FAIL'; } if ($params['return_code'] != 'SUCCESS') { return 'STATUS FAIL'; } + $order = Order::where('order_no', $params['out_order_no'])->where('status', Order::STATUS_WAIT_PAY)->first(); + $app = App::where('app_id', $order->app_id)->first(); $order->status = Order::STATUS_PAYED; $order->payed_at = date('Y-m-d H:i:s'); $order->exchange_rate = $params['exchange_rate'] ?: 0; $order->cny_amount = $params['cny_amount'] ?: 0; $order->save(); if ($order->save()) { + Log::info('notifyToOut url:' . $order->notify_url, 'omipay'); + $result = Notification::post($order->notify_url, $this->buildNotifyParams($order, $app)); + if ($result != 'SUCCESS') { + return 'NOTIFY FAIL'; + } return 'SUCCESS'; } return 'UPDATE FAIL'; } + + protected function buildNotifyParams(Order $roder, App $app) { + $params = [ + 'app_id' => $order->app_id, + 'nonce_str' => StringHelper::getRandomString(32), + 'timestamp' => time(), + ]; + $bizData = [ + 'code' => 'SUCCESS', + 'order_no' => $roder->order_no, + 'out_order_no' => $roder->out_order_no, + 'currency' => $roder->currency, + 'total_amount' => $roder->amount, + 'order_time' => date('YmdHis', strtotime($roder->created_at)), + 'pay_time' => date('YmdHis', strtotime($roder->payed_at)), + 'exchange_rate' => $roder->exchange_rate, + 'cny_amount' => $roder->cny_amount, + ]; + $params['data'] = json_encode($bizData); + $params['sign'] = PlatformSigner::sign($params, $app->app_key); + + Log::info('notifyToOut params:', $params, 'omipay'); + + return $params; + } }