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 @@