Skip to content

Commit a6a8ae7

Browse files
authored
feat(error): normalize magento2 error handling (#731)
1 parent d54c3e9 commit a6a8ae7

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ vendor
6565

6666
woocommerce-openpix*.zip
6767

68+
*OpenPixConfig.php
69+
6870
*pot~
6971
*po~
7072

7173
*.zip
7274
*/*.zip
7375

74-
.jest-cache
76+
.jest-cache

Pix/Controller/Index/Webhook.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@ public function execute()
3737
$resultJson = $this->resultJsonFactory->create();
3838
$body = file_get_contents('php://input');
3939

40-
$this->helperData->log(__(sprintf('Start webhook')));
40+
$this->helperData->log('Start webhook');
4141

4242
if (!$this->validateRequest($body)) {
4343
$ip = $this->webhookHandler->getRemoteIp();
4444

45-
$this->helperData->log(
46-
__(sprintf('Invalid webhook attempt from IP %s', $ip))
47-
);
45+
$this->helperData->log("Invalid webhook attempt from IP $ip");
4846

4947
$resultJson->setHttpResponseCode(400);
5048

@@ -53,17 +51,15 @@ public function execute()
5351
]);
5452
}
5553

56-
$this->helperData->log(__(sprintf("Webhook New Event!\n%s", $body)));
54+
$this->helperData->debugJson("Webhook New Event!", 'debug event', \json_decode($body, true));
5755

5856
$result = $this->webhookHandler->handle($body);
5957

60-
if (isset($result['error'])) {
61-
$resultJson->setHttpResponseCode(400);
62-
return $resultJson->setData(['error' => $result['error']]);
63-
}
58+
$statusCode = isset($result['error']) && \strlen($result['error']) > 0 ? 400 : 200;
59+
60+
$resultJson->setHttpResponseCode($statusCode);
6461

65-
$resultJson->setHttpResponseCode(200);
66-
return $resultJson->setData(['success' => $result['success']]);
62+
return $resultJson->setData($result);
6763
}
6864

6965
public function verifySignature(string $payload, string $signature)
@@ -77,12 +73,15 @@ public function verifySignature(string $payload, string $signature)
7773
'sha256WithRSAEncryption'
7874
);
7975

80-
$this->helperData->log(
81-
__(sprintf(
82-
"\nSignature: %s\nPayload: %s\nisValid: %s\npublicKey: %s",
83-
$signature, $payload, $verify == 1 ? "true" : "false", $publicKey
84-
))
85-
);
76+
if(!$verify) {
77+
$this->helperData->log('Invalid signature');
78+
$this->helperData->log(
79+
__(sprintf(
80+
"\nSignature: %s\nPayload: %s\nisValid: %s\npublicKey: %s",
81+
$signature, $payload, $verify == 1 ? "true" : "false", $publicKey
82+
))
83+
);
84+
}
8685

8786
return $verify;
8887
}

Pix/Helper/WebhookHandler.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,17 @@ public function handle($body)
139139
}
140140

141141
if ($this->isValidWebhookPayload($jsonBody)) {
142-
$this->_helperData->log(
143-
'OpenPix WebApi::ProcessWebhook Invalid Payload',
144-
self::LOG_NAME,
145-
$jsonBody
146-
);
147-
148142
$charge = $jsonBody['charge'];
149143
$pix = $jsonBody['pix'];
150144

151145
return $this->chargePaid->chargePaid($charge, $pix);
152146
}
153147

148+
$this->_helperData->log(
149+
"OpenPix WebApi::ProcessWebhook Invalid Payload event: $event",
150+
self::LOG_NAME
151+
);
152+
154153
return ['error' => 'Invalid Payload', 'success' => null];
155154
} catch (\Exception $e) {
156155
$this->_helperData->log(
@@ -162,7 +161,7 @@ public function handle($body)
162161
)
163162
);
164163
return [
165-
'error' => 'Internal server error',
164+
'error' => 'Fail when interpreting webhook JSON',
166165
'success' => null,
167166
];
168167
}

Pix/Model/Pix/Pix.php

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function __construct(
6363
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
6464
\Magento\Payment\Helper\Data $paymentData,
6565
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
66+
\Magento\Payment\Model\Method\Logger $logger,
6667
\Magento\Framework\HTTP\Client\Curl $curl,
6768
\OpenPix\Pix\Helper\Data $helper,
6869
\Magento\Store\Model\StoreManagerInterface $storeManager,
@@ -80,6 +81,7 @@ public function __construct(
8081
$customAttributeFactory,
8182
$paymentData,
8283
$scopeConfig,
84+
$logger,
8385
$resource,
8486
$resourceCollection,
8587
$data
@@ -92,6 +94,7 @@ public function __construct(
9294
$this->_curl = $curl;
9395
}
9496

97+
9598
/**
9699
* Determine method availability based on quote amount and config data
97100
*

0 commit comments

Comments
 (0)