Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Card saving optional
Browse files Browse the repository at this point in the history
Added configuration switch to decide if customer can save and pay by
saved credit card
  • Loading branch information
piotrjozwiak committed Dec 11, 2017
1 parent 9164a2a commit d5193f3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
6 changes: 6 additions & 0 deletions Api/TpayCardsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ public function isCustomerLoggedIn();
* @return int|null
*/
public function getCheckoutCustomerId();

/**
* @return bool
*/
public function getCardSaveEnabled();

}
33 changes: 17 additions & 16 deletions Controller/tpaycards/CardPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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'],
Expand All @@ -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);
}
}

Expand All @@ -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']
);
}
Expand Down
9 changes: 9 additions & 0 deletions Model/TpayCards.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
18 changes: 10 additions & 8 deletions Model/TpayCardsConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -88,6 +89,7 @@ public function getConfig()
'redirectUrl' => $tpay->getPaymentRedirectUrl(),
'isCustomerLoggedIn' => $tpay->isCustomerLoggedIn(),
'customerTokens' => $customerTokensData,
'isSavingEnabled' => $tpay->getCardSaveEnabled(),
],
],
];
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<label>RSA key</label>
<validate>validate-no-empty</validate>
</field>
<field id="card_save_enabled" translate="label" type="select" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable credit card saving</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="send_invoice_email" translate="label" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Send new invoice email to customer</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
},
Expand Down

0 comments on commit d5193f3

Please sign in to comment.