Completed
Pull Request — master (#398)
by Carlos
03:31 queued 16s
created

src/Payment/Notify.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Payment;
22
23
use EasyWeChat\Core\Exceptions\FaultException;
24
use EasyWeChat\Support\Collection;
25
use EasyWeChat\Support\XML;
26
use Symfony\Component\HttpFoundation\Request;
27
28
/**
29
 * Class Notify.
30
 */
31
class Notify
32
{
33
    /**
34
     * Merchant instance.
35
     *
36
     * @var \EasyWeChat\Payment\Merchant
37
     */
38
    protected $merchant;
39
40
    /**
41
     * Request instance.
42
     *
43
     * @var \Symfony\Component\HttpFoundation\Request
44
     */
45
    protected $request;
46
47
    /**
48
     * Payment notify (extract from XML).
49
     *
50
     * @var Collection
51
     */
52
    protected $notify;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param Merchant $merchant
58
     * @param Request  $request
59
     */
60 6
    public function __construct(Merchant $merchant, Request $request = null)
61
    {
62 6
        $this->merchant = $merchant;
63 6
        $this->request = $request ?: Request::createFromGlobals();
64 6
    }
65
66
    /**
67
     * Validate the request params.
68
     *
69
     * @return bool
70
     */
71 1
    public function isValid()
72
    {
73 1
        $localSign = generate_sign($this->getNotify()->except('sign')->all(), $this->merchant->key, 'md5');
74
75 1
        return $localSign === $this->getNotify()->get('sign');
76
    }
77
78
    /**
79
     * Return the notify body from request.
80
     *
81
     * @return \EasyWeChat\Support\Collection
82
     *
83
     * @throws \EasyWeChat\Core\Exceptions\FaultException
84
     */
85 4
    public function getNotify()
86
    {
87 4
        if (!empty($this->notify)) {
88 3
            return $this->notify;
89
        }
90
        try {
91 4
            $xml = XML::parse(strval($this->request->getContent()));
92 4
        } catch (\Throwable $t) {
0 ignored issues
show
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
93
            throw new FaultException('Invalid request XML: '.$t->getMessage(), 400);
94 1
        } catch (\Exception $e) {
95 1
            throw new FaultException('Invalid request XML: '.$e->getMessage(), 400);
96
        }
97
98 3
        if (!is_array($xml) || empty($xml)) {
99
            throw new FaultException('Invalid request XML.', 400);
100
        }
101
102 3
        return $this->notify = new Collection($xml);
103
    }
104
}
105