Completed
Pull Request — master (#1001)
by mingyoung
01:33
created

Application::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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\ServiceContainer;
17
use EasyWeChat\Kernel\Support;
18
use EasyWeChat\OfficialAccount;
19
20
/**
21
 * Class Application.
22
 *
23
 * @property \EasyWeChat\Payment\Bill\Client               $bill
24
 * @property \EasyWeChat\Payment\Jssdk\Client              $jssdk
25
 * @property \EasyWeChat\Payment\Order\Client              $order
26
 * @property \EasyWeChat\Payment\Refund\Client             $refund
27
 * @property \EasyWeChat\Payment\Coupon\Client             $coupon
28
 * @property \EasyWeChat\Payment\Reverse\Client            $reverse
29
 * @property \EasyWeChat\Payment\Redpack\Client            $redpack
30
 * @property \EasyWeChat\BasicService\Url\Client           $url
31
 * @property \EasyWeChat\Payment\Transfer\Client           $transfer
32
 * @property \EasyWeChat\OfficialAccount\Auth\AccessToken  $access_token
33
 *
34
 * @method mixed pay(array $attributes)
35
 * @method mixed authCodeToOpenid(string $authCode)
36
 */
37
class Application extends ServiceContainer
38
{
39
    /**
40
     * @var array
41
     */
42
    protected $providers = [
43
        OfficialAccount\Auth\ServiceProvider::class,
44
        BasicService\Url\ServiceProvider::class,
45
        Base\ServiceProvider::class,
46
        Bill\ServiceProvider::class,
47
        Coupon\ServiceProvider::class,
48
        Jssdk\ServiceProvider::class,
49
        Merchant\ServiceProvider::class,
50
        Order\ServiceProvider::class,
51
        Redpack\ServiceProvider::class,
52
        Refund\ServiceProvider::class,
53
        Reverse\ServiceProvider::class,
54
        Sandbox\ServiceProvider::class,
55
        Transfer\ServiceProvider::class,
56
    ];
57
58
    /**
59
     * @var array
60
     */
61
    protected $defaultConfig = [
62
        'http' => [
63
            'base_uri' => 'https://api.mch.weixin.qq.com/',
64
        ],
65
    ];
66
67
    /**
68
     * Build payment scheme for product.
69
     *
70
     * @param string $productId
71
     *
72
     * @return string
73
     */
74
    public function scheme(string $productId): string
75
    {
76
        $params = [
77
            'appid' => $this['config']->app_id,
78
            'mch_id' => $this['config']->mch_id,
79
            'time_stamp' => time(),
80
            'nonce_str' => uniqid(),
81
            'product_id' => $productId,
82
        ];
83
84
        $params['sign'] = Support\generate_sign($params, $this['config']->key);
85
86
        return 'weixin://wxpay/bizpayurl?'.http_build_query($params);
87
    }
88
89
    /**
90
     * @param \Closure $closure
91
     *
92
     * @return \Symfony\Component\HttpFoundation\Response
93
     *
94
     * @codeCoverageIgnore
95
     */
96
    public function handlePaidNotify(Closure $closure)
97
    {
98
        return (new Notify\Paid($this))->handle($closure);
99
    }
100
101
    /**
102
     * @param \Closure $closure
103
     *
104
     * @return \Symfony\Component\HttpFoundation\Response
105
     *
106
     * @codeCoverageIgnore
107
     */
108
    public function handleRefundedNotify(Closure $closure)
109
    {
110
        return (new Notify\Refunded($this))->handle($closure);
111
    }
112
113
    /**
114
     * @param \Closure $closure
115
     *
116
     * @return \Symfony\Component\HttpFoundation\Response
117
     *
118
     * @codeCoverageIgnore
119
     */
120
    public function handleScannedNotify(Closure $closure)
121
    {
122
        return (new Notify\Scanned($this))->handle($closure);
123
    }
124
125
    /**
126
     * Set sub-merchant.
127
     *
128
     * @param string      $mchId
129
     * @param string|null $appId
130
     *
131
     * @return $this
132
     */
133
    public function setSubMerchant(string $mchId, string $appId = null)
134
    {
135
        $this['config']->set('sub_mch_id', $mchId);
136
        $this['config']->set('sub_appid', $appId);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function inSandbox(): bool
145
    {
146
        return (bool) $this['config']->get('sandbox');
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getKey()
153
    {
154
        return $this->inSandbox() ? $this['sandbox']->getKey() : $this['config']->key;
155
    }
156
157
    /**
158
     * @param string $name
159
     * @param array  $arguments
160
     *
161
     * @return mixed
162
     */
163
    public function __call($name, $arguments)
164
    {
165
        return call_user_func_array([$this['base'], $name], $arguments);
166
    }
167
}
168