From ee8ad4eed6817c3058804ad8640c70d4a8e380c4 Mon Sep 17 00:00:00 2001 From: zhuchunshu Date: Sun, 6 Aug 2023 16:06:56 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=E4=BF=AE=E5=A4=8D=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E5=8F=91=E8=B5=B7=E6=94=AF=E4=BB=98=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Plugins/Core/src/Lib/Pay/PayService.php | 82 +++++++++++++-------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/app/Plugins/Core/src/Lib/Pay/PayService.php b/app/Plugins/Core/src/Lib/Pay/PayService.php index 62eb17ea3..d443be285 100644 --- a/app/Plugins/Core/src/Lib/Pay/PayService.php +++ b/app/Plugins/Core/src/Lib/Pay/PayService.php @@ -1,6 +1,6 @@ where('id', $user_id)->exists()) { + if (! User::query()->where('id', $user_id)->exists()) { return Json_Api(403, false, ['msg' => '用户不存在']); } // 截取标题 $title = Str::limit($title, 200); // 判断金额类型 - if (!is_numeric($total_amount)) { + if (! is_numeric($total_amount)) { return Json_Api(403, false, ['msg' => '订单金额格式不正确']); } // 检索支付方式 @@ -68,15 +70,16 @@ public function create(string|int $user_id, string $title, string $total_amount, // 定时关闭订单 $this->OrderCloseJobService->push(['order_id' => $order->id, 'check_payment' => false, 'payServer' => $payServer]); $payServerHandler = $payServer['handler']; - if (!@method_exists(new $payServerHandler(), 'create')) { + if (! @method_exists(new $payServerHandler(), 'create')) { return Json_Api(500, false, ['msg' => '支付插件:' . $payment_method[1] . '无有效的订单创建方法']); } return call_user_func([new $payServerHandler(), 'create'], $order); } + /** * 获取所有支付接口. */ - public function getInterfaces() : array + public function getInterfaces(): array { $data = []; foreach (Itf()->get('Pay') as $key => $pay) { @@ -86,6 +89,7 @@ public function getInterfaces() : array } return $data; } + /** * 获取所有支付接口. */ @@ -99,6 +103,7 @@ public function get_ename_Interfaces() } return $data; } + /** * 获取配置内容. * @param string $default @@ -107,27 +112,29 @@ public function get_ename_Interfaces() */ public function get_options(string $name, $default = '') { - if (!cache()->has('admin.options.pay.' . $name)) { + if (! cache()->has('admin.options.pay.' . $name)) { cache()->set('admin.options.pay.' . $name, @PayConfig::query()->where('name', $name)->first()->value); } return $this->core_default(cache()->get('admin.options.pay.' . $name), $default); } + /** * 清理配置缓存. * @throws \Psr\SimpleCache\InvalidArgumentException */ - public function clean_options() : bool + public function clean_options(): bool { foreach (PayConfig::query()->get() as $value) { cache()->delete('admin.options.pay.' . $value->name); } return true; } + /** * 获取已启动支付插件. * @throws \Psr\SimpleCache\InvalidArgumentException */ - public function get_enabled() : array + public function get_enabled(): array { // 获取在数据库中已启用的支付插件 $pays = json_decode(pay()->get_options('enable', '[]')); @@ -143,11 +150,12 @@ public function get_enabled() : array } return $enable; } + /** * 获取已启动支付插件. * @throws \Psr\SimpleCache\InvalidArgumentException */ - public function get_enabled_data() : array + public function get_enabled_data(): array { // 获取在数据库中已启用的支付插件 $pays = json_decode(pay()->get_options('enable', '[]')); @@ -161,6 +169,7 @@ public function get_enabled_data() : array } return $enable; } + /** * 处理回调通知. * @param string $id 订单号(由SForum系统生产) @@ -171,9 +180,9 @@ public function get_enabled_data() : array * @param string $amount_total 总金额 * @return array|bool */ - public function notify(string $id, string $status, string $trade_no, string|int|float $payer_total, array $notify_result, string|int|float $amount_total, string|null $payment_method = null) : bool|array + public function notify(string $id, string $status, string $trade_no, string | int | float $payer_total, array $notify_result, string | int | float $amount_total, string | null $payment_method = null): bool | array { - if (!PayOrder::query()->where('id', $id)->exists()) { + if (! PayOrder::query()->where('id', $id)->exists()) { return Json_Api(403, false, ['msg' => '订单号不存在']); } // 更新数据 @@ -183,29 +192,31 @@ public function notify(string $id, string $status, string $trade_no, string|int| // 响应结果 return true; } + /** * 获取支付插件信息. * @param $id * @param $ename * @return array|mixed */ - public function get_pay_plugin_data($id, $ename) : mixed + public function get_pay_plugin_data($id, $ename): mixed { - if (!array_key_exists($id, $this->getInterfaces())) { + if (! array_key_exists($id, $this->getInterfaces())) { return '支付方式不存在,id检索失败'; } // 检索支付方式ename - if (!Arr::has($this->getInterfaces()[$id], 'ename') || $this->getInterfaces()[$id]['ename'] !== $ename) { + if (! Arr::has($this->getInterfaces()[$id], 'ename') || $this->getInterfaces()[$id]['ename'] !== $ename) { return '支付方式不存在,ename检索失败'; } return $this->getInterfaces()[$id]; } + /** * 查询订单. * @param $trade_no * @return array|ResponseInterface */ - public function find($trade_no, bool $refund = false) : array|ResponseInterface + public function find($trade_no, bool $refund = false): array | ResponseInterface { $order = PayOrder::query()->where('trade_no', $trade_no)->first(); $payment_method = json_decode($order->payment_method, true); @@ -216,20 +227,22 @@ public function find($trade_no, bool $refund = false) : array|ResponseInterface // 支付插件信息 $payServer = $this->get_ename_Interfaces()[$payment_method[1]]; $payServerHandler = $payServer['handler']; - if (!@method_exists(new $payServerHandler(), 'find')) { + if (! @method_exists(new $payServerHandler(), 'find')) { return Json_Api(500, false, ['msg' => '支付插件:' . $payment_method[1] . '无有效的订单查询方法']); } //$result = (new $payServerHandler())->find($order, $refund); $result = call_user_func([new $payServerHandler(), 'find'], $order, $refund); return view('App::Pay.admin.order_show', ['order' => $order, 'result' => $result]); } + /** * 生成前端html代码 */ - public function generate_html() : PayGenerate + public function generate_html(): PayGenerate { return new PayGenerate(); } + /** * 关闭订单. * @param $id @@ -256,7 +269,7 @@ public function close($id, $check_payment = true, $payServer = null) $payServer = $this->get_ename_Interfaces()[$payment_method[1]]; } $payServerHandler = $payServer['handler']; - if (!@method_exists(new $payServerHandler(), 'close')) { + if (! @method_exists(new $payServerHandler(), 'close')) { return Json_Api(500, false, ['msg' => '支付插件:' . $payment_method[1] . '无有效的订单查询方法']); } //return (new $payServerHandler())->close($order); @@ -264,6 +277,7 @@ public function close($id, $check_payment = true, $payServer = null) } return Json_Api(403, false, ['msg' => '当前订单状态不允许关闭']); } + /** * 取消订单. * @param $id @@ -275,7 +289,7 @@ public function cancel($id, $check_payment = true, $payServer = null) { $order = PayOrder::query()->find($id); if (Str::is('*待支付*', '*' . $order->status . '*') || Str::is('*未支付*', '*' . $order->status . '*') || Str::is('*未付款*', '*' . $order->status . '*')) { - if (!$order->trade_no) { + if (! $order->trade_no) { PayOrder::query()->where('id', $order->id)->update(['status' => '订单取消']); return Json_Api(200, true, ['msg' => '取消订单成功!']); } @@ -289,7 +303,7 @@ public function cancel($id, $check_payment = true, $payServer = null) $payServer = $this->get_ename_Interfaces()[$payment_method[1]]; } $payServerHandler = $payServer['handler']; - if (!@method_exists(new $payServerHandler(), 'cancel')) { + if (! @method_exists(new $payServerHandler(), 'cancel')) { return Json_Api(500, false, ['msg' => '支付插件:' . $payment_method[1] . '无有效的订单查询方法']); } //return (new $payServerHandler())->cancel($order); @@ -297,40 +311,42 @@ public function cancel($id, $check_payment = true, $payServer = null) } return Json_Api(403, false, ['msg' => '当前订单状态不允许取消']); } + /** * 检索支付方式. * @throws InvalidArgumentException * @return array|bool */ - public function check_payment(array $payment_method) : bool|array + public function check_payment(array $payment_method): bool | array { // 检索支付方式 - if (!is_array($payment_method) || !is_numeric($payment_method[0]) || !is_string($payment_method[1])) { + if (! is_array($payment_method) || ! is_numeric($payment_method[0]) || ! is_string($payment_method[1])) { return Json_Api(403, false, ['msg' => '支付方式格式不正确']); } // 检索支付方式id - if (!Arr::has($this->getInterfaces(), $payment_method[0])) { + if (! Arr::has($this->getInterfaces(), $payment_method[0])) { return Json_Api(403, false, ['msg' => '支付方式不存在,id检索失败']); } // 检索支付方式ename - if (!Arr::has($this->getInterfaces()[$payment_method[0]], 'ename') || $this->getInterfaces()[$payment_method[0]]['ename'] !== $payment_method[1]) { + if (! Arr::has($this->getInterfaces()[$payment_method[0]], 'ename') || $this->getInterfaces()[$payment_method[0]]['ename'] !== $payment_method[1]) { return Json_Api(403, false, ['msg' => '支付方式不存在,ename检索失败']); } // 检索支付方式id - if (!is_array($this->get_enabled_data()[$payment_method[0]])) { + if (! is_array($this->get_enabled_data()[$payment_method[0]])) { return Json_Api(403, false, ['msg' => '此支付扩展未启用']); } return true; } + /** * 对未付款订单进行付款. * @param $order_id * @param $payment * @return array|bool|mixed */ - public function paying($order_id, $payment = null) : mixed + public function paying($order_id, $payment = null): mixed { - if (!PayOrder::query()->where('id', $order_id)->exists()) { + if (! PayOrder::query()->where('id', $order_id)->exists()) { return Json_Api(403, false, ['msg' => '订单不存在']); } // 获取订单信息 @@ -354,16 +370,18 @@ public function paying($order_id, $payment = null) : mixed // 支付插件信息 $payServer = $this->get_ename_Interfaces()[$payment_method[1]]; $payServerHandler = $payServer['handler']; - if (!@method_exists(new $payServerHandler(), 'create')) { + if (! @method_exists(new $payServerHandler(), 'create')) { return Json_Api(500, false, ['msg' => '支付插件:' . $payment_method[1] . '无有效的订单创建方法']); } // return (new $payServerHandler())->create($order); return call_user_func([new $payServerHandler(), 'create'], $order); } + private function core_Itf_id($name, $id) { return \Hyperf\Stringable\Str::after($id, $name . '_'); } + private function core_default($string = null, $default = null) { if ($string) { @@ -371,4 +389,4 @@ private function core_default($string = null, $default = null) } return $default; } -} \ No newline at end of file +}