Skip to content

Commit

Permalink
Merge pull request #209 from pagarme/feature/PAOPN-456-refactor-pix-b…
Browse files Browse the repository at this point in the history
…lock

PAOPN-456: Refactoring pix block and template
  • Loading branch information
mateus-picoloto authored Jul 26, 2023
2 parents a06f220 + 78d23ad commit 7f36bee
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 83 deletions.
67 changes: 63 additions & 4 deletions Block/Payment/Pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@
use Magento\Sales\Api\Data\OrderInterface as Order;
use Magento\Sales\Api\Data\OrderPaymentInterface as Payment;
use Pagarme\Pagarme\Helper\Payment\Pix as PixHelper;

class Pix extends Template
{
/**
* @var CheckoutSession
*/
protected $checkoutSession;

/**
* @var array
*/
private $pixInfo;

/**
* @var PixHelper
*/
private $pixHelper;

/**
* Link constructor.
* @param Context $context
* @param CheckoutSession $checkoutSession
*/
public function __construct(Context $context, CheckoutSession $checkoutSession)
public function __construct(Context $context, CheckoutSession $checkoutSession, PixHelper $pixHelper)
{
$this->checkoutSession = $checkoutSession;
$this->pixHelper = $pixHelper;
parent::__construct($context, []);
}

Expand Down Expand Up @@ -61,12 +77,55 @@ protected function getPayment()
return $this->getLastOrder()->getPayment();
}

/**
* @return bool
*/
public function showPixInformation()
{
return !empty($this->getPixInfo());
}

/**
* @return mixed
*/
public function getPixUrl()
{
return $this->getPixInfo()['qr_code_url'] ?? '';
}

/**
* @return mixed
*/
public function getPixQrCode()
{
return $this->getPixInfo()['qr_code'] ?? '';
}

/**
* @return string
*/
public function getPixInfo()
public function getErrorCopyMessage()
{
$pixHelper = new PixHelper();
return $pixHelper->getQrCode($this->getPayment());
return __('Failed to copy! Please, manually copy the code using the field bellow the button.');
}

/**
* @return string
*/
public function getSuccessMessage()
{
return __('PIX code copied!');
}

/**
* @return array
*/
private function getPixInfo()
{
if (empty($this->pixInfo)) {
$this->pixInfo = $this->pixHelper->getQrCode($this->getPayment());
}

return $this->pixInfo;
}
}
8 changes: 7 additions & 1 deletion i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,10 @@
"Canceled Amount","Canceled Amount"
"Refunded Amount","Refunded Amount"
"Capture","Capture"
"Cancel","Cancel"
"Cancel","Cancel",
"Failed to copy! Please, manually copy the code using the field bellow the button.","Failed to copy! Please, manually copy the code using the field bellow the button."
"PIX code copied!","PIX code copied!"
"Copy PIX code","Copy PIX code"
"Open your bank app","Open your bank app"
"Scan the QR Code","Scan the QR Code"
"Confirm the payment","Confirm the payment"
8 changes: 7 additions & 1 deletion i18n/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,10 @@
"Canceled Amount","Valor Cancelado"
"Refunded Amount","Valor Reembolsado"
"Capture","Capturar"
"Cancel","Cancelar"
"Cancel","Cancelar",
"Failed to copy! Please, manually copy the code using the field bellow the button.","Falha ao copiar! Por favor, copie o código manualmente utilizando o campo abaixo do botão."
"PIX code copied!","Código PIX copiado!"
"Copy PIX code","Copiar código PIX"
"Open your bank app","Abra o app do seu banco"
"Scan the QR Code","Escaneie o QR Code"
"Confirm the payment","Confirme o pagamento"
3 changes: 3 additions & 0 deletions view/frontend/layout/checkout_onepage_success.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="Pagarme_Pagarme/css/pagarme_success_page.css" />
</head>
<body>
<referenceContainer name="content">
<block class="Pagarme\Pagarme\Block\Payment\Billet"
Expand Down
5 changes: 5 additions & 0 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ var config = {
'Pagarme_Pagarme/js/mixin/billing-address-mixin': true
}
}
},
map: {
'*': {
pixCheckoutSuccess: 'Pagarme_Pagarme/js/view/payment/checkout/success/pix'
}
}
};
119 changes: 42 additions & 77 deletions view/frontend/templates/payment/pix.phtml
Original file line number Diff line number Diff line change
@@ -1,99 +1,64 @@
<?php
$pixInfo = $this->getPixInfo();
if (!empty($pixInfo)):
/**
* @var \Pagarme\Pagarme\Block\Payment\Pix $block
*/
if ($block->showPixInformation()):
?>
<hr />
<div style="margin: 0 auto; width: 420px;">
<!-- PIX -->
<div style="margin: 0 auto 30px; text-align: center;">
<img width="150" src="https://cdn.mundipagg.com/assets/images/site/illustrations/pix-bc-logo-2.png" alt=>
<div class="pix-wrapper">
<div class="pix-logo-wrapper">
<img
width="150"
src="https://cdn.mundipagg.com/assets/images/site/illustrations/pix-bc-logo-2.png"
alt="pix logo"
/>
</div>
<!-- ./PIX -->

<!-- QR Code -->
<div style="margin: 0 auto 10px; text-align: center;">
<!-- SUBSTITUIR PELO QRCODE -->
<img width="200" src="<?= $pixInfo['qr_code_url']?>" />
<!-- ./SUBSTITUIR PELO QRCODE -->
<div class="qr-code-wrapper">
<img width="200" src="<?= $block->getPixUrl(); ?>" alt="pix qr code" />
</div>
<!-- ./QR Code -->

<!-- AÇÕES -->
<div style="margin: 0 auto 0px; min-height: 10px; text-align: center;">
<!-- COPIAR -->
<div style="display: inline-block;">
<svg style="display: inline-block; vertical-align: middle; margin: 0 5px 0 0; width: 20px" aria-hidden="true"
focusable="false" data-prefix="fas" data-icon="copy" class="svg-inline--fa fa-copy fa-w-14" role="img"
xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
<div class="copy-qr-code-wrapper">
<div class="copy-inline">
<svg aria-hidden="true"
focusable="false"
data-prefix="fas"
data-icon="copy"
class="svg-inline--fa fa-copy fa-w-14 copy-pix"
role="img"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 448 512">
<path fill="blue"
d="M320 448v40c0 13.255-10.745 24-24 24H24c-13.255 0-24-10.745-24-24V120c0-13.255 10.745-24 24-24h72v296c0 30.879 25.121 56 56 56h168zm0-344V0H152c-13.255 0-24 10.745-24 24v368c0 13.255 10.745 24 24 24h272c13.255 0 24-10.745 24-24V128H344c-13.2 0-24-10.8-24-24zm120.971-31.029L375.029 7.029A24 24 0 0 0 358.059 0H352v96h96v-6.059a24 24 0 0 0-7.029-16.97z">
</path>
</svg>
<a onclick="copyQrCode()" style="display: inline-block; vertical-align: middle; margin: 0 5px 0 0; cursor: pointer;">Copiar
código PIX</a>
<input style="opacity: 0; user-select: none; pointer-events: none;" type="text" value="<?= $pixInfo['qr_code']?>" id="mp_qr_code" />
<a class="copy-pix"><?= __('Copy PIX code'); ?></a>
<input id="pagarme_qr_code" type="text" value="<?= $block->getPixQrCode() ?>" />
</div>
<!-- ./COPIAR -->
</div>
<!-- ./AÇÕES -->

<!-- INSTRUÇÕES -->
<div style="margin: 0 auto 30px; text-align: center;">
<div style="display: inline-block; vertical-align: top; margin: 0 10px; width: 25%;">
<div>Abra o app do seu banco</div>
<div style="display: inline-block;background-color: gainsboro;border-radius: 50%;padding: 10px 15px;margin-top: 10px;">
1</div>
<div class="pix-instructions-wrapper">
<div class="pix-instruction">
<div><?= __('Open your bank app'); ?></div>
<div class="pix-instruction-number">1</div>
</div>

<div style="display: inline-block; vertical-align: top; margin: 0 10px; width: 25%;">
<div>Escaneie o QR Code</div>
<div style="display: inline-block;background-color: gainsboro;border-radius: 50%;padding: 10px 15px;margin-top: 10px;">
2</div>
<div class="pix-instruction">
<div><?= __('Scan the QR Code'); ?></div>
<div class="pix-instruction-number">2</div>
</div>

<div style="display: inline-block; vertical-align: top; margin: 0 10px; width: 25%;">
<div>Confirme o pagamento</div>
<div style="display: inline-block;background-color: gainsboro;border-radius: 50%;padding: 10px 15px;margin-top: 10px;">
3</div>
<div class="pix-instruction">
<div><?= __('Confirm the payment'); ?></div>
<div class="pix-instruction-number">3</div>
</div>
</div>
<!-- ./INSTRUÇÕES -->
</div>

<script>
window.copyQrCode = async function() {
const qrCodeElement = document.getElementById("mp_qr_code");

if (!qrCodeElement) {
return;
}

const rawCode = qrCodeElement.getAttribute("value");
const alternativeCopyQrCode = (rawCode) => {
qrCodeElement.setAttribute("style", "margin: 1.5em 0;")
qrCodeElement.focus();
qrCodeElement.select();
alert("Falha ao copiar! Por favor, copie o código manualmente utilizando o campo abaixo do botão.");
};

if (window.isSecureContext && navigator.clipboard) {
try {
await navigator.clipboard.writeText(rawCode);
alert("Código PIX copiado!");
} catch (err) {
alternativeCopyQrCode(rawCode);
<script type="text/x-magento-init">
{
"*": {
"pixCheckoutSuccess": {
"errorCopyMessage": "<?= $block->getErrorCopyMessage(); ?>",
"successCopyMessage": "<?= $block->getSuccessMessage(); ?>"
}
return;
}

qrCodeElement.select();
qrCodeElement.setSelectionRange(0, 99999);
try {
document.execCommand('copy', false);
alert("Código PIX copiado!");
} catch (err) {
alternativeCopyQrCode(rawCode);
}
}
</script>
<?php endif ?>
<?php endif ?>
64 changes: 64 additions & 0 deletions view/frontend/web/css/pagarme_success_page.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.pix-wrapper {
margin: 0 auto;
width: 360px;
}

.pix-wrapper .pix-logo-wrapper {
margin: 0 auto 30px;
text-align: center;
}

.pix-wrapper .qr-code-wrapper {
margin: 0 auto 10px;
text-align: center;
}

.pix-wrapper .copy-qr-code-wrapper {
margin: 0 auto 0;
min-height: 10px;
text-align: center;
}

.pix-wrapper .copy-qr-code-wrapper .copy-inline {
display: inline-block;
padding-bottom: 32px;
}

.pix-wrapper .copy-qr-code-wrapper .copy-inline svg {
display: inline-block;
vertical-align: middle;
margin: 0 5px 0 0;
width: 20px;
}

.pix-wrapper .copy-qr-code-wrapper .copy-inline a {
display: inline-block;
vertical-align: middle;
margin: 0 5px 0 0;
cursor: pointer;
}

.pix-wrapper .copy-qr-code-wrapper .copy-inline input {
margin: 1.5em 0;
display: none;
}

.pix-wrapper .pix-instructions-wrapper {
margin: 0 auto 30px;
text-align: center;
}

.pix-wrapper .pix-instructions-wrapper .pix-instruction {
display: inline-block;
vertical-align: top;
margin: 0 10px;
width: 25%;
}

.pix-wrapper .pix-instructions-wrapper .pix-instruction .pix-instruction-number {
display: inline-block;
background-color: gainsboro;
border-radius: 50%;
padding: 10px 15px;
margin-top: 10px;
}
39 changes: 39 additions & 0 deletions view/frontend/web/js/view/payment/checkout/success/pix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
define(['jquery'], ($) => {
return (config) => {
$('.copy-pix').click(async () => {
const qrCodeJqueryElement = $('#pagarme_qr_code');

if (qrCodeJqueryElement.length < 1) {
return;
}

const rawCode = qrCodeJqueryElement.attr("value");
const alternativeCopyQrCode = () => {
qrCodeJqueryElement.show();
qrCodeJqueryElement.focus();
qrCodeJqueryElement.select();
alert(config.errorCopyMessage);
};

if (window.isSecureContext && navigator.clipboard) {
try {
await navigator.clipboard.writeText(rawCode);
alert(config.successCopyMessage);
} catch (err) {
alternativeCopyQrCode();
}
return;
}

const [qrCodeDOMElement] = qrCodeJqueryElement;
qrCodeDOMElement.select();
qrCodeDOMElement.setSelectionRange(0, qrCodeJqueryElement.val().length);
try {
document.execCommand('copy', false);
alert(config.successCopyMessage);
} catch (err) {
alternativeCopyQrCode();
}
});
};
});

0 comments on commit 7f36bee

Please sign in to comment.