Skip to content

Commit d09ea2e

Browse files
authored
Feat/improve webhook ecpj 181 #575
2 parents be3dfc1 + a6ab361 commit d09ea2e

4 files changed

Lines changed: 369 additions & 8 deletions

File tree

src/Controller/Orders.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function create_order(WC_Order $wc_order, $payment_method, $form_fields)
7676
return $orderService->createOrderAtPagarme($orderDecorator);
7777
} catch (Exception $e) {
7878
if (!empty($this->settings)) {
79-
$this->settings->log()->add('woo-pagarme', 'CREATE ORDER ERROR: ' . $e->__toString());
79+
$this->settings->log()->error('CREATE ORDER ERROR: ' . $e->__toString());
8080
}
8181
error_log($e->__toString());
8282
return $e;

src/Controller/Webhooks.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
exit(0);
77
}
88

9+
use Pagarme\Core\Webhook\Services\WebhookValidatorService;
910
use Woocommerce\Pagarme\Helper\Utils;
1011
use Woocommerce\Pagarme\Core;
1112
use Woocommerce\Pagarme\Model\Config;
1213
use Woocommerce\Pagarme\Model\Order;
14+
use WP_Error;
1315

1416
class Webhooks
1517
{
18+
const WEBHOOK_SIGNATURE_HEADER = 'HTTP_X_WEBHOOK_ASYMMETRIC_SIGNATURE';
19+
1620
private $config;
1721

1822
public function __construct()
@@ -23,17 +27,45 @@ public function __construct()
2327

2428
public function handle_requests()
2529
{
26-
$body = Utils::get_json_post_data();
30+
$webHookSignature = $_SERVER[self::WEBHOOK_SIGNATURE_HEADER] ?? null;
31+
if (!$webHookSignature) {
32+
$this->config->log()->info('Unauthorized Webhook Received: no signature header found!');
33+
wp_die(
34+
'Unauthorized Webhook Received: no signature header found!',
35+
'Unauthorized',
36+
array('response' => 401)
37+
);
38+
}
39+
$payload = file_get_contents('php://input');
40+
if (empty($payload)) {
41+
$this->config->log()->info('Unauthorized Webhook Received: empty payload!');
42+
wp_die(
43+
"Unauthorized Webhook Received: empty payload!",
44+
'Unauthorized',
45+
array('response' => 401)
46+
);
47+
}
48+
49+
if (!WebhookValidatorService::validateSignature($payload, $webHookSignature)) {
50+
$this->config->log()->info('Unauthorized Webhook Received: invalid signature!');
51+
wp_die(
52+
'Unauthorized Webhook Received: invalid signature!',
53+
'Unauthorized Webhook Received',
54+
array('response' => 401)
55+
);
56+
}
57+
58+
$body = json_decode($payload);
2759

2860
if (empty($body)) {
29-
$this->config->log()->add('woo-pagarme', 'Webhook Received: empty body!');
61+
$this->config->log()->info('Webhook Received: empty body!');
3062
return;
3163
}
3264
if (!$this->orderByWoocommerce($body->data->code, $body->data->order->metadata, $body->id) ) {
3365
return;
3466
}
3567

36-
$this->config->log()->add('woo-pagarme', 'Webhook Received: ' . json_encode($body, JSON_PRETTY_PRINT));
68+
$this->config->log()->info('Webhook Received: ' . json_encode($body, JSON_PRETTY_PRINT));
3769

3870
$event = $this->sanitize_event_name($body->type);
3971

@@ -69,7 +101,7 @@ private function orderByWoocommerce($orderId, $metadata, $webhookId)
69101
{
70102
if(!wc_get_order($orderId)) {
71103
if(strpos($this->getMetadata($metadata), "Woocommerce") !== false) {
72-
$this->config->log()->add('woo-pagarme', 'Webhook Received but not proccessed: ' . $webhookId);
104+
$this->config->log()->info('Webhook Received but not proccessed: ' . $webhookId);
73105
}
74106
return false;
75107
}

src/Model/Order.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,13 @@ private function handle_shipping_properties($prop)
228228

229229
private function log($content)
230230
{
231-
232-
$file = 'woo-pagarme';
233231
$message =
234232
'ORDER STATUS UPDATE: #' .
235233
$this->wc_order->get_id() .
236234
json_encode($content, JSON_PRETTY_PRINT);
237235

238236
if (!empty($this->settings)) {
239-
$this->settings->log()->add($file, $message);
237+
$this->settings->log()->info($message);
240238
}
241239
}
242240

0 commit comments

Comments
 (0)