Passed
Push — master ( 7ff004...a83eae )
by Songda
01:54
created

SignPlugin::payloadToString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat;
6
7
use Closure;
8
use GuzzleHttp\Psr7\Utils;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidConfigException;
12
use Yansongda\Pay\Exception\InvalidParamsException;
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Supports\Collection;
16
use Yansongda\Supports\Str;
17
18
class SignPlugin implements PluginInterface
19
{
20
    /**
21
     * @throws \Yansongda\Pay\Exception\ContainerException
22
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
23
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
24
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
25
     * @throws \Exception
26
     */
27
    public function assembly(Rocket $rocket, Closure $next): Rocket
28
    {
29
        Logger::info('[wechat][SignPlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        $timestamp = time();
32
        $random = Str::random(32);
33
        $body = $this->payloadToString($rocket->getPayload());
34
        $contents = $this->getContents($rocket, $timestamp, $random);
35
        $authorization = $this->getWechatAuthorization($rocket->getParams(), $timestamp, $random, $contents);
36
        $radar = $rocket->getRadar()->withHeader('Authorization', $authorization);
37
38
        if (!empty($rocket->getParams()['_serial_no'])) {
39
            $radar = $radar->withHeader('Wechatpay-Serial', $rocket->getParams()['_serial_no']);
40
        }
41
42
        if (!empty($body)) {
43
            $radar = $radar->withBody(Utils::streamFor($body));
44
        }
45
46
        $rocket->setRadar($radar);
47
48
        Logger::info('[wechat][SignPlugin] 插件装载完毕', ['rocket' => $rocket]);
49
50
        return $next($rocket);
51
    }
52
53
    /**
54
     * @throws \Yansongda\Pay\Exception\ContainerException
55
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
56
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
57
     */
58
    protected function getWechatAuthorization(array $params, int $timestamp, string $random, string $contents): string
59
    {
60
        $config = get_wechat_config($params);
61
        $mchPublicCertPath = $config->get('mch_public_cert_path');
62
63
        if (empty($mchPublicCertPath)) {
64
            throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Missing Wechat Config -- [mch_public_cert_path]');
65
        }
66
67
        $ssl = openssl_x509_parse(get_public_cert($mchPublicCertPath));
68
69
        if (empty($ssl['serialNumberHex'])) {
70
            throw new InvalidConfigException(Exception::WECHAT_CONFIG_ERROR, 'Parse [mch_public_cert_path] Serial Number Error');
71
        }
72
73
        $auth = sprintf(
74
            'mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
75
            $config->get('mch_id', ''),
76
            $random,
77
            $timestamp,
78
            $ssl['serialNumberHex'],
79
            get_wechat_sign($params, $contents),
80
        );
81
82
        return 'WECHATPAY2-SHA256-RSA2048 '.$auth;
83
    }
84
85
    /**
86
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
87
     */
88
    protected function getContents(Rocket $rocket, int $timestamp, string $random): string
89
    {
90
        $request = $rocket->getRadar();
91
92
        if (is_null($request)) {
93
            throw new InvalidParamsException(Exception::REQUEST_NULL_ERROR);
94
        }
95
96
        $uri = $request->getUri();
97
98
        return $request->getMethod()."\n".
99
            $uri->getPath().(empty($uri->getQuery()) ? '' : '?'.$uri->getQuery())."\n".
100
            $timestamp."\n".
101
            $random."\n".
102
            $this->payloadToString($rocket->getPayload())."\n";
103
    }
104
105
    protected function payloadToString(?Collection $payload): string
106
    {
107
        return (is_null($payload) || 0 === $payload->count()) ? '' : $payload->toJson();
108
    }
109
}
110