forked from jorgehs91/magento2-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebhookManagement.php
61 lines (56 loc) · 1.96 KB
/
WebhookManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Pagarme\Pagarme\Model;
use Magento\Framework\DB\Transaction;
use Magento\Framework\Phrase;
use Magento\Framework\Webapi\Exception as M2WebApiException;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\CreditmemoFactory;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\Service\CreditmemoService;
use Magento\Sales\Model\Service\InvoiceService;
use Magento\Sales\Model\Service\OrderService;
use Pagarme\Core\Kernel\Exceptions\AbstractPagarmeCoreException;
use Pagarme\Core\Webhook\Exceptions\WebhookAlreadyHandledException;
use Pagarme\Core\Webhook\Exceptions\WebhookHandlerNotFoundException;
use Pagarme\Core\Webhook\Services\WebhookReceiverService;
use Pagarme\Pagarme\Api\WebhookManagementInterface;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
class WebhookManagement implements WebhookManagementInterface
{
/**
* @api
* @param mixed $id
* @param mixed $type
* @param mixed $data
* @return array|bool
*/
public function save($id, $type, $data)
{
try {
Magento2CoreSetup::bootstrap();
$postData = new \stdClass();
$postData->id = $id;
$postData->type = $type;
$postData->data = $data;
$webhookReceiverService = new WebhookReceiverService();
return $webhookReceiverService->handle($postData);
} catch (WebhookHandlerNotFoundException $e) {
return [
"message" => $e->getMessage(),
"code" => 200
];
} catch (WebhookAlreadyHandledException $e) {
return [
"message" => $e->getMessage(),
"code" => 200
];
} catch(AbstractPagarmeCoreException $e) {
throw new M2WebApiException(
new Phrase($e->getMessage()),
0,
$e->getCode()
);
}
}
}