Passed
Push — master ( 59fc67...7db16d )
by Carlos
03:19
created

Application::codeUrlScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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;
13
14
use Closure;
15
use EasyWeChat\BasicService;
16
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
17
use EasyWeChat\Kernel\ServiceContainer;
18
use EasyWeChat\Kernel\Support;
19
use EasyWeChat\OfficialAccount;
20
21
/**
22
 * Class Application.
23
 *
24
 * @property \EasyWeChat\Payment\Bill\Client              $bill
25
 * @property \EasyWeChat\Payment\Jssdk\Client             $jssdk
26
 * @property \EasyWeChat\Payment\Order\Client             $order
27
 * @property \EasyWeChat\Payment\Refund\Client            $refund
28
 * @property \EasyWeChat\Payment\Coupon\Client            $coupon
29
 * @property \EasyWeChat\Payment\Reverse\Client           $reverse
30
 * @property \EasyWeChat\Payment\Redpack\Client           $redpack
31
 * @property \EasyWeChat\BasicService\Url\Client          $url
32
 * @property \EasyWeChat\Payment\Transfer\Client          $transfer
33
 * @property \EasyWeChat\Payment\Security\Client          $security
34
 * @property \EasyWeChat\OfficialAccount\Auth\AccessToken $access_token
35
 *
36
 * @method mixed pay(array $attributes)
37
 * @method mixed authCodeToOpenid(string $authCode)
38
 */
39
class Application extends ServiceContainer
40
{
41
    /**
42
     * @var array
43
     */
44
    protected $providers = [
45
        OfficialAccount\Auth\ServiceProvider::class,
46
        BasicService\Url\ServiceProvider::class,
47
        Base\ServiceProvider::class,
48
        Bill\ServiceProvider::class,
49
        Coupon\ServiceProvider::class,
50
        Jssdk\ServiceProvider::class,
51
        Merchant\ServiceProvider::class,
52
        Order\ServiceProvider::class,
53
        Redpack\ServiceProvider::class,
54
        Refund\ServiceProvider::class,
55
        Reverse\ServiceProvider::class,
56
        Sandbox\ServiceProvider::class,
57
        Transfer\ServiceProvider::class,
58
        Security\ServiceProvider::class,
59
    ];
60
61
    /**
62
     * @var array
63
     */
64
    protected $defaultConfig = [
65
        'http' => [
66
            'base_uri' => 'https://api.mch.weixin.qq.com/',
67
        ],
68
    ];
69
70
    /**
71
     * Build payment scheme for product.
72
     *
73
     * @param string $productId
74
     *
75
     * @return string
76
     */
77
    public function scheme(string $productId): string
78
    {
79
        $params = [
80
            'appid' => $this['config']->app_id,
81
            'mch_id' => $this['config']->mch_id,
82
            'time_stamp' => time(),
83
            'nonce_str' => uniqid(),
84
            'product_id' => $productId,
85
        ];
86
87
        $params['sign'] = Support\generate_sign($params, $this['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

87
        $params['sign'] = /** @scrutinizer ignore-call */ Support\generate_sign($params, $this['config']->key);
Loading history...
88
89
        return 'weixin://wxpay/bizpayurl?'.http_build_query($params);
90
    }
91
92
    /**
93
     * @param string $codeUrl
94
     *
95
     * @return string
96
     */
97
    public function codeUrlScheme(string $codeUrl)
98
    {
99
        return \sprintf('weixin://wxpay/bizpayurl?sr=%s', $codeUrl);
100
    }
101
102
    /**
103
     * @param \Closure $closure
104
     *
105
     * @return \Symfony\Component\HttpFoundation\Response
106
     *
107
     * @codeCoverageIgnore
108
     *
109
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
110
     */
111
    public function handlePaidNotify(Closure $closure)
112
    {
113
        return (new Notify\Paid($this))->handle($closure);
114
    }
115
116
    /**
117
     * @param \Closure $closure
118
     *
119
     * @return \Symfony\Component\HttpFoundation\Response
120
     *
121
     * @codeCoverageIgnore
122
     *
123
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
124
     */
125
    public function handleRefundedNotify(Closure $closure)
126
    {
127
        return (new Notify\Refunded($this))->handle($closure);
128
    }
129
130
    /**
131
     * @param \Closure $closure
132
     *
133
     * @return \Symfony\Component\HttpFoundation\Response
134
     *
135
     * @codeCoverageIgnore
136
     *
137
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
138
     */
139
    public function handleScannedNotify(Closure $closure)
140
    {
141
        return (new Notify\Scanned($this))->handle($closure);
142
    }
143
144
    /**
145
     * Set sub-merchant.
146
     *
147
     * @param string      $mchId
148
     * @param string|null $appId
149
     *
150
     * @return $this
151
     */
152
    public function setSubMerchant(string $mchId, string $appId = null)
153
    {
154
        $this['config']->set('sub_mch_id', $mchId);
155
        $this['config']->set('sub_appid', $appId);
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function inSandbox(): bool
164
    {
165
        return (bool) $this['config']->get('sandbox');
166
    }
167
168
    /**
169
     * @param string|null $endpoint
170
     *
171
     * @return string
172
     *
173
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
174
     */
175
    public function getKey(string $endpoint = null)
176
    {
177
        if ('sandboxnew/pay/getsignkey' === $endpoint) {
178
            return $this['config']->key;
179
        }
180
181
        $key = $this->inSandbox() ? $this['sandbox']->getKey() : $this['config']->key;
182
183
        if (32 !== strlen($key)) {
184
            throw new InvalidArgumentException(sprintf("'%s' should be 32 chars length.", $key));
185
        }
186
187
        return $key;
188
    }
189
190
    /**
191
     * @param string $name
192
     * @param array  $arguments
193
     *
194
     * @return mixed
195
     */
196
    public function __call($name, $arguments)
197
    {
198
        return call_user_func_array([$this['base'], $name], $arguments);
199
    }
200
}
201