From 01690d0236371f9160b3ad26ff247228a545fba4 Mon Sep 17 00:00:00 2001 From: vinchent Date: Mon, 12 Aug 2024 13:31:16 +0200 Subject: [PATCH] Js for plan --- static/js/stripe-plan.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/static/js/stripe-plan.js b/static/js/stripe-plan.js index a9900aa..faa7c21 100644 --- a/static/js/stripe-plan.js +++ b/static/js/stripe-plan.js @@ -8,7 +8,7 @@ import { processing, } from './common.js'; -export function val() { +export function val(plan_id, api) { let form = document.getElementById("charge_form"); if (form.checkValidity() === false) { @@ -20,7 +20,7 @@ export function val() { form.classList.add("was-validated"); hidePayButton(); - let amountToCharge = document.getElementById("amount").value; + // let amountToCharge = document.getElementById("amount").value; stripe.createPaymentMethod({ type: 'card', @@ -28,13 +28,39 @@ export function val() { billing_details: { 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) { showCardError(result.error.message); } else { // 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 + }); } }