Passed
Push — master ( a1bcd2...a1b58e )
by Songda
02:08
created

SignPlugin::getMchPublicCertSerialNumber()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 3
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\InvalidParamsException;
12
use Yansongda\Pay\Logger;
13
use Yansongda\Pay\Rocket;
14
use Yansongda\Supports\Collection;
15
use Yansongda\Supports\Str;
16
17
class SignPlugin implements PluginInterface
18
{
19
    /**
20
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
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
        $radar = $rocket->getRadar()->withHeader('Authorization', get_wechat_authorization(
35
            $rocket->getParams(), $timestamp, $random, $this->getContents($rocket, $timestamp, $random))
36
        );
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\InvalidParamsException
55
     */
56
    protected function getContents(Rocket $rocket, int $timestamp, string $random): string
57
    {
58
        $request = $rocket->getRadar();
59
60
        if (is_null($request)) {
61
            throw new InvalidParamsException(Exception::REQUEST_NULL_ERROR);
62
        }
63
64
        $uri = $request->getUri();
65
66
        return $request->getMethod()."\n".
67
            $uri->getPath().(empty($uri->getQuery()) ? '' : '?'.$uri->getQuery())."\n".
68
            $timestamp."\n".
69
            $random."\n".
70
            $this->payloadToString($rocket->getPayload())."\n";
71
    }
72
73
    protected function payloadToString(?Collection $payload): string
74
    {
75
        return (is_null($payload) || 0 === $payload->count()) ? '' : $payload->toJson();
76
    }
77
}
78