Issues (93)

src/Plugin/Wechat/V3/CallbackPlugin.php (2 issues)

Labels
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3;
6
7
use Closure;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Direction\NoHttpRequestDirection;
11
use Yansongda\Artful\Exception\ContainerException;
12
use Yansongda\Artful\Exception\InvalidConfigException;
13
use Yansongda\Artful\Exception\InvalidParamsException;
14
use Yansongda\Artful\Exception\ServiceNotFoundException;
15
use Yansongda\Artful\Logger;
16
use Yansongda\Artful\Rocket;
17
use Yansongda\Pay\Exception\DecryptException;
18
use Yansongda\Pay\Exception\Exception;
19
use Yansongda\Pay\Exception\InvalidSignException;
20
use Yansongda\Supports\Collection;
21
22
use function Yansongda\Pay\decrypt_wechat_resource;
23
use function Yansongda\Pay\get_wechat_config;
24
use function Yansongda\Pay\verify_wechat_sign;
25
26
class CallbackPlugin implements PluginInterface
27
{
28
    /**
29
     * @throws ContainerException
30
     * @throws InvalidConfigException
31
     * @throws InvalidParamsException
32
     * @throws InvalidSignException
33
     * @throws ServiceNotFoundException
34
     * @throws DecryptException
35
     */
36
    public function assembly(Rocket $rocket, Closure $next): Rocket
37
    {
38
        Logger::debug('[Wechat][V3][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]);
39
40
        $this->init($rocket);
41
42
        $params = $rocket->getParams();
43
44
        /* @phpstan-ignore-next-line */
45
        verify_wechat_sign($rocket->getDestinationOrigin(), $params);
0 ignored issues
show
It seems like $rocket->getDestinationOrigin() can also be of type null; however, parameter $message of Yansongda\Pay\verify_wechat_sign() does only seem to accept Psr\Http\Message\Respons...\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        verify_wechat_sign(/** @scrutinizer ignore-type */ $rocket->getDestinationOrigin(), $params);
Loading history...
46
47
        $body = json_decode((string) $rocket->getDestination()->getBody(), true);
0 ignored issues
show
The method getBody() does not exist on Yansongda\Supports\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $body = json_decode((string) $rocket->getDestination()->/** @scrutinizer ignore-call */ getBody(), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
49
        $rocket->setDirection(NoHttpRequestDirection::class)->setPayload(new Collection($body));
50
51
        $body['resource'] = decrypt_wechat_resource($body['resource'] ?? [], get_wechat_config($params));
52
53
        $rocket->setDestination(new Collection($body));
54
55
        Logger::info('[Wechat][V3][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]);
56
57
        return $next($rocket);
58
    }
59
60
    /**
61
     * @throws InvalidParamsException
62
     */
63
    protected function init(Rocket $rocket): void
64
    {
65
        $request = $rocket->getParams()['_request'] ?? null;
66
        $params = $rocket->getParams()['_params'] ?? [];
67
68
        if (!$request instanceof ServerRequestInterface) {
69
            throw new InvalidParamsException(Exception::PARAMS_CALLBACK_REQUEST_INVALID, '参数异常: 微信回调参数不正确');
70
        }
71
72
        $rocket->setDestination(clone $request)
73
            ->setDestinationOrigin($request)
74
            ->setParams($params);
75
    }
76
}
77