Js for plan

This commit is contained in:
vinchent 2024-08-12 13:31:16 +02:00
parent fde0fdb7fa
commit 01690d0236

View File

@ -8,7 +8,7 @@ import {
processing, processing,
} from './common.js'; } from './common.js';
export function val() { export function val(plan_id, api) {
let form = document.getElementById("charge_form"); let form = document.getElementById("charge_form");
if (form.checkValidity() === false) { if (form.checkValidity() === false) {
@ -20,7 +20,7 @@ export function val() {
form.classList.add("was-validated"); form.classList.add("was-validated");
hidePayButton(); hidePayButton();
let amountToCharge = document.getElementById("amount").value; // let amountToCharge = document.getElementById("amount").value;
stripe.createPaymentMethod({ stripe.createPaymentMethod({
type: 'card', type: 'card',
@ -28,13 +28,39 @@ export function val() {
billing_details: { billing_details: {
email: document.getElementById("cardholder-email").value, email: document.getElementById("cardholder-email").value,
}, },
}).then(stripePaymentMethodHandler); }).then((result) => {
stripePaymentMethodHandler(result, plan_id, api);
});
} }
function stripePaymentMethodHandler(result) { function stripePaymentMethodHandler(result, plan_id, api) {
if (result.error) { if (result.error) {
showCardError(result.error.message); showCardError(result.error.message);
} else { } else {
// create a customer and subscribe to plan // create a customer and subscribe to plan
let payload = {
plan: plan_id,
payment_method: result.paymentMethod.id,
email: document.getElementById("cardholder-email").value,
last_four: result.paymentMethod.card.last4,
};
const requestOptions = {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
};
fetch(api + "/api/create-customer-and-subscribe-to-plan", requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
processing.classList.add("d-none");
// set hidden vars
// submit the form
});
} }
} }