Issues (93)

Plugin/Wechat/V3/Pay/Combine/MiniInvokePlugin.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Combine;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidConfigException;
12
use Yansongda\Artful\Exception\InvalidResponseException;
13
use Yansongda\Artful\Exception\ServiceNotFoundException;
14
use Yansongda\Artful\Logger;
15
use Yansongda\Artful\Rocket;
16
use Yansongda\Pay\Exception\Exception;
17
use Yansongda\Pay\Pay;
18
use Yansongda\Supports\Collection;
19
use Yansongda\Supports\Config;
20
use Yansongda\Supports\Str;
21
22
use function Yansongda\Pay\get_wechat_config;
23
use function Yansongda\Pay\get_wechat_sign;
24
25
/**
26
 * @see https://pay.weixin.qq.com/docs/merchant/apis/combine-payment/orders/mini-transfer-payment.html
27
 * @see https://pay.weixin.qq.com/docs/partner/apis/combine-payment/orders/mini-transfer-payment.html
28
 */
29
class MiniInvokePlugin implements PluginInterface
30
{
31
    /**
32
     * @throws ContainerException
33
     * @throws InvalidConfigException
34
     * @throws InvalidResponseException
35
     * @throws ServiceNotFoundException
36
     * @throws Throwable                生成随机串失败
37
     */
38
    public function assembly(Rocket $rocket, Closure $next): Rocket
39
    {
40
        /* @var Rocket $rocket */
41
        $rocket = $next($rocket);
42
43
        Logger::debug('[Wechat][Pay][Combine][MiniInvokePlugin] 插件开始装载', ['rocket' => $rocket]);
44
45
        $destination = $rocket->getDestination();
46
        $prepayId = $destination?->get('prepay_id');
0 ignored issues
show
The method get() does not exist on Psr\Http\Message\MessageInterface. ( Ignorable by Annotation )

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

46
        /** @scrutinizer ignore-call */ 
47
        $prepayId = $destination?->get('prepay_id');

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...
47
48
        if (is_null($prepayId)) {
49
            Logger::error('[Wechat][Pay][Combine][MiniInvokePlugin] 预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求', $destination?->all() ?? null);
0 ignored issues
show
The method all() does not exist on Psr\Http\Message\MessageInterface. ( Ignorable by Annotation )

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

49
            Logger::error('[Wechat][Pay][Combine][MiniInvokePlugin] 预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求', $destination?->/** @scrutinizer ignore-call */ all() ?? null);

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...
50
51
            throw new InvalidResponseException(Exception::RESPONSE_MISSING_NECESSARY_PARAMS, $destination?->get('message') ?? '预下单失败:响应缺少 `prepay_id` 参数,请自行检查参数是否符合微信要求', $destination?->all() ?? null);
52
        }
53
54
        $params = $rocket->getParams();
55
        $config = get_wechat_config($params);
56
        $payload = $rocket->getPayload();
57
58
        $rocket->setDestination($this->getInvokeConfig($payload, $config, $prepayId));
59
60
        Logger::info('[Wechat][Pay][Combine][MiniInvokePlugin] 插件装载完毕', ['rocket' => $rocket]);
61
62
        return $rocket;
63
    }
64
65
    /**
66
     * @throws InvalidConfigException
67
     * @throws Throwable              生成随机串失败
68
     */
69
    protected function getInvokeConfig(?Collection $payload, array $config, string $prepayId): Config
70
    {
71
        $invokeConfig = new Config([
72
            'appId' => $this->getAppId($payload, $config),
73
            'timeStamp' => time().'',
74
            'nonceStr' => Str::random(32),
75
            'package' => 'prepay_id='.$prepayId,
76
            'signType' => 'RSA',
77
        ]);
78
79
        $invokeConfig->set('paySign', $this->getSign($invokeConfig, $config));
80
81
        return $invokeConfig;
82
    }
83
84
    /**
85
     * @throws InvalidConfigException
86
     */
87
    protected function getSign(Collection $invokeConfig, array $config): string
88
    {
89
        $contents = $invokeConfig->get('appId', '')."\n".
90
            $invokeConfig->get('timeStamp', '')."\n".
91
            $invokeConfig->get('nonceStr', '')."\n".
92
            $invokeConfig->get('package', '')."\n";
93
94
        return get_wechat_sign($config, $contents);
95
    }
96
97
    protected function getAppId(?Collection $payload, array $config): string
98
    {
99
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
100
            return $payload?->get('_invoke_appid') ?? $config['sub_mini_app_id'] ?? '';
101
        }
102
103
        return $payload?->get('_invoke_appid') ?? $config['mini_app_id'] ?? '';
104
    }
105
}
106