Passed
Push — master ( 796ce4...223732 )
by Songda
04:12 queued 02:06
created

InvokePrepayPlugin::getSign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 2
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\ContainerDependencyException
16
     * @throws \Yansongda\Pay\Exception\ContainerException
17
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
18
     * @throws \Exception
19
     */
20
    protected function getInvokeConfig(Rocket $rocket, string $prepayId): Config
21
    {
22
        $config = new Config([
23
            'appid' => $this->getAppid($rocket),
24
            'partnerid' => get_wechat_config($rocket->getParams())->get('mch_id'),
25
            'prepayid' => $prepayId,
26
            'package' => 'Sign=WXPay',
27
            'noncestr' => Str::random(32),
28
            'timestamp' => time().'',
29
        ]);
30
31
        $config->set('sign', $this->getSign($config, $rocket->getParams()));
32
33
        return $config;
34
    }
35
36
    /**
37
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
38
     * @throws \Yansongda\Pay\Exception\ContainerException
39
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
40
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
41
     */
42
    protected function getSign(Collection $invokeConfig, array $params): string
43
    {
44
        $contents = $invokeConfig->get('appid', '')."\n".
45
            $invokeConfig->get('timestamp', '')."\n".
46
            $invokeConfig->get('nonceStr', '')."\n".
47
            $invokeConfig->get('package', '')."\n";
48
49
        return get_wechat_sign($params, $contents);
50
    }
51
52
    protected function getAppid(Rocket $rocket): string
53
    {
54
        $config = get_wechat_config($rocket->getParams());
55
56
        return $config->get('app_id', '');
57
    }
58
}
59