Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add check for wrong cards #658

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib/components/billing/paymentBoxes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
}
}
});

observer.observe(element, { childList: true });
});

onDestroy(() => {
observer.disconnect();
});

$: if (element) {
observer.observe(element, { childList: true });
}
</script>

<RadioBoxes elements={methods} total={methods?.length} variableName="$id" name="payment" bind:group>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/layout/containerHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
{/if}

<header class:u-flex={isFlex} class="u-gap-12 common-section u-main-space-between u-flex-wrap">
<div class="u-flex u-cross-child-center u-gap-16 u-flex-wrap">
<div class="u-flex u-cross-child-center u-cross-center u-gap-16 u-flex-wrap">
<Heading tag={titleTag} size={titleSize}>{title}</Heading>
{#if isCloud && isLimited}
<DropList bind:show={showDropdown} width="16">
Expand Down
6 changes: 4 additions & 2 deletions src/routes/console/account/payments/paymentModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@
}
}
});

observer.observe(element, { childList: true });
});

onDestroy(() => {
observer.disconnect();
document.documentElement.classList.remove('u-overflow-hidden');
});

$: if (element) {
observer.observe(element, { childList: true });
}
</script>

<FakeModal bind:show title="Add payment method" bind:error onSubmit={handleSubmit}>
Expand Down
35 changes: 28 additions & 7 deletions src/routes/console/wizard/cloudOrganization/paymentDetails.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,44 @@
let methods: PaymentList;
let name: string;
let budgetEnabled = false;
let initialPaymentMethodId: string;

onMount(async () => {
methods = await sdk.forConsole.billing.listPaymentMethods();
$createOrganization.paymentMethodId =
initialPaymentMethodId =
methods.paymentMethods.find((method) => !!method?.last4)?.$id ?? null;
$createOrganization.paymentMethodId = initialPaymentMethodId;
});

async function handleSubmit() {
if ($createOrganization.billingBudget < 0) {
throw new Error('Budget cannot be negative');
}
try {
const method = await submitStripeCard(name);
$createOrganization.paymentMethodId = method.$id;
invalidate(Dependencies.PAYMENT_METHODS);
} catch (e) {
throw new Error('Something went wrong. Please try again.');
if ($createOrganization.paymentMethodId) {
const card = await sdk.forConsole.billing.getPaymentMethod(
$createOrganization.paymentMethodId
);
if (!card?.last4) {
throw new Error(
'The payment method you selected is not valid. Please select a different one.'
);
}
} else {
try {
const method = await submitStripeCard(name);
const card = await sdk.forConsole.billing.getPaymentMethod(method.$id);
if (card?.last4) {
$createOrganization.paymentMethodId = card.$id;
} else {
throw new Error(
'The payment method you selected is not valid. Please select a different one.'
);
}
invalidate(Dependencies.PAYMENT_METHODS);
} catch (e) {
$createOrganization.paymentMethodId = initialPaymentMethodId;
throw new Error(e.message);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
let filteredMethods: PaymentMethodData[];
let name: string;
let budgetEnabled = false;

let initialPaymentMethodId: string;
onMount(async () => {
methods = await sdk.forConsole.billing.listPaymentMethods();
filteredMethods = methods?.paymentMethods.filter((method) => !!method?.last4);

$changeOrganizationTier.paymentMethodId =
initialPaymentMethodId =
$organization?.paymentMethodId ??
$organization?.backupPaymentMethodId ??
filteredMethods[0]?.$id ??
null;
$changeOrganizationTier.paymentMethodId = initialPaymentMethodId;
$changeOrganizationTier.billingBudget = $organization?.billingBudget;
budgetEnabled = !!$organization?.billingBudget;
});
Expand All @@ -36,19 +37,36 @@
if ($changeOrganizationTier.billingBudget < 0) {
throw new Error('Budget cannot be negative');
}
if ($changeOrganizationTier.paymentMethodId) {
const card = await sdk.forConsole.billing.getPaymentMethod(
$changeOrganizationTier.paymentMethodId
);
if (!card?.last4) {
throw new Error(
'The payment method you selected is not valid. Please select a different one.'
);
}
} else {
try {
await submitStripeCard(name);
const latestMethods = await sdk.forConsole.billing.listPaymentMethods();
const paymentMethod = symmetricDifference(
methods.paymentMethods,
latestMethods.paymentMethods
)[0] as PaymentMethodData;
const card = await sdk.forConsole.billing.getPaymentMethod(paymentMethod.$id);
if (card?.last4) {
$changeOrganizationTier.paymentMethodId = paymentMethod.$id;
} else {
throw new Error(
'The payment method you selected is not valid. Please select a different one.'
);
}

try {
await submitStripeCard(name);
const latestMethods = await sdk.forConsole.billing.listPaymentMethods();
const paymentMethod = symmetricDifference(
methods.paymentMethods,
latestMethods.paymentMethods
)[0] as PaymentMethodData;

$changeOrganizationTier.paymentMethodId = paymentMethod.$id;
invalidate(Dependencies.PAYMENT_METHODS);
} catch (e) {
throw new Error(e.message);
invalidate(Dependencies.PAYMENT_METHODS);
} catch (e) {
throw new Error(e.message);
}
}
}

Expand Down