Issues (93)

src/Plugin/Wechat/V2/Pay/Redpack/SendPlugin.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V2\Pay\Redpack;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Packer\XmlPacker;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Pay;
16
use Yansongda\Supports\Collection;
17
use Yansongda\Supports\Str;
18
19
use function Yansongda\Pay\get_wechat_config;
20
use function Yansongda\Pay\get_wechat_type_key;
21
22
/**
23
 * @see https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon_sl.php?chapter=13_4&index=3
24
 */
25
class SendPlugin implements PluginInterface
26
{
27
    /**
28
     * @throws ContainerException
29
     * @throws ServiceNotFoundException
30
     * @throws Throwable                随机字符串生成失败
31
     */
32
    public function assembly(Rocket $rocket, Closure $next): Rocket
33
    {
34
        Logger::debug('[Wechat][V2][Pay][Redpack][SendPlugin] 插件开始装载', ['rocket' => $rocket]);
35
        $payload = $rocket->getPayload();
36
        $params = $rocket->getParams();
37
        $config = get_wechat_config($params);
38
39
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
40
            $data = $this->service($payload, $config, $params);
0 ignored issues
show
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...k\SendPlugin::service() does only seem to accept Yansongda\Supports\Collection, 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

40
            $data = $this->service(/** @scrutinizer ignore-type */ $payload, $config, $params);
Loading history...
41
        }
42
43
        $rocket->setPacker(XmlPacker::class)
44
            ->mergePayload(array_merge(
45
                [
46
                    '_url' => 'mmpaymkttransfers/sendredpack',
47
                    '_content_type' => 'application/xml',
48
                    'nonce_str' => Str::random(32),
49
                    '_http' => [
50
                        'ssl_key' => $config['mch_secret_cert'],
51
                        'cert' => $config['mch_public_cert_path'],
52
                    ],
53
                ],
54
                $data ?? $this->normal($config, $params)
55
            ));
56
57
        Logger::info('[Wechat][V2][Pay][Pos][PayPlugin] 插件装载完毕', ['rocket' => $rocket]);
58
59
        return $next($rocket);
60
    }
61
62
    protected function normal(array $config, array $params): array
63
    {
64
        return [
65
            'wxappid' => $config[get_wechat_type_key($params)] ?? '',
66
            'mch_id' => $config['mch_id'] ?? '',
67
        ];
68
    }
69
70
    protected function service(Collection $payload, array $config, array $params): array
71
    {
72
        $wechatTypeKey = get_wechat_type_key($params);
73
74
        return [
75
            'wxappid' => $config[$wechatTypeKey] ?? '',
76
            'mch_id' => $config['mch_id'] ?? '',
77
            'sub_mch_id' => $payload->get('sub_mch_id', $config['sub_mch_id'] ?? ''),
78
            'msgappid' => $config['sub_'.$wechatTypeKey],
79
        ];
80
    }
81
}
82