From 32c3747391c7e12a4a99bc15936dbd2d7a60d495 Mon Sep 17 00:00:00 2001 From: ZBrettonYe Date: Mon, 4 Jan 2021 17:38:22 +0000 Subject: [PATCH] Clean code --- app/Http/Controllers/Gateway/PayBeaver.php | 76 +++++++++---------- app/Http/Controllers/PaymentController.php | 4 +- app/Models/Order.php | 5 -- composer.lock | 56 +++++++------- .../2021_01_04_094946_drop_node_ping.php | 6 +- ...021_01_04_172833_add-paybeaver-payment.php | 2 +- resources/views/admin/config/system.blade.php | 2 +- 7 files changed, 72 insertions(+), 79 deletions(-) diff --git a/app/Http/Controllers/Gateway/PayBeaver.php b/app/Http/Controllers/Gateway/PayBeaver.php index 89640eb1..446d0980 100644 --- a/app/Http/Controllers/Gateway/PayBeaver.php +++ b/app/Http/Controllers/Gateway/PayBeaver.php @@ -2,17 +2,16 @@ /** * Created by PayBeaver - * Version: 2020-12-06 + * Version: 2020-12-06. */ namespace App\Http\Controllers\Gateway; use App\Models\Payment; -use Log; use Auth; use Http; use Illuminate\Http\JsonResponse; -use Illuminate\Http\Request; +use Log; use Response; class PayBeaver extends AbstractPayment @@ -21,10 +20,10 @@ class PayBeaver extends AbstractPayment private $appSecret; private $url = 'https://api.paybeaver.com/api/v1/developer'; - public function __construct($appId, $appSecret) + public function __construct() { - $this->appId = $appId; - $this->appSecret = $appSecret; + $this->appId = sysConfig('paybeaver_app_id'); + $this->appSecret = sysConfig('paybeaver_app_secret'); } public function purchase($request): JsonResponse @@ -41,21 +40,48 @@ class PayBeaver extends AbstractPayment if (isset($result['message'])) { Log::warning('创建订单错误:'.$result['message']); + return Response::json(['status' => 'fail', 'message' => '创建订单失败:'.$result['message']]); } - if (!isset($result['data']) || !isset($result['data']['pay_url'])) { + if (! isset($result['data']['pay_url'])) { Log::warning('创建订单错误:未知错误'); + return Response::json(['status' => 'fail', 'message' => '创建订单失败:未知错误']); } $payment->update(['url' => $result['data']['pay_url']]); + return Response::json(['status' => 'success', 'url' => $result['data']['pay_url'], 'message' => '创建订单成功!']); } + private function createOrder($params) + { + $params['sign'] = $this->sign($params); + + $response = Http::post($this->url.'/orders', $params); + + if ($response->ok()) { + return $response->json(); + } + + return Response::json(['status' => 'fail', 'message' => '获取失败!请检查配置信息']); + } + + private function sign($params) + { + if (isset($params['sign'])) { + unset($params['sign']); + } + ksort($params); + reset($params); + + return strtolower(md5(http_build_query($params).$this->appSecret)); + } + public function notify($request): void { - if (!$this->paybeaverVerify($request->post())) { + if (! $this->paybeaverVerify($request->post())) { exit(json_encode(['status' => 400])); } @@ -71,40 +97,8 @@ class PayBeaver extends AbstractPayment exit(json_encode(['status' => 500])); } - protected function createOrder($params) + private function paybeaverVerify($params) { - $params['sign'] = $this->sign($params); - return $this->request('/orders', $params); - } - - protected function paybeaverVerify($params) - { - // Log::warning('got sign ' . $params['sign']); - // Log::warning('calc sign ' . $this->sign($params)); return hash_equals($params['sign'], $this->sign($params)); } - - protected function sign($params) - { - // Log::warning('paybeaver app secret: ' . $this->appSecret); - // Log::warning('query: ' . http_build_query($params) . $this->appSecret); - if (isset($params['sign'])) unset($params['sign']); - ksort($params); - reset($params); - return strtolower(md5(http_build_query($params) . $this->appSecret)); - } - - protected function request($path, $data) { - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, "{$this->url}{$path}"); - curl_setopt($curl, CURLOPT_POST, 1); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); - curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); - $data = curl_exec($curl); - curl_close($curl); - return json_decode($data, true); - } } diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 4a288726..fe5475b2 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -8,10 +8,10 @@ use App\Http\Controllers\Gateway\CodePay; use App\Http\Controllers\Gateway\EPay; use App\Http\Controllers\Gateway\F2Fpay; use App\Http\Controllers\Gateway\Local; +use App\Http\Controllers\Gateway\PayBeaver; use App\Http\Controllers\Gateway\PayJs; use App\Http\Controllers\Gateway\PayPal; use App\Http\Controllers\Gateway\Stripe; -use App\Http\Controllers\Gateway\PayBeaver; use App\Models\Coupon; use App\Models\Goods; use App\Models\Order; @@ -62,7 +62,7 @@ class PaymentController extends Controller case 'stripe': return new Stripe(); case 'paybeaver': - return new PayBeaver(sysConfig('paybeaver_app_id'), sysConfig('paybeaver_app_secret')); + return new PayBeaver(); default: Log::warning('未知支付:'.self::$method); diff --git a/app/Models/Order.php b/app/Models/Order.php index 2ba98e90..79bb3f53 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -199,11 +199,6 @@ class Order extends Model case 6: $pay_type_icon = $base_path.'stripe.png'; break; - case 7: - $pay_type_icon = $base_path.'coin.png'; - break; - case 0: - case 4: default: $pay_type_icon = $base_path.'coin.png'; } diff --git a/composer.lock b/composer.lock index 973d6ab3..410a1e98 100644 --- a/composer.lock +++ b/composer.lock @@ -776,16 +776,16 @@ }, { "name": "egulias/email-validator", - "version": "2.1.24", + "version": "2.1.25", "source": { "type": "git", "url": "https://github.com/egulias/EmailValidator.git", - "reference": "ca90a3291eee1538cd48ff25163240695bd95448" + "reference": "0dbf5d78455d4d6a41d186da50adc1122ec066f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/egulias/EmailValidator/zipball/ca90a3291eee1538cd48ff25163240695bd95448", - "reference": "ca90a3291eee1538cd48ff25163240695bd95448", + "url": "https://api.github.com/repos/egulias/EmailValidator/zipball/0dbf5d78455d4d6a41d186da50adc1122ec066f4", + "reference": "0dbf5d78455d4d6a41d186da50adc1122ec066f4", "shasum": "" }, "require": { @@ -832,7 +832,7 @@ ], "support": { "issues": "https://github.com/egulias/EmailValidator/issues", - "source": "https://github.com/egulias/EmailValidator/tree/2.1.24" + "source": "https://github.com/egulias/EmailValidator/tree/2.1.25" }, "funding": [ { @@ -840,7 +840,7 @@ "type": "github" } ], - "time": "2020-11-14T15:56:27+00:00" + "time": "2020-12-29T14:50:06+00:00" }, { "name": "ezyang/htmlpurifier", @@ -3307,16 +3307,16 @@ }, { "name": "phpoffice/phpspreadsheet", - "version": "1.15.0", + "version": "1.16.1", "source": { "type": "git", "url": "https://github.com/PHPOffice/PhpSpreadsheet.git", - "reference": "a8e8068b31b8119e1daa5b1eb5715a3a8ea8305f" + "reference": "76d4323b85129d0c368149c831a07a3e258b2b50" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/a8e8068b31b8119e1daa5b1eb5715a3a8ea8305f", - "reference": "a8e8068b31b8119e1daa5b1eb5715a3a8ea8305f", + "url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/76d4323b85129d0c368149c831a07a3e258b2b50", + "reference": "76d4323b85129d0c368149c831a07a3e258b2b50", "shasum": "" }, "require": { @@ -3333,10 +3333,11 @@ "ext-xmlwriter": "*", "ext-zip": "*", "ext-zlib": "*", + "ezyang/htmlpurifier": "^4.13", "maennchen/zipstream-php": "^2.1", - "markbaker/complex": "^1.5|^2.0", - "markbaker/matrix": "^1.2|^2.0", - "php": "^7.2|^8.0", + "markbaker/complex": "^1.5||^2.0", + "markbaker/matrix": "^1.2||^2.0", + "php": "^7.2||^8.0", "psr/http-client": "^1.0", "psr/http-factory": "^1.0", "psr/simple-cache": "^1.0" @@ -3347,7 +3348,7 @@ "jpgraph/jpgraph": "^4.0", "mpdf/mpdf": "^8.0", "phpcompatibility/php-compatibility": "^9.3", - "phpunit/phpunit": "^8.5|^9.3", + "phpunit/phpunit": "^8.5||^9.3", "squizlabs/php_codesniffer": "^3.5", "tecnickcom/tcpdf": "^6.3" }, @@ -3401,9 +3402,9 @@ ], "support": { "issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues", - "source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/1.15.0" + "source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/1.16.1" }, - "time": "2020-10-11T13:20:59+00:00" + "time": "2020-12-31T18:03:49+00:00" }, { "name": "phpoption/phpoption", @@ -7548,16 +7549,16 @@ }, { "name": "andrey-helldar/support", - "version": "v1.30.2", + "version": "v1.30.3", "source": { "type": "git", "url": "https://github.com/andrey-helldar/support.git", - "reference": "e463d3c5ed4302ab04001a04d3264fc78acac858" + "reference": "6278f7ba38fdbfb3a22b602a67620e1133703464" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/andrey-helldar/support/zipball/e463d3c5ed4302ab04001a04d3264fc78acac858", - "reference": "e463d3c5ed4302ab04001a04d3264fc78acac858", + "url": "https://api.github.com/repos/andrey-helldar/support/zipball/6278f7ba38fdbfb3a22b602a67620e1133703464", + "reference": "6278f7ba38fdbfb3a22b602a67620e1133703464", "shasum": "" }, "require": { @@ -7565,7 +7566,8 @@ "ext-dom": "*", "ext-json": "*", "ext-mbstring": "*", - "php": "^7.1.3|^8.0" + "php": "^7.1.3|^8.0", + "symfony/deprecation-contracts": "^2.0" }, "require-dev": { "ext-bcmath": "*", @@ -7628,7 +7630,7 @@ "type": "custom" } ], - "time": "2020-12-25T21:28:47+00:00" + "time": "2020-12-29T18:59:09+00:00" }, { "name": "arcanedev/laravel-lang", @@ -8482,16 +8484,16 @@ }, { "name": "facade/ignition", - "version": "2.5.3", + "version": "2.5.8", "source": { "type": "git", "url": "https://github.com/facade/ignition.git", - "reference": "d8dc4f90ed469f9f9313b976fb078c20585d5c99" + "reference": "8e907d81244649c5ea746e2ec30c32c5f59df472" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/facade/ignition/zipball/d8dc4f90ed469f9f9313b976fb078c20585d5c99", - "reference": "d8dc4f90ed469f9f9313b976fb078c20585d5c99", + "url": "https://api.github.com/repos/facade/ignition/zipball/8e907d81244649c5ea746e2ec30c32c5f59df472", + "reference": "8e907d81244649c5ea746e2ec30c32c5f59df472", "shasum": "" }, "require": { @@ -8555,7 +8557,7 @@ "issues": "https://github.com/facade/ignition/issues", "source": "https://github.com/facade/ignition" }, - "time": "2020-12-09T20:25:45+00:00" + "time": "2020-12-29T09:12:55+00:00" }, { "name": "facade/ignition-contracts", diff --git a/database/migrations/2021_01_04_094946_drop_node_ping.php b/database/migrations/2021_01_04_094946_drop_node_ping.php index 73a66a16..40a6bfd2 100644 --- a/database/migrations/2021_01_04_094946_drop_node_ping.php +++ b/database/migrations/2021_01_04_094946_drop_node_ping.php @@ -1,7 +1,6 @@ delete(); + $permission = Permission::where('name', 'admin.node.pingLog')->first(); + if ($permission) { + $permission->delete(); + } } public function down() diff --git a/database/migrations/2021_01_04_172833_add-paybeaver-payment.php b/database/migrations/2021_01_04_172833_add-paybeaver-payment.php index 387d8890..576cf616 100644 --- a/database/migrations/2021_01_04_172833_add-paybeaver-payment.php +++ b/database/migrations/2021_01_04_172833_add-paybeaver-payment.php @@ -1,7 +1,7 @@
- 请到海狸支付 PayBeaver申请账号 + 请到海狸支付 PayBeaver申请账号