Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added recipient webhook validation #329

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions Model/WebhookManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ class WebhookManagement implements WebhookManagementInterface
*/
protected $account;

/**
* @var WebhookReceiverService
*/
protected $webhookReceiverService;

public function __construct(
OrderFactory $orderFactory,
Account $account
Account $account,
WebhookReceiverService $webhookReceiverService
) {
$this->orderFactory = $orderFactory;
$this->account = $account;
$this->webhookReceiverService = $webhookReceiverService;
}

/**
* @api
* @param mixed $id
Expand All @@ -52,7 +60,10 @@ public function save($id, $type, $data, $account)
$postData->type = $type;
$postData->data = $data;

if($this->hasMagentoOrder($data) === false) {
if (
$this->hasMagentoOrder($data) === false
&& $this->isNotRecipientWebhook($type)
) {
$this->logWebhookIdCaseExistsMetadata($data, $id);
return [
"message" => "Webhook Received",
Expand All @@ -64,14 +75,13 @@ public function save($id, $type, $data, $account)
$this->account->saveAccountIdFromWebhook($account);
}

$webhookReceiverService = new WebhookReceiverService();
return $webhookReceiverService->handle($postData);
return $this->webhookReceiverService->handle($postData);
} catch (WebhookHandlerNotFoundException | WebhookAlreadyHandledException $e) {
return [
"message" => $e->getMessage(),
"code" => 200
];
} catch(AbstractPagarmeCoreException $e) {
} catch (AbstractPagarmeCoreException $e) {
throw new M2WebApiException(
new Phrase($e->getMessage()),
0,
Expand All @@ -82,41 +92,49 @@ public function save($id, $type, $data, $account)
private function logWebhookIdCaseExistsMetadata($webhookData, $webhookId)
{
$metadata = $this->getMetadata($webhookData);
if($metadata === false || !array_key_exists('platformVersion', $metadata)) {
if ($metadata === false || !array_key_exists('platformVersion', $metadata)) {
return;
}
if(strpos($metadata['platformVersion'], "Magento") !== false) {
if (strpos($metadata['platformVersion'], "Magento") !== false) {
$logService = new LogService(
'Webhook',
true
);
$logService->info(
"Webhook Received but not proccessed",
(object)['webhookId' => $webhookId
]);
(object)[
'webhookId' => $webhookId
]
);
}
}
private function getMetadata($data)
{
$metadata = false;
if(!array_key_exists('order', $data) && !array_key_exists('subscription', $data)) {
if (!array_key_exists('order', $data) && !array_key_exists('subscription', $data)) {
return false;
}
if(array_key_exists('metadata', $data)) {
if (array_key_exists('metadata', $data)) {
$metadata = $data['metadata'];
}
return $metadata;
}

private function hasMagentoOrder($data)
{
$code = 0;
if(array_key_exists('subscription', $data)) {
if (array_key_exists('subscription', $data)) {
$code = $data['subscription']['code'];
}
if(array_key_exists('order', $data)) {
if (array_key_exists('order', $data)) {
$code = $data['order']['code'];
}
$order = $this->orderFactory->create()->loadByIncrementId($code);
return $order->getId() ?? false;
}

private function isNotRecipientWebhook($type)
{
return strpos($type, 'recipient') === false;
}
}
110 changes: 110 additions & 0 deletions Test/Unit/Model/WebhookManagementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Pagarme\Pagarme\Test\Unit\Model;

use Mockery;
use Magento\Sales\Model\Order;
use Pagarme\Pagarme\Model\Account;
use Magento\Sales\Model\OrderFactory;
use Pagarme\Pagarme\Test\Unit\BaseTest;
use Pagarme\Pagarme\Model\WebhookManagement;
use Pagarme\Core\Webhook\Services\WebhookReceiverService;

class WebhookManagementTest extends BaseTest
{
public function testSaveWithRecipientWebhook()
{
$moduleCoreSetupMock = Mockery::mock('alias:Pagarme\Core\Kernel\Abstractions\AbstractModuleCoreSetup');
$moduleCoreSetupMock->shouldReceive('bootstrap')
->andReturnSelf();


$orderMock = Mockery::mock(Order::class);
$orderMock->shouldReceive('loadByIncrementId')
->andReturnSelf();
$orderMock->shouldReceive('getId')
->andReturnFalse();

$orderFactoryMock = Mockery::mock(OrderFactory::class);
$orderFactoryMock->shouldReceive('create')
->andReturn($orderMock);
$accountMock = Mockery::mock(Account::class);

$webhookReceiverServiceMock = Mockery::mock(WebhookReceiverService::class);
$webhookRecipientResponse = [
'message' => 'Recipient updated',
'code' => 200
];
$webhookReceiverServiceMock->shouldReceive('handle')
->once()
->andReturn($webhookRecipientResponse);

$webhookManagement = new WebhookManagement($orderFactoryMock, $accountMock, $webhookReceiverServiceMock);

$id = "hook_aaaaaaaaaaaaaaaa";
$type = "recipient.updated";
$data = [
"id" => 'rp_xxxxxxxxxxxxxxxx',
"name" => "Test recipient",
"email" => "test@recipient.test",
"document" => "11111111111",
"description" => "Test description",
"type" => "individual",
"payment_mode" => "bank_transfer",
"status" => "active",
"kyc_details" =>
[
"status" => "approved"
],
];

$account = [
"id" => "acc_xxxxxxxxxxxxxxxx",
"name" => "Account Test"
];
$result = $webhookManagement->save($id, $type, $data, $account);

$this->assertSame($webhookRecipientResponse, $result);
}


public function testSaveWithNonPlatformWebhook()
{
$moduleCoreSetupMock = Mockery::mock('alias:Pagarme\Core\Kernel\Abstractions\AbstractModuleCoreSetup');
$moduleCoreSetupMock->shouldReceive('bootstrap')
->andReturnSelf();


$orderMock = Mockery::mock(Order::class);
$orderMock->shouldReceive('loadByIncrementId')
->andReturnSelf();
$orderMock->shouldReceive('getId')
->andReturnFalse();

$orderFactoryMock = Mockery::mock(OrderFactory::class);
$orderFactoryMock->shouldReceive('create')
->andReturn($orderMock);

$accountMock = Mockery::mock(Account::class);

$webhookReceiverServiceMock = Mockery::mock(WebhookReceiverService::class);
$expectedResponse = [
'message' => 'Webhook Received',
'code' => 200
];

$webhookManagement = new WebhookManagement($orderFactoryMock, $accountMock, $webhookReceiverServiceMock);

$id = "hook_aaaaaaaaaaaaaaaa";
$type = "charge.paid";
$data = [];

$account = [
"id" => "acc_xxxxxxxxxxxxxxxx",
"name" => "Account Test"
];
$result = $webhookManagement->save($id, $type, $data, $account);

$this->assertSame($expectedResponse, $result);
}
}
Loading