Loading...
@@ -95,6 +92,13 @@ Buy one widget
{{ end }}
{{ define "js" }}
-
-
+
+
{{ end }}
diff --git a/cmd/web/templates/terminal.page.gohtml b/cmd/web/templates/terminal.page.gohtml
index 0463e73..683998a 100644
--- a/cmd/web/templates/terminal.page.gohtml
+++ b/cmd/web/templates/terminal.page.gohtml
@@ -49,10 +49,7 @@ Virtual Terminal
Loading...
@@ -78,8 +75,10 @@ Virtual Terminal
{{ end }}
{{ define "js" }}
-
-
+
{{ end }}
diff --git a/static/js/common.js b/static/js/common.js
new file mode 100644
index 0000000..028525f
--- /dev/null
+++ b/static/js/common.js
@@ -0,0 +1,63 @@
+export let card;
+export let stripe;
+const cardMessages = document.getElementById("card-messages");
+const payButton = document.getElementById("pay-button");
+export const processing = document.getElementById("processing-payment");
+
+export function hidePayButton() {
+ payButton.classList.add("d-none");
+ processing.classList.remove("d-none");
+}
+
+export function showPayButton() {
+ payButton.classList.remove("d-none");
+ processing.classList.add("d-none");
+}
+
+export function showCardError(msg) {
+ cardMessages.classList.add("alert-danger");
+ cardMessages.classList.remove("alert-success");
+ cardMessages.classList.remove("d-none");
+ cardMessages.innerText = msg;
+}
+
+export function showCardSuccess() {
+ cardMessages.classList.add("alert-success");
+ cardMessages.classList.remove("alert-danger");
+ cardMessages.classList.remove("d-none");
+ cardMessages.innerText = "Trasaction successful";
+}
+
+export function stripeInit(pubKey) {
+ stripe = Stripe(pubKey);
+
+ (function () {
+ // create stripe & elements
+ const elements = stripe.elements();
+ const style = {
+ base: {
+ fontSize: '16px',
+ lineHeight: '24px',
+ }
+ };
+
+ // create card entry
+ card = elements.create('card', {
+ style: style,
+ hidePostalCode: true,
+ });
+ card.mount("#card-element");
+
+ // check for input errors
+ card.addEventListener('change', function (event) {
+ var displayError = document.getElementById("card-errors");
+ if (event.error) {
+ displayError.classList.remove('d-none');
+ displayError.textContent = event.error.message;
+ } else {
+ displayError.classList.add('d-none');
+ displayError.textContent = "";
+ }
+ });
+ })();
+}
diff --git a/static/js/stripe.js b/static/js/stripe.js
index 4219400..4fa2c85 100644
--- a/static/js/stripe.js
+++ b/static/js/stripe.js
@@ -1,34 +1,14 @@
-let card;
-let stripe;
-const cardMessages = document.getElementById("card-messages");
-const payButton = document.getElementById("pay-button");
-const processing = document.getElementById("processing-payment");
+import {
+ hidePayButton,
+ showPayButton,
+ showCardError,
+ showCardSuccess,
+ stripe,
+ card,
+ processing,
+} from './common.js';
-function hidePayButton() {
- payButton.classList.add("d-none");
- processing.classList.remove("d-none");
-}
-
-function showPayButton() {
- payButton.classList.remove("d-none");
- processing.classList.add("d-none");
-}
-
-function showCardError(msg) {
- cardMessages.classList.add("alert-danger");
- cardMessages.classList.remove("alert-success");
- cardMessages.classList.remove("d-none");
- cardMessages.innerText = msg;
-}
-
-function showCardSuccess() {
- cardMessages.classList.add("alert-success");
- cardMessages.classList.remove("alert-danger");
- cardMessages.classList.remove("d-none");
- cardMessages.innerText = "Trasaction successful";
-}
-
-function val(api) {
+export function val(api) {
let form = document.getElementById("charge_form");
if (form.checkValidity() === false) {
@@ -57,7 +37,7 @@ function val(api) {
};
fetch(api + "/api/payment-intent", requestOptions)
- .then(response => response.text())
+ .then(response => response.text())
.then(response => {
let data;
try {
@@ -69,24 +49,24 @@ function val(api) {
name: document.getElementById("cardholder-name").value,
}
}
- }).then(function(result) {
- if (result.error) {
- // card declined, or sth went wrong with the card
- showCardError(result.error.message);
- showPayButton();
- } else if (result.paymentIntent) {
- if (result.paymentIntent.status === "succeeded") {
- // we have charged the card
- document.getElementById("payment_intent").value = result.paymentIntent.id;
- document.getElementById("payment_method").value = result.paymentIntent.payment_method;
- document.getElementById("payment_amount").value = result.paymentIntent.amount;
- document.getElementById("payment_currency").value = result.paymentIntent.currency;
- processing.classList.add("d-none");
- showCardSuccess();
- document.getElementById("charge_form").submit();
- }
+ }).then(function (result) {
+ if (result.error) {
+ // card declined, or sth went wrong with the card
+ showCardError(result.error.message);
+ showPayButton();
+ } else if (result.paymentIntent) {
+ if (result.paymentIntent.status === "succeeded") {
+ // we have charged the card
+ document.getElementById("payment_intent").value = result.paymentIntent.id;
+ document.getElementById("payment_method").value = result.paymentIntent.payment_method;
+ document.getElementById("payment_amount").value = result.paymentIntent.amount;
+ document.getElementById("payment_currency").value = result.paymentIntent.currency;
+ processing.classList.add("d-none");
+ showCardSuccess();
+ document.getElementById("charge_form").submit();
}
- })
+ }
+ })
console.log(data);
} catch (err) {
@@ -97,36 +77,3 @@ function val(api) {
});
}
-function stripeInit(pubKey) {
- stripe = Stripe(pubKey);
-
- (function () {
- // create stripe & elements
- const elements = stripe.elements();
- const style = {
- base: {
- fontSize: '16px',
- lineHeight: '24px',
- }
- };
-
- // create card entry
- card = elements.create('card', {
- style:style,
- hidePostalCode: true,
- });
- card.mount("#card-element");
-
- // check for input errors
- card.addEventListener('change', function(event) {
- var displayError = document.getElementById("card-errors");
- if (event.error) {
- displayError.classList.remove('d-none');
- displayError.textContent = event.error.message;
- } else {
- displayError.classList.add('d-none');
- displayError.textContent = "";
- }
- });
- })();
-}