Test Setup Failed
Push — master ( 268613...cb859e )
by Carlos
03:04
created

Client::contractConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 2
rs 10
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 1
    public function bridgeConfig(string $prepayId, bool $json = true)
41
    {
42
        $params = [
43 1
            'appId' => $this->app['config']->sub_appid ?: $this->app['config']->app_id,
44 1
            'timeStamp' => strval(time()),
45 1
            'nonceStr' => uniqid(),
46 1
            'package' => "prepay_id=$prepayId",
47 1
            'signType' => 'MD5',
48
        ];
49
50 1
        $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 1
        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 1
    public function sdkConfig(string $prepayId): array
67
    {
68 1
        $config = $this->bridgeConfig($prepayId, false);
69
70 1
        $config['timestamp'] = $config['timeStamp'];
71 1
        unset($config['timeStamp']);
72
73 1
        return $config;
74
    }
75
76
    /**
77
     * Generate app payment parameters.
78
     *
79
     * @param string $prepayId
80
     *
81
     * @return array
82
     */
83 1
    public function appConfig(string $prepayId): array
84
    {
85
        $params = [
86 1
            'appid' => $this->app['config']->app_id,
87 1
            'partnerid' => $this->app['config']->mch_id,
88 1
            'prepayid' => $prepayId,
89 1
            'noncestr' => uniqid(),
90 1
            'timestamp' => time(),
91 1
            'package' => 'Sign=WXPay',
92
        ];
93
94 1
        $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 1
        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 1
    public function shareAddressConfig($accessToken, bool $json = true)
108
    {
109 1
        if ($accessToken instanceof AccessTokenInterface) {
110 1
            $accessToken = $accessToken->getToken();
111
        }
112
113
        $params = [
114 1
            'appId' => $this->app['config']->app_id,
115 1
            'scope' => 'jsapi_address',
116 1
            'timeStamp' => strval(time()),
117 1
            'nonceStr' => uniqid(),
118 1
            'signType' => 'SHA1',
119
        ];
120
121
        $signParams = [
122 1
            'appid' => $params['appId'],
123 1
            'url' => $this->getUrl(),
124 1
            'timestamp' => $params['timeStamp'],
125 1
            'noncestr' => $params['nonceStr'],
126 1
            'accesstoken' => strval($accessToken),
127
        ];
128
129 1
        ksort($signParams);
130
131 1
        $params['addrSign'] = sha1(urldecode(http_build_query($signParams)));
132
133 1
        return $json ? json_encode($params) : $params;
134
    }
135
136
    /**
137
     * Generate js config for contract of mini program
138
     *
139
     * @param array $params
140
     * @return array
141
     */
142
    public function contractConfig(array $params): array
143
    {
144
        $params['appid'] = $this->app['config']->app_id;
145
        $params['timestamp'] = time();
146
147
        $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

147
        $params['sign'] = /** @scrutinizer ignore-call */ Support\generate_sign($params, $this->app['config']->key);
Loading history...
148
149
        return $params;
150
    }
151
}
152