Completed
Pull Request — master (#590)
by
unknown
03:20
created

Notify::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
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
/**
13
 * Notify.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Payment;
23
24
use EasyWeChat\Core\Exceptions\FaultException;
25
use EasyWeChat\Support\Collection;
26
use EasyWeChat\Support\XML;
27
use Symfony\Component\HttpFoundation\Request;
28
29
/**
30
 * Class Notify.
31
 */
32
class Notify
33
{
34
    /**
35
     * Merchant instance.
36
     *
37
     * @var \EasyWeChat\Payment\Merchant
38
     */
39
    protected $merchant;
40
41
    /**
42
     * Request instance.
43
     *
44
     * @var \Symfony\Component\HttpFoundation\Request
45
     */
46
    protected $request;
47
48
    /**
49
     * Payment notify (extract from XML).
50
     *
51
     * @var Collection
52
     */
53
    protected $notify;
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param Merchant $merchant
59
     * @param Request  $request
60
     */
61 6
    public function __construct(Merchant $merchant, Request $request = null)
62
    {
63 6
        $this->merchant = $merchant;
64 6
        $this->request = $request ?: Request::createFromGlobals();
65 6
    }
66
67
    /**
68
     * Validate the request params.
69
     *
70
     * @return bool
71
     */
72 1
    public function isValid()
73
    {
74 1
        $localSign = generate_sign($this->getNotify()->except('sign')->all(), $this->merchant->key, 'md5');
75
76 1
        return $localSign === $this->getNotify()->get('sign');
77
    }
78
79
    /**
80
     * Return the notify body from request.
81
     *
82
     * @return \EasyWeChat\Support\Collection
83
     *
84
     * @throws \EasyWeChat\Core\Exceptions\FaultException
85
     */
86 4
    public function getNotify()
87
    {
88 4
        if (!empty($this->notify)) {
89 3
            return $this->notify;
90
        }
91
        try {
92 4
            $xml = XML::parse(strval($this->request->getContent()));
93 1
        } catch (\Throwable $t) {
94 1
            throw new FaultException('Invalid request XML: '.$t->getMessage(), 400);
95
        } catch (\Exception $e) {
96
            throw new FaultException('Invalid request XML: '.$e->getMessage(), 400);
97
        }
98
99 3
        if (!is_array($xml) || empty($xml)) {
100
            throw new FaultException('Invalid request XML.', 400);
101
        }
102
103 3
        return $this->notify = new Collection($xml);
104
    }
105
}
106