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 WannanBigPig\Alipay\Kernel\Events\SignFailed; |
17
|
|
|
use WannanBigPig\Alipay\Kernel\Support\Support; |
18
|
|
|
use WannanBigPig\Supports\AccessData; |
19
|
|
|
use WannanBigPig\Supports\Events; |
20
|
|
|
use WannanBigPig\Supports\Logs\Log; |
21
|
|
|
|
22
|
|
|
trait Notify |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $response; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* handle |
37
|
|
|
* |
38
|
|
|
* @param \Closure $closure |
39
|
|
|
* @param mixed $data |
40
|
|
|
* |
41
|
|
|
* @return mixed|\Symfony\Component\HttpFoundation\Response |
42
|
|
|
* |
43
|
|
|
* @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException |
44
|
|
|
*/ |
45
|
1 |
|
protected function handle(Closure $closure, $data = null): Response |
46
|
|
|
{ |
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
|
|
|
$this->response = $this->fail(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return Response::create($this->response); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* setData |
72
|
|
|
* |
73
|
|
|
* @param $data |
74
|
|
|
*/ |
75
|
1 |
|
protected function setData($data) |
76
|
|
|
{ |
77
|
1 |
|
$this->data = $data; |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* getData |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
protected function getData() |
86
|
|
|
{ |
87
|
|
|
return $this->data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* getRequset |
92
|
|
|
* |
93
|
|
|
* @return \WannanBigPig\Supports\AccessData |
94
|
|
|
*/ |
95
|
1 |
|
protected function getRequset(): AccessData |
96
|
|
|
{ |
97
|
1 |
|
if (is_null($this->data)) { |
98
|
|
|
$request = Request::createFromGlobals(); |
99
|
|
|
|
100
|
|
|
$this->data = $request->request->count() > 0 ? $request->request->all() : $request->query->all(); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return new AccessData($this->data); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* notifyIdVerify |
108
|
|
|
* |
109
|
|
|
* @param $partner |
110
|
|
|
* @param $notify_id |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
1 |
|
public function notifyIdVerify($partner, $notify_id) |
115
|
|
|
{ |
116
|
1 |
|
if (Support::$config->get('env') === 'dev') { |
117
|
1 |
|
$res = file_get_contents( |
118
|
1 |
|
"https://mapi.alipaydev.com/gateway.do?service=notify_verify&partner={$partner}¬ify_id={$notify_id}" |
119
|
|
|
); |
120
|
|
|
} else { |
121
|
|
|
$res = file_get_contents( |
122
|
|
|
"https://mapi.alipay.com/gateway.do?service=notify_verify&partner={$partner}¬ify_id={$notify_id}" |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
if ($res === 'true') { |
127
|
1 |
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
Log::error("[ Sign Failed ] 支付宝通知 notify_id 合法性校验失败 " . $res); |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* success |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
1 |
|
public function success() |
141
|
|
|
{ |
142
|
1 |
|
return self::SUCCESS; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* fail |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function fail() |
151
|
|
|
{ |
152
|
|
|
return self::FAIL; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|