Passed
Push — master ( d08d15...8f60e5 )
by Songda
02:32 queued 11s
created

should_http_request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
use Yansongda\Pay\Contract\ConfigInterface;
6
use Yansongda\Pay\Exception\InvalidConfigException;
7
use Yansongda\Pay\Parser\NoHttpRequestParser;
8
use Yansongda\Pay\Pay;
9
use Yansongda\Pay\Rocket;
10
use Yansongda\Supports\Config;
11
use Yansongda\Supports\Str;
12
13
if (!function_exists('should_do_http_request')) {
14
    function should_do_http_request(Rocket $rocket): bool
15
    {
16
        $direction = $rocket->getDirection();
17
18
        return is_null($direction) ||
19
            (NoHttpRequestParser::class !== $direction &&
20
            !in_array(NoHttpRequestParser::class, class_parents($direction)));
21
    }
22
}
23
24
if (!function_exists('get_alipay_config')) {
25
    /**
26
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
27
     * @throws \Yansongda\Pay\Exception\ContainerException
28
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
29
     */
30
    function get_alipay_config(array $params): Config
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
    {
32
        $alipay = Pay::get(ConfigInterface::class)->get('alipay');
33
34
        $config = $params['_config'] ?? 'default';
35
36
        return new Config($alipay[$config] ?? []);
37
    }
38
}
39
40
if (!function_exists('get_public_crt_or_private_cert')) {
41
    /**
42
     * @return false|resource|string
43
     */
44
    function get_public_crt_or_private_cert(string $key)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
    {
46
        if (Str::endsWith($key, '.crt')) {
47
            $key = file_get_contents($key);
48
        } elseif (Str::endsWith($key, '.pem')) {
49
            $key = openssl_pkey_get_private(
50
                Str::startsWith($key, 'file://') ? $key : 'file://'.$key
51
            );
52
        } else {
53
            $key = "-----BEGIN RSA PRIVATE KEY-----\n".
54
                wordwrap($key, 64, "\n", true).
55
                "\n-----END RSA PRIVATE KEY-----";
56
        }
57
58
        return $key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $key also could return the type OpenSSLAsymmetricKey which is incompatible with the documented return type false|resource|string.
Loading history...
59
    }
60
}
61
62
if (!function_exists('verify_alipay_response')) {
63
    /**
64
     * @param string $sign base64decode 之后的
65
     *
66
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
67
     * @throws \Yansongda\Pay\Exception\ContainerException
68
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
69
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
70
     */
71
    function verify_alipay_response(array $params, string $contents, string $sign): bool
72
    {
73
        $public = get_alipay_config($params)->get('alipay_public_cert_path');
74
75
        if (is_null($public)) {
76
            throw new InvalidConfigException(InvalidConfigException::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [alipay_public_cert_path]');
77
        }
78
79
        return 1 === openssl_verify(
80
            $contents,
81
            $sign,
82
            get_public_crt_or_private_cert($public),
83
            OPENSSL_ALGO_SHA256);
84
    }
85
}
86
87
if (!function_exists('get_wechat_config')) {
88
    /**
89
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
90
     * @throws \Yansongda\Pay\Exception\ContainerException
91
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
92
     */
93
    function get_wechat_config(array $params): Config
94
    {
95
        $wechat = Pay::get(ConfigInterface::class)->get('wechat');
96
97
        $config = $params['_config'] ?? 'default';
98
99
        return new Config($wechat[$config] ?? []);
100
    }
101
}
102