diff --git a/Block/Adminhtml/System/Config/Form/Field/cbtLabel.php b/Block/Adminhtml/System/Config/Form/Field/cbtLabel.php new file mode 100644 index 0000000..b87bf7c --- /dev/null +++ b/Block/Adminhtml/System/Config/Form/Field/cbtLabel.php @@ -0,0 +1,34 @@ +helper = $helper; + parent::__construct($context); + } + + + protected function _getElementHtml(AbstractElement $element) + { + return $this->helper->getCbtCountry(); + } +} \ No newline at end of file diff --git a/Block/Catalog/Installments.php b/Block/Catalog/Installments.php index ef70805..ed23fd0 100644 --- a/Block/Catalog/Installments.php +++ b/Block/Catalog/Installments.php @@ -28,8 +28,8 @@ class Installments extends JsConfig /** * @var ClearpayPayovertime */ - private $clearpayPayovertime; - + private $clearpayPayovertime; + /** * @var Resolver */ @@ -92,9 +92,9 @@ public function canShow(): bool public function getTypeOfProduct() { $product = $this->registry->registry('product'); - return $product->getTypeId(); + return $product->getTypeId(); } - + /** * @return string */ @@ -102,7 +102,7 @@ public function getFinalAmount() { // get product $product = $this->registry->registry('product'); - + // set if final price is exist $price = $product->getPriceInfo()->getPrice('final_price')->getValue(); @@ -116,13 +116,12 @@ public function canUseCurrency() { $canUse=false; //Check for Supported currency + if($this->clearpayConfig->getCurrencyCode()) { $canUse= $this->clearpayPayovertime->canUseForCurrency($this->clearpayConfig->getCurrencyCode()); } return $canUse; - } - } diff --git a/Block/Config.php b/Block/Config.php index 97faf58..e1254f0 100644 --- a/Block/Config.php +++ b/Block/Config.php @@ -4,6 +4,7 @@ use Magento\Framework\View\Element\Template; use Clearpay\Clearpay\Model\Config\Payovertime; +use Clearpay\Clearpay\Model\Payovertime as ClearpayPayovertime; use Magento\Framework\Json\Helper\Data; class Config extends Template @@ -29,11 +30,13 @@ public function __construct( Payovertime $payovertime, Data $dataHelper, Template\Context $context, + ClearpayPayovertime $clearpayPayovertime, array $data ) { $this->_payOverTime = $payovertime; $this->_dataHelper = $dataHelper; + $this->clearpayPayovertime = $clearpayPayovertime; parent::__construct($context, $data); } @@ -59,12 +62,7 @@ public function getClearpayJsUrl() */ public function checkCurrency() { - $supportedCurrency=['GBP']; - if(in_array($this->_payOverTime->getCurrencyCode(),$supportedCurrency)){ - return true; - } - else{ - return false; - } + return $this->clearpayPayovertime->canUseForCurrency($this->_payOverTime->getCurrencyCode()) && $this->_payOverTime->isActive(); + } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 23447dc..38dbb90 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,25 @@ # Clearpay Magento 2 Extension Changelog +## Version 3.4.1 + +_Wed 05 May 2021_ + +### Supported Editions & Versions + +Tested and verified in clean installations of Magento 2: + +- Magento Enterprise Edition (EE) version 2.4.2 + +### Highlights + +- Introduced multi-currency and Cross Border Trade (CBT) support for UK merchants only. +- Improved handling of exceptions at Checkout. +- Improved JS configurations. +- Improved support for native Terms and Conditions. +- Improved support for local currency formatting on product detail pages. + +--- + ## Version 3.4.0 _Wed 24 Mar 2021_ diff --git a/Controller/Payment/Response.php b/Controller/Payment/Response.php index 3ff60b1..7a2c23d 100644 --- a/Controller/Payment/Response.php +++ b/Controller/Payment/Response.php @@ -40,6 +40,7 @@ class Response extends \Magento\Framework\App\Action\Action protected $_quoteValidator; protected $_timezone; protected $_clearpayApiPayment; + protected $_expressPayment; /** * Response constructor. @@ -60,6 +61,10 @@ class Response extends \Magento\Framework\App\Action\Action * @param \Magento\Sales\Model\Order\Payment\Transaction\Repository $transactionRepository * @param \Magento\Framework\Notification\NotifierInterface $notifierPool * @param \Clearpay\Clearpay\Model\Adapter\V2\ClearpayOrderPaymentCapture $paymentCapture + * @param \Magento\Quote\Model\QuoteValidator $quoteValidator + * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone + * @param \Clearpay\Clearpay\Model\Adapter\ClearpayPayment $clearpayApiPayment + * @param \Clearpay\Clearpay\Model\Adapter\ClearpayExpressPayment $expressPayment */ public function __construct( \Magento\Framework\App\Action\Context $context, @@ -82,7 +87,8 @@ public function __construct( \Clearpay\Clearpay\Model\Adapter\V2\ClearpayOrderPaymentCapture $paymentCapture, \Magento\Quote\Model\QuoteValidator $quoteValidator, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone, - \Clearpay\Clearpay\Model\Adapter\ClearpayPayment $clearpayApiPayment + \Clearpay\Clearpay\Model\Adapter\ClearpayPayment $clearpayApiPayment, + \Clearpay\Clearpay\Model\Adapter\ClearpayExpressPayment $expressPayment ) { $this->_resultForwardFactory = $resultForwardFactory; $this->response = $response; @@ -104,6 +110,7 @@ public function __construct( $this->_quoteValidator = $quoteValidator; $this->_timezone = $timezone; $this->_clearpayApiPayment = $clearpayApiPayment; + $this->_expressPayment = $expressPayment; parent::__construct($context); } @@ -164,12 +171,12 @@ private function _processAuthCapture($query) if (!$response_check) { // Check the order token being use throw new \Magento\Framework\Exception\LocalizedException(__('There are issues when processing your payment. Invalid Token')); - } elseif ($merchant_order_id != $response_check['merchantReference']) { - // Check order id - throw new \Magento\Framework\Exception\LocalizedException(__('There are issues when processing your payment. Invalid Merchant Reference')); } elseif (round($quote->getBaseGrandTotal(), 2) != round($response_check['amount']['amount'], 2)) { // Check the order amount throw new \Magento\Framework\Exception\LocalizedException(__('There are issues when processing your payment. Invalid Amount')); + } elseif ($this->_expressPayment->isCartUpdated($quote, $response_check['items'])) { + // Check cart Items + throw new \Magento\Framework\Exception\LocalizedException(__('There are issues when processing your payment. Invalid Cart Items')); } if(!$this->_helper->getConfig('payment/clearpaypayovertime/payment_flow') || $this->_helper->getConfig('payment/clearpaypayovertime/payment_flow')=="immediate" || $quote->getIsVirtual()){ diff --git a/Helper/Data.php b/Helper/Data.php index 4d5f679..902f9a2 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -12,17 +12,21 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper protected $_logger; protected $_clearpayConfig; protected $_moduleList; + protected $_countryFactory; public function __construct( \Magento\Framework\App\Helper\Context $context, \Clearpay\Clearpay\Model\Logger\Logger $logger, \Clearpay\Clearpay\Model\Config\Payovertime $clearpayConfig, - \Magento\Framework\Module\ModuleListInterface $moduleList + \Magento\Framework\Module\ModuleListInterface $moduleList, + \Magento\Directory\Model\CountryFactory $countryFactory + ) { parent::__construct($context); $this->_logger = $logger; $this->_clearpayConfig = $clearpayConfig; $this->_moduleList = $moduleList; + $this->_countryFactory = $countryFactory; } public function debug($message, array $context = []) @@ -38,6 +42,26 @@ public function getModuleVersion() return $moduleInfo['setup_version']; } + public function getCbtCountry() + { + $cbtEnabled="Disabled"; + if($this->_clearpayConfig->isCbtEnabled()){ + $cbtEnabled = "Enabled"; + $cbtCountries = $this->_clearpayConfig->getCbtCountry(); + if(!empty($cbtCountries)){ + $cbtCountryCode=explode(",",$cbtCountries); + $counrtyNames=[]; + foreach($cbtCountryCode AS $countryCode){ + if($country = $this->_countryFactory->create()->loadByCode($countryCode)){ + $counrtyNames[] = $country->getName(); + } + } + $cbtEnabled = $cbtEnabled." [ ".implode(" | ",$counrtyNames)." ]"; + } + } + return $cbtEnabled; + } + public function getConfig($config_path) { return $this->scopeConfig->getValue( diff --git a/Model/Adapter/ClearpayExpressPayment.php b/Model/Adapter/ClearpayExpressPayment.php index 5ffd63d..769022d 100644 --- a/Model/Adapter/ClearpayExpressPayment.php +++ b/Model/Adapter/ClearpayExpressPayment.php @@ -193,7 +193,7 @@ public function formatAmount($amount) public function isCartUpdated($quoteData, $responseItems) { $isCartupdated = false; - $quoteItems = $quoteData->getAllItems(); + $quoteItems = $quoteData->getAllVisibleItems(); if ($quoteData->getItemsCount()!= count($responseItems)) { $isCartupdated = true; $this->_helper->debug('Cart Items count does not match. Quote Count : '.$quoteData->getItemsCount().' & Response count : '.count($responseItems)); diff --git a/Model/Config/Payovertime.php b/Model/Config/Payovertime.php index 7ee896e..520ca5b 100644 --- a/Model/Config/Payovertime.php +++ b/Model/Config/Payovertime.php @@ -425,6 +425,24 @@ public function getExpressCheckoutKey() { return $this->_getConfigData(self::EXPRESS_CHECKOUT_KEY); } + /** + * Get cbt enabled/disabled + * + * @return int + */ + public function isCbtEnabled() + { + return $this->_getConfigData(self::ENABLE_CBT); + } + + /* Get cbt countries + * + * @return string + */ + public function getCbtCountry() + { + return $this->_getConfigData(self::CBT_COUNTRY); + } } diff --git a/Model/Config/Save/Plugin.php b/Model/Config/Save/Plugin.php index 570915e..000494b 100644 --- a/Model/Config/Save/Plugin.php +++ b/Model/Config/Save/Plugin.php @@ -119,14 +119,10 @@ public function aroundSave( } $countryName=""; - if($enable_cbt=="1"){ - $countryName = $this->_scopeConfig->getValue('general/country/default', $scope,$scopeId); - if(isset($response['CBT']['countries']) && !empty($response['CBT']['countries'])){ - if(is_array($response['CBT']['countries'])){ - $countryName .=",".implode(",",$response['CBT']['countries']); - } - } + if($enable_cbt=="1" && !empty($response['CBT']['countries']) && is_array($response['CBT']['countries'])){ + $countryName =implode(",",$response['CBT']['countries']); } + diff --git a/Model/ConfigProvider.php b/Model/ConfigProvider.php index 8a08e1a..a48ab4f 100644 --- a/Model/ConfigProvider.php +++ b/Model/ConfigProvider.php @@ -20,14 +20,19 @@ class ConfigProvider implements ConfigProviderInterface * @var Config\Payovertime */ protected $clearpayConfig; + /** + * @var Payovertime + */ + protected $clearpayPayovertime; /** * ConfigProvider constructor. * @param Config\Payovertime $config */ - public function __construct(\Clearpay\Clearpay\Model\Config\Payovertime $config) + public function __construct(\Clearpay\Clearpay\Model\Config\Payovertime $config,\Clearpay\Clearpay\Model\Payovertime $clearpayPayovertime) { $this->clearpayConfig = $config; + $this->clearpayPayovertime = $clearpayPayovertime; } /** @@ -51,6 +56,7 @@ public function getConfig() 'paymentAction' => $this->clearpayConfig->getPaymentAction(), 'termsConditionUrl' => self::TERMS_CONDITION_LINK, 'currencyCode' => $this->clearpayConfig->getCurrencyCode(), + 'baseCurrencyCode' => $this->clearpayPayovertime->getStoreCurrencyCode(), ], ], ]); diff --git a/Model/Payovertime.php b/Model/Payovertime.php index 16f5b9c..3af5ac0 100644 --- a/Model/Payovertime.php +++ b/Model/Payovertime.php @@ -54,7 +54,7 @@ class Payovertime extends \Magento\Payment\Model\Method\AbstractMethod /** * For dependency injection */ - protected $supportedContryCurrencyCodes = array('GB'=>'GBP'); + protected $supportedContryCurrencyCodes = array('GB'=>'GBP','AU'=>'AUD','NZ'=>'NZD','CA'=>'CAD'); protected $clearpayPaymentTypeCode = self::CLEARPAY_PAYMENT_TYPE_CODE; protected $logger; @@ -321,14 +321,14 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) public function canUseForCurrency($currencyCode) { $canUseForCurrency= false; - + $storeCurrencyCode=$this->getStoreCurrencyCode(); if (in_array($currencyCode, $this->supportedContryCurrencyCodes) ) { - $canUseForCurrency=parent::canUseForCurrency($currencyCode); - //Currency Check for Cross Border trade - if(!empty($this->getConfigData('enable_cbt'))){ + if ($currencyCode==$storeCurrencyCode ) { + $canUseForCurrency=parent::canUseForCurrency($currencyCode); + }else if(!empty($this->getConfigData('enable_cbt')) && !empty($this->getConfigData('cbt_country'))){ + //Currency Check for Cross Border trade $specifiedCountires=explode(",",$this->getConfigData('cbt_country')); - $canUseForCurrency=false; foreach($specifiedCountires AS $country){ if(isset($this->supportedContryCurrencyCodes[$country]) && ($currencyCode==$this->supportedContryCurrencyCodes[$country])){ $canUseForCurrency=parent::canUseForCurrency($currencyCode); @@ -408,4 +408,16 @@ public function fetchTransactionInfo(InfoInterface $payment, $transactionId) // return to the parent return parent::fetchTransactionInfo($payment, $transactionId); } + /** + * Calculated the merchant's store currency code + * + * @return $text + */ + public function getStoreCurrencyCode() + { + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); + $store = $storeManager->getStore(); + return $store->getBaseCurrencyCode(); + } } diff --git a/assets.ini b/assets.ini deleted file mode 100644 index 37a2c6c..0000000 --- a/assets.ini +++ /dev/null @@ -1,13 +0,0 @@ -; These are assets values in the Clearpay - Magento2 plugin - -[GBP] - name = "UK" - product_page1 = "or 4 interest-free payments of " - product_page2 = ' - Learn more' - product_page_from = "or 4 interest-free payments from " - cart_page2 = "
Use your payment card, instant approval. - Learn more" - cart_page1 = "Check out with Clearpay and pay by instalments. - Simply pay 4 fortnightly instalments of " - diff --git a/composer.json b/composer.json index 19e1fe5..504a332 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "license" : "OSL-3.0", "type" : "magento2-module", "description" : "Magento 2 Clearpay Payment Module", - "version" : "3.4.0", + "version" : "3.4.1", "authors" : [{ "name" : "Clearpay", "homepage" : "https://www.clearpay.co.uk" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 06f4270..f76b589 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -94,6 +94,12 @@ + + + + Clearpay\Clearpay\Block\Adminhtml\System\Config\Form\Field\cbtLabel + + diff --git a/etc/module.xml b/etc/module.xml index 1a9762f..7b63607 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -8,7 +8,7 @@ */ --> - + diff --git a/view/frontend/templates/clearpay/product.phtml b/view/frontend/templates/clearpay/product.phtml index 080c2de..9c31763 100644 --- a/view/frontend/templates/clearpay/product.phtml +++ b/view/frontend/templates/clearpay/product.phtml @@ -5,6 +5,7 @@ * @author Clearpay * @copyright 2016-2020 Clearpay https://www.clearpay.co.uk */ + /** @var \Clearpay\Clearpay\Block\Catalog\Installments $block */ $product_type = $block->getTypeOfProduct(); $data_amount = $block->getFinalAmount(); diff --git a/view/frontend/web/js/view/cart/clearpay-cart.js b/view/frontend/web/js/view/cart/clearpay-cart.js index 9f9d47f..4d08683 100644 --- a/view/frontend/web/js/view/cart/clearpay-cart.js +++ b/view/frontend/web/js/view/cart/clearpay-cart.js @@ -21,7 +21,8 @@ require( //CountryCode Object to pass in initialize function. var countryCurrencyMapping ={GBP:"GB"}; - var countryCode = (clearpayData.currencyCode in countryCurrencyMapping)? countryCurrencyMapping[clearpayData.currencyCode]:''; + var countryCode = (clearpayData.baseCurrencyCode in countryCurrencyMapping)? countryCurrencyMapping[clearpayData.baseCurrencyCode]:''; + var isShippingRequired= (!quote.isVirtual())?true:false; if( $("#clearpay-express-button").length && countryCode!=""){ AfterPay.initializeForPopup({ @@ -57,15 +58,17 @@ require( }, onComplete: function (orderData) { - $("body").trigger('processStart'); - if (orderData.data.status == 'SUCCESS') { + + if (orderData.data.status == 'SUCCESS') { $.ajax({ url: mageUrl.build("clearpay/payment/express")+'?action=confirm', method: 'POST', data: orderData.data, + beforeSend: function(){ + $("body").trigger('processStart'); + }, success: function(result){ - $("body").trigger('processStop'); if (result.success) { //To Clear mini-cart var sections = ['cart']; @@ -74,18 +77,22 @@ require( window.location.href = mageUrl.build("checkout/onepage/success"); } - } + }, + complete: function(){ + $("body").trigger('processStop'); + } }); } - $("body").trigger('processStop'); + }, pickup: false, }); } } - + + $(document).ready(function() { initClearpayExpress(); }); diff --git a/view/frontend/web/js/view/product/clearpay-products.js b/view/frontend/web/js/view/product/clearpay-products.js index 8457ec5..09ef4d4 100644 --- a/view/frontend/web/js/view/product/clearpay-products.js +++ b/view/frontend/web/js/view/product/clearpay-products.js @@ -27,19 +27,7 @@ require( let price_raw = $(".page-main [data-price-type=finalPrice]:first").text() || ''; if (!price_raw) price_raw = $('.page-main .product-info-price .price-final_price .price-wrapper:not([data-price-type="oldPrice"]) span.price:first').text(); - - var price = price_raw.match(/[\d\.]+/g); - - var product_variant_price=parseFloat($('.page-main .product-info-price span.price-final_price > span[data-price-type="finalPrice"]').attr('data-price-amount')); - if(price != null){ - if (price[1]) { - product_variant_price = price[0]+price[1]; - } else { - product_variant_price = price[0]; - } - } - - $('afterpay-placement').attr('data-amount',product_variant_price); + $('afterpay-placement').attr('data-amount',price_raw); } diff --git a/view/frontend/web/template/payment/clearpaypayovertime.html b/view/frontend/web/template/payment/clearpaypayovertime.html index 21bf6b0..c5bf692 100644 --- a/view/frontend/web/template/payment/clearpaypayovertime.html +++ b/view/frontend/web/template/payment/clearpaypayovertime.html @@ -27,6 +27,11 @@ +
+ + + +