refactor(join): auto-open Helcim modal after form submit

This commit is contained in:
Jennie Robinson Faber 2026-04-19 12:21:10 +01:00
parent 5a4c09f988
commit 1bb59f07be

View file

@ -624,15 +624,15 @@ const flowStepLabel = computed(() => {
} }
}); });
// Step 1: Create customer
const handleSubmit = async () => { const handleSubmit = async () => {
if (isSubmitting.value || !isFormValid.value) return; if (isSubmitting.value || !isFormValid.value) return;
isSubmitting.value = true; isSubmitting.value = true;
errorMessage.value = ""; errorMessage.value = "";
flowState.value = "creating-customer";
try { try {
// Create customer in Helcim // Create customer
const response = await $fetch("/api/helcim/customer", { const response = await $fetch("/api/helcim/customer", {
method: "POST", method: "POST",
body: { body: {
@ -645,82 +645,58 @@ const handleSubmit = async () => {
}, },
}); });
if (response.success) { if (!response.success) {
customerId.value = response.customerId; throw new Error("Failed to create account.");
customerCode.value = response.customerCode;
// Token is now set as httpOnly cookie by the server
// No need to manually set cookie on client side
// Move to next step
if (needsPayment.value) {
currentStep.value = 2;
// Initialize HelcimPay.js session for card verification
await initializeHelcimPay(customerId.value, customerCode.value, 0);
} else {
// For free tier, create subscription directly
await createSubscription();
// Check member status to ensure user is properly authenticated
await checkMemberStatus();
// Automatically redirect to welcome page after a short delay
setTimeout(() => {
navigateTo("/welcome");
}, 3000); // 3 second delay to show success message
}
} }
} catch (error) {
console.error("Error creating customer:", error);
errorMessage.value =
error.data?.message || "Failed to create account. Please try again.";
} finally {
isSubmitting.value = false;
}
};
// Step 2: Process payment customerId.value = response.customerId;
const processPayment = async () => { customerCode.value = response.customerCode;
if (isSubmitting.value) return;
isSubmitting.value = true; // Free tier: no Helcim modal, go straight to subscription.
errorMessage.value = ""; if (!needsPayment.value) {
flowState.value = "creating-subscription";
await createSubscription();
return;
}
// Paid tier: initialize HelcimPay session, then auto-open modal.
flowState.value = "opening-payment";
await initializeHelcimPay(customerId.value, customerCode.value, 0);
try {
// Verify payment through HelcimPay.js
const paymentResult = await verifyPayment(); const paymentResult = await verifyPayment();
if (!paymentResult?.success) {
throw new Error("Payment was not completed.");
}
paymentToken.value = paymentResult.cardToken;
if (paymentResult.success) { flowState.value = "processing-payment";
paymentToken.value = paymentResult.cardToken; await $fetch("/api/helcim/verify-payment", {
method: "POST",
body: {
cardToken: paymentResult.cardToken,
customerId: customerId.value,
},
});
// Verify payment on server flowState.value = "creating-subscription";
const verifyResult = await $fetch("/api/helcim/verify-payment", { const subscriptionResult = await createSubscription(
method: "POST", paymentResult.cardToken,
body: { );
cardToken: paymentResult.cardToken,
customerId: customerId.value,
},
});
// Create subscription (don't let subscription errors prevent form progression) if (!subscriptionResult || subscriptionResult.success === false) {
const subscriptionResult = await createSubscription( // Payment succeeded but subscription couldn't be created.
paymentResult.cardToken, // Keep overlay in success state; admin follow-up will reconcile.
); successMessage.value =
"Payment successful. Subscription setup may need manual completion.";
if (!subscriptionResult || !subscriptionResult.success) { flowState.value = "success";
console.warn(
"Subscription creation failed but payment succeeded:",
subscriptionResult?.error,
);
// Still progress to success page since payment worked
currentStep.value = 3;
successMessage.value =
"Payment successful! Subscription setup may need manual completion.";
}
} }
} catch (error) { } catch (error) {
console.error("Payment process error:", error); console.error("Join flow error:", error);
errorMessage.value = errorMessage.value =
error.message || "Payment verification failed. Please try again."; error.data?.message ||
error.message ||
"Something went wrong. Please try again.";
flowState.value = "error";
} finally { } finally {
isSubmitting.value = false; isSubmitting.value = false;
} }
@ -775,14 +751,6 @@ const createSubscription = async (cardToken = null) => {
} }
}; };
// Go back to previous step
const goBack = () => {
if (currentStep.value > 1) {
currentStep.value--;
errorMessage.value = "";
}
};
const closeFlowOverlay = () => { const closeFlowOverlay = () => {
flowState.value = "idle"; flowState.value = "idle";
errorMessage.value = ""; errorMessage.value = "";