Skip to content

Commit

Permalink
Fix:修复不能发起支付的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Aug 6, 2023
1 parent 2d6f316 commit ee8ad4e
Showing 1 changed file with 50 additions and 32 deletions.
82 changes: 50 additions & 32 deletions app/Plugins/Core/src/Lib/Pay/PayService.php
@@ -1,6 +1,6 @@
<?php

declare (strict_types=1);
declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
Expand All @@ -18,35 +18,37 @@
use App\Plugins\Core\src\Models\PayOrder;
use App\Plugins\User\src\Models\User;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Arr;
use Hyperf\Stringable\Str;
use Hyperf\Utils\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;

class PayService
{
/**
* 关闭订单服务
*/
#[Inject]
protected OrderCloseJobService $OrderCloseJobService;

/**
* @param int|string $user_id 用户id
* @param string $title 订单标题
* @param string $total_amount 订单金额
* @param float|int|string $total_amount 订单金额
* @param array $payment_method 支付方式
* @throws InvalidArgumentException
* @return array|bool
*/
public function create(string|int $user_id, string $title, string $total_amount, array $payment_method) : bool|array
public function create(string | int $user_id, string $title, string | int | float $total_amount, array $payment_method): bool | array
{
// 判断用户是否存在
if (!User::query()->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' => '订单金额格式不正确']);
}
// 检索支付方式
Expand All @@ -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) {
Expand All @@ -86,6 +89,7 @@ public function getInterfaces() : array
}
return $data;
}

/**
* 获取所有支付接口.
*/
Expand All @@ -99,6 +103,7 @@ public function get_ename_Interfaces()
}
return $data;
}

/**
* 获取配置内容.
* @param string $default
Expand All @@ -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', '[]'));
Expand All @@ -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', '[]'));
Expand All @@ -161,6 +169,7 @@ public function get_enabled_data() : array
}
return $enable;
}

/**
* 处理回调通知.
* @param string $id 订单号(由SForum系统生产)
Expand All @@ -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' => '订单号不存在']);
}
// 更新数据
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -256,14 +269,15 @@ 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);
return call_user_func([new $payServerHandler(), 'close'], $order);
}
return Json_Api(403, false, ['msg' => '当前订单状态不允许关闭']);
}

/**
* 取消订单.
* @param $id
Expand All @@ -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' => '取消订单成功!']);
}
Expand All @@ -289,48 +303,50 @@ 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);
return call_user_func([new $payServerHandler(), 'cancel'], $order);
}
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' => '订单不存在']);
}
// 获取订单信息
Expand All @@ -354,21 +370,23 @@ 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) {
return $string;
}
return $default;
}
}
}

0 comments on commit ee8ad4e

Please sign in to comment.