From 8e8214ffdab125eb6f566ce42f67bb4e46e411dd Mon Sep 17 00:00:00 2001 From: Dinwy Date: Wed, 3 Jul 2024 17:09:41 +0900 Subject: [PATCH] Add publishable key in config and fix rendering issue --- .../Controller/KomojuField/ProcessToken.php | 6 +- .../Komoju/Payments/Gateway/Config/Config.php | 10 +++ .../Payments/Model/Ui/ConfigProvider.php | 1 + .../Observer/RestoreQuoteFromSession.php | 3 + .../Komoju/Payments/etc/adminhtml/system.xml | 5 ++ src/app/code/Komoju/Payments/etc/config.xml | 1 + .../js/view/payment/method-renderer/form.js | 65 +++++++++++-------- .../frontend/web/template/payment/form.html | 12 ++-- 8 files changed, 69 insertions(+), 34 deletions(-) diff --git a/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php b/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php index 036cf7bef..26f452694 100644 --- a/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php +++ b/src/app/code/Komoju/Payments/Controller/KomojuField/ProcessToken.php @@ -10,6 +10,7 @@ use Magento\Sales\Model\Order; use Komoju\Payments\Api\KomojuApi; +use Komoju\Payments\Gateway\Config\Config; use Komoju\Payments\Model\ExternalPaymentFactory; use Komoju\Payments\Model\ExternalPayment; @@ -20,6 +21,7 @@ class ProcessToken extends Action protected JsonFactory $jsonResultFactory; protected KomojuApi $komojuApi; protected Session $checkoutSession; + protected Config $config; protected LoggerInterface $logger; protected Order $order; protected ExternalPaymentFactory $externalPaymentFactory; @@ -32,12 +34,14 @@ public function __construct( ExternalPaymentFactory $externalPaymentFactory, ExternalPayment $externalPayment, KomojuApi $komojuApi, + Config $config, LoggerInterface $logger ) { $this->jsonResultFactory = $jsonResultFactory; $this->checkoutSession = $checkoutSession; $this->externalPayment = $externalPaymentFactory->create(); $this->komojuApi = $komojuApi; + $this->config = $config; $this->logger = $logger; parent::__construct($context); } @@ -67,7 +71,7 @@ public function execute() $session = $this->komojuApi->createSession([ 'amount' => $order->getGrandTotal(), 'currency' => $currencyCode, - 'default_locale' => 'en', + 'default_locale' => $this->config->getKomojuLocale(), 'email' => $order->getCustomerEmail(), 'metadata' => ['note' => 'testing'], 'payment_data' => [ diff --git a/src/app/code/Komoju/Payments/Gateway/Config/Config.php b/src/app/code/Komoju/Payments/Gateway/Config/Config.php index 92b336bba..25eea4e14 100644 --- a/src/app/code/Komoju/Payments/Gateway/Config/Config.php +++ b/src/app/code/Komoju/Payments/Gateway/Config/Config.php @@ -80,6 +80,16 @@ public function getSecretKey($storeId = null) return $this->getValue('secret_key', $storeId); } + /** + * Returns the value of the "Publishable Key" + * @param int|null $storeId + * @return string + */ + public function getPublishableKey($storeId = null) + { + return $this->getValue('publishable_key', $storeId); + } + /** * Returns the value of the "Webhook Secret Token" on the field on the admin * page. diff --git a/src/app/code/Komoju/Payments/Model/Ui/ConfigProvider.php b/src/app/code/Komoju/Payments/Model/Ui/ConfigProvider.php index 315b8b9ef..3a7305795 100644 --- a/src/app/code/Komoju/Payments/Model/Ui/ConfigProvider.php +++ b/src/app/code/Komoju/Payments/Model/Ui/ConfigProvider.php @@ -92,6 +92,7 @@ public function getConfig() 'available_payment_methods' => $this->createPaymentMethodOptions(), 'merchant_id' => $this->config->getMerchantId(), 'redirect_url' => $this->config->getRedirectUrl(), + 'publishable_key' => $this->config->getPublishableKey(), 'show_title' => $this->config->showTitle() ] ] diff --git a/src/app/code/Komoju/Payments/Observer/RestoreQuoteFromSession.php b/src/app/code/Komoju/Payments/Observer/RestoreQuoteFromSession.php index 2668f88b3..46630cec4 100644 --- a/src/app/code/Komoju/Payments/Observer/RestoreQuoteFromSession.php +++ b/src/app/code/Komoju/Payments/Observer/RestoreQuoteFromSession.php @@ -49,6 +49,9 @@ public function execute(Observer $observer) if ($order && $order->getStatus() == Order::STATE_PENDING_PAYMENT) { $this->checkoutSession->restoreQuote(); + } else { + $this->checkoutSession->clearQuote(); + $this->checkoutSession->clearStorage(); } } } diff --git a/src/app/code/Komoju/Payments/etc/adminhtml/system.xml b/src/app/code/Komoju/Payments/etc/adminhtml/system.xml index f4ab62c3d..f27ad9986 100644 --- a/src/app/code/Komoju/Payments/etc/adminhtml/system.xml +++ b/src/app/code/Komoju/Payments/etc/adminhtml/system.xml @@ -27,6 +27,11 @@ payment/komoju_payments/secret_key Magento\Config\Model\Config\Backend\Encrypted + + + payment/komoju_payments/publishable_key + Magento\Config\Model\Config\Backend\Encrypted + The webhook URL will be at /komoju/hostedpage/webhook of this site diff --git a/src/app/code/Komoju/Payments/etc/config.xml b/src/app/code/Komoju/Payments/etc/config.xml index 7141aab73..0a3752c99 100644 --- a/src/app/code/Komoju/Payments/etc/config.xml +++ b/src/app/code/Komoju/Payments/etc/config.xml @@ -12,6 +12,7 @@ 0 + 1 diff --git a/src/app/code/Komoju/Payments/view/frontend/web/js/view/payment/method-renderer/form.js b/src/app/code/Komoju/Payments/view/frontend/web/js/view/payment/method-renderer/form.js index dd6596ff0..00c29401c 100644 --- a/src/app/code/Komoju/Payments/view/frontend/web/js/view/payment/method-renderer/form.js +++ b/src/app/code/Komoju/Payments/view/frontend/web/js/view/payment/method-renderer/form.js @@ -79,25 +79,28 @@ define( var redirectUrl = url.build('checkout/onepage/success'); var message = $t("There was an error obtaining the payment token. Please try again."); - if (self.komojuToken()) { - self.sendToken(self.komojuToken()).done(function(response) { - if (response.success) { - if (response.data && response.data.redirect_url) { - redirectUrl = response.data.redirect_url; - } - $.mage.redirect(redirectUrl); - } else { - messageList.addErrorMessage({ message: response.message }); + if (!self.komojuFieldEnabledMethods.includes(self.komojuMethod()) || !self.komojuToken()) { + redirectUrl = this.redirectUrl() + "?payment_method=" + this.komojuMethod(); + $.mage.redirect(redirectUrl); + return; + } + + self.sendToken(self.komojuToken()).done(function(response) { + if (response.success) { + if (response.data && response.data.redirect_url) { + redirectUrl = response.data.redirect_url; } - }).fail(function() { - message = $t("There was an error processing your payment. Please try again."); - messageList.addErrorMessage({ message: message }); + $.mage.redirect(redirectUrl); + } else { + messageList.addErrorMessage({ message: response.message }); fullScreenLoader.stopLoader(); - }); - } else { + } + }).fail(function(error) { + console.error('Error during token submission:', error); + message = $t("There was an error processing your payment. Please try again."); messageList.addErrorMessage({ message: message }); fullScreenLoader.stopLoader(); - } + }); }, getData: function() { @@ -108,8 +111,9 @@ define( }, placeOrder: function (data, event) { - var boundSuper = this._super.bind(this); var self = this; + var boundSuper = this._super.bind(this); + var message = $t("There was an error processing your payment. Please try again."); if (!this.validate()) { return false; @@ -117,20 +121,21 @@ define( fullScreenLoader.startLoader(); - if (self.komojuFieldEnabledMethods.includes(self.komojuMethod())) { - self.submitPayment().then(function (token) { - self.komojuToken(token); - boundSuper(data, event); - }).catch(function () { - var message = $t("There was an error processing your payment. Please try again."); - - messageList.addErrorMessage({ message: message }); - fullScreenLoader.stopLoader(); - }); - } else { + if (!self.komojuFieldEnabledMethods.includes(self.komojuMethod())) { self.komojuToken(null); boundSuper(data, event); + return; } + + self.submitPayment().then(function (token) { + self.komojuToken(token); + boundSuper(data, event); + }).catch(function (error) { + console.error('Error during token submission:', JSON.stringify(error)); + + messageList.addErrorMessage({ message: message }); + fullScreenLoader.stopLoader(); + }); }, sendToken: function (token) { @@ -196,6 +201,12 @@ define( return config.title; }, + getPublishableKey: function () { + var config = this.getConfig(); + + return config.publishable_key; + }, + getSession: function () { return JSON.stringify(this.komojuSession()); }, diff --git a/src/app/code/Komoju/Payments/view/frontend/web/template/payment/form.html b/src/app/code/Komoju/Payments/view/frontend/web/template/payment/form.html index 3ff370eca..58675dee5 100644 --- a/src/app/code/Komoju/Payments/view/frontend/web/template/payment/form.html +++ b/src/app/code/Komoju/Payments/view/frontend/web/template/payment/form.html @@ -8,7 +8,6 @@

-
+ komoju-api="https://komoju.com">
-