Skip to content

Commit

Permalink
feat: added config and status validation and error modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Picoloto committed May 7, 2024
1 parent b78b861 commit cdd4ce3
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 29 deletions.
9 changes: 7 additions & 2 deletions Block/Customer/Marketplace/Kyc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +44,11 @@ public function __construct(
$this->collectionFactory = $collectionFactory;
parent::__construct($context, $data);
}


/**
* @return Recipient
*/
public function getRecipient()
{
return $this->collectionFactory->create()->addFieldToFilter(
Expand All @@ -53,11 +58,11 @@ public function getRecipient()
}

/**
* Get customerid
* Get customerId
*
* @return int
*/
public function getCustomerId()
protected function getCustomerId()
{
return $this->customerSession->getCustomerId();
}
Expand Down
3 changes: 3 additions & 0 deletions Model/Api/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion Model/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Pagarme\Pagarme\Model;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Model\AbstractModel;

class Recipient extends AbstractModel
Expand Down
43 changes: 42 additions & 1 deletion Test/Unit/Model/Api/RecipientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions i18n/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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."
4 changes: 3 additions & 1 deletion view/adminhtml/web/js/grid/columns/recipientStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<p>Problema ao gerar validação de segurança, por favor tente novamente mais tarde.</p>`;
this.mageAlert(errorContent, translate('Error!'));
}

},
Expand Down
7 changes: 6 additions & 1 deletion view/frontend/layout/marketplace_account_dashboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
</head>
<body>
<referenceContainer name="content">
<block class="Pagarme\Pagarme\Block\Customer\Marketplace\Kyc" name="pagarme_marketplace_kyc" template="customer/marketplace/kyc.phtml" before="marketplace_account_dashboard" cacheable="false"/>
<block class="Pagarme\Pagarme\Block\Customer\Marketplace\Kyc"
name="pagarme_marketplace_kyc"
template="customer/marketplace/kyc.phtml"
before="marketplace_account_dashboard"
cacheable="false"
ifconfig="pagarme_pagarme/marketplace/active"/>
</referenceContainer>
</body>
</page>
17 changes: 11 additions & 6 deletions view/frontend/templates/customer/marketplace/kyc.phtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php
/**
* @var $block Kyc
* @var \Pagarme\Pagarme\Block\Customer\Marketplace\Kyc $block Kyc
*/

use Pagarme\Pagarme\Block\Customer\Marketplace\Kyc;
$recipient = $block->getRecipient();
$recipientId = $recipient->getId();

$recipientId = $block->getRecipient()->getId();

if (empty($recipientId)) {
if (empty($recipientId) || $recipient->getStatus() !== \Pagarme\Core\Marketplace\Interfaces\RecipientInterface::VALIDATION_REQUESTED) {
return;
}

Expand All @@ -34,14 +33,20 @@ if (empty($recipientId)) {
</div>
</div>

<div id="modal-content">
<div id="modal-success-content">
<div class="modal-inner-content pagarme-kyc-modal">
<p><?= __('Point the smartphone camera to read the QRCode.') ?></p>
<p><img src="" alt="QRCode" id="pagarme-kyc-qrcode" /></p>
<p><?= sprintf(__('Or use this %slink%s.'), '<a id="pagarme-kyc-link" target="_blank" rel="noreferrer">', '</a>') ?></p>
</div>
</div>

<div id="modal-error-content">
<div class="modal-inner-content pagarme-kyc-modal">
<p><?= __('Problem to generate security validation, please try again later.') ?></p>
</div>
</div>

<script type="text/x-magento-init">
{
"#pagarme-kyc-start-validation": {
Expand Down
38 changes: 21 additions & 17 deletions view/frontend/web/js/view/marketplace/kyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ define([
'loader'
], function (modal, mageUrl, $) {
return function (config) {
const successModalOptions = {
responsive: true,
innerScroll: true,
// @todo: Using portuguese title for the translation did't work.
title: $.mage.__('Iniciar validação'),
buttons: [{
text: $.mage.__('Close'),
click: function () {
this.closeModal();
}
}]
};
modal(successModalOptions, $('#modal-success-content'));

const errorModalOptions = { ...successModalOptions };
errorModalOptions.title = $.mage.__('Error!');
modal(errorModalOptions, $('#modal-error-content'));

$('#pagarme-kyc-start-validation').on('click', async function (){
const body = $('body');
Expand All @@ -17,27 +34,14 @@ define([
if (response) {
$('#pagarme-kyc-qrcode').attr('src', response.qr_code);
$('#pagarme-kyc-link').attr('href', response.url);
$('.pagarme-kyc-modal').show();

const options = {
responsive: true,
innerScroll: true,
// @todo: Using portuguese title for the translation did't work.
title: $.mage.__('Iniciar validação'),
buttons: [{
text: $.mage.__('Close'),
click: function () {
this.closeModal();
}
}]
};
$('#modal-success-content .pagarme-kyc-modal').show();

const popup = modal(options, $('#modal-content'));
$('#modal-content').modal('openModal');
$('#modal-success-content').modal('openModal');
}
} catch (exception) {
body.loader('hide');
// this.mageAlert(exception?.responseJSON?.message, $.mage.__('Error!'));
$('#modal-error-content .pagarme-kyc-modal').show();
$('#modal-error-content').modal('openModal');
}
});
};
Expand Down

0 comments on commit cdd4ce3

Please sign in to comment.