From 6c4063c1e89659280cf1225ebd78ae39c661e3d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20M=C3=BCller?= <p.mueller@pcsg.de>
Date: Tue, 6 Mar 2018 12:48:15 +0100
Subject: [PATCH] feat: check API configuration and display error msg if
 misconfigured

---
 bin/controls/PaymentDisplay.js                |  4 +++
 locale.xml                                    |  8 ++---
 .../ERP/Payments/Amazon/PaymentDisplay.html   | 14 ++++-----
 .../ERP/Payments/Amazon/PaymentDisplay.php    | 30 ++++++++++++++++++-
 4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/bin/controls/PaymentDisplay.js b/bin/controls/PaymentDisplay.js
index cbf0280..414ab59 100644
--- a/bin/controls/PaymentDisplay.js
+++ b/bin/controls/PaymentDisplay.js
@@ -62,6 +62,10 @@ define('package/quiqqer/payment-amazon/bin/controls/PaymentDisplay', [
             var self = this;
             var Elm  = this.getElm();
 
+            if (!Elm.getElement('.quiqqer-payment-amazon-content')) {
+                return;
+            }
+
             this.$MsgElm     = Elm.getElement('.quiqqer-payment-amazon-message');
             this.$AuthBtnElm = Elm.getElement('#quiqqer-payment-amazon-btn');
             this.$WalletElm  = Elm.getElement('#quiqqer-payment-amazon-wallet');
diff --git a/locale.xml b/locale.xml
index 9118afe..42d8e44 100644
--- a/locale.xml
+++ b/locale.xml
@@ -61,6 +61,10 @@
             <de><![CDATA[Bezahlung der Bestellung #[oderId]]]></de>
             <en><![CDATA[Payment of order #[orderId]]]></en>
         </locale>
+        <locale name="controls.PaymentDisplay.configuration_error">
+            <de><![CDATA[Die Zahlung mit Amazon Pay kann zur Zeit leider nicht durchgeführt werden. Bitte wählen Sie eine andere Zahlungsweise.]]></de>
+            <en><![CDATA[Unfortunately, Amazon Pay can not be used at the moment. Please choose a different method of payment.]]></en>
+        </locale>
 
         <!-- Settings -->
         <locale name="settings.menu.title">
@@ -223,10 +227,6 @@
             <de><![CDATA[Bei der Authentifizierung mit Amazon ist ein Fehler aufgetreten. Bitte versuchen Sie erneut, sich mit Ihrem Amazon-Konto anzumelden oder wählen Sie eine andere Zahlungsweise.]]></de>
             <en><![CDATA[There was an error authenticating with Amazon. Please try again to sign in with your Amazon account or choose another form of payment.]]></en>
         </locale>
-        <locale name="controls.PaymentDisplay.configuration_error">
-            <de><![CDATA[Die Zahlung mit Amazon Pay kann zur Zeit leider nicht durchgeführt werden. Bitte wählen Sie eine andere Zahlungsweise.]]></de>
-            <en><![CDATA[Unfortunately, Amazon Pay can not be used at the moment. Please choose a different method of payment.]]></en>
-        </locale>
         <locale name="controls.PaymentDisplay.info">
             <de><![CDATA[Bitte klicken Sie auf die "amazon pay"-Schaltfläche und loggen sich mit Ihrem Amazon-Konto ein, um mit dem Zahlungsprozess fortzufahren.]]></de>
             <en><![CDATA[Please click the "amazon pay" button and sign in with your Amazon account to continue with the payment process.]]></en>
diff --git a/src/QUI/ERP/Payments/Amazon/PaymentDisplay.html b/src/QUI/ERP/Payments/Amazon/PaymentDisplay.html
index 4cd2541..5126f02 100644
--- a/src/QUI/ERP/Payments/Amazon/PaymentDisplay.html
+++ b/src/QUI/ERP/Payments/Amazon/PaymentDisplay.html
@@ -2,15 +2,15 @@
     <p>{locale group="quiqqer/payment-amazon" var="PaymentDisplay.no_javascript"}</p>
 </noscript>
 
-<style>
-    .quiqqer-order-ordering-step {
-        height:500px !important;
-    }
-</style>
-
+{if !$apiSetUp}
+<p class="message-error">
+    {locale group="quiqqer/payment-amazon" var="controls.PaymentDisplay.configuration_error"}
+</p>
+{else}
 <div class="quiqqer-payment-amazon-message"></div>
 <div class="quiqqer-payment-amazon-content">
     <div id="quiqqer-payment-amazon-btn" data-size="{$btn_size}" data-color="{$btn_color}"></div>
     <div id="quiqqer-payment-amazon-wallet"></div>
     <div id="quiqqer-payment-amazon-btn-pay" data-price="{$display_price}"></div>
-</div>
\ No newline at end of file
+</div>
+{/if}
\ No newline at end of file
diff --git a/src/QUI/ERP/Payments/Amazon/PaymentDisplay.php b/src/QUI/ERP/Payments/Amazon/PaymentDisplay.php
index 3e73918..7d455d8 100644
--- a/src/QUI/ERP/Payments/Amazon/PaymentDisplay.php
+++ b/src/QUI/ERP/Payments/Amazon/PaymentDisplay.php
@@ -50,7 +50,8 @@ class PaymentDisplay extends QUI\Control
         $Engine->assign(array(
             'btn_size'      => Provider::getWidgetsSetting('btn_size'),
             'btn_color'     => Provider::getWidgetsSetting('btn_color'),
-            'display_price' => $PriceCalculation->getSum()->formatted()
+            'display_price' => $PriceCalculation->getSum()->formatted(),
+            'apiSetUp'      => $this->isApiSetUp()
         ));
 
         $this->setJavaScriptControlOption('orderhash', $Order->getHash());
@@ -60,4 +61,31 @@ class PaymentDisplay extends QUI\Control
 
         return $Engine->fetch(dirname(__FILE__) . '/PaymentDisplay.html');
     }
+
+    /**
+     * Check if the Amazon Pay API settings are correct
+     *
+     * @return bool
+     * @throws QUI\Exception
+     */
+    protected function isApiSetUp()
+    {
+        $Conf        = QUI::getPackage('quiqqer/payment-amazon')->getConfig();
+        $apiSettings = $Conf->getSection('api');
+
+        foreach ($apiSettings as $k => $v) {
+            if (empty($v)) {
+                QUI\System\Log::addError(
+                    'Your Amazon Pay API credentials seem to be (partially) missing.'
+                    . ' Amazon Pay CAN NOT be used at the moment. Please enter all your'
+                    . ' API credentials. See https://dev.quiqqer.com/quiqqer/payment-amazon/wikis/api-configuration'
+                    . ' for further instructions.'
+                );
+
+                return false;
+            }
+        }
+
+        return true;
+    }
 }
-- 
GitLab