Passed
Push — master ( 34feb7...8dc288 )
by wannanbigpig
02:47
created

Notify::handle()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.3353

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 9
nop 2
dl 0
loc 27
ccs 8
cts 17
cp 0.4706
crap 4.3353
rs 9.6666
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 Exception;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
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'),
59
                        Support::$config->get('event.method'),
60
                        $this->getData(),
61
                        'Notification request parameter validation signature failed'
62
                    )
63
                );
64 1
                $this->response = $this->fail();
65
            }
66
        } catch (Exception $e) {
67
            $this->response = $this->fail();
68
        }
69
70 1
        return Response::create($this->response);
71
    }
72
73
    /**
74
     * setData
75
     *
76
     * @param $data
77
     */
78 1
    protected function setData($data)
79
    {
80 1
        $this->data = $data;
81 1
    }
82
83
    /**
84
     * getData
85
     *
86
     * @return mixed
87
     */
88
    protected function getData()
89
    {
90
        return $this->data;
91
    }
92
93
    /**
94
     * getRequset
95
     *
96
     * @return \WannanBigPig\Supports\AccessData
97
     */
98 1
    protected function getRequset(): AccessData
99
    {
100 1
        if (is_null($this->data)) {
101
            $request = Request::createFromGlobals();
102
103
            $this->data = $request->request->count() > 0 ? $request->request->all() : $request->query->all();
104
        }
105
106 1
        return new AccessData($this->data);
107
    }
108
109
    /**
110
     * notifyIdVerify
111
     *
112
     * @param $partner
113
     * @param $notify_id
114
     *
115
     * @return bool
116
     */
117 1
    public function notifyIdVerify($partner, $notify_id)
118
    {
119 1
        if (Support::$config->get('env') === 'dev') {
120 1
            $res = file_get_contents(
121 1
                "https://mapi.alipaydev.com/gateway.do?service=notify_verify&partner={$partner}&notify_id={$notify_id}"
122
            );
123
        } else {
124
            $res = file_get_contents(
125
                "https://mapi.alipay.com/gateway.do?service=notify_verify&partner={$partner}&notify_id={$notify_id}"
126
            );
127
        }
128
129 1
        if ($res === 'true') {
130 1
            return true;
131
        }
132
133
        Log::error("[ Sign Failed ] 支付宝通知 notify_id 合法性校验失败 " . $res);
134
135
        return false;
136
    }
137
138
    /**
139
     * success
140
     *
141
     * @return string
142
     */
143 1
    public function success()
144
    {
145 1
        return self::SUCCESS;
146
    }
147
148
    /**
149
     * fail
150
     *
151
     * @return string
152
     */
153
    public function fail()
154
    {
155
        return self::FAIL;
156
    }
157
}
158