Completed
Pull Request — master (#976)
by mingyoung
01:31
created

Application::setSubMerchant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
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
        Order\ServiceProvider::class,
50
        Redpack\ServiceProvider::class,
51
        Refund\ServiceProvider::class,
52
        Reverse\ServiceProvider::class,
53
        Sandbox\ServiceProvider::class,
54
        Transfer\ServiceProvider::class,
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    protected $defaultConfig = [
61
        'http' => [
62
            'base_uri' => 'https://api.mch.weixin.qq.com/',
63
        ],
64
    ];
65
66
    /**
67
     * Build payment scheme for product.
68
     *
69
     * @param string $productId
70
     *
71
     * @return string
72
     */
73
    public function scheme(string $productId): string
74
    {
75
        $params = [
76
            'appid' => $this['config']->app_id,
77
            'mch_id' => $this['config']->mch_id,
78
            'time_stamp' => time(),
79
            'nonce_str' => uniqid(),
80
            'product_id' => $productId,
81
        ];
82
83
        $params['sign'] = Support\generate_sign($params, $this['config']->key);
84
85
        return 'weixin://wxpay/bizpayurl?'.http_build_query($params);
86
    }
87
88
    /**
89
     * @param \Closure $closure
90
     *
91
     * @return \Symfony\Component\HttpFoundation\Response
92
     */
93
    public function handlePaidNotify(Closure $closure)
94
    {
95
        return (new Notify\Paid($this))->handle($closure);
96
    }
97
98
    /**
99
     * @param \Closure $closure
100
     *
101
     * @return \Symfony\Component\HttpFoundation\Response
102
     */
103
    public function handleRefundedNotify(Closure $closure)
104
    {
105
        return (new Notify\Refunded($this))->handle($closure);
106
    }
107
108
    /**
109
     * @param \Closure $closure
110
     *
111
     * @return \Symfony\Component\HttpFoundation\Response
112
     */
113
    public function handleScannedNotify(Closure $closure)
114
    {
115
        return (new Notify\Scanned($this))->handle($closure);
116
    }
117
118
    /**
119
     * Set sub-merchant.
120
     *
121
     * @param string      $mchId
122
     * @param string|null $appId
123
     *
124
     * @return $this
125
     */
126
    public function setSubMerchant(string $mchId, string $appId = null)
127
    {
128
        $this['config']->set('sub_mch_id', $mchId);
129
        $this['config']->set('sub_appid', $appId);
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function inSandbox(): bool
138
    {
139
        return (bool) $this['config']->get('sandbox');
140
    }
141
142
    /**
143
     * @param string $name
144
     * @param array  $arguments
145
     *
146
     * @return mixed
147
     */
148
    public function __call($name, $arguments)
149
    {
150
        return call_user_func_array([$this['base'], $name], $arguments);
151
    }
152
}
153