Passed
Push — master ( c43396...56f253 )
by wannanbigpig
02:27
created

Notify::success()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Notify.php
4
 *
5
 * Created by PhpStorm.
6
 *
7
 * author: liuml  <[email protected]>
8
 * DateTime: 2019-04-16  17:20
9
 */
10
11
namespace WannanBigPig\Alipay\Notify;
12
13
use Closure;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Throwable;
17
use WannanBigPig\Alipay\Kernel\Events\SignFailed;
18
use WannanBigPig\Alipay\Kernel\Support\Support;
19
use WannanBigPig\Supports\AccessData;
20
use WannanBigPig\Supports\Events;
21
use WannanBigPig\Supports\Logs\Log;
22
23
trait Notify
24
{
25
26
    /**
27
     * @var array
28
     */
29
    protected $data;
30
31
    /**
32
     * @var string
33
     */
34
    protected $response;
35
36
    /**
37
     * handle
38
     *
39
     * @param  \Closure    $closure
40
     * @param  array|null  $data
41
     *
42
     * @return \Symfony\Component\HttpFoundation\Response
43
     */
44 1
    protected function handle(Closure $closure, $data = null): Response
45
    {
46
        try {
47 1
            $this->setData($data);
48 1
            $request = $this->getRequset();
49 1
            Log::info('支付宝回调数据', $request->get());
50
51
            // 签名验证
52 1
            if (Support::notifyVerify($request->get())) {
53 1
                $this->response = call_user_func($closure, $request, $this);
54
            } else {
55
                Events::dispatch(
56
                    SignFailed::NAME,
57
                    new SignFailed(
58
                        Support::$config->get('event.driver'),
0 ignored issues
show
Bug introduced by
It seems like WannanBigPig\Alipay\Kern...ig->get('event.driver') can also be of type array; however, parameter $driver of WannanBigPig\Alipay\Kern...gnFailed::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
                        /** @scrutinizer ignore-type */ Support::$config->get('event.driver'),
Loading history...
59
                        Support::$config->get('event.method'),
0 ignored issues
show
Bug introduced by
It seems like WannanBigPig\Alipay\Kern...ig->get('event.method') can also be of type array; however, parameter $method of WannanBigPig\Alipay\Kern...gnFailed::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                        /** @scrutinizer ignore-type */ Support::$config->get('event.method'),
Loading history...
60
                        $this->getData(),
61
                        '支付宝异步通知请求参数验签失败'
62
                    )
63
                );
64 1
                $this->response = $this->fail();
65
            }
66
        } catch (Throwable $e) {
67
            Events::dispatch(
68
                SignFailed::NAME,
69
                new SignFailed(
70
                    Support::$config->get('event.driver'),
71
                    Support::$config->get('event.method'),
72
                    $this->getData(),
0 ignored issues
show
Bug introduced by
It seems like $this->getData() can also be of type null; however, parameter $result of WannanBigPig\Alipay\Kern...gnFailed::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                    /** @scrutinizer ignore-type */ $this->getData(),
Loading history...
73
                    $e->getMessage()
74
                )
75
            );
76
            $this->response = $this->fail();
77
        }
78
79 1
        return Response::create($this->response);
80
    }
81
82
    /**
83
     * setData
84
     *
85
     * @param $data
86
     */
87 1
    protected function setData($data)
88
    {
89 1
        $this->data = $data;
90 1
    }
91
92
    /**
93
     * getData
94
     *
95
     * @return mixed
96
     */
97
    protected function getData()
98
    {
99
        return $this->data;
100
    }
101
102
    /**
103
     * getRequset
104
     *
105
     * @return \WannanBigPig\Supports\AccessData
106
     */
107 1
    protected function getRequset(): AccessData
108
    {
109 1
        if (is_null($this->data)) {
110
            $request = Request::createFromGlobals();
111
112
            $this->data = $request->request->count() > 0 ? $request->request->all() : $request->query->all();
113
        }
114
115 1
        return new AccessData($this->data);
116
    }
117
118
    /**
119
     * notifyIdVerify
120
     *
121
     * @param $partner
122
     * @param $notify_id
123
     *
124
     * @return bool
125
     */
126 1
    public function notifyIdVerify($partner, $notify_id)
127
    {
128 1
        if (Support::$config->get('env') === 'dev') {
129 1
            $res = file_get_contents(
130 1
                "https://mapi.alipaydev.com/gateway.do?service=notify_verify&partner={$partner}&notify_id={$notify_id}"
131
            );
132
        } else {
133
            $res = file_get_contents(
134
                "https://mapi.alipay.com/gateway.do?service=notify_verify&partner={$partner}&notify_id={$notify_id}"
135
            );
136
        }
137
138 1
        if ($res === 'true') {
139 1
            return true;
140
        }
141
142
        Log::error("[ Sign Failed ] 支付宝通知 notify_id 合法性校验失败 " . $res);
143
144
        return false;
145
    }
146
147
    /**
148
     * success
149
     *
150
     * @return string
151
     */
152 1
    public function success()
153
    {
154 1
        return self::SUCCESS;
155
    }
156
157
    /**
158
     * fail
159
     *
160
     * @return string
161
     */
162
    public function fail()
163
    {
164
        return self::FAIL;
165
    }
166
}
167