1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaylyu\Alipay\F2fpay\Notify; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Kaylyu\Alipay\Kernel\Exceptions\Exception; |
7
|
|
|
|
8
|
|
|
abstract class Handler |
9
|
|
|
{ |
10
|
|
|
const SUCCESS = 'success'; |
11
|
|
|
const FAILED = 'failed'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var |
15
|
|
|
* @author kaylv <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $message; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $fail; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $attributes = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check sign. |
36
|
|
|
* If failed, throws an exception. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $check = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Respond with sign. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $sign = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Handler constructor. |
51
|
|
|
* @param $app |
52
|
|
|
*/ |
53
|
|
|
public function __construct($app) |
54
|
|
|
{ |
55
|
|
|
$this->app = $app; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 处理验签入口 |
60
|
|
|
* @param Closure $closure |
61
|
|
|
* @author kaylv <[email protected]> |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
abstract public function handle(Closure $closure); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $message |
68
|
|
|
*/ |
69
|
|
|
public function fail(string $message) |
70
|
|
|
{ |
71
|
|
|
$this->fail = $message; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* 响应数据 |
76
|
|
|
* @author kaylv <[email protected]> |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function toResponse() |
80
|
|
|
{ |
81
|
|
|
return is_null($this->fail) ? self::SUCCESS : self::FAILED; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 获取参数并验签 |
86
|
|
|
* @author kaylv <[email protected]> |
87
|
|
|
* @return array |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
|
|
public function getMessage(): array |
91
|
|
|
{ |
92
|
|
|
if (!empty($this->message)) { |
93
|
|
|
return $this->message; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//获取参数 |
97
|
|
|
$message = $this->getInput(); |
98
|
|
|
|
99
|
|
|
if (empty($message)) { |
100
|
|
|
throw new Exception('Invalid Request.', 400); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->check) { |
104
|
|
|
$this->validate($message); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->message = $message; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 验签 |
112
|
|
|
* @param $message |
113
|
|
|
* @author kaylv <[email protected]> |
114
|
|
|
* @throws Exception |
115
|
|
|
*/ |
116
|
|
|
protected function validate($message) |
117
|
|
|
{ |
118
|
|
|
throw new Exception('Invalid AopClient.', 400); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* 处理入口 |
123
|
|
|
* @param mixed $result |
124
|
|
|
*/ |
125
|
|
|
protected function strict($result) |
126
|
|
|
{ |
127
|
|
|
if (true !== $result && is_null($this->fail)) { |
128
|
|
|
$this->fail(strval($result)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* 获取参数 |
134
|
|
|
* @author kaylv <[email protected]> |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
protected function getInput() |
138
|
|
|
{ |
139
|
|
|
//获取请求参数 |
140
|
|
|
$content = $this->app['request']->all(); |
141
|
|
|
|
142
|
|
|
return $content; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|