From 466ae2ef32350bc185fad14b76077a1bcbca3d28 Mon Sep 17 00:00:00 2001 From: Hugo Posnic Date: Fri, 22 Sep 2023 10:40:09 +0200 Subject: [PATCH] 3.3.0 --- Controller/Standard/PaymentCreated.php | 14 ++++---------- Controller/Standard/Response.php | 3 --- Controller/WebhookAbstract.php | 8 ++++++++ Gateway/Config/Config.php | 14 +++++++++++++- Gateway/Connect.php | 12 +++++++++++- Gateway/RequestToPay.php | 12 +++++++++++- Helper/Fintecture.php | 16 +++++++++++++++- Helper/Stats.php | 2 ++ Model/Ui/ConfigProvider.php | 2 ++ composer.json | 2 +- etc/adminhtml/system.xml | 12 ++++++++++++ etc/config.xml | 3 +++ etc/module.xml | 2 +- i18n/en_US.csv | 4 +++- i18n/fr_FR.csv | 4 +++- view/adminhtml/web/js/config.js | 15 ++++++++++----- view/frontend/web/css/style.css | 2 +- .../method-renderer/fintecture-bnpl.js | 19 +++++++++++++++++++ .../method-renderer/fintecture-payment.js | 19 ++++++++++++++++++- .../web/template/payment/fintecture-bnpl.html | 10 ++++++++-- .../web/template/payment/fintecture.html | 10 ++++++++-- 21 files changed, 153 insertions(+), 32 deletions(-) diff --git a/Controller/Standard/PaymentCreated.php b/Controller/Standard/PaymentCreated.php index 032ae88..350a98f 100644 --- a/Controller/Standard/PaymentCreated.php +++ b/Controller/Standard/PaymentCreated.php @@ -38,18 +38,11 @@ public function execute() 'refundedSessionId' => $this->request->getParam('refunded_session_id', ''), ]; - $decodedState = json_decode(base64_decode($params['state'])); - if (!is_object($decodedState) || !property_exists($decodedState, 'order_id')) { - $this->fintectureLogger->error('Webhook', [ - 'message' => "Can't find an order id in the state", - ]); - $result->setHttpResponseCode(400); - $result->setContents('invalid_order'); - - return $result; + if (!in_array($params['type'], self::ALLOWED_WEBHOOK_TYPES)) { + $result->setContents('invalid_webhook_type'); } - $order = $this->fintectureHelper->getOrderByIncrementId($decodedState->order_id); + $order = $this->fintectureHelper->getOrderBySessionId($params['sessionId']); if (!$order) { $this->fintectureLogger->error('Webhook', [ 'message' => 'No order found', @@ -66,6 +59,7 @@ public function execute() try { $isRefund = !empty($params['refundedSessionId']); if ($isRefund) { + $decodedState = json_decode(base64_decode($params['state'])); if (property_exists($decodedState, 'creditmemo_transaction_id')) { return $this->refund($order, $params['status'], $decodedState->creditmemo_transaction_id); } else { diff --git a/Controller/Standard/Response.php b/Controller/Standard/Response.php index 93031ad..7fec9bc 100644 --- a/Controller/Standard/Response.php +++ b/Controller/Standard/Response.php @@ -47,9 +47,6 @@ public function execute() return $this->redirectToCart(); } - $order->setExtOrderId($sessionId); - $this->orderRepository->save($order); - $pisToken = $this->sdk->pisClient->token->generate(); if (!$pisToken->error) { $this->sdk->pisClient->setAccessToken($pisToken); // set token of PIS client diff --git a/Controller/WebhookAbstract.php b/Controller/WebhookAbstract.php index f94d3d7..61db39b 100644 --- a/Controller/WebhookAbstract.php +++ b/Controller/WebhookAbstract.php @@ -46,6 +46,14 @@ abstract class WebhookAbstract implements CsrfAwareActionInterface /** @var Config */ protected $config; + /** @var array */ + protected const ALLOWED_WEBHOOK_TYPES = [ + 'PayByBank', + 'Refund', + 'BuyNowPayLater', + 'ManualTransfer', + ]; + public function __construct( FintectureLogger $fintectureLogger, FintectureHelper $fintectureHelper, diff --git a/Gateway/Config/Config.php b/Gateway/Config/Config.php index 95262f3..9f6f5f5 100644 --- a/Gateway/Config/Config.php +++ b/Gateway/Config/Config.php @@ -7,7 +7,7 @@ class Config extends BaseConfig { const CODE = 'fintecture'; - const VERSION = '3.2.0'; + const VERSION = '3.3.0'; const KEY_SHOP_NAME = 'general/store_information/name'; const KEY_ACTIVE = 'active'; @@ -31,6 +31,8 @@ class Config extends BaseConfig const KEY_CUSTOM_RECONCILIATION_FIELD_ACTIVE = 'custom_reconciliation_field_active'; const KEY_CUSTOM_RECONCILIATION_FIELD = 'custom_reconciliation_field'; const KEY_RECOMMEND_IT_BADGE = 'recommend_it_badge'; + const KEY_FIRST_POSITION_ACTIVE = 'first_position_active'; + const KEY_FIRST_POSITION_AMOUNT = 'first_position_amount'; public function getShopName(): ?string { @@ -159,6 +161,16 @@ public function isRecommendedItBadgeActive(): bool return (bool) $this->getValue(self::KEY_RECOMMEND_IT_BADGE); } + public function isFirstPositionActive(): bool + { + return (bool) $this->getValue(self::KEY_FIRST_POSITION_ACTIVE); + } + + public function getFirstPositionAmount(): float + { + return (float) $this->getValue(self::KEY_FIRST_POSITION_AMOUNT); + } + public function getNewOrderStatus(): string { $status = $this->getValue('payment/fintecture/new_order_status'); diff --git a/Gateway/Connect.php b/Gateway/Connect.php index 790caac..f5c61ff 100644 --- a/Gateway/Connect.php +++ b/Gateway/Connect.php @@ -8,6 +8,7 @@ use Fintecture\Payment\Helper\Fintecture as FintectureHelper; use Fintecture\Payment\Logger\Logger; use Fintecture\Util\Crypto; +use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Order; class Connect @@ -24,16 +25,21 @@ class Connect /** @var Config */ protected $config; + /** @var OrderRepositoryInterface */ + protected $orderRepository; + public function __construct( FintectureHelper $fintectureHelper, Logger $fintectureLogger, Sdk $sdk, - Config $config + Config $config, + OrderRepositoryInterface $orderRepository ) { $this->fintectureHelper = $fintectureHelper; $this->fintectureLogger = $fintectureLogger; $this->sdk = $sdk; $this->config = $config; + $this->orderRepository = $orderRepository; } public function get(Order $order, array $data): ApiResponse @@ -74,6 +80,10 @@ public function get(Order $order, array $data): ApiResponse throw new \Exception($apiResponse->errorMsg); } + $sessionId = $apiResponse->meta->session_id ?? ''; + $order->setExtOrderId($sessionId); + $this->orderRepository->save($order); + return $apiResponse; } } diff --git a/Gateway/RequestToPay.php b/Gateway/RequestToPay.php index 071f750..d5b44863 100644 --- a/Gateway/RequestToPay.php +++ b/Gateway/RequestToPay.php @@ -8,6 +8,7 @@ use Fintecture\Payment\Helper\Fintecture as FintectureHelper; use Fintecture\Payment\Logger\Logger; use Fintecture\Util\Crypto; +use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Order; class RequestToPay @@ -24,16 +25,21 @@ class RequestToPay /** @var Config */ protected $config; + /** @var OrderRepositoryInterface */ + protected $orderRepository; + public function __construct( FintectureHelper $fintectureHelper, Logger $fintectureLogger, Sdk $sdk, - Config $config + Config $config, + OrderRepositoryInterface $orderRepository ) { $this->fintectureHelper = $fintectureHelper; $this->fintectureLogger = $fintectureLogger; $this->sdk = $sdk; $this->config = $config; + $this->orderRepository = $orderRepository; } public function get(Order $order, array $data): ApiResponse @@ -58,6 +64,10 @@ public function get(Order $order, array $data): ApiResponse throw new \Exception($apiResponse->errorMsg); } + $sessionId = $apiResponse->meta->session_id ?? ''; + $order->setExtOrderId($sessionId); + $this->orderRepository->save($order); + return $apiResponse; } } diff --git a/Helper/Fintecture.php b/Helper/Fintecture.php index 650d8e1..c7949e9 100644 --- a/Helper/Fintecture.php +++ b/Helper/Fintecture.php @@ -98,6 +98,20 @@ public function getOrderByIncrementId(string $incrementId): ?Order return $order; } + public function getOrderBySessionId(string $sessionId): ?Order + { + if (!preg_match('/^[0-9a-f]{32}$/', $sessionId)) { + return null; + } + + $searchCriteria = $this->searchCriteriaBuilder->addFilter('ext_order_id', $sessionId)->create(); + $orderList = $this->orderRepository->getList($searchCriteria)->getItems(); + /** @var Order|null $order */ + $order = array_pop($orderList); + + return $order; + } + public function getSessionIdByOrderId(string $orderId): ?string { $searchCriteria = $this->searchCriteriaBuilder->addFilter('order_id', $orderId)->create(); @@ -279,7 +293,7 @@ public function generatePayload(Order $order, string $type, string $method = '') 'attributes' => [ 'amount' => (string) round((float) $order->getBaseGrandTotal(), 2), 'currency' => $order->getOrderCurrencyCode(), - 'communication' => self::PAYMENT_COMMUNICATION . $order->getId(), + 'communication' => self::PAYMENT_COMMUNICATION . $order->getIncrementId(), ], ], ]; diff --git a/Helper/Stats.php b/Helper/Stats.php index a2edd10..6d6e4b5 100644 --- a/Helper/Stats.php +++ b/Helper/Stats.php @@ -107,6 +107,8 @@ public function getConfigurationSummary(): array 'module_checkout_design' => $this->config->getCheckoutDesign(), 'module_recommended_it' => $this->config->isRecommendedItBadgeActive(), 'module_recommended_bnpl' => $this->bnplConfig->isRecommendedBnplBadgeActive(), + 'module_force_position' => $this->config->isFirstPositionActive(), + 'module_force_position_min_amount' => $this->config->getFirstPositionAmount(), ]; } } diff --git a/Model/Ui/ConfigProvider.php b/Model/Ui/ConfigProvider.php index dc97b8e..787b2a3 100644 --- a/Model/Ui/ConfigProvider.php +++ b/Model/Ui/ConfigProvider.php @@ -25,6 +25,8 @@ public function getConfig() 'active' => $this->gatewayConfig->isActive(), 'checkoutDesign' => $this->gatewayConfig->getCheckoutDesign(), 'recommendItBadge' => $this->gatewayConfig->isRecommendedItBadgeActive(), + 'firstPositionActive' => $this->gatewayConfig->isFirstPositionActive(), + 'firstPositionAmount' => $this->gatewayConfig->getFirstPositionAmount(), ], ], ]; diff --git a/composer.json b/composer.json index 0edb5a4..1aac78d 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "email": "contact@fintecture.com" }, "type": "magento2-module", - "version": "3.2.0", + "version": "3.3.0", "license": [ "GPL-3.0" ], diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index e0d227b..5221e20 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -152,6 +152,18 @@ Recommend the payment method on checkout page payment/fintecture/recommend_it_badge + + + + Magento\Config\Model\Config\Source\Yesno + payment/fintecture/first_position_active + + + + + payment/fintecture/first_position_amount + validate-number + diff --git a/etc/config.xml b/etc/config.xml index 5f6f6e9..1d8413f 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -38,6 +38,9 @@ qrcode it 0 + 0 + 0 + 0 0 diff --git a/etc/module.xml b/etc/module.xml index d6b0b60..a8c46fb 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/i18n/en_US.csv b/i18n/en_US.csv index 2ee1289..e14f6bc 100644 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -127,4 +127,6 @@ "Your purchase is confirmed!","Your purchase is confirmed!" """Recommended"" badge", """Recommended"" badge" "Recommend the payment method on checkout page","Recommend the payment method on checkout page" -"RECOMMENDED","RECOMMENDED" \ No newline at end of file +"RECOMMENDED","RECOMMENDED" +"First position depending of cart amount","First position depending of cart amount" +"Minimal cart amount to place Fintecture in first position (in €)","Minimal cart amount to place Fintecture in first position (in €)" diff --git a/i18n/fr_FR.csv b/i18n/fr_FR.csv index 9fe6a47..6797b9e 100644 --- a/i18n/fr_FR.csv +++ b/i18n/fr_FR.csv @@ -127,4 +127,6 @@ "Your purchase is confirmed!","Votre achat est confirmé !" """Recommended"" badge", "Badge ""recommandé""" "Recommend the payment method on checkout page","Recommander la méthode de paiement dans la page de paiement" -"RECOMMENDED","RECOMMANDÉ" \ No newline at end of file +"RECOMMENDED","RECOMMANDÉ" +"First position depending of cart amount","Première position en fonction du montant du panier" +"Minimal cart amount to place Fintecture in first position (in €)","Montant minimum du panier pour placer Fintecture en première position (en €)" diff --git a/view/adminhtml/web/js/config.js b/view/adminhtml/web/js/config.js index 6ce728b..610d8f5 100644 --- a/view/adminhtml/web/js/config.js +++ b/view/adminhtml/web/js/config.js @@ -106,11 +106,16 @@ requirejs(['jquery', 'mage/translate'], function ($, $t) { } const it_st_input = document.querySelector('input[id$="fintecture_design_options_checkout_design_selectionist"]'); + if (it_st_input) { + it_st_input.disabled = true; + } const it_st_short_input = document.querySelector('input[id$="fintecture_design_options_checkout_design_selectionist_short"]'); + if (it_st_short_input) { + it_st_short_input.style.marginLeft = '15px'; + } const it_st_long_input = document.querySelector('input[id$="fintecture_design_options_checkout_design_selectionist_long"]'); - - it_st_input.disabled = true; - it_st_short_input.style.marginLeft = '15px'; - it_st_long_input.style.marginLeft = '15px'; - }); + if (it_st_long_input) { + it_st_long_input.style.marginLeft = '15px'; + } + }); }); \ No newline at end of file diff --git a/view/frontend/web/css/style.css b/view/frontend/web/css/style.css index 3f81329..ec666ba 100644 --- a/view/frontend/web/css/style.css +++ b/view/frontend/web/css/style.css @@ -1,5 +1,5 @@ .checkout_block { - padding-left: 30px; + margin-bottom: 40px; } .checkout_block p { diff --git a/view/frontend/web/js/view/payment/method-renderer/fintecture-bnpl.js b/view/frontend/web/js/view/payment/method-renderer/fintecture-bnpl.js index cf5fda1..d34c06c 100644 --- a/view/frontend/web/js/view/payment/method-renderer/fintecture-bnpl.js +++ b/view/frontend/web/js/view/payment/method-renderer/fintecture-bnpl.js @@ -38,6 +38,25 @@ define([ }, isRecommendedBnplBadgeActive: function () { return window.checkoutConfig.payment.fintecture_bnpl.recommendBnplBadge; + }, + forcePosition: function() { + var totalCartAmount = quote.getTotals()()['base_grand_total']; + var firstPositionActive = window.checkoutConfig.payment.fintecture.firstPositionActive; + var firstPositionAmount = window.checkoutConfig.payment.fintecture.firstPositionAmount; + + if (firstPositionActive) { + if (totalCartAmount > firstPositionAmount) { + var paymentModuleIT = document.getElementById('fintecture-it'); + var paymentModuleBNPL = document.getElementById('fintecture-bnpl'); + var paymentMethodsList = document.querySelector('.payment-group .step-title'); + + if (paymentModuleIT && paymentModuleBNPL && paymentMethodsList) { + paymentModuleIT.after(paymentModuleBNPL); + } else if (paymentModuleBNPL && paymentMethodsList) { + paymentMethodsList.after(paymentModuleBNPL); + } + } + } } }); }); diff --git a/view/frontend/web/js/view/payment/method-renderer/fintecture-payment.js b/view/frontend/web/js/view/payment/method-renderer/fintecture-payment.js index da9396d..a1c10a2 100644 --- a/view/frontend/web/js/view/payment/method-renderer/fintecture-payment.js +++ b/view/frontend/web/js/view/payment/method-renderer/fintecture-payment.js @@ -1,7 +1,8 @@ define([ 'Magento_Checkout/js/view/payment/default', + 'Magento_Checkout/js/model/quote', 'mage/url', -], function (Component, url) { +], function (Component, quote, url) { 'use strict'; return Component.extend({ @@ -18,6 +19,22 @@ define([ }, isRecommendedItBadgeActive: function () { return window.checkoutConfig.payment.fintecture.recommendItBadge; + }, + forcePosition: function() { + var totalCartAmount = quote.getTotals()()['base_grand_total']; + var firstPositionActive = window.checkoutConfig.payment.fintecture.firstPositionActive; + var firstPositionAmount = window.checkoutConfig.payment.fintecture.firstPositionAmount; + + if (firstPositionActive) { + if (totalCartAmount > firstPositionAmount) { + var paymentModuleIT = document.getElementById('fintecture-it'); + var paymentMethodsList = document.querySelector('.payment-group .step-title'); + + if (paymentModuleIT && paymentMethodsList) { + paymentMethodsList.after(paymentModuleIT); + } + } + } } }); }); diff --git a/view/frontend/web/template/payment/fintecture-bnpl.html b/view/frontend/web/template/payment/fintecture-bnpl.html index 95ce337..ab5edce 100644 --- a/view/frontend/web/template/payment/fintecture-bnpl.html +++ b/view/frontend/web/template/payment/fintecture-bnpl.html @@ -1,4 +1,4 @@ -
+
@@ -11,7 +11,7 @@
-
+

@@ -39,6 +39,12 @@
+
+ + + +
+