Passed
Pull Request — master (#1252)
by Keal
02:21
created

Client::shareAddressConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
ccs 0
cts 22
cp 0
crap 12
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Payment\Jssdk;
13
14
use EasyWeChat\BasicService\Jssdk\Client as JssdkClient;
15
use EasyWeChat\Kernel\Support;
16
use Overtrue\Socialite\AccessTokenInterface;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author overtrue <[email protected]>
22
 */
23
class Client extends JssdkClient
24
{
25
    /**
26
     * [WeixinJSBridge] Generate js config for payment.
27
     *
28
     * <pre>
29
     * WeixinJSBridge.invoke(
30
     *  'getBrandWCPayRequest',
31
     *  ...
32
     * );
33
     * </pre>
34
     *
35
     * @param string $prepayId
36
     * @param bool   $json
37
     *
38
     * @return string|array
39
     */
40
    public function bridgeConfig(string $prepayId, bool $json = true)
41
    {
42
        $params = [
43
            'appId' => $this->app['config']->sub_appid ?: $this->app['config']->app_id,
44
            'timeStamp' => strval(time()),
45
            'nonceStr' => uniqid(),
46
            'package' => "prepay_id=$prepayId",
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $prepayId instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
47
            'signType' => 'MD5',
48
        ];
49
50
        $params['paySign'] = Support\generate_sign($params, $this->app['config']->key, 'md5');
0 ignored issues
show
Bug introduced by
The function generate_sign was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $params['paySign'] = /** @scrutinizer ignore-call */ Support\generate_sign($params, $this->app['config']->key, 'md5');
Loading history...
51
52
        return $json ? json_encode($params) : $params;
53
    }
54
55
    /**
56
     * [JSSDK] Generate js config for payment.
57
     *
58
     * <pre>
59
     * wx.chooseWXPay({...});
60
     * </pre>
61
     *
62
     * @param string $prepayId
63
     *
64
     * @return array
65
     */
66
    public function sdkConfig(string $prepayId): array
67
    {
68
        $config = $this->bridgeConfig($prepayId, false);
69
70
        $config['timestamp'] = $config['timeStamp'];
71
        unset($config['timeStamp']);
72
73
        return $config;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $config could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    /**
77
     * Generate app payment parameters.
78
     *
79
     * @param string $prepayId
80
     *
81
     * @return array
82
     */
83
    public function appConfig(string $prepayId): array
84
    {
85
        $params = [
86
            'appid' => $this->app['config']->app_id,
87
            'partnerid' => $this->app['config']->mch_id,
88
            'prepayid' => $prepayId,
89
            'noncestr' => uniqid(),
90
            'timestamp' => time(),
91
            'package' => 'Sign=WXPay',
92
        ];
93
94
        $params['sign'] = Support\generate_sign($params, $this->app['config']->key);
0 ignored issues
show
Bug introduced by
The function generate_sign was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        $params['sign'] = /** @scrutinizer ignore-call */ Support\generate_sign($params, $this->app['config']->key);
Loading history...
95
96
        return $params;
97
    }
98
99
    /**
100
     * Generate js config for share user address.
101
     *
102
     * @param string|\Overtrue\Socialite\AccessTokenInterface $accessToken
103
     * @param bool                                            $json
104
     *
105
     * @return string|array
106
     */
107
    public function shareAddressConfig($accessToken, bool $json = true)
108
    {
109
        if ($accessToken instanceof AccessTokenInterface) {
110
            $accessToken = $accessToken->getToken();
111
        }
112
113
        $params = [
114
            'appId' => $this->app['config']->app_id,
115
            'scope' => 'jsapi_address',
116
            'timeStamp' => strval(time()),
117
            'nonceStr' => uniqid(),
118
            'signType' => 'SHA1',
119
        ];
120
121
        $signParams = [
122
            'appid' => $params['appId'],
123
            'url' => $this->getUrl(),
124
            'timestamp' => $params['timeStamp'],
125
            'noncestr' => $params['nonceStr'],
126
            'accesstoken' => strval($accessToken),
127
        ];
128
129
        ksort($signParams);
130
131
        $params['addrSign'] = sha1(urldecode(http_build_query($signParams)));
132
133
        return $json ? json_encode($params) : $params;
134
    }
135
}
136