|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Graham Owens ([email protected]) |
|
4
|
|
|
* Company: PartFire Ltd (www.partfire.co.uk) |
|
5
|
|
|
* Console: Discovery |
|
6
|
|
|
* |
|
7
|
|
|
* User: gra |
|
8
|
|
|
* Date: 20/01/17 |
|
9
|
|
|
* Time: 09:52 |
|
10
|
|
|
* Project: PartFire MangoPay Bundle |
|
11
|
|
|
* File: HookHandleService.php |
|
12
|
|
|
* |
|
13
|
|
|
**/ |
|
14
|
|
|
|
|
15
|
|
|
namespace PartFire\MangoPayBundle\Services; |
|
16
|
|
|
|
|
17
|
|
|
use Composer\EventDispatcher\Event; |
|
18
|
|
|
use Fruitful\FFConstants; |
|
19
|
|
|
use PartFire\CommonBundle\Services\Output\CommonOutputInterface; |
|
20
|
|
|
use PartFire\MangoPayBundle\Entity\Hook; |
|
|
|
|
|
|
21
|
|
|
use PartFire\MangoPayBundle\Entity\Repository\HookRepository; |
|
22
|
|
|
use PartFire\MangoPayBundle\Entity\Repository\MangoPayRepositoryFactory; |
|
23
|
|
|
use PartFire\MangoPayBundle\Event\MangoPayWebhookEvent; |
|
24
|
|
|
use PartFire\MangoPayBundle\MangoPayConstants; |
|
25
|
|
|
use PartFire\SlackBundle\Services\SlackService; |
|
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
27
|
|
|
|
|
28
|
|
|
class HookHandleService |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var IdentityCheckFactoryRepository |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $mangoPayRepositoryFactory; |
|
34
|
|
|
protected $slackService; |
|
35
|
|
|
private $payIn; |
|
36
|
|
|
private $payOut; |
|
37
|
|
|
private $transfer; |
|
38
|
|
|
private $kyc; |
|
39
|
|
|
|
|
40
|
|
|
const CHECK_COMPLETE = "check.completed"; |
|
41
|
|
|
const REPORT_COMPLETE = "report.completed"; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
MangoPayRepositoryFactory $mangoPayRepositoryFactory, |
|
46
|
|
|
SlackService $slackService, |
|
47
|
|
|
EventDispatcherInterface $event, |
|
48
|
|
|
PayIn $payIn, |
|
49
|
|
|
PayOut $payOut, |
|
50
|
|
|
Transfer $transfer, |
|
51
|
|
|
Kyc $kyc |
|
52
|
|
|
) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->mangoPayRepositoryFactory = $mangoPayRepositoryFactory; |
|
55
|
|
|
$this->slackService = $slackService; |
|
56
|
|
|
$this->event = $event; |
|
|
|
|
|
|
57
|
|
|
$this->payIn = $payIn; |
|
58
|
|
|
$this->payOut = $payOut; |
|
59
|
|
|
$this->transfer = $transfer; |
|
60
|
|
|
$this->kyc = $kyc; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function processRequest($getArray) |
|
64
|
|
|
{ |
|
65
|
|
|
$formattedJson = json_encode($getArray); |
|
66
|
|
|
$this->sendMessage( |
|
67
|
|
|
"New Webhook received from MangoPay \n ```" . $formattedJson . "```", |
|
68
|
|
|
':leftwards_arrow_with_hook:' |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$hookQueueEntity = new Hook(); |
|
72
|
|
|
$hookQueueEntity->setEventType($getArray[MangoPayConstants::HOOK_EVENT_TYPE]); |
|
73
|
|
|
$hookQueueEntity->setResourceId($getArray[MangoPayConstants::HOOK_RESOURCE_ID]); |
|
74
|
|
|
$hookQueueEntity->setDate($getArray[MangoPayConstants::HOOK_DATE]); |
|
75
|
|
|
|
|
76
|
|
|
$hookQueueEntity->setRawHookData($formattedJson); |
|
77
|
|
|
|
|
78
|
|
|
$this->mangoPayRepositoryFactory->saveAndGetEntity($hookQueueEntity); |
|
79
|
|
|
|
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function processNewWebhooks(CommonOutputInterface $output) |
|
84
|
|
|
{ |
|
85
|
|
|
$output->info(count($this->getAllNewWebhooks()) . " new hooks to check"); |
|
86
|
|
|
|
|
87
|
|
|
foreach($this->getAllNewWebhooks() as $webHook) { |
|
88
|
|
|
$output->infoid(" Processing hook ID " . $webHook->getResourceId() . " - Action Type = " . $webHook->getEventType()); |
|
89
|
|
|
try { |
|
90
|
|
|
$this->processWebhook($webHook, $output); |
|
91
|
|
|
} catch (\Exception $e) { |
|
92
|
|
|
$output->error($e->getMessage()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function processWebhook(Hook $hook, CommonOutputInterface $commonOutput) |
|
99
|
|
|
{ |
|
100
|
|
|
$hook = $this->setWebhookInProgress($hook); |
|
101
|
|
|
if (MangoPayConstants::isEventTypeOk($hook->getEventType())) { |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
} else { |
|
104
|
|
|
$this->sendErrorMessage( |
|
105
|
|
|
"Received webhook from MangoPay *" . $hook->getEventType() ."*, which is unknown. Firing event anyway \n ```" . json_encode($hook->getRawHookData()) . "```", |
|
106
|
|
|
':anguished:' |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$dtoResponse = $this->getDTOReponse($hook, $commonOutput); |
|
111
|
|
|
$hook->setDto($dtoResponse); |
|
112
|
|
|
$hook = $this->mangoPayRepositoryFactory->saveAndGetEntity($hook); |
|
113
|
|
|
|
|
114
|
|
|
$webhookEvent = new MangoPayWebhookEvent(); |
|
115
|
|
|
$webhookEvent->setOutput($commonOutput); |
|
116
|
|
|
$webhookEvent->setHook($hook); |
|
117
|
|
|
$webhookEvent->setDto($dtoResponse); |
|
118
|
|
|
|
|
119
|
|
|
$this->event->dispatch(MangoPayWebhookEvent::NAME, $webhookEvent); |
|
120
|
|
|
$this->setWebhookToActioned($hook); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function getDTOReponse(Hook $hook, CommonOutputInterface $commonOutput) |
|
124
|
|
|
{ |
|
125
|
|
|
$dto = null; |
|
126
|
|
|
$errorMsg = null; |
|
127
|
|
|
|
|
128
|
|
|
if (MangoPayConstants::isEventTypeToDoWithPayInNormal($hook->getEventType())) { |
|
129
|
|
|
$dto = $this->payIn->getPayIn($hook->getResourceId()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (MangoPayConstants::isEventTypeToDoWithPayOutNormal($hook->getEventType())) { |
|
133
|
|
|
$dto = $this->payOut->get($hook->getResourceId()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (MangoPayConstants::isEventTypeToDoWithTransferNormal($hook->getEventType())) { |
|
137
|
|
|
$dto = $this->transfer->get($hook->getResourceId()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (MangoPayConstants::isEventTypeToDoWithPayInRefund($hook->getEventType())) { |
|
141
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (MangoPayConstants::isEventTypeToDoWithPayOutRefund($hook->getEventType())) { |
|
145
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (MangoPayConstants::isEventTypeToDoWithTransferRefund($hook->getEventType())) { |
|
149
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if (MangoPayConstants::isEventTypeToDoWithPayInRepudiation($hook->getEventType())) { |
|
153
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (MangoPayConstants::isEventTypeToDoWithKYC($hook->getEventType())) { |
|
157
|
|
|
$dto = $this->kyc->getDocument($hook->getResourceId()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (MangoPayConstants::isEventTypeToDoWithDisputeDocument($hook->getEventType())) { |
|
161
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if (MangoPayConstants::isEventTypeToDoWithDispute($hook->getEventType())) { |
|
165
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (MangoPayConstants::isEventTypeToDoWithTransferSettlement($hook->getEventType())) { |
|
169
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (MangoPayConstants::isEventTypeToDoWithMandate($hook->getEventType())) { |
|
173
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (MangoPayConstants::isEventTypeToDoWithPreauthorisation($hook->getEventType())) { |
|
177
|
|
|
$errorMsg = $hook->getEventType() . " NOT YET HANDLED. NEEDS CODING UP!"; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if (!is_null($errorMsg)) { |
|
181
|
|
|
$this->sendMessage( |
|
182
|
|
|
"Error, no code to action HookHandleService \n ```" . $errorMsg . "```", |
|
183
|
|
|
':leftwards_arrow_with_hook:' |
|
184
|
|
|
); |
|
185
|
|
|
$commonOutput->error($errorMsg); |
|
186
|
|
|
|
|
187
|
|
|
throw new \Exception("Inbound hook with event " . $hook->getEventType() . " not handled!"); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $dto; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
private function setWebhookInProgress(Hook $hook) : Hook |
|
194
|
|
|
{ |
|
195
|
|
|
$hook->setStatus(MangoPayConstants::HOOK_IN_PROGRESS); |
|
196
|
|
|
return $this->mangoPayRepositoryFactory->saveAndGetEntity($hook); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
private function setWebhookToActioned(Hook $hook) |
|
200
|
|
|
{ |
|
201
|
|
|
$hook->setStatus(MangoPayConstants::HOOK_ACTIONED); |
|
202
|
|
|
$this->mangoPayRepositoryFactory->saveAndGetEntity($hook); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
private function getIdentityCheckReportByOnfidoReportId(OnfidoHookQueue $onfidoHookQueue) : ?IdentityCheckReport |
|
|
|
|
|
|
206
|
|
|
{ |
|
207
|
|
|
return $this->mangoPayRepositoryFactory->getIdentityCheckReport()->findOneByOnfidoReportId( |
|
208
|
|
|
$onfidoHookQueue->getResourceId() |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
private function getAllNewWebhooks() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->getHookRepo()->findBy( |
|
215
|
|
|
[ |
|
216
|
|
|
'status' => MangoPayConstants::HOOK_NEW |
|
217
|
|
|
] |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
private function getHookRepo() : HookRepository |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->mangoPayRepositoryFactory->getHook(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// Made public so tests pass |
|
227
|
|
|
|
|
228
|
|
|
public function isValidOnfidoRequest($bodyJson) |
|
229
|
|
|
{ |
|
230
|
|
|
if ($bodyJson instanceof \stdClass) { |
|
231
|
|
|
|
|
232
|
|
|
if (!isset($bodyJson->payload->resource_type)) { |
|
233
|
|
|
return false; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
if (!isset($bodyJson->payload->action)) { |
|
237
|
|
|
return false; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if (!isset($bodyJson->payload->object->id)) { |
|
241
|
|
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if (!isset($bodyJson->payload->object->href) || |
|
245
|
|
|
!strstr($bodyJson->payload->object->href, 'api.onfido.com/v') === true |
|
246
|
|
|
) { |
|
247
|
|
|
return false; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return true; |
|
251
|
|
|
} |
|
252
|
|
|
return false; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
private function sendMessage(string $message, string $emoji) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->slackService->sendMessage($message, 'ff-mangopay-webhooks', $emoji); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
private function sendErrorMessage(string $message, string $emoji) |
|
261
|
|
|
{ |
|
262
|
|
|
$this->slackService->sendMessage($message, 'ff-error', $emoji); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
} |
|
266
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: