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'), |
|
|
|
|
59
|
|
|
Support::$config->get('event.method'), |
|
|
|
|
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(), |
|
|
|
|
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}¬ify_id={$notify_id}" |
131
|
|
|
); |
132
|
|
|
} else { |
133
|
|
|
$res = file_get_contents( |
134
|
|
|
"https://mapi.alipay.com/gateway.do?service=notify_verify&partner={$partner}¬ify_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
|
|
|
|