Completed
Push — master ( 623373...511182 )
by Carlos
36s
created

Payment   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 3
Metric Value
wmc 20
c 8
b 1
f 3
lcom 1
cbo 8
dl 0
loc 234
ccs 77
cts 77
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A scheme() 0 14 1
B handleNotify() 0 27 4
A configForPayment() 0 14 2
A configForAppPayment() 0 15 1
B configForShareAddress() 0 26 3
A setMerchant() 0 4 1
A getMerchant() 0 4 1
A getNotify() 0 4 1
A setAPI() 0 4 1
A getAPI() 0 4 2
A __call() 0 6 2
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
/**
13
 * Payment.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Payment;
22
23
use EasyWeChat\Core\AccessToken;
24
use EasyWeChat\Core\Exceptions\FaultException;
25
use EasyWeChat\Support\Url as UrlHelper;
26
use EasyWeChat\Support\XML;
27
use Symfony\Component\HttpFoundation\Response;
28
29
/**
30
 * Class Payment.
31
 */
32
class Payment
33
{
34
    /**
35
     * Scheme base path.
36
     */
37
    const SCHEME_PATH = 'weixin://wxpay/bizpayurl';
38
39
    /**
40
     * @var API
41
     */
42
    protected $api;
43
44
    /**
45
     * Merchant instance.
46
     *
47
     * @var \EasyWeChat\Payment\Merchant
48
     */
49
    protected $merchant;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param Merchant $merchant
55
     */
56 8
    public function __construct(Merchant $merchant)
57
    {
58 8
        $this->merchant = $merchant;
59 8
    }
60
61
    /**
62
     * Build payment scheme for product.
63
     *
64
     * @param string $productId
65
     *
66
     * @return string
67
     */
68 1
    public function scheme($productId)
69
    {
70
        $params = [
71 1
            'appid' => $this->merchant->app_id,
72 1
            'mch_id' => $this->merchant->merchant_id,
73 1
            'time_stamp' => time(),
74 1
            'nonce_str' => uniqid(),
75 1
            'product_id' => $productId,
76 1
        ];
77
78 1
        $params['sign'] = generate_sign($params, $this->merchant->key, 'md5');
79
80 1
        return self::SCHEME_PATH.'?'.http_build_query($params);
81
    }
82
83
    /**
84
     * Handle payment notify.
85
     *
86
     * @param callable $callback
87
     *
88
     * @return Response
89
     */
90 2
    public function handleNotify(callable $callback)
91
    {
92 2
        $notify = $this->getNotify();
93
94 2
        if (!$notify->isValid()) {
95 1
            throw new FaultException('Invalid request XML.', 400);
96
        }
97
98 1
        $notify = $notify->getNotify();
99 1
        $successful = $notify->get('result_code') === 'SUCCESS';
100
101 1
        $handleResult = call_user_func_array($callback, [$notify, $successful]);
102
103 1
        if (is_bool($handleResult) && $handleResult) {
104
            $response = [
105 1
                'return_code' => 'SUCCESS',
106 1
                'return_msg' => 'OK',
107 1
            ];
108 1
        } else {
109
            $response = [
110 1
                'return_code' => 'FAIL',
111 1
                'return_msg' => $handleResult,
112 1
            ];
113
        }
114
115 1
        return new Response(XML::build($response));
116
    }
117
118
    /**
119
     * Gernerate js config for payment.
120
     *
121
     * @param string $prepayId
122
     * @param bool   $json
123
     *
124
     * @return string|array
125
     */
126 1
    public function configForPayment($prepayId, $json = true)
127
    {
128
        $params = [
129 1
            'appId' => $this->merchant->app_id,
130 1
            'timeStamp' => strval(time()),
131 1
            'nonceStr' => uniqid(),
132 1
            'package' => "prepay_id=$prepayId",
133 1
            'signType' => 'MD5',
134 1
        ];
135
136 1
        $params['paySign'] = generate_sign($params, $this->merchant->key, 'md5');
137
138 1
        return $json ? json_encode($params) : $params;
139
    }
140
141
    /**
142
     * Generate app payment parameters.
143
     *
144
     * @param string $prepayId
145
     *
146
     * @return array
147
     */
148 1
    public function configForAppPayment($prepayId)
149
    {
150
        $params = [
151 1
            'appid' => $this->merchant->app_id,
152 1
            'partnerid' => $this->merchant->merchant_id,
153 1
            'prepayid' => $prepayId,
154 1
            'noncestr' => uniqid(),
155 1
            'timestamp' => time(),
156 1
            'package' => 'Sign=WXPay',
157 1
        ];
158
159 1
        $params['sign'] = generate_sign($params, $this->merchant->key);
160
161 1
        return $params;
162
    }
163
164
    /**
165
     * Generate js config for share user address.
166
     *
167
     * @param string|accessToken $accessToken
168
     * @param bool               $json
169
     *
170
     * @return string|array
171
     */
172 2
    public function configForShareAddress($accessToken, $json = true)
173
    {
174 1
        if ($accessToken instanceof AccessToken) {
175 1
            $accessToken = $accessToken->getToken();
176 1
        }
177
178
        $params = [
179 1
            'appId' => $this->merchant->app_id,
180 1
            'scope' => 'jsapi_address',
181 1
            'timeStamp' => strval(time()),
182 1
            'nonceStr' => uniqid(),
183 1
            'signType' => 'SHA1',
184 1
        ];
185
186
        $signParams = [
187 1
            'appid' => $params['appId'],
188 1
            'url' => UrlHelper::current(),
189 1
            'timestamp' => $params['timeStamp'],
190 1
            'noncestr' => $params['nonceStr'],
191 1
            'accesstoken' => $accessToken,
192 1
        ];
193
194 1
        $params['addrSign'] = generate_sign($signParams, $this->merchant->key, 'sha1');
195
196 2
        return $json ? json_encode($params) : $params;
197
    }
198
199
    /**
200
     * Merchant setter.
201
     *
202
     * @param Merchant $merchant
203
     */
204 1
    public function setMerchant(Merchant $merchant)
205
    {
206 1
        $this->merchant = $merchant;
207 1
    }
208
209
    /**
210
     * Merchant getter.
211
     *
212
     * @return Merchant
213
     */
214 1
    public function getMerchant()
215
    {
216 1
        return $this->merchant;
217
    }
218
219
    /**
220
     * Return Notify instance.
221
     *
222
     * @return \EasyWeChat\Payment\Notify
223
     */
224 1
    public function getNotify()
225
    {
226 1
        return new Notify($this->merchant);
227
    }
228
229
    /**
230
     * API setter.
231
     *
232
     * @param API $api
233
     */
234 1
    public function setAPI(API $api)
235
    {
236 1
        $this->api = $api;
237 1
    }
238
239
    /**
240
     * Return API instance.
241
     *
242
     * @return API
243
     */
244 1
    public function getAPI()
245
    {
246 1
        return $this->api ?: $this->api = new API($this->getMerchant());
247
    }
248
249
    /**
250
     * Magic call.
251
     *
252
     * @param string $method
253
     * @param array  $args
254
     *
255
     * @return mixed
256
     *
257
     * @codeCoverageIgnore
258
     */
259
    public function __call($method, $args)
260
    {
261
        if (is_callable([$this->getAPI(), $method])) {
262
            return call_user_func_array([$this->api, $method], $args);
263
        }
264
    }
265
}
266