diff --git a/Observer/CreditCardDataAssignObserver.php b/Observer/CreditCardDataAssignObserver.php index 71ccc6ca..4d270f65 100644 --- a/Observer/CreditCardDataAssignObserver.php +++ b/Observer/CreditCardDataAssignObserver.php @@ -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 { @@ -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()); + } + }