From 22beafd3e4bfebf6c0e29793a5b5ca473572f9c2 Mon Sep 17 00:00:00 2001 From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:20:10 -0300 Subject: [PATCH 1/3] fix: when credit card data is empty --- Block/Payment/Info/BaseCardInfo.php | 3 +++ view/adminhtml/templates/info/card.phtml | 2 ++ view/frontend/templates/info/card.phtml | 2 ++ 3 files changed, 7 insertions(+) diff --git a/Block/Payment/Info/BaseCardInfo.php b/Block/Payment/Info/BaseCardInfo.php index 7c7a72c3..29836de4 100644 --- a/Block/Payment/Info/BaseCardInfo.php +++ b/Block/Payment/Info/BaseCardInfo.php @@ -41,6 +41,9 @@ public function getTransactionInfo() } $charge = current($orderObject->getCharges()); + if ($charge->getLastTransaction()->getCardData() == null) { + return []; + } $lastFourDigitsWithDots = sprintf( "**** **** **** %s", $charge->getLastTransaction()->getCardData()->getLastFourDigits()->getValue() diff --git a/view/adminhtml/templates/info/card.phtml b/view/adminhtml/templates/info/card.phtml index 0cc47db3..515f1b28 100644 --- a/view/adminhtml/templates/info/card.phtml +++ b/view/adminhtml/templates/info/card.phtml @@ -3,6 +3,8 @@ * @var \Pagarme\Core\Kernel\Aggregates\Transaction $info */ $info = $this->getTransactionInfo(); + if(empty($info)) + return; ?> getTitle()); ?> diff --git a/view/frontend/templates/info/card.phtml b/view/frontend/templates/info/card.phtml index 67862452..c9ba3dda 100644 --- a/view/frontend/templates/info/card.phtml +++ b/view/frontend/templates/info/card.phtml @@ -3,6 +3,8 @@ * @var \Pagarme\Core\Kernel\Aggregates\Transaction $info */ $info = $this->getTransactionInfo(); + if(empty($info)) + return; ?> getTitle()); ?> From c204af357fb7f3810ea8469c525101cd00908488 Mon Sep 17 00:00:00 2001 From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:28:15 -0300 Subject: [PATCH 2/3] fix: code smell --- view/adminhtml/templates/info/card.phtml | 3 ++- view/frontend/templates/info/card.phtml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/view/adminhtml/templates/info/card.phtml b/view/adminhtml/templates/info/card.phtml index 515f1b28..5aebcf62 100644 --- a/view/adminhtml/templates/info/card.phtml +++ b/view/adminhtml/templates/info/card.phtml @@ -3,8 +3,9 @@ * @var \Pagarme\Core\Kernel\Aggregates\Transaction $info */ $info = $this->getTransactionInfo(); - if(empty($info)) + if(empty($info)) { return; + } ?> getTitle()); ?> diff --git a/view/frontend/templates/info/card.phtml b/view/frontend/templates/info/card.phtml index c9ba3dda..28cb0f32 100644 --- a/view/frontend/templates/info/card.phtml +++ b/view/frontend/templates/info/card.phtml @@ -3,8 +3,9 @@ * @var \Pagarme\Core\Kernel\Aggregates\Transaction $info */ $info = $this->getTransactionInfo(); - if(empty($info)) + if(empty($info)) { return; + } ?> getTitle()); ?> From 4f06cea0e4cc96d6d8fcf603e339e0199e2ecfb9 Mon Sep 17 00:00:00 2001 From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:39:03 -0300 Subject: [PATCH 3/3] refact: split in two functions --- Block/Payment/Info/BaseCardInfo.php | 44 +++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/Block/Payment/Info/BaseCardInfo.php b/Block/Payment/Info/BaseCardInfo.php index 29836de4..2874e3ef 100644 --- a/Block/Payment/Info/BaseCardInfo.php +++ b/Block/Payment/Info/BaseCardInfo.php @@ -16,11 +16,34 @@ abstract class BaseCardInfo extends Cc /** * @return array + */ + public function getTransactionInfo() + { + $charge = $this->getLastCharge(); + if ($charge->getLastTransaction()->getCardData() == null) { + return []; + } + $lastFourDigitsWithDots = sprintf( + "**** **** **** %s", + $charge->getLastTransaction()->getCardData()->getLastFourDigits()->getValue() + ); + return array_merge( + $charge->getAcquirerTidCapturedAndAuthorize(), + ['tid' => $charge->getLastTransaction()->getAcquirerTid() ?? ""], + ['cardBrand' => $charge->getLastTransaction()->getCardData()->getBrand()->getName() ?? ""], + ['installments' => $this->getInfo()->getAdditionalInformation('cc_installments') ?? ""], + ['lastFour' => $lastFourDigitsWithDots], + ['acquirerMessage' => $charge->getLastTransaction()->getAcquirerMessage() ?? ""] + ); + } + + /** + * @return mixed|array|Charge * @throws InvalidParamException * @throws LocalizedException * @throws Exception */ - public function getTransactionInfo() + private function getLastCharge() { Magento2CoreSetup::bootstrap(); $orderService = new OrderService(); @@ -40,24 +63,9 @@ public function getTransactionInfo() return []; } - $charge = current($orderObject->getCharges()); - if ($charge->getLastTransaction()->getCardData() == null) { - return []; - } - $lastFourDigitsWithDots = sprintf( - "**** **** **** %s", - $charge->getLastTransaction()->getCardData()->getLastFourDigits()->getValue() - ); - return array_merge( - $charge->getAcquirerTidCapturedAndAuthorize(), - ['tid' => $charge->getLastTransaction()->getAcquirerTid() ?? ""], - ['cardBrand' => $charge->getLastTransaction()->getCardData()->getBrand()->getName() ?? ""], - ['installments' => $this->getInfo()->getAdditionalInformation('cc_installments') ?? ""], - ['lastFour' => $lastFourDigitsWithDots], - ['acquirerMessage' => $charge->getLastTransaction()->getAcquirerMessage() ?? ""] - ); + return current($orderObject->getCharges()); } - + /** * @param mixed $orderService * @param mixed $pagarmeId