Skip to content

Commit

Permalink
Merge pull request #658 from appwrite/fix-billing-last-minute
Browse files Browse the repository at this point in the history
fix: add check for wrong cards
  • Loading branch information
eldadfux authored Dec 19, 2023
2 parents f13bfa5 + b66740f commit 499d545
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
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

3 comments on commit 499d545

@vercel
Copy link

@vercel vercel bot commented on 499d545 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 499d545 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

console-preview – ./

console-next.vercel.app
console-preview-appwrite.vercel.app
console-preview-git-main-appwrite.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 499d545 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

console-cloud – ./

console-cloud-git-main-appwrite.vercel.app
console-cloud-appwrite.vercel.app
console-cloud.vercel.app

Please sign in to comment.