diff --git a/clearpay.php b/clearpay.php index ea4d817..56ade3a 100644 --- a/clearpay.php +++ b/clearpay.php @@ -52,12 +52,53 @@ class Clearpay extends PaymentModule * @var array */ public $defaultCountriesPerRegion = array( - 'AU' => '["AU"]', - 'CA' => '["CA"]', + 'AU' => '["AU", "EN"]', + 'CA' => '["CA", "EN"]', 'ES' => '["ES", "IT", "FR"]', - 'GB' => '["GB"]', - 'NZ' => '["NZ"]', - 'US' => '["US"]', + 'GB' => '["GB", "EN"]', + 'NZ' => '["NZ", "EN"]', + 'US' => '["US", "EN"]', + ); + + /** + * Default locale iso country codes per country + * + * @var array + */ + public $defaultIsoCountryCodePerCountry = array( + 'AU' => 'en_AU', + 'CA' => 'en_CA', + 'ES' => 'es_ES', + 'GB' => 'en_GB', + 'NZ' => 'en_NZ', + 'US' => 'en_US', + ); + + /** + * allowed currency per region + * + * @var array + */ + public $allowedCurrencyPerRegion = array( + 'AU' => 'AUD', + 'CA' => 'CAD', + 'ES' => 'EUR', + 'GB' => 'GBP', + 'NZ' => 'NZD', + 'US' => 'USD', + ); + + /** + * Default currency per region + * + * @var array + */ + public $defaultLanguagePerCurrency = array( + 'AUD' => 'AU', + 'CAD' => 'CA', + 'GBP' => 'GB', + 'NZD' => 'NZ', + 'USD' => 'US', ); /** @@ -78,6 +119,7 @@ class Clearpay extends PaymentModule * @var null $shippingAddress */ protected $shippingAddress = null; + /** * @var null $billingAddress */ @@ -99,7 +141,7 @@ public function __construct() { $this->name = 'clearpay'; $this->tab = 'payments_gateways'; - $this->version = '1.1.1'; + $this->version = '1.1.2'; $this->author = $this->l('Clearpay'); $this->currencies = true; $this->currencies_mode = 'checkbox'; @@ -245,14 +287,13 @@ public function isPaymentMethodAvailable() $publicKey = Configuration::get('CLEARPAY_PUBLIC_KEY'); $secretKey = Configuration::get('CLEARPAY_SECRET_KEY'); - $allowedCountries = json_decode(Configuration::get('CLEARPAY_ALLOWED_COUNTRIES')); - $language = $this->getCurrentLanguage(); $categoryRestriction = $this->isCartRestricted($this->context->cart); + $currencyRestriction = $this->isRestrictedByLangOrCurrency(); return ( $isEnabled && $totalAmount >= $displayMinAmount && $totalAmount <= $displayMaxAmount && - in_array(Tools::strtoupper($language), $allowedCountries) && + !$currencyRestriction && !$categoryRestriction && $publicKey && $secretKey @@ -281,30 +322,40 @@ private function getButtonTemplateVars(Cart $cart) */ public function hookHeader($params) { - if ( - Context::getContext()->controller->php_self === 'product' || - Context::getContext()->controller->php_self === 'order' - ) { - echo ''; + if (version_compare(_PS_VERSION_, '1.7', 'lt')) { + $template = $this->hookDisplayWrapperTop(); + if (!empty($template)) { + echo($template); + } } - if (_PS_VERSION_ >= "1.7") { - $this->context->controller->registerJavascript( - sha1(mt_rand(1, 90000)), - self::CLEARPAY_JS_CDN_URL, - array('server' => 'remote') - ); - - } else { - $this->context->controller->addJS(self::CLEARPAY_JS_CDN_URL); + if (Context::getContext()->controller->php_self === 'product') { + try { + echo ''; + } catch (\Exception $exception) { + // Continue + } + } + if (Context::getContext()->controller->php_self === 'order') { + if (_PS_VERSION_ >= "1.7") { + $this->context->controller->registerJavascript( + sha1(mt_rand(1, 90000)), + self::CLEARPAY_JS_CDN_URL, + array('server' => 'remote') + ); + } else { + $this->context->controller->addJS(self::CLEARPAY_JS_CDN_URL); + } } } @@ -336,18 +387,7 @@ public function hookPaymentOptions() $checkoutText = $this->l('4 interest-free payments of') . ' ' . $amountWithCurrency; } $templateConfigs['TITLE'] = (string) $checkoutText; - $language = Language::getLanguage($this->context->language->id); - if (isset($language['locale'])) { - $language = $language['locale']; - } else { - $language = $language['language_code']; - } - $templateConfigs['ISO_COUNTRY_CODE'] = str_replace('-', '_', $language); - // Preserve Uppercase in locale - if (Tools::strlen($templateConfigs['ISO_COUNTRY_CODE']) == 5) { - $templateConfigs['ISO_COUNTRY_CODE'] = Tools::substr($templateConfigs['ISO_COUNTRY_CODE'], 0, 2) . - Tools::strtoupper(Tools::substr($templateConfigs['ISO_COUNTRY_CODE'], 2, 4)); - } + $templateConfigs['ISO_COUNTRY_CODE'] = $this->getIsoCountryCode(); $templateConfigs['CURRENCY'] = $this->currency; $templateConfigs['TOTAL_AMOUNT'] = $totalAmount; $description = $this->l('You will be redirected to Clearpay to fill out your payment information.'); @@ -358,12 +398,12 @@ public function hookPaymentOptions() $templateConfigs['MORE_INFO_TEXT'] = $this->l('More info'); $templateConfigs['LOGO_TEXT'] = $this->l("Clearpay"); $templateConfigs['ICON'] = 'https://static.afterpay.com/app/icon-128x128.png'; - $templateConfigs['LOGO_BADGE'] = 'https://static.afterpay.com/email/logo-clearpay-colour.png'; + $templateConfigs['LOGO_BADGE'] ='https://static.afterpay.com/logo/compact-badge-clearpay-black-on-mint.svg'; $templateConfigs['LOGO_OPC'] = Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/views/img/logo_opc.png'); $templateConfigs['PAYMENT_URL'] = $link->getModuleLink('clearpay', 'payment'); $mobileViewLayout = Tools::strtolower('four-by-one'); $isMobileLayout = $this->context->isMobile(); - if ($this->context->isMobile()){ + if ($isMobileLayout) { $mobileViewLayout = Tools::strtolower('two-by-two'); } $templateConfigs['AP_MOBILE_LAYOUT'] = $mobileViewLayout; @@ -636,7 +676,6 @@ private function renderForm() */ public function getContent() { - $message = ''; $settingsKeys = array(); $settingsKeys[] = 'CLEARPAY_IS_ENABLED'; $settingsKeys[] = 'CLEARPAY_PUBLIC_KEY'; @@ -674,8 +713,7 @@ public function getContent() } // auto update configuration price thresholds and allowed countries in background - $language = $this->getCurrentLanguage(); - if ($isEnabled && !empty($publicKey) && !empty($secretKey) && !empty($environment) && !empty($language)) { + if ($isEnabled && !empty($publicKey) && !empty($secretKey) && !empty($environment)) { try { if (!empty($publicKey) && !empty($secretKey) && $isEnabled) { $merchantAccount = new Afterpay\SDK\MerchantAccount(); @@ -755,7 +793,7 @@ public function getContent() } } - $logo = 'https://static.afterpay.com/email/logo-clearpay-colour.png'; + $logo = 'https://static.afterpay.com/icon/clearpay-logo-colour-transparent.svg'; $tpl = $this->local_path.'views/templates/admin/config-info.tpl'; $header = $this->l('Clearpay Configuration Panel'); $button1 = $this->l('Contact us'); @@ -825,12 +863,12 @@ public function hookPayment($params) $templateConfigs['MORE_INFO_TEXT'] = $this->l('More info'); $templateConfigs['LOGO_TEXT'] = $this->l("Clearpay"); $templateConfigs['ICON'] = 'https://static.afterpay.com/app/icon-128x128.png'; - $templateConfigs['LOGO_BADGE'] = 'https://static.afterpay.com/email/logo-clearpay-colour.png'; + $templateConfigs['LOGO_BADGE'] ='https://static.afterpay.com/logo/compact-badge-clearpay-black-on-mint.svg'; $templateConfigs['LOGO_OPC'] = Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/views/img/logo_opc.png'); $templateConfigs['PAYMENT_URL'] = $link->getModuleLink('clearpay', 'payment'); $mobileViewLayout = Tools::strtolower('four-by-one'); $isMobileLayout = $this->context->isMobile(); - if ($this->context->isMobile()){ + if ($isMobileLayout) { $mobileViewLayout = Tools::strtolower('two-by-two'); } $templateConfigs['AP_MOBILE_LAYOUT'] = $mobileViewLayout; @@ -900,32 +938,22 @@ public function templateDisplay($templateName = '') $isEnabled = Configuration::get('CLEARPAY_IS_ENABLED'); $allowedCountries = json_decode(Configuration::get('CLEARPAY_ALLOWED_COUNTRIES')); - $language = $this->getCurrentLanguage(); + $language = $this->getCurrentLanguageCode(); + $restrictedByLangOrCurrency = $this->isRestrictedByLangOrCurrency(); if ($isEnabled && $simulatorIsEnabled && $amount > 0 && ($amount >= Configuration::get('CLEARPAY_MIN_AMOUNT') || $templateName === 'product.tpl') && ($amount <= Configuration::get('CLEARPAY_MAX_AMOUNT') || $templateName === 'product.tpl') && - in_array(Tools::strtoupper($language), $allowedCountries) && - !$categoryRestriction + !$categoryRestriction && + !$restrictedByLangOrCurrency ) { $templateConfigs['PS_VERSION'] = str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)); $templateConfigs['SDK_URL'] = self::CLEARPAY_JS_CDN_URL; $templateConfigs['CLEARPAY_MIN_AMOUNT'] = Configuration::get('CLEARPAY_MIN_AMOUNT'); $templateConfigs['CLEARPAY_MAX_AMOUNT'] = Configuration::get('CLEARPAY_MAX_AMOUNT'); $templateConfigs['CURRENCY'] = $this->currency; - $language = Language::getLanguage($this->context->language->id); - if (isset($language['locale'])) { - $language = $language['locale']; - } else { - $language = $language['language_code']; - } - $templateConfigs['ISO_COUNTRY_CODE'] = str_replace('-', '_', $language); - // Preserve Uppercase in locale - if (Tools::strlen($templateConfigs['ISO_COUNTRY_CODE']) == 5) { - $templateConfigs['ISO_COUNTRY_CODE'] = Tools::substr($templateConfigs['ISO_COUNTRY_CODE'], 0, 2) . - Tools::strtoupper(Tools::substr($templateConfigs['ISO_COUNTRY_CODE'], 2, 4)); - } + $templateConfigs['ISO_COUNTRY_CODE'] = $this->getIsoCountryCode(); $templateConfigs['AMOUNT_WITH_CURRENCY'] = $templateConfigs['AMOUNT'] . $this->currencySymbol; if ($this->currency === 'GBP') { $templateConfigs['AMOUNT_WITH_CURRENCY'] = $this->currencySymbol. $templateConfigs['AMOUNT']; @@ -937,7 +965,7 @@ public function templateDisplay($templateName = '') 'views/templates/hook/' . $templateName ); } else { - if ($isEnabled && $templateName === 'product.tpl' && Configuration::get('AFTERPAY_LOGS') === 'on') { + if ($isEnabled && $templateName === 'product.tpl' && Configuration::get('CLEARPAY_LOGS') === 'on') { $logMessage = ''; if (!$simulatorIsEnabled) { $logMessage .= "Clearpay: Simulator is disabled by 'self::SIMULATOR_IS_ENABLED'. "; @@ -1090,15 +1118,14 @@ public function hookDisplayPaymentTop($params) */ public function hookActionOrderStatusUpdate($params) { - if (empty($order) || empty($order->payment) || $order->payment != self::PRODUCT_PAYMENT_NAME) { - return false; - } - $newOrderStatus = null; $order = null; if (!empty($params) && !empty($params['id_order'])) { $order = new Order((int)$params['id_order']); } + if (empty($order) || empty($order->payment) || $order->payment != self::PRODUCT_PAYMENT_NAME) { + return false; + } if (!empty($params) && !empty($params['newOrderStatus'])) { $newOrderStatus = $params['newOrderStatus']; @@ -1238,14 +1265,13 @@ public function checkLogoExists() } /** - * Get user language + * Get user language Code */ - private function getCurrentLanguage() + private function getCurrentLanguageCode() { - $language = 'EN'; $allowedCountries = json_decode(Configuration::get('CLEARPAY_ALLOWED_COUNTRIES')); if (is_null($allowedCountries)) { - return $language; + return 'NonAccepted'; } $lang = Language::getLanguage($this->context->language->id); $langArray = explode("-", $lang['language_code']); @@ -1254,24 +1280,38 @@ private function getCurrentLanguage() } $language = Tools::strtoupper($langArray[count($langArray)-1]); - // Prevent null language detection + if ($this->currency != 'EUR' && in_array(Tools::strtoupper($langArray[0]), $allowedCountries)) { + return Tools::strtoupper($langArray[0]); + } if (in_array(Tools::strtoupper($language), $allowedCountries)) { return $language; } - if ($this->shippingAddress) { - $language = Country::getIsoById($this->shippingAddress->id_country); - if (in_array(Tools::strtoupper($language), $allowedCountries)) { - return $language; - } + + return 'NonAccepted('.$lang['language_code'].')'; + } + + /** + * Get user language Id + */ + private function getCurrentLanguageId() + { + $allowedCountries = json_decode(Configuration::get('CLEARPAY_ALLOWED_COUNTRIES')); + if (is_null($allowedCountries)) { + return '-1'; } - if ($this->billingAddress) { - $language = Country::getIsoById($this->billingAddress->id_country); - if (in_array(Tools::strtoupper($language), $allowedCountries)) { - return $language; - } + $lang = Language::getLanguage($this->context->language->id); + $langArray = explode("-", $lang['language_code']); + if (count($langArray) != 2 && isset($lang['locale'])) { + $langArray = explode("-", $lang['locale']); } - return $language; + $language = Tools::strtoupper($langArray[count($langArray)-1]); + + if (in_array(Tools::strtoupper($language), $allowedCountries)) { + return $this->context->language->id; + } + + return '-1'; } /** @@ -1298,6 +1338,43 @@ public function getApiVersionPerRegion($region = '') return json_encode(array($region)); } + /** + * @return mixed|string|string[] + */ + public function getIsoCountryCode() + { + if ($this->currency != 'EUR') { + if (!isset($this->defaultLanguagePerCurrency[$this->currency])) { + return 'NonAccepted'; + } + $language = $this->defaultLanguagePerCurrency[$this->currency]; + return $this->defaultIsoCountryCodePerCountry[$language]; + } + + $languageId = $this->getCurrentLanguageId(); + if ($languageId == -1) { + return 'NonAccepted'; + } + + $language = Language::getLanguage($languageId); + + if (isset($language['locale'])) { + $language = $language['locale']; + } else { + $language = $language['language_code']; + } + if (Tools::strlen($language) == 5) { + $part1 = Tools::substr($language, 0, 2); + $part2 = Tools::strtoupper(Tools::substr($language, 2, 4)); + // Spanish dialects: + if ($part1 == 'ca' || $part1 == 'gl' || $part1 == 'eu') { + $part1 = 'es'; + } + $language = $part1 . $part2; + } + return str_replace('-', '_', $language); + } + /** * @param null $amount * @return string @@ -1339,4 +1416,17 @@ private function isCartRestricted($cart) } return false; } + + /** + * @return bool + */ + private function isRestrictedByLangOrCurrency() + { + $language = $this->getCurrentLanguageCode(); + $allowedCountries = json_decode(Configuration::get('CLEARPAY_ALLOWED_COUNTRIES')); + $return = (in_array(Tools::strtoupper($language), $allowedCountries) && + $this->allowedCurrencyPerRegion[Configuration::get('CLEARPAY_REGION')] == $this->currency + ); + return !$return; + } } diff --git a/config.xml b/config.xml index 907dd8b..fe3866c 100644 --- a/config.xml +++ b/config.xml @@ -1,12 +1,12 @@ - clearpay - - - - - - 1 - 1 + clearpay + + + + + + 1 + 1 \ No newline at end of file diff --git a/controllers/front/payment.php b/controllers/front/payment.php index 0075a59..e9df6c9 100644 --- a/controllers/front/payment.php +++ b/controllers/front/payment.php @@ -34,6 +34,15 @@ class ClearpayPaymentModuleFrontController extends AbstractController 'US' => 'v2', ); + /** + * Default currency per region + * + * @var array + */ + public $defaultLanguagePerCurrency = array( + 'GBP' => 'GB', + ); + /** * @param $region * @return string @@ -224,7 +233,7 @@ public function postProcess() } $header = $this->module->name . '/' . $this->module->version - . ' (Prestashop/' . _PS_VERSION_ . '; PHP/' . phpversion() . '; Merchant/' . $paymentObjData['publicKey'] + . ' (Prestashop/' . _PS_VERSION_ . '; PHP/' . phpversion() .'; Merchant/' . $paymentObjData['publicKey'] . ') ' . _PS_BASE_URL_SSL_.__PS_BASE_URI__; $clearpayPaymentObj->addHeader('User-Agent', $header); $clearpayPaymentObj->addHeader('Country', $paymentObjData['countryCode']); @@ -317,6 +326,11 @@ private function addPaymentV2Options(CreateCheckout $clearpayPaymentObj, $paymen */ private function getCountryCode($paymentObjData) { + // for uk currency + if (isset($this->defaultLanguagePerCurrency[$paymentObjData['currency']])) { + return $this->defaultLanguagePerCurrency[$paymentObjData['currency']]; + } + $allowedCountries = json_decode(Configuration::get('CLEARPAY_ALLOWED_COUNTRIES')); $lang = Language::getLanguage($this->context->language->id); $langArray = explode("-", $lang['language_code']); diff --git a/translations/es.php b/translations/es.php index 00119db..8f25f71 100644 --- a/translations/es.php +++ b/translations/es.php @@ -9,7 +9,7 @@ $_MODULE['<{clearpay}prestashop>clearpay_bf556a495a6d99f008fcf01ae7178134'] = ' cuotas sin costes extras.'; $_MODULE['<{clearpay}prestashop>clearpay_92d4773a9f32fc3333cb8238471cf763'] = 'Falta la extension de php: curl'; $_MODULE['<{clearpay}prestashop>clearpay_5984afe5af3bc7d4a01ba1d722c00a38'] = 'La versión mínima soportada de PHP es 5.3.0'; -$_MODULE['<{clearpay}prestashop>clearpay_8a04b447fe7942b43631e4bfe52b9026'] = 'O 4 pagos de'; +$_MODULE['<{clearpay}prestashop>clearpay_8a04b447fe7942b43631e4bfe52b9026'] = 'O 4 pagos sin intereses de'; $_MODULE['<{clearpay}prestashop>clearpay_23a58bf9274bedb19375e527a0744fa9'] = 'con'; $_MODULE['<{clearpay}prestashop>clearpay_d1acd9ba7f1857100ab8a99b2632fb78'] = '4 pagos sin coste de'; $_MODULE['<{clearpay}prestashop>clearpay_dda982da36845de9d4d9e839d796b02b'] = 'Serás redirigido a la plataforma de Clearpay para completar tu información de pago. '; diff --git a/views/templates/hook/checkout.tpl b/views/templates/hook/checkout.tpl index c32e928..ff44a77 100755 --- a/views/templates/hook/checkout.tpl +++ b/views/templates/hook/checkout.tpl @@ -13,7 +13,7 @@ } p.payment_module a.clearpay-checkout { background: url('{$ICON|escape:'htmlall':'UTF-8'}') 5px 5px no-repeat #fbfbfb; - background-size: 79px; + background-size: 80px; } p.payment_module a.clearpay-checkout.ps_version_1-7 { background: none; @@ -25,7 +25,7 @@ } p.payment_module a.clearpay-checkout.ps_version_1-6 { background-color: #fbfbfb; - max-height: 90px; + font-size: 1.3em; } p.payment_module a.clearpay-checkout.ps_version_1-6:after { display: block; @@ -217,16 +217,16 @@ {if $PS_VERSION !== '1-7'}
-
+
+ {/if} {if $PS_VERSION === '1-7'}
diff --git a/views/templates/hook/onepagecheckout.tpl b/views/templates/hook/onepagecheckout.tpl index 62878a1..de69ce3 100755 --- a/views/templates/hook/onepagecheckout.tpl +++ b/views/templates/hook/onepagecheckout.tpl @@ -13,14 +13,14 @@ } p.payment_module a.clearpay-checkout { background: url('{$ICON|escape:'htmlall':'UTF-8'}') 5px 5px no-repeat #fbfbfb; - background-size: 79px; + background-size: 80px; } p.payment_module a.clearpay-checkout.ps_version_1-7 { background: none; } p.payment_module a.clearpay-checkout.ps_version_1-6 { background-color: #fbfbfb; - max-height: 90px; + font-size: 1.3em; } p.payment_module a.clearpay-checkout.ps_version_1-6:after { display: block; @@ -210,12 +210,11 @@ {if $PS_VERSION !== '1-7'}