Passed
Push — master ( ac2972...bd56ba )
by Songda
01:56
created

InvokePrepayPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInvokeConfig() 0 14 1
A getSign() 0 8 1
A getConfigKey() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Pay\App;
6
7
use Yansongda\Pay\Rocket;
8
use Yansongda\Supports\Collection;
9
use Yansongda\Supports\Config;
10
use Yansongda\Supports\Str;
11
12
class InvokePrepayPlugin extends \Yansongda\Pay\Plugin\Wechat\Pay\Common\InvokePrepayPlugin
13
{
14
    /**
15
     * @throws \Yansongda\Pay\Exception\ContainerException
16
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
17
     * @throws \Exception
18
     */
19
    protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
20
    {
21
        $config = new Config([
22
            'appid' => $this->getAppId($rocket),
23
            'partnerid' => get_wechat_config($rocket->getParams())->get('mch_id'),
24
            'prepayid' => $prepayId,
25
            'package' => 'Sign=WXPay',
26
            'noncestr' => Str::random(32),
27
            'timestamp' => time().'',
28
        ]);
29
30
        $config->set('sign', $this->getSign($config, $rocket->getParams()));
31
32
        return $config;
33
    }
34
35
    /**
36
     * @throws \Yansongda\Pay\Exception\ContainerException
37
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
38
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
39
     */
40
    protected function getSign(Collection $invokeConfig, array $params): string
41
    {
42
        $contents = $invokeConfig->get('appid', '')."\n".
43
            $invokeConfig->get('timestamp', '')."\n".
44
            $invokeConfig->get('noncestr', '')."\n".
45
            $invokeConfig->get('prepayid', '')."\n";
46
47
        return get_wechat_sign($params, $contents);
48
    }
49
50
    protected function getConfigKey(): string
51
    {
52
        return 'app_id';
53
    }
54
}
55