|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the wannanbigpig/alipay-sdk-php. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) wannanbigpig <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace EasyAlipay\Payment\Notify; |
|
12
|
|
|
|
|
13
|
|
|
use Closure; |
|
14
|
|
|
use EasyAlipay\Kernel\Exceptions\InvalidSignException; |
|
15
|
|
|
use EasyAlipay\Kernel\Traits\Helpers; |
|
16
|
|
|
use EasyAlipay\Payment\Application; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use WannanBigPig\Supports\Collection; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Handle |
|
22
|
|
|
* |
|
23
|
|
|
* @author liuml <[email protected]> |
|
24
|
|
|
* @DateTime 2019-07-23 17:24 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Handle |
|
27
|
|
|
{ |
|
28
|
|
|
use Helpers; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fail = 'fail'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $success = 'success'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $data; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $response; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var \EasyAlipay\Payment\Application |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $app; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Handle constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \EasyAlipay\Payment\Application $app |
|
59
|
|
|
*/ |
|
60
|
14 |
|
public function __construct(Application $app) |
|
61
|
|
|
{ |
|
62
|
14 |
|
$this->app = $app; |
|
63
|
14 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* run. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Closure $closure |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
71
|
|
|
*/ |
|
72
|
14 |
|
public function run(Closure $closure): Response |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
14 |
|
$response = \call_user_func($closure, $this->getMessage(), [$this, 'response']); |
|
76
|
12 |
|
} catch (\Exception $e) { |
|
77
|
12 |
|
$this->app['logger']->error('Alipay asynchronous notification fatal error:'.$e->getMessage()); |
|
78
|
12 |
|
$response = $this->response(false); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
14 |
|
return Response::create($response); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* response. |
|
86
|
|
|
* |
|
87
|
|
|
* @param bool $result |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
14 |
|
public function response(bool $result = true): string |
|
92
|
|
|
{ |
|
93
|
14 |
|
if ($result === true) { |
|
94
|
2 |
|
return $this->success; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
12 |
|
return $this->fail; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* getMessage. |
|
102
|
|
|
* |
|
103
|
|
|
* @return \WannanBigPig\Supports\Collection |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException |
|
106
|
|
|
* @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException |
|
107
|
|
|
*/ |
|
108
|
14 |
|
public function getMessage(): Collection |
|
109
|
|
|
{ |
|
110
|
14 |
|
$signData = $request = $this->app['request']->request->all(); |
|
111
|
14 |
|
unset($signData['sign'], $signData['sign_type']); |
|
112
|
14 |
|
if ($this->verify($this->getSignContentUrlencode($signData), $request['sign'], $request['sign_type'])) { |
|
113
|
2 |
|
return new Collection($request); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
throw new InvalidSignException('Asynchronous notification signature verification failed'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|