Skip to content

Commit

Permalink
Improve observers code
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaMelazzo committed Aug 15, 2023
1 parent 7d1291e commit 60fabea
Showing 1 changed file with 58 additions and 38 deletions.
96 changes: 58 additions & 38 deletions Observer/CreditCardDataAssignObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
namespace Pagarme\Pagarme\Observer;


use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Framework\Event\Observer;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\PaymentInterface;
use Pagarme\Core\Payment\Repositories\SavedCardRepository;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
use Pagarme\Pagarme\Model\Cards;
use Pagarme\Pagarme\Model\CardsRepository;
use Pagarme\Pagarme\Helper\MultiBuyerDataAssign;
use Pagarme\Pagarme\Model\CardsRepository;

class CreditCardDataAssignObserver extends AbstractDataAssignObserver
{
Expand Down Expand Up @@ -58,48 +53,73 @@ public function execute(Observer $observer)
$info->setAdditionalInformation('cc_saved_card', '0');

if ($additionalData->getCcSavedCard()) {
$cardId = $additionalData->getCcSavedCard();
$card = $this->cardsRepository->getById($cardId);

$info->setAdditionalInformation('cc_saved_card', $additionalData->getCcSavedCard());
$info->setAdditionalInformation('cc_type', $card->getBrand());
$info->setAdditionalInformation(
'cc_last_4',
(string) $card->getLastFourNumbers()
);
$info->addData([
'cc_type' => $card->getBrand(),
'cc_owner' => $card->getCardHolderName(),
'cc_last_4' => (string) $card->getLastFourNumbers()
]);
$this->fillSavedCardData($additionalData, $info);
}else{
$info->setAdditionalInformation('cc_saved_card', $additionalData->getCcSavedCard());
$info->setAdditionalInformation('cc_type', $additionalData->getCcType());
if ($additionalData->getCcLast4()) {
$info->setAdditionalInformation('cc_last_4', substr($additionalData->getCcLast4(),-4));
}
$info->setAdditionalInformation('cc_token_credit_card', $additionalData->getCcTokenCreditCard());
$info->addData([
'cc_type' => $additionalData->getCcType(),
'cc_owner' => $additionalData->getCcOwner(),
'cc_last_4' => $additionalData->getCcLast4(),
'cc_exp_month' => $additionalData->getCcExpMonth(),
'cc_exp_year' => $additionalData->getCcExpYear(),
'cc_token_credit_card' => $additionalData->getCcTokenCreditCard(),
]);

$info->setAdditionalInformation('cc_savecard', $additionalData->getCcSavecard());
$this->fillNotSavedCardData($info, $additionalData);
}

$multiBuyerDataAssign = new MultiBuyerDataAssign();
$multiBuyerDataAssign->setCcMultiBuyer($info, $additionalData);

$info->setAdditionalInformation('cc_installments', 1);

if ($additionalData->getCcInstallments()) {
$info->setAdditionalInformation('cc_installments', (int) $additionalData->getCcInstallments());
$info->setAdditionalInformation(
'cc_installments',
(int) $additionalData->getCcInstallments()
);
}

return $this;
}

/**
* @param DataObject $additionalData
* @param $info
*/
private function fillSavedCardData(DataObject $additionalData, $info)
{
$cardId = $additionalData->getCcSavedCard();
$card = $this->cardsRepository->getById($cardId);

$info->setAdditionalInformation('cc_saved_card', $additionalData->getCcSavedCard());
$info->setAdditionalInformation('cc_type', $card->getBrand());
$info->setAdditionalInformation(
'cc_last_4',
(string) $card->getLastFourNumbers()
);
$info->addData([
'cc_type' => $card->getBrand(),
'cc_owner' => $card->getCardHolderName(),
'cc_last_4' => (string) $card->getLastFourNumbers()
]);
}

/**
* @param $info
* @param DataObject $additionalData
*/
private function fillNotSavedCardData($info, DataObject $additionalData)
{
$info->setAdditionalInformation('cc_saved_card', $additionalData->getCcSavedCard());
$info->setAdditionalInformation('cc_type', $additionalData->getCcType());
if ($additionalData->getCcLast4()) {
$info->setAdditionalInformation(
'cc_last_4',
substr($additionalData->getCcLast4(),-4)
);
}
$info->setAdditionalInformation('cc_token_credit_card', $additionalData->getCcTokenCreditCard());
$info->addData([
'cc_type' => $additionalData->getCcType(),
'cc_owner' => $additionalData->getCcOwner(),
'cc_last_4' => $additionalData->getCcLast4(),
'cc_exp_month' => $additionalData->getCcExpMonth(),
'cc_exp_year' => $additionalData->getCcExpYear(),
'cc_token_credit_card' => $additionalData->getCcTokenCreditCard(),
]);

$info->setAdditionalInformation('cc_savecard', $additionalData->getCcSavecard());
}

}

0 comments on commit 60fabea

Please sign in to comment.