From cdd4ce3ad317e5f84dc5a2a0d51c0b6487b37b42 Mon Sep 17 00:00:00 2001 From: Mateus Picoloto Date: Tue, 7 May 2024 09:43:02 -0300 Subject: [PATCH] feat: added config and status validation and error modal --- Block/Customer/Marketplace/Kyc.php | 9 +++- Model/Api/Recipient.php | 3 ++ Model/Recipient.php | 1 - Test/Unit/Model/Api/RecipientTest.php | 43 ++++++++++++++++++- i18n/pt_BR.csv | 2 + .../web/js/grid/columns/recipientStatus.js | 4 +- .../layout/marketplace_account_dashboard.xml | 7 ++- .../templates/customer/marketplace/kyc.phtml | 17 +++++--- view/frontend/web/js/view/marketplace/kyc.js | 38 ++++++++-------- 9 files changed, 95 insertions(+), 29 deletions(-) diff --git a/Block/Customer/Marketplace/Kyc.php b/Block/Customer/Marketplace/Kyc.php index 83d7c83c..98123226 100644 --- a/Block/Customer/Marketplace/Kyc.php +++ b/Block/Customer/Marketplace/Kyc.php @@ -11,6 +11,7 @@ namespace Pagarme\Pagarme\Block\Customer\Marketplace; use Magento\Customer\Model\Session; +use Pagarme\Pagarme\Model\Recipient; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Pagarme\Pagarme\Model\ResourceModel\Recipients\CollectionFactory; @@ -43,7 +44,11 @@ public function __construct( $this->collectionFactory = $collectionFactory; parent::__construct($context, $data); } + + /** + * @return Recipient + */ public function getRecipient() { return $this->collectionFactory->create()->addFieldToFilter( @@ -53,11 +58,11 @@ public function getRecipient() } /** - * Get customerid + * Get customerId * * @return int */ - public function getCustomerId() + protected function getCustomerId() { return $this->customerSession->getCustomerId(); } diff --git a/Model/Api/Recipient.php b/Model/Api/Recipient.php index f6b8ac63..8c2a97a0 100644 --- a/Model/Api/Recipient.php +++ b/Model/Api/Recipient.php @@ -176,6 +176,9 @@ public function createKycLink(string $id) throw new NoSuchEntityException(__('Recipient not founded.')); } $kycLink = $this->recipientService->createKycLink($recipientModel->getPagarmeId()); + if (empty($kycLink->url) || empty($kycLink->base64_qrcode)) { + throw new NoSuchEntityException(__('Security validation not generated.')); + } $kycResponse = $this->kycLinkResponseFactory->create(); $kycResponse->setUrl($kycLink->url) ->setQrCode('data:image/svg+xml;base64,' . $kycLink->base64_qrcode); diff --git a/Model/Recipient.php b/Model/Recipient.php index 254308ba..1e5ce179 100644 --- a/Model/Recipient.php +++ b/Model/Recipient.php @@ -2,7 +2,6 @@ namespace Pagarme\Pagarme\Model; -use Magento\Framework\DataObject\IdentityInterface; use Magento\Framework\Model\AbstractModel; class Recipient extends AbstractModel diff --git a/Test/Unit/Model/Api/RecipientTest.php b/Test/Unit/Model/Api/RecipientTest.php index 0f968e95..2955ec8d 100644 --- a/Test/Unit/Model/Api/RecipientTest.php +++ b/Test/Unit/Model/Api/RecipientTest.php @@ -59,8 +59,9 @@ public function testCreateKycLinkShouldReturnQrCodeAndLink() $result = $recipientModelApi->createKycLink(1); + $expectedBase64QrCode = "data:image/svg+xml;base64,$base64QrCode"; $this->assertSame($kycUrl, $result->getUrl()); - $this->assertSame($base64QrCode, $result->getQrCode()); + $this->assertSame($expectedBase64QrCode, $result->getQrCode()); } public function testCreateKycLinkShouldNotFoundRecipient() @@ -99,4 +100,44 @@ public function testCreateKycLinkShouldNotFoundRecipient() $recipientModelApi->createKycLink(1); } + + public function testCreateKycLinkShouldNotGenerateSecurityValidation() + { + $requestMock = Mockery::mock(Request::class); + + $recipientModelMock = Mockery::mock(RecipientModel::class); + $recipientModelMock->shouldReceive('getPagarmeId') + ->andReturn('rp_xxxxxxxxxxxxxxxx'); + + $recipientFactoryMock = Mockery::mock(RecipientFactory::class); + $recipientFactoryMock->shouldReceive('create') + ->andReturn($recipientModelMock); + + $resourceModelRecipientMock = Mockery::mock(ResourceModelRecipient::class); + $resourceModelRecipientMock->shouldReceive('load') + ->andReturnSelf(); + + $coreRecipientMock = Mockery::mock(CoreRecipient::class); + + $createKyLinkResponseMock = new CreateKycLinkResponse(); + $recipientServiceMock = Mockery::mock(RecipientService::class); + $recipientServiceMock->shouldReceive('createKycLink') + ->andReturn($createKyLinkResponseMock); + + $kycLinkResponseFactoryMock = Mockery::mock(KycLinkResponseInterfaceFactory::class); + + $recipientModelApi = new Recipient( + $requestMock, + $recipientFactoryMock, + $resourceModelRecipientMock, + $coreRecipientMock, + $recipientServiceMock, + $kycLinkResponseFactoryMock + ); + + $this->expectException(NoSuchEntityException::class); + $this->expectExceptionMessage('Security validation not generated.'); + + $recipientModelApi->createKycLink(1); + } } \ No newline at end of file diff --git a/i18n/pt_BR.csv b/i18n/pt_BR.csv index dc00f14a..3c313e72 100644 --- a/i18n/pt_BR.csv +++ b/i18n/pt_BR.csv @@ -601,3 +601,5 @@ "Blocked","Bloqueado" "Inactive","Inativo" "Recipient not founded.","Recebedor não encontrado." +"Security validation not generated.","Validação de segurança não gerada." +"Problem to generate security validation, please try again later.","Problema ao gerar validação de segurança, por favor tente novamente mais tarde." diff --git a/view/adminhtml/web/js/grid/columns/recipientStatus.js b/view/adminhtml/web/js/grid/columns/recipientStatus.js index cd934e83..558da3aa 100644 --- a/view/adminhtml/web/js/grid/columns/recipientStatus.js +++ b/view/adminhtml/web/js/grid/columns/recipientStatus.js @@ -77,7 +77,9 @@ define([ } catch (exception) { $('body').loader('hide'); $('body').notification('clear'); - this.mageAlert(exception?.responseJSON?.message, translate('Error!')); + // TODO: Using portuguese string because mage/translate and knockout i18n were not working + const errorContent = `

Problema ao gerar validação de segurança, por favor tente novamente mais tarde.

`; + this.mageAlert(errorContent, translate('Error!')); } }, diff --git a/view/frontend/layout/marketplace_account_dashboard.xml b/view/frontend/layout/marketplace_account_dashboard.xml index 75a350b0..59833303 100644 --- a/view/frontend/layout/marketplace_account_dashboard.xml +++ b/view/frontend/layout/marketplace_account_dashboard.xml @@ -14,7 +14,12 @@ - + diff --git a/view/frontend/templates/customer/marketplace/kyc.phtml b/view/frontend/templates/customer/marketplace/kyc.phtml index ad248cbb..dc19d7ce 100644 --- a/view/frontend/templates/customer/marketplace/kyc.phtml +++ b/view/frontend/templates/customer/marketplace/kyc.phtml @@ -1,13 +1,12 @@ getRecipient(); +$recipientId = $recipient->getId(); -$recipientId = $block->getRecipient()->getId(); - -if (empty($recipientId)) { +if (empty($recipientId) || $recipient->getStatus() !== \Pagarme\Core\Marketplace\Interfaces\RecipientInterface::VALIDATION_REQUESTED) { return; } @@ -34,7 +33,7 @@ if (empty($recipientId)) { -