From f80b66f3baa29e3f57c73e3bfb96303889b615b4 Mon Sep 17 00:00:00 2001 From: elf <360197197@qq.com> Date: Fri, 28 Apr 2023 02:17:08 +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/Command/JinlingCommand.php | 2 +- app/Helper/OmiPay/AbstractApi.php | 5 +++++ app/Request/JsapiPayRequest.php | 3 ++- app/Service/PaymentService.php | 20 +++++++++++++------- config/config.php | 1 + config/routes.php | 2 +- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/Command/JinlingCommand.php b/app/Command/JinlingCommand.php index 31b1740..5690bde 100644 --- a/app/Command/JinlingCommand.php +++ b/app/Command/JinlingCommand.php @@ -68,7 +68,7 @@ class JinlingCommand extends HyperfCommand $data = [ 'order_name' => '测试订单', 'currency' => 'CNY', - 'amount' => '20', + 'amount' => '2000', 'notify_url' => 'http://www.baidu.com', 'redirect_url' => 'http://www.google.com', 'out_order_no' => '1122', diff --git a/app/Helper/OmiPay/AbstractApi.php b/app/Helper/OmiPay/AbstractApi.php index f3e78f6..59fc511 100644 --- a/app/Helper/OmiPay/AbstractApi.php +++ b/app/Helper/OmiPay/AbstractApi.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Helper\OmiPay; +use App\Helper\Log; use App\Helper\OmiPay\Request\AbstractRequest; use App\Helper\StringHelper; use App\Helper\TimeHelper; @@ -30,10 +31,13 @@ abstract class AbstractApi $params = self::getCommonParams(); $params = array_merge($params, $request->getParams()); try { + Log::info('url:' . $request->getUrl(), [], 'omipay'); $response = self::getClient()->post($request->getUrl(), [ 'query' => $params ]); + Log::info('request:', $params, 'omipay'); $body = (string)$response->getBody(); + Log::info('response:' . $body, [], 'omipay'); $result = json_decode($body, true); if (empty($result)) { $result = [ @@ -44,6 +48,7 @@ abstract class AbstractApi } return new Result($result); } catch (Exception $e) { + Log::error('error:' . $e->getMessage(), [], 'omipay'); return new Result([ 'return_code' => 'FAIL', 'error_code' => 'NETWORK_ERROR', diff --git a/app/Request/JsapiPayRequest.php b/app/Request/JsapiPayRequest.php index e621f20..ec3b31f 100644 --- a/app/Request/JsapiPayRequest.php +++ b/app/Request/JsapiPayRequest.php @@ -10,10 +10,11 @@ class JsapiPayRequest extends ApiRequest { public function rules(): array { + $amountList = [500, 1000, 2000, 3000, 5000, 8000, 9800, 19800, 29800, 39800, 49800, 59800, 69800, 79800, 89800, 99800]; return [ 'order_name' => ['required', 'max:64'], 'out_order_no' => ['required', 'max:32'], - 'amount' => ['required', Rule::in([5,10,20,30,50,80,98,198,298,398,498,598,698,798,898,998])], + 'amount' => ['required', Rule::in($amountList)], 'currency' => ['required', Rule::in(['CNY'])], 'show_pc_pay_url' => ['integer', Rule::in([1, 2])], 'direct_pay' => ['integer', Rule::in([1, 2])], diff --git a/app/Service/PaymentService.php b/app/Service/PaymentService.php index ee6200a..86f30bd 100644 --- a/app/Service/PaymentService.php +++ b/app/Service/PaymentService.php @@ -21,6 +21,7 @@ class PaymentService extends AbstractService $order->app_id = $app->app_id; $order->order_no = $this->generateOrderNo(); $order->out_order_no = $params['out_order_no']; + $order->order_name = $params['order_name']; $order->amount = $params['amount']; $order->currency = $params['currency']; $order->redirect_url = $params['redirect_url']; @@ -64,13 +65,18 @@ class PaymentService extends AbstractService throw new BusinessException('订单重复'); } $order = $this->createOrder($app, $params); + $result = Api::getExchangeRate('AUD', 'CNY'); + if (!$result->isSuccess()) { + throw new BusinessException('获取汇率接口失败'); + } + $rate = $result->get('rate'); $result = Api::makeJSAPIOrder( $order->order_name, $order->order_no, - $order->currency, - $order->amount, - 'http://146.70.113.165:9501/payment/notify', - 'http://146.70.113.165:9501/payment/page?order_no=' . $order->order_no + 'AUD', + intval($order->amount / $rate), + config('base_url') . '/payment/notify', + config('base_url') . '/payment/page?order_no=' . $order->order_no ); $this->updateOrderResult($order, $result); return $order; @@ -85,9 +91,9 @@ class PaymentService extends AbstractService } $order = Order::where('order_no', $params['out_order_no'])->where('status', Order::STATUS_WAIT_PAY)->first(); $order->status = Order::STATUS_PAYED; - $order->payed_at = date('Y-m-d'); - $order->exchange_rate = $params['exchange_rate']; - $order->cny_amount = $params['cny_amount']; + $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()) { return 'SUCCESS'; diff --git a/config/config.php b/config/config.php index 38823e0..2d59b07 100644 --- a/config/config.php +++ b/config/config.php @@ -16,6 +16,7 @@ return [ 'app_name' => env('APP_NAME', 'skeleton'), 'app_env' => env('APP_ENV', 'dev'), 'scan_cacheable' => env('SCAN_CACHEABLE', false), + 'base_url' => env('BASE_URL'), StdoutLoggerInterface::class => [ 'log_level' => [ LogLevel::ALERT, diff --git a/config/routes.php b/config/routes.php index b714c23..9175e65 100644 --- a/config/routes.php +++ b/config/routes.php @@ -12,6 +12,6 @@ Router::get('/favicon.ico', function () { Router::addGroup('/payment',function () { Router::post('/jsapi', [PayController::class, 'jsapi']); - Router::post('/page', [PageController::class, 'index']); + Router::get('/page', [PageController::class, 'index']); Router::post('/notify', [PayController::class, 'index']); });