Skip to content

Commit

Permalink
Merge pull request #203 from pagarme/feature/PAOPN-459-refactor-bille…
Browse files Browse the repository at this point in the history
…tcreditcard-block

PAOPN-459: Refactoring BilletCreditCard block and template
  • Loading branch information
mateus-picoloto authored Jul 14, 2023
2 parents 8adeb37 + 0a0e7b3 commit 991ccac
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 49 deletions.
148 changes: 125 additions & 23 deletions Block/Payment/Info/BilletCreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,90 +11,134 @@

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)
{
$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();
}

/**
* @return string
* @throws LocalizedException
*/
public function getCardLast4()
{
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');
}

/**
* @return string|null
* @throws LocalizedException
*/
public function getBilletUrl()
{
$billetHelper = new BilletHelper();
return $billetHelper->getBilletUrl($this->getInfo());

}

public function getCcAmount()
{
return $this->getInfo()->getAdditionalInformation('cc_cc_amount');
}

/**
* @return false|string
* @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);
}

/**
* @return false|string
* @throws LocalizedException
*/
public function getBilletAmount()
{
return (float)$this->getInfo()->getAdditionalInformation('cc_billet_amount');
return $this->numberFormatter->formatToLocalCurrency(
$this->getInfo()->getAdditionalInformation('cc_billet_amount')
);
}

/**
* @return array
* @throws InvalidParamException
* @throws LocalizedException
*/
public function getTransactionInfo()
{
$orderService = new OrderService();
Expand All @@ -110,7 +154,7 @@ public function getTransactionInfo()
}

/**
* @var \Pagarme\Core\Kernel\Aggregates\Order orderObject
* @var Order $orderObject
*/
$orderObject = $orderService->getOrderByPagarmeId(new OrderId($orderPagarmeId));

Expand Down Expand Up @@ -139,7 +183,65 @@ public function getTransactionInfo()
return $transactionList;
}

private function getTid(Charge $charge)
/**
* @return string
*/
public function getCreditCardInformation()
{
$creditCardInformation = '';
if (empty($this->getCcTypeName())) {
return $creditCardInformation;
}

$creditCardInformation .= sprintf('<p>%s</p>', __($this->getTitle()));
$creditCardInformation .= sprintf(
'<strong class="box-title"><span>%s</span></strong>',
__('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;
}

/**
* @return string
* @throws LocalizedException
*/
public function getPrintBillet()
{
$printBillet = '';

$canShowPrintBillet = !empty($this->getBilletUrl()) && $this->getInfo()->getOrder()->getStatus() === 'pending';
if (!$canShowPrintBillet) {
return $printBillet;
}

$printBillet .= sprintf(
'<a class="action tocart primary" id="pagarme-link-boleto" href="%s" target="_blank">%s</a>',
$this->getBilletUrl(),
__('Print Billet')
);
return $printBillet;
}

/**
* @param mixed $title
* @param mixed $information
* @return string
*/
private function formatCreditCardData($title, $information)
{
return sprintf('<p><b>%s: </b>%s</p>', $title, $information);
}

/**
* @param mixed $charge
* @return string|null
*/
private function getTid($charge)
{
$transaction = $charge->getLastTransaction();

Expand Down
35 changes: 9 additions & 26 deletions view/frontend/templates/info/billetCreditCard.phtml
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
<?php if ($this->getCcType()): ?>
<span><?php echo __($this->getTitle()); ?></span>
<br/>
<br/>
<strong class="box-title">
<span><?php echo __('Credit Card'); ?></span>
</strong>
<br/>
<span><b><?php echo __('Amount') ?>: </b><?php echo 'R$ ' . number_format($this->getCcAmountWithTax(),2,',', '.'); ?></span>
<br/>
<span><b><?php echo __('Brand'); ?>: </b><?php echo $this->getCcBrand(); ?></span>
<br/>
<span><b><?php echo __('Number'); ?>: </b><?php echo $this->getCardLast4(); ?></span>
<br/>
<span><b><?php echo __('Installments'); ?>: </b><?php echo $this->getInstallments(); ?></span>

<?php endif ?>

<?php
/**
* @var Pagarme\Pagarme\Block\Payment\Info\BilletCreditCard $block
*/
?>
<?= $block->getCreditCardInformation() ?>
<hr />
<strong class="box-title">
<span><?php echo __('Billet'); ?></span>
<span><?= __('Billet'); ?></span>
</strong>
<br/>
<span><b><?php echo __('Amount') ?>: </b><?php echo 'R$ ' . number_format($this->getBilletAmount(),2,',', '.'); ?></span>
<?php if ($this->getBilletUrl() && $this->getInfo()->getOrder()->getStatus() == 'pending'): ?>
<br/>
<br/>
<a class="action tocart primary" id="pagarme-link-boleto" href="<?php echo $this->getBilletUrl() ?>" target="_blank"><?php echo __('Print Billet') ?></a>
<?php endif ?>
<p><b><?= __('Amount') ?>: </b><?= $block->getBilletAmount(); ?></p>
<?= $block->getPrintBillet() ?>

0 comments on commit 991ccac

Please sign in to comment.