From abaf490500e7926fb96e1d6bfe2803b1e79c052b Mon Sep 17 00:00:00 2001 From: Mateus Picoloto Date: Wed, 12 Jul 2023 15:19:22 -0300 Subject: [PATCH 1/3] refactoring BilletCreditCard block and template --- Block/Payment/Info/BilletCreditCard.php | 136 ++++++++++++++---- Helper/NumberFormatHelper.php | 27 ++++ .../templates/info/billetCreditCard.phtml | 34 ++--- 3 files changed, 147 insertions(+), 50 deletions(-) create mode 100644 Helper/NumberFormatHelper.php diff --git a/Block/Payment/Info/BilletCreditCard.php b/Block/Payment/Info/BilletCreditCard.php index d2e97833..ea2acfbc 100644 --- a/Block/Payment/Info/BilletCreditCard.php +++ b/Block/Payment/Info/BilletCreditCard.php @@ -11,68 +11,98 @@ namespace Pagarme\Pagarme\Block\Payment\Info; +use Exception; use Magento\Framework\DataObject; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\View\Element\Template\Context; use Magento\Payment\Block\Info\Cc; +use Magento\Payment\Model\Config; use Pagarme\Core\Kernel\Aggregates\Charge; +use Pagarme\Core\Kernel\Aggregates\Order; +use Pagarme\Core\Kernel\Exceptions\InvalidParamException; use Pagarme\Core\Kernel\Services\OrderService; use Pagarme\Core\Kernel\ValueObjects\Id\OrderId; use Pagarme\Pagarme\Concrete\Magento2CoreSetup; use Pagarme\Pagarme\Concrete\Magento2PlatformOrderDecorator; +use Pagarme\Pagarme\Helper\NumberFormatHelper; use Pagarme\Pagarme\Helper\Payment\Billet as BilletHelper; class BilletCreditCard extends Cc { const TEMPLATE = 'Pagarme_Pagarme::info/billetCreditCard.phtml'; + /** + * @var NumberFormatHelper + */ + private $numberFormatter; + + public function __construct( + Context $context, + Config $paymentConfig, + NumberFormatHelper $numberFormatter, + array $data = [] + ) { + $this->numberFormatter = $numberFormatter; + parent::__construct($context, $paymentConfig, $data); + } + /** * {@inheritdoc} + * @throws LocalizedException */ - protected function _prepareSpecificInformation($transport = null) + protected function _prepareSpecificInformation($transport = null): DataObject { - $transport = new DataObject([ + $specificInformation = new DataObject([ (string)__('Print Billet') => $this->getInfo()->getAdditionalInformation('billet_url') ]); - $transport = parent::_prepareSpecificInformation($transport); - return $transport; + return parent::_prepareSpecificInformation($specificInformation); } - public function _construct() + /** + * @throws Exception + */ + protected function _construct() { Magento2CoreSetup::bootstrap(); $this->setTemplate(self::TEMPLATE); } - public function getCcType() - { - return $this->getCcTypeName(); - } - - public function getCardNumber() - { - return '**** **** **** ' . $this->getInfo()->getCcLast4(); - } - - public function getCardLast4() + /** + * @throws LocalizedException + */ + public function getCardLast4(): string { return '**** **** **** ' . $this->getInfo()->getAdditionalInformation('cc_last_4'); } + /** + * @throws LocalizedException + */ public function getCcBrand() { return $this->getInfo()->getAdditionalInformation('cc_type'); } + /** + * @throws LocalizedException + */ public function getTitle() { return $this->getInfo()->getAdditionalInformation('method_title'); } + /** + * @throws LocalizedException + */ public function getInstallments() { return $this->getInfo()->getAdditionalInformation('cc_installments'); } + /** + * @throws LocalizedException + */ public function getBilletUrl() { $billetHelper = new BilletHelper(); @@ -80,21 +110,30 @@ public function getBilletUrl() } - public function getCcAmount() - { - return $this->getInfo()->getAdditionalInformation('cc_cc_amount'); - } - + /** + * @throws LocalizedException + */ public function getCcAmountWithTax() { - return (float)$this->getInfo()->getAdditionalInformation('cc_cc_amount') + (float)$this->getInfo()->getAdditionalInformation('cc_cc_tax_amount'); + $ccAmountWithTax = (float) $this->getInfo()->getAdditionalInformation('cc_cc_amount') + + (float) $this->getInfo()->getAdditionalInformation('cc_cc_tax_amount'); + return $this->numberFormatter->formatToLocalCurrency($ccAmountWithTax); } + /** + * @throws LocalizedException + */ public function getBilletAmount() { - return (float)$this->getInfo()->getAdditionalInformation('cc_billet_amount'); + return $this->numberFormatter->formatToLocalCurrency( + $this->getInfo()->getAdditionalInformation('cc_billet_amount') + ); } + /** + * @throws InvalidParamException + * @throws LocalizedException + */ public function getTransactionInfo() { $orderService = new OrderService(); @@ -110,7 +149,7 @@ public function getTransactionInfo() } /** - * @var \Pagarme\Core\Kernel\Aggregates\Order orderObject + * @var Order $orderObject */ $orderObject = $orderService->getOrderByPagarmeId(new OrderId($orderPagarmeId)); @@ -139,7 +178,54 @@ public function getTransactionInfo() return $transactionList; } - private function getTid(Charge $charge) + public function getCreditCardInformation(): string + { + $creditCardInformation = ''; + if (empty($this->getCcTypeName())) { + return $creditCardInformation; + } + + $creditCardInformation .= sprintf('%s', __($this->getTitle())); + $creditCardInformation .= '

'; + $creditCardInformation .= sprintf( + '%s', + __('Credit Card') + ); + $creditCardInformation .= $this->formatCreditCardData(__('Amount'), $this->getCcAmountWithTax()); + $creditCardInformation .= $this->formatCreditCardData(__('Brand'), $this->getCcBrand()); + $creditCardInformation .= $this->formatCreditCardData(__('Number'), $this->getCardLast4()); + $creditCardInformation .= $this->formatCreditCardData(__('Installments'), $this->getInstallments()); + + return $creditCardInformation; + } + + /** + * @throws LocalizedException + */ + public function getPrintBillet(): string + { + $printBillet = ''; + + $canShowPrintBillet = !empty($this->getBilletUrl()) && $this->getInfo()->getOrder()->getStatus() === 'pending'; + if (!$canShowPrintBillet) { + return $printBillet; + } + + $printBillet .= '

'; + $printBillet .= sprintf( + '%s', + $this->getBilletUrl(), + __('Print Billet') + ); + return $printBillet; + } + + private function formatCreditCardData($title, $information): string + { + return sprintf('
%s: %s', $title, $information); + } + + private function getTid(Charge $charge): ?string { $transaction = $charge->getLastTransaction(); diff --git a/Helper/NumberFormatHelper.php b/Helper/NumberFormatHelper.php new file mode 100644 index 00000000..00446adc --- /dev/null +++ b/Helper/NumberFormatHelper.php @@ -0,0 +1,27 @@ +numberFormatter = new NumberFormatter('pt-BR', NumberFormatter::CURRENCY); + } + + public function formatToLocalCurrency($number) + { + return $this->numberFormatter->format($number); + } + +} diff --git a/view/frontend/templates/info/billetCreditCard.phtml b/view/frontend/templates/info/billetCreditCard.phtml index b331c360..bb557604 100644 --- a/view/frontend/templates/info/billetCreditCard.phtml +++ b/view/frontend/templates/info/billetCreditCard.phtml @@ -1,29 +1,13 @@ -getCcType()): ?> - getTitle()); ?> -
-
- - - -
- : getCcAmountWithTax(),2,',', '.'); ?> -
- : getCcBrand(); ?> -
- : getCardLast4(); ?> -
- : getInstallments(); ?> - - - + + getCreditCardInformation() ?>
- +
- : getBilletAmount(),2,',', '.'); ?> -getBilletUrl() && $this->getInfo()->getOrder()->getStatus() == 'pending'): ?> -
-
- - + : getBilletAmount(); ?> + getPrintBillet() ?> From 5bd486029ccab6a1931920f00b80b98517eb9900 Mon Sep 17 00:00:00 2001 From: Mateus Picoloto Date: Wed, 12 Jul 2023 19:06:24 -0300 Subject: [PATCH 2/3] removing line break elements and changing to use paragraph elements --- Block/Payment/Info/BilletCreditCard.php | 6 ++---- view/frontend/templates/info/billetCreditCard.phtml | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Block/Payment/Info/BilletCreditCard.php b/Block/Payment/Info/BilletCreditCard.php index ea2acfbc..32c6b10d 100644 --- a/Block/Payment/Info/BilletCreditCard.php +++ b/Block/Payment/Info/BilletCreditCard.php @@ -185,8 +185,7 @@ public function getCreditCardInformation(): string return $creditCardInformation; } - $creditCardInformation .= sprintf('%s', __($this->getTitle())); - $creditCardInformation .= '

'; + $creditCardInformation .= sprintf('

%s

', __($this->getTitle())); $creditCardInformation .= sprintf( '%s', __('Credit Card') @@ -211,7 +210,6 @@ public function getPrintBillet(): string return $printBillet; } - $printBillet .= '

'; $printBillet .= sprintf( '%s', $this->getBilletUrl(), @@ -222,7 +220,7 @@ public function getPrintBillet(): string private function formatCreditCardData($title, $information): string { - return sprintf('
%s: %s', $title, $information); + return sprintf('

%s: %s

', $title, $information); } private function getTid(Charge $charge): ?string diff --git a/view/frontend/templates/info/billetCreditCard.phtml b/view/frontend/templates/info/billetCreditCard.phtml index bb557604..436cdb8f 100644 --- a/view/frontend/templates/info/billetCreditCard.phtml +++ b/view/frontend/templates/info/billetCreditCard.phtml @@ -8,6 +8,5 @@ -
- : getBilletAmount(); ?> +

: getBilletAmount(); ?>

getPrintBillet() ?> From 0a0e7b3be0c797497729e8c7b952c87dca375170 Mon Sep 17 00:00:00 2001 From: Mateus Picoloto Date: Fri, 14 Jul 2023 09:05:15 -0300 Subject: [PATCH 3/3] changing to return type be in comment section --- Block/Payment/Info/BilletCreditCard.php | 30 ++++++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Block/Payment/Info/BilletCreditCard.php b/Block/Payment/Info/BilletCreditCard.php index 32c6b10d..636fd545 100644 --- a/Block/Payment/Info/BilletCreditCard.php +++ b/Block/Payment/Info/BilletCreditCard.php @@ -50,7 +50,7 @@ public function __construct( * {@inheritdoc} * @throws LocalizedException */ - protected function _prepareSpecificInformation($transport = null): DataObject + protected function _prepareSpecificInformation($transport = null) { $specificInformation = new DataObject([ (string)__('Print Billet') => $this->getInfo()->getAdditionalInformation('billet_url') @@ -69,9 +69,10 @@ protected function _construct() } /** + * @return string * @throws LocalizedException */ - public function getCardLast4(): string + public function getCardLast4() { return '**** **** **** ' . $this->getInfo()->getAdditionalInformation('cc_last_4'); } @@ -101,6 +102,7 @@ public function getInstallments() } /** + * @return string|null * @throws LocalizedException */ public function getBilletUrl() @@ -111,6 +113,7 @@ public function getBilletUrl() } /** + * @return false|string * @throws LocalizedException */ public function getCcAmountWithTax() @@ -121,6 +124,7 @@ public function getCcAmountWithTax() } /** + * @return false|string * @throws LocalizedException */ public function getBilletAmount() @@ -131,6 +135,7 @@ public function getBilletAmount() } /** + * @return array * @throws InvalidParamException * @throws LocalizedException */ @@ -178,7 +183,10 @@ public function getTransactionInfo() return $transactionList; } - public function getCreditCardInformation(): string + /** + * @return string + */ + public function getCreditCardInformation() { $creditCardInformation = ''; if (empty($this->getCcTypeName())) { @@ -199,9 +207,10 @@ public function getCreditCardInformation(): string } /** + * @return string * @throws LocalizedException */ - public function getPrintBillet(): string + public function getPrintBillet() { $printBillet = ''; @@ -218,12 +227,21 @@ public function getPrintBillet(): string return $printBillet; } - private function formatCreditCardData($title, $information): string + /** + * @param mixed $title + * @param mixed $information + * @return string + */ + private function formatCreditCardData($title, $information) { return sprintf('

%s: %s

', $title, $information); } - private function getTid(Charge $charge): ?string + /** + * @param mixed $charge + * @return string|null + */ + private function getTid($charge) { $transaction = $charge->getLastTransaction();