Working on js for plans

This commit is contained in:
vinchent 2024-08-11 22:58:06 +02:00
parent 85c1928c31
commit fde0fdb7fa
4 changed files with 48 additions and 4 deletions

View File

@ -91,6 +91,12 @@ Bronze Plan
{{ end }} {{ end }}
{{ define "js" }} {{ define "js" }}
<script src="https://js.stripe.com/v3/"></script> <script src="https://js.stripe.com/v3/"></script>
<script src="/static/js/stripe.js"></script> <script type="module">
<script>stripeInit('{{.StripePubKey}}');</script> import {stripeInit} from "/static/js/common.js";
import {val} from "/static/js/stripe-plan.js"
stripeInit('{{.StripePubKey}}');
document.getElementById("pay-button").addEventListener("click", () => {
val({{.API}});
})
</script>
{{ end }} {{ end }}

View File

@ -92,7 +92,6 @@ Buy one widget
{{ end }} {{ end }}
{{ define "js" }} {{ define "js" }}
<script src="https://js.stripe.com/v3/"></script> <script src="https://js.stripe.com/v3/"></script>
<script type="module" src="/static/js/stripe.js"></script>
<script type="module"> <script type="module">
import {stripeInit} from "/static/js/common.js"; import {stripeInit} from "/static/js/common.js";
import {val} from "/static/js/stripe.js" import {val} from "/static/js/stripe.js"

View File

@ -75,7 +75,6 @@ Virtual Terminal
{{ end }} {{ end }}
{{ define "js" }} {{ define "js" }}
<script src="https://js.stripe.com/v3/"></script> <script src="https://js.stripe.com/v3/"></script>
<script type="module" src="/static/js/stripe.js"></script>
<script type="module"> <script type="module">
import {stripeInit} from "/static/js/common.js"; import {stripeInit} from "/static/js/common.js";
import {val} from "/static/js/stripe.js" import {val} from "/static/js/stripe.js"

40
static/js/stripe-plan.js Normal file
View File

@ -0,0 +1,40 @@
import {
hidePayButton,
showPayButton,
showCardError,
showCardSuccess,
stripe,
card,
processing,
} from './common.js';
export function val() {
let form = document.getElementById("charge_form");
if (form.checkValidity() === false) {
this.event.preventDefault();
this.event.stopPropagation();
form.classList.add("was-validated");
return;
}
form.classList.add("was-validated");
hidePayButton();
let amountToCharge = document.getElementById("amount").value;
stripe.createPaymentMethod({
type: 'card',
card: card,
billing_details: {
email: document.getElementById("cardholder-email").value,
},
}).then(stripePaymentMethodHandler);
}
function stripePaymentMethodHandler(result) {
if (result.error) {
showCardError(result.error.message);
} else {
// create a customer and subscribe to plan
}
}