From d5193f3a624dd3066c1c45a53567d0990901a8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20J=C3=B3=C5=BAwiak?= Date: Mon, 11 Dec 2017 16:05:57 +0100 Subject: [PATCH] Card saving optional Added configuration switch to decide if customer can save and pay by saved credit card --- Api/TpayCardsInterface.php | 6 ++++ Controller/tpaycards/CardPayment.php | 33 ++++++++++--------- Model/TpayCards.php | 9 +++++ Model/TpayCardsConfigProvider.php | 18 +++++----- etc/adminhtml/system.xml | 4 +++ .../method-renderer/tpaycards-method.js | 3 +- 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/Api/TpayCardsInterface.php b/Api/TpayCardsInterface.php index 9249b9e..f0ec0cb 100644 --- a/Api/TpayCardsInterface.php +++ b/Api/TpayCardsInterface.php @@ -110,4 +110,10 @@ public function isCustomerLoggedIn(); * @return int|null */ public function getCheckoutCustomerId(); + + /** + * @return bool + */ + public function getCardSaveEnabled(); + } diff --git a/Controller/tpaycards/CardPayment.php b/Controller/tpaycards/CardPayment.php index d378651..29943e5 100644 --- a/Controller/tpaycards/CardPayment.php +++ b/Controller/tpaycards/CardPayment.php @@ -107,7 +107,9 @@ public function execute() $this->tpayService->setOrderStatePendingPayment($orderId); $additionalPaymentInformation = $paymentData['additional_information']; - if (isset($additionalPaymentInformation['card_id']) && $additionalPaymentInformation['card_id'] !== false) { + if (isset($additionalPaymentInformation['card_id']) && $additionalPaymentInformation['card_id'] !== false + && $this->tpay->getCardSaveEnabled() + ) { $cardId = (int)$additionalPaymentInformation['card_id']; return $this->processSavedCardPayment($orderId, $cardId); } else { @@ -191,9 +193,16 @@ private function addToPaymentData($orderId, $key, $value) private function processNewCardPayment($orderId, $additionalPaymentInformation) { + $saveCard = isset($additionalPaymentInformation['card_save']) && $this->tpay->getCardSaveEnabled() ? + (bool)$additionalPaymentInformation['card_save'] : false; $paymentCardFactory = (new ApiProvider($this->tpay, $this->paymentCardFactory))->getTpayPaymentCardFactory(); - $result = $this->createNewCardPayment($orderId, $additionalPaymentInformation); + $localData = $this->tpay->getTpayFormData($orderId); + try { + $result = $this->createNewCardPayment($orderId, $additionalPaymentInformation, $saveCard); + } catch (TException $e) { + return $this->trySaleAgain($localData, $orderId, $saveCard); + } if (isset($result[ResponseFields::URL3DS])) { $url3ds = $result[ResponseFields::URL3DS]; $this->tpayService->addCommentToHistory($orderId, '3DS Transaction link ' . $url3ds); @@ -202,8 +211,6 @@ private function processNewCardPayment($orderId, $additionalPaymentInformation) return $this->_redirect($url3ds); } else { - $localData = $this->tpay->getTpayFormData($orderId); - if (isset($result[ResponseFields::STATUS]) && (int)$result[ResponseFields::STATUS] === 'correct') { $paymentCardFactory->validateNon3dsSign($result['sign'], isset($result['test_mode']) ? '1' : '', $result['sale_auth'], '', $result['card'], $localData['kwota'], $result['date'], @@ -216,12 +223,9 @@ private function processNewCardPayment($orderId, $additionalPaymentInformation) ->setCustomerToken($this->tpay->getCustomerId($orderId), $result['cli_auth'], $result['card'], $additionalPaymentInformation['card_vendor']); } - - if ((int)$result[ResponseFields::RESULT] === 1 && $result[ResponseFields::STATUS] === 'correct') { - return $this->_redirect(static::SUCCESS_PATH); - } else { - $this->trySaleAgain($localData, $orderId, (bool)$additionalPaymentInformation['card_save']); - } + return (int)$result[ResponseFields::RESULT] === 1 && $result[ResponseFields::STATUS] === 'correct' ? + $this->_redirect(static::SUCCESS_PATH) : + $this->trySaleAgain($localData, $orderId, $saveCard); } } @@ -230,21 +234,18 @@ private function processNewCardPayment($orderId, $additionalPaymentInformation) * * @param int $orderId * @param array $additionalPaymentInformation - * + * @param $saveCard * @return array */ - private function createNewCardPayment($orderId, array $additionalPaymentInformation) + private function createNewCardPayment($orderId, array $additionalPaymentInformation, $saveCard) { - $oneTimer = isset($additionalPaymentInformation['card_save']) ? - !(bool)$additionalPaymentInformation['card_save'] : true; - $data = $this->tpay->getTpayFormData($orderId); $cardData = str_replace(' ', '+', $additionalPaymentInformation['card_data']); unset($additionalPaymentInformation['card_data']); $data = array_merge($data, $additionalPaymentInformation); return $this->apiFactory->secureSale($data['nazwisko'], $data['email'], $data['opis'], $data['kwota'], - $cardData, $data['currency'], $data['crc'], $oneTimer, $data['jezyk'], true, $data['pow_url'], + $cardData, $data['currency'], $data['crc'], !$saveCard, $data['jezyk'], true, $data['pow_url'], $data['pow_url_blad'] ); } diff --git a/Model/TpayCards.php b/Model/TpayCards.php index af3929e..beb1fb9 100644 --- a/Model/TpayCards.php +++ b/Model/TpayCards.php @@ -382,4 +382,13 @@ public function getCheckoutCustomerId() $customerSession = $objectManager->get('Magento\Customer\Model\Session'); return $customerSession->getCustomerId(); } + + /** + * {@inheritdoc} + */ + public function getCardSaveEnabled() + { + return (bool)$this->getConfigData('card_save_enabled'); + } + } diff --git a/Model/TpayCardsConfigProvider.php b/Model/TpayCardsConfigProvider.php index b2f2bdd..8f641d5 100644 --- a/Model/TpayCardsConfigProvider.php +++ b/Model/TpayCardsConfigProvider.php @@ -67,16 +67,17 @@ public function __construct( public function getConfig() { $tpay = $this->getPaymentMethodInstance(); - $customerTokens = $this->tokensService->getCustomerTokens($tpay->getCheckoutCustomerId()); $customerTokensData = []; - foreach ($customerTokens as $key => $value) { - $customerTokensData[] = [ - 'cardShortCode' => $value['cardShortCode'], - 'id' => $value['tokenId'], - 'vendor' => $value['vendor'], - ]; + if ($tpay->getCardSaveEnabled()) { + $customerTokens = $this->tokensService->getCustomerTokens($tpay->getCheckoutCustomerId()); + foreach ($customerTokens as $key => $value) { + $customerTokensData[] = [ + 'cardShortCode' => $value['cardShortCode'], + 'id' => $value['tokenId'], + 'vendor' => $value['vendor'], + ]; + } } - $config = [ 'tpaycards' => [ 'payment' => [ @@ -88,6 +89,7 @@ public function getConfig() 'redirectUrl' => $tpay->getPaymentRedirectUrl(), 'isCustomerLoggedIn' => $tpay->isCustomerLoggedIn(), 'customerTokens' => $customerTokensData, + 'isSavingEnabled' => $tpay->getCardSaveEnabled(), ], ], ]; diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 8bf5196..00a7773 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -45,6 +45,10 @@ validate-no-empty + + + Magento\Config\Model\Config\Source\Yesno + Magento\Config\Model\Config\Source\Yesno diff --git a/view/frontend/web/js/view/payment/method-renderer/tpaycards-method.js b/view/frontend/web/js/view/payment/method-renderer/tpaycards-method.js index 76a7895..95f67e6 100644 --- a/view/frontend/web/js/view/payment/method-renderer/tpaycards-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/tpaycards-method.js @@ -62,7 +62,8 @@ define( return $.extend(true, parent, {'additional_data': paymentData}); }, showSaveBox: function () { - if (window.checkoutConfig.tpaycards.payment.isCustomerLoggedIn) { + if (window.checkoutConfig.tpaycards.payment.isCustomerLoggedIn + && window.checkoutConfig.tpaycards.payment.isSavingEnabled) { $('.amPmCheckbox').css('display', 'block'); } },