From b161c10b8248217d84a0f9b83d9b86fd80c32e07 Mon Sep 17 00:00:00 2001 From: Igor Goltsov Date: Fri, 5 Nov 2021 15:56:28 +0300 Subject: [PATCH 1/4] MPGS-590: Remove unnecessary error handling for SimplifyCommerce.hostedPayments() callback --- views/js/simplify.embedded.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/views/js/simplify.embedded.js b/views/js/simplify.embedded.js index 6d3256a..9ad3c88 100644 --- a/views/js/simplify.embedded.js +++ b/views/js/simplify.embedded.js @@ -302,22 +302,13 @@ $(function () { /** * Payment initialization */ - var hostedPaymentsOnject = SimplifyCommerce.hostedPayments( + var hostedPaymentsObject = SimplifyCommerce.hostedPayments( paymentCallback, window.getEmbeddedConfig() ); - hostedPaymentsOnject.closeOnCompletion(); + hostedPaymentsObject.closeOnCompletion(); function paymentCallback(response) { - if (response && response.length > 0 && response[0].error) { - setState({ - paymentError: response[0].error, - }); - - hostedPaymentsOnject.enablePayBtn(); - return; - } - if (response && response.cardToken) { elements .paymentForm From 31b2d8acf67b0f1ec65799bbd712cd678a2e2ba2 Mon Sep 17 00:00:00 2001 From: Igor Goltsov Date: Fri, 5 Nov 2021 16:47:53 +0300 Subject: [PATCH 2/4] MPGS-590: Adjust naming for the constants used with the saved/new card option --- views/js/simplify.embedded.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/views/js/simplify.embedded.js b/views/js/simplify.embedded.js index 9ad3c88..82de4b5 100644 --- a/views/js/simplify.embedded.js +++ b/views/js/simplify.embedded.js @@ -20,8 +20,8 @@ $(function () { return; } - var PAYMENT_OPTION_OLD = 'old'; - var PAYMENT_OPTION_NEW = 'new'; + var CARD_TYPE_OLD = 'old'; + var CART_TYPE_NEW = 'new'; var PAYMENT_CODE = 'simplifycommerce_embedded'; /** @@ -63,7 +63,7 @@ $(function () { var state = { hasSavedCard: window.simplifyHasSavedCard, paymentError: false, - chosenPaymentOption: PAYMENT_OPTION_OLD, + chosenCardType: CARD_TYPE_OLD, isCardDeletionInProgress: false, isCardDeleted: false, conditionsApproved: elements.conditionApproveElement.prop('checked'), @@ -92,26 +92,26 @@ $(function () { elements.savedCardSelector.on('click', function () { setState({ - chosenPaymentOption: PAYMENT_OPTION_OLD + chosenCardType: CARD_TYPE_OLD }); }); elements.newCardSelector.on('click', function () { setState({ - chosenPaymentOption: PAYMENT_OPTION_NEW, + chosenCardType: CART_TYPE_NEW, isCardDeletionInProgress: false }); }); elements.savedCardRadio.on('change', function () { setState({ - chosenPaymentOption: $(this).is(":checked") ? PAYMENT_OPTION_OLD : PAYMENT_OPTION_NEW + chosenCardType: $(this).is(":checked") ? CARD_TYPE_OLD : CART_TYPE_NEW }) }); elements.newCardRadio.on('change', function () { setState({ - chosenPaymentOption: $(this).is(":checked") ? PAYMENT_OPTION_NEW : PAYMENT_OPTION_OLD + chosenCardType: $(this).is(":checked") ? CART_TYPE_NEW : CARD_TYPE_OLD }) }); @@ -147,7 +147,7 @@ $(function () { setState({ isCardDeletionInProgress: true, isCardDeleted: true, - chosenPaymentOption: PAYMENT_OPTION_NEW, + chosenCardType: CART_TYPE_NEW, }); return false; }); @@ -235,14 +235,14 @@ $(function () { } /** Handle Payment Option */ - if (state.hasSavedCard && state.chosenPaymentOption === PAYMENT_OPTION_OLD) { + if (state.hasSavedCard && state.chosenCardType === CARD_TYPE_OLD) { elements.newCardForm.addClass('hidden'); elements.savedCardSelector.addClass('selected'); elements.newCardSelector.removeClass('selected'); elements.savedCardRadio.prop("checked", true); elements.newCardRadio.prop("checked", false); elements.paymentButtonContainer.show(); - } else if (state.hasSavedCard && state.chosenPaymentOption === PAYMENT_OPTION_NEW) { + } else if (state.hasSavedCard && state.chosenCardType === CART_TYPE_NEW) { elements.newCardForm.removeClass('hidden'); elements.newCardSelector.addClass('selected'); elements.savedCardSelector.removeClass('selected'); From f61ddfb97021dbf93b4c6135adc25d6e17e0aee6 Mon Sep 17 00:00:00 2001 From: Igor Goltsov Date: Mon, 8 Nov 2021 15:53:31 +0300 Subject: [PATCH 3/4] MPGS-590: Remove unnecessary error handler from JS code --- views/js/simplify.embedded.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/views/js/simplify.embedded.js b/views/js/simplify.embedded.js index 82de4b5..1c07c8c 100644 --- a/views/js/simplify.embedded.js +++ b/views/js/simplify.embedded.js @@ -180,23 +180,7 @@ $(function () { /** Handle Payment Errors */ if (state.paymentError) { - if (state.paymentError.code === "validation") { - var errorMessages = { - 'card.number': 'The card number you entered is invalid.', - 'card.expYear': 'The expiry year on the card is invalid.' - }; - - var fieldErrors = state.paymentError.fieldErrors, - fieldErrorsLength = fieldErrors.length, - errorList = ""; - - for (var i = 0; i < fieldErrorsLength; i++) { - errorList += '' + errorMessages[fieldErrors[i].field] + - ' ' + fieldErrors[i].message + '.'; - } - - elements.errorsContainer.html(errorList) - } else if (state.paymentError.code === "gateway") { + if (state.paymentError.code === "gateway") { elements.errorsContainer.html(state.paymentError.message); } else { elements.errorsContainer.html( From 85fa0af3f2ae946b37223eb8d2312a30290ff513 Mon Sep 17 00:00:00 2001 From: Igor Goltsov Date: Mon, 8 Nov 2021 17:06:30 +0300 Subject: [PATCH 4/4] MPGS-590: Fix the issue with the impossibility to place an Order using Saved Card --- simplifycommerce.php | 176 +++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 80 deletions(-) diff --git a/simplifycommerce.php b/simplifycommerce.php index 8a97163..9ef92ed 100644 --- a/simplifycommerce.php +++ b/simplifycommerce.php @@ -20,7 +20,7 @@ use PrestaShopBundle\Controller\Admin\Sell\Order\ActionsBarButton; use PrestaShopBundle\Controller\Admin\Sell\Order\ActionsBarButtonsCollection; -if ( ! defined('_PS_VERSION_')) { +if (!defined('_PS_VERSION_')) { exit; } @@ -57,26 +57,26 @@ class SimplifyCommerce extends PaymentModule */ public function __construct() { - $this->name = 'simplifycommerce'; - $this->tab = 'payments_gateways'; - $this->version = '2.2.0'; - $this->author = 'Mastercard'; + $this->name = 'simplifycommerce'; + $this->tab = 'payments_gateways'; + $this->version = '2.2.0'; + $this->author = 'Mastercard'; $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); - $this->currencies = true; + $this->currencies = true; $this->currencies_mode = 'checkbox'; $this->module_key = '8b7703c5901ec736bd931bbbb8cfd13c'; parent::__construct(); - $this->displayName = $this->l('Mastercard Payment Gateway Services - Simplify'); - $this->description = $this->l('Payments made easy - Start securely accepting card payments instantly.'); + $this->displayName = $this->l('Mastercard Payment Gateway Services - Simplify'); + $this->description = $this->l('Payments made easy - Start securely accepting card payments instantly.'); $this->confirmUninstall = $this->l('Warning: Are you sure you want to uninstall this module?'); - $this->defaultTitle = $this->l('Pay with Card'); - $this->controllerAdmin = 'AdminSimplify'; + $this->defaultTitle = $this->l('Pay with Card'); + $this->controllerAdmin = 'AdminSimplify'; - if ( ! count(Currency::checkPaymentCurrencies($this->id))) { + if (!count(Currency::checkPaymentCurrencies($this->id))) { $this->warning = $this->trans( 'No currency has been set for this module.', array(), @@ -92,15 +92,15 @@ public function __construct() */ private function installTab() { - $tab = new Tab(); + $tab = new Tab(); $tab->class_name = $this->controllerAdmin; - $tab->active = 1; - $tab->name = array(); + $tab->active = 1; + $tab->name = array(); foreach (Language::getLanguages(true) as $lang) { $tab->name[$lang['id_lang']] = $this->name; } $tab->id_parent = -1; - $tab->module = $this->name; + $tab->module = $this->name; return $tab->add(); } @@ -113,7 +113,7 @@ private function installTab() private function uninstallTab() { $id_tab = (int)Tab::getIdFromClassName($this->controllerAdmin); - $tab = new Tab($id_tab); + $tab = new Tab($id_tab); if (Validate::isLoadedObject($tab)) { return $tab->delete(); } @@ -123,7 +123,7 @@ private function uninstallTab() public function checkCurrency($cart) { - $currency_order = new Currency((int)($cart->id_currency)); + $currency_order = new Currency((int)($cart->id_currency)); $currencies_module = $this->getCurrency((int)$cart->id_currency); if (is_array($currencies_module)) { @@ -150,7 +150,7 @@ public function getLangLink() public function hookDisplayHeader() { - if ( ! $this->active) { + if (!$this->active) { return; } @@ -180,7 +180,7 @@ public function hookDisplayHeader() public function install() { // Install admin tab - if ( ! $this->installTab()) { + if (!$this->installTab()) { return false; } @@ -217,11 +217,11 @@ public function hookActionGetAdminOrderButtons(array $params) } $isAuthorized = $order->current_state == Configuration::get('SIMPLIFY_OS_AUTHORIZED'); - $canVoid = $isAuthorized; - $canCapture = $isAuthorized; - $canAction = $isAuthorized || $canVoid || $canCapture; + $canVoid = $isAuthorized; + $canCapture = $isAuthorized; + $canAction = $isAuthorized || $canVoid || $canCapture; - if ( ! $canAction) { + if (!$canAction) { return; } @@ -289,9 +289,9 @@ public function hookDisplayAdminOrderLeft($params) } $isAuthorized = $order->current_state == Configuration::get('SIMPLIFY_OS_AUTHORIZED'); - $canVoid = $isAuthorized; - $canCapture = $isAuthorized; - $canRefund = $order->current_state == Configuration::get('PS_OS_PAYMENT'); + $canVoid = $isAuthorized; + $canCapture = $isAuthorized; + $canRefund = $order->current_state == Configuration::get('PS_OS_PAYMENT'); $canAction = $isAuthorized || $canVoid || $canCapture || $canRefund; @@ -313,23 +313,22 @@ public function hookDisplayAdminOrderLeft($params) public function installOrderState() { - if ( ! Configuration::get('SIMPLIFY_OS_AUTHORIZED') - || ! Validate::isLoadedObject(new OrderState(Configuration::get('SIMPLIFY_OS_AUTHORIZED')))) { - + if (!Configuration::get('SIMPLIFY_OS_AUTHORIZED') + || !Validate::isLoadedObject(new OrderState(Configuration::get('SIMPLIFY_OS_AUTHORIZED')))) { $order_state = new OrderState(); foreach (Language::getLanguages() as $language) { - $order_state->name[$language['id_lang']] = 'Payment Authorized'; + $order_state->name[$language['id_lang']] = 'Payment Authorized'; $order_state->template[$language['id_lang']] = 'payment'; } $order_state->send_email = true; - $order_state->color = '#4169E1'; - $order_state->hidden = false; - $order_state->delivery = false; - $order_state->logable = true; - $order_state->paid = true; - $order_state->invoice = false; + $order_state->color = '#4169E1'; + $order_state->hidden = false; + $order_state->delivery = false; + $order_state->logable = true; + $order_state->paid = true; + $order_state->invoice = false; if ($order_state->add()) { - $source = _PS_ROOT_DIR_.'/img/os/10.gif'; + $source = _PS_ROOT_DIR_.'/img/os/10.gif'; $destination = _PS_ROOT_DIR_.'/img/os/'.(int)$order_state->id.'.gif'; copy($source, $destination); } @@ -389,8 +388,8 @@ public function initSimplify() { include(dirname(__FILE__).'/lib/Simplify.php'); - $api_keys = $this->getSimplifyAPIKeys(); - Simplify::$publicKey = $api_keys->public_key; + $api_keys = $this->getSimplifyAPIKeys(); + Simplify::$publicKey = $api_keys->public_key; Simplify::$privateKey = $api_keys->private_key; } @@ -401,11 +400,11 @@ public function initSimplify() */ public function hookPaymentOptions($params) { - if ( ! $this->active) { + if (!$this->active) { return false; } - if ( ! $this->checkCurrency($params['cart'])) { + if (!$this->checkCurrency($params['cart'])) { return; } @@ -482,7 +481,7 @@ public function hookPaymentOptions($params) $this->smarty->assign('currency_iso', $currency->iso_code); $options = []; - if ( ! Configuration::get('SIMPLIFY_ENABLED_PAYMENT_WINDOW')) { + if (!Configuration::get('SIMPLIFY_ENABLED_PAYMENT_WINDOW')) { return $options; } @@ -490,7 +489,6 @@ public function hookPaymentOptions($params) $this->smarty->assign('enabled_payment_window', 0); $this->smarty->assign('enabled_embedded', 1); $options[] = $this->getEmbeddedPaymentOption(); - } else { $this->smarty->assign('enabled_payment_window', 1); $this->smarty->assign('enabled_embedded', 0); @@ -502,7 +500,7 @@ public function hookPaymentOptions($params) protected function safe($field) { - $copy = $field; + $copy = $field; $encoding = mb_detect_encoding($field); if ($encoding !== 'ASCII') { if (function_exists('transliterator_transliterate')) { @@ -518,7 +516,7 @@ protected function safe($field) return $field; } } - if ( ! $field) { + if (!$field) { // if translit turned the string into any false-like value, return original instead return $copy; } @@ -560,7 +558,7 @@ public function getEmbeddedPaymentOption() */ public function hookOrderConfirmation($params) { - if ( ! isset($params['objOrder']) || ($params['objOrder']->module != $this->name)) { + if (!isset($params['objOrder']) || ($params['objOrder']->module != $this->name)) { return false; } @@ -583,16 +581,16 @@ public function hookOrderConfirmation($params) */ public function processPayment() { - if ( ! $this->active) { + if (!$this->active) { return false; } $currency_order = new Currency((int)($this->context->cart->id_currency)); // Extract POST parameters from the request - $simplify_token_post = Tools::getValue('simplifyToken'); + $simplify_token_post = Tools::getValue('simplifyToken'); $delete_customer_card_post = Tools::getValue('deleteCustomerCard'); - $save_customer_post = Tools::getValue('saveCustomer'); + $save_customer_post = Tools::getValue('saveCustomer'); Logger::addLog( $this->l('Simplify Commerce - Save Customer = '.$save_customer_post), @@ -605,14 +603,14 @@ public function processPayment() $charge_customer_card = Tools::getValue('chargeCustomerCard'); - $token = ! empty($simplify_token_post) ? $simplify_token_post : null; - $should_delete_customer = ! empty($delete_customer_card_post) ? $delete_customer_card_post : false; - $should_save_customer = ! empty($save_customer_post) ? $save_customer_post : false; - $should_charge_customer_card = ! empty($charge_customer_card) ? $charge_customer_card : false; + $token = !empty($simplify_token_post) ? $simplify_token_post : null; + $should_delete_customer = !empty($delete_customer_card_post) ? $delete_customer_card_post : false; + $should_save_customer = !empty($save_customer_post) ? $save_customer_post : false; + $should_charge_customer_card = !empty($charge_customer_card) ? $charge_customer_card : false; include(dirname(__FILE__).'/lib/Simplify.php'); - $api_keys = $this->getSimplifyAPIKeys(); - Simplify::$publicKey = $api_keys->public_key; + $api_keys = $this->getSimplifyAPIKeys(); + Simplify::$publicKey = $api_keys->public_key; Simplify::$privateKey = $api_keys->private_key; // look up the customer @@ -688,7 +686,7 @@ public function processPayment() $payment_status = null; try { - $amount = $charge * 100; // Cart total amount + $amount = $charge * 100; // Cart total amount $description = $this->context->shop->name.$this->l(' Order Number: ').(int)$this->context->cart->id; if (isset($simplify_customer_id) && ($should_charge_customer_card == 'true' || $should_save_customer == 'on')) { @@ -704,20 +702,10 @@ public function processPayment() 'token' => $token, // Token returned by Simplify Card Tokenization 'description' => $description, 'currency' => $currency_order->iso_code, + 'card' => $this->getCardBillingInformation(), ); } - $cardholder_details = $this->getCardholderDetails(); - $country_iso = Country::getIsoById($cardholder_details->id_country); - $requestData['card'] = array( - 'addressCity' => $this->safe($cardholder_details->city), - 'addressCountry' => $country_iso, - 'addressLine1' => $this->safe($cardholder_details->address1), - 'addressLine2' => $this->safe($cardholder_details->address2), - 'addressState' => isset($cardholder_details->state) ? $this->safe($cardholder_details->state) : '', - 'addressZip' => $this->safe($cardholder_details->postcode), - ); - $txn_mode = Configuration::get('SIMPLIFY_TXN_MODE'); if ($txn_mode === self::TXN_MODE_PURCHASE) { @@ -782,16 +770,16 @@ public function processPayment() if (isset($payment[0])) { $payment[0]->transaction_id = pSQL($simplify_payment->id); - $payment_card = $simplify_payment->card; + $payment_card = $simplify_payment->card; if ($payment_card) { - $payment[0]->card_number = pSQL($payment_card->last4); - $payment[0]->card_brand = pSQL($payment_card->type); + $payment[0]->card_number = pSQL($payment_card->last4); + $payment[0]->card_brand = pSQL($payment_card->type); $payment[0]->card_expiration = sprintf( "%s/%s", pSQL($payment_card->expMonth), pSQL($payment_card->expYear) ); - $payment[0]->card_holder = pSQL($payment_card->name); + $payment[0]->card_holder = pSQL($payment_card->name); } $payment[0]->save(); } @@ -866,7 +854,7 @@ private function getSimplifyCustomerID($customer_id) $simplify_customer_id = null; try { - $customer = Simplify_Customer::findCustomer($customer_id); + $customer = Simplify_Customer::findCustomer($customer_id); $simplify_customer_id = $customer->id; } catch (Simplify_ApiException $e) { // can't find the customer on Simplify, so no need to delete @@ -904,7 +892,12 @@ private function deleteCustomerFromDB() /** * Function to create a new Simplify customer and to store its id in the database. * + * @param $token + * * @return string Simplify customer's id. + * + * @throws PrestaShopDatabaseException + * @throws PrestaShopException */ private function createNewSimplifyCustomer($token) { @@ -914,9 +907,11 @@ private function createNewSimplifyCustomer($token) 'email' => (string)$this->context->cookie->email, 'name' => (string)$this->context->cookie->customer_firstname.' '.(string)$this->context->cookie->customer_lastname, 'token' => $token, - 'reference' => $this->context->shop->name.$this->l( - ' Customer ID:' - ).' '.(int)$this->context->cookie->id_customer, + 'reference' => sprintf( + "%s %d", + $this->context->shop->name, + (int)$this->context->cookie->id_customer + ), ) ); @@ -941,8 +936,8 @@ private function createNewSimplifyCustomer($token) */ private function getSimplifyAPIKeys() { - $api_keys = new stdClass; - $api_keys->public_key = Configuration::get('SIMPLIFY_MODE') ? + $api_keys = new stdClass; + $api_keys->public_key = Configuration::get('SIMPLIFY_MODE') ? Configuration::get('SIMPLIFY_PUBLIC_KEY_LIVE') : Configuration::get('SIMPLIFY_PUBLIC_KEY_TEST'); $api_keys->private_key = Configuration::get('SIMPLIFY_MODE') ? Configuration::get('SIMPLIFY_PRIVATE_KEY_LIVE') : Configuration::get('SIMPLIFY_PRIVATE_KEY_TEST'); @@ -1022,7 +1017,7 @@ public function checkKeyPrefix() */ public function checkRequirements() { - $tests = array('result' => true); + $tests = array('result' => true); $tests['curl'] = array( 'name' => $this->l('PHP cURL extension must be enabled on your server'), 'result' => extension_loaded('curl'), @@ -1031,7 +1026,7 @@ public function checkRequirements() if (Configuration::get('SIMPLIFY_MODE')) { $tests['ssl'] = array( 'name' => $this->l('SSL must be enabled on your store (before entering Live mode)'), - 'result' => Configuration::get('PS_SSL_ENABLED') || ( ! empty($_SERVER['HTTPS']) && Tools::strtolower( + 'result' => Configuration::get('PS_SSL_ENABLED') || (!empty($_SERVER['HTTPS']) && Tools::strtolower( $_SERVER['HTTPS'] ) != 'off'), ); @@ -1057,7 +1052,7 @@ public function checkRequirements() } foreach ($tests as $k => $test) { - if ($k != 'result' && ! $test['result']) { + if ($k != 'result' && !$test['result']) { $tests['result'] = false; } } @@ -1184,4 +1179,25 @@ public function getContent() return $html; } + + /** + * @return array + * + * @throws PrestaShopDatabaseException + * @throws PrestaShopException + */ + private function getCardBillingInformation() + { + $cardholder_details = $this->getCardholderDetails(); + $country_iso = Country::getIsoById($cardholder_details->id_country); + + return array( + 'addressCity' => $this->safe($cardholder_details->city), + 'addressCountry' => $country_iso, + 'addressLine1' => $this->safe($cardholder_details->address1), + 'addressLine2' => $this->safe($cardholder_details->address2), + 'addressState' => isset($cardholder_details->state) ? $this->safe($cardholder_details->state) : '', + 'addressZip' => $this->safe($cardholder_details->postcode), + ); + } }