Skip to content

Commit c38bb98

Browse files
committed
fix: add check for wrong cards
1 parent 61bcc59 commit c38bb98

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

src/routes/console/wizard/cloudOrganization/paymentDetails.svelte

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,44 @@
1919
let methods: PaymentList;
2020
let name: string;
2121
let budgetEnabled = false;
22+
let initialPaymentMethodId: string;
2223
2324
onMount(async () => {
2425
methods = await sdk.forConsole.billing.listPaymentMethods();
25-
$createOrganization.paymentMethodId =
26+
initialPaymentMethodId =
2627
methods.paymentMethods.find((method) => !!method?.last4)?.$id ?? null;
28+
$createOrganization.paymentMethodId = initialPaymentMethodId;
2729
});
2830
2931
async function handleSubmit() {
3032
if ($createOrganization.billingBudget < 0) {
3133
throw new Error('Budget cannot be negative');
3234
}
33-
try {
34-
const method = await submitStripeCard(name);
35-
$createOrganization.paymentMethodId = method.$id;
36-
invalidate(Dependencies.PAYMENT_METHODS);
37-
} catch (e) {
38-
throw new Error('Something went wrong. Please try again.');
35+
if ($createOrganization.paymentMethodId) {
36+
const card = await sdk.forConsole.billing.getPaymentMethod(
37+
$createOrganization.paymentMethodId
38+
);
39+
if (!card?.last4) {
40+
throw new Error(
41+
'The payment method you selected is not valid. Please select a different one.'
42+
);
43+
}
44+
} else {
45+
try {
46+
const method = await submitStripeCard(name);
47+
const card = await sdk.forConsole.billing.getPaymentMethod(method.$id);
48+
if (card?.last4) {
49+
$createOrganization.paymentMethodId = card.$id;
50+
} else {
51+
throw new Error(
52+
'The payment method you selected is not valid. Please select a different one.'
53+
);
54+
}
55+
invalidate(Dependencies.PAYMENT_METHODS);
56+
} catch (e) {
57+
$createOrganization.paymentMethodId = initialPaymentMethodId;
58+
throw new Error(e.message);
59+
}
3960
}
4061
}
4162

src/routes/console/wizard/cloudOrganizationChangeTier/paymentDetails.svelte

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
let filteredMethods: PaymentMethodData[];
1919
let name: string;
2020
let budgetEnabled = false;
21-
21+
let initialPaymentMethodId: string;
2222
onMount(async () => {
2323
methods = await sdk.forConsole.billing.listPaymentMethods();
2424
filteredMethods = methods?.paymentMethods.filter((method) => !!method?.last4);
2525
26-
$changeOrganizationTier.paymentMethodId =
26+
initialPaymentMethodId =
2727
$organization?.paymentMethodId ??
2828
$organization?.backupPaymentMethodId ??
2929
filteredMethods[0]?.$id ??
3030
null;
31+
$changeOrganizationTier.paymentMethodId = initialPaymentMethodId;
3132
$changeOrganizationTier.billingBudget = $organization?.billingBudget;
3233
budgetEnabled = !!$organization?.billingBudget;
3334
});
@@ -36,19 +37,36 @@
3637
if ($changeOrganizationTier.billingBudget < 0) {
3738
throw new Error('Budget cannot be negative');
3839
}
40+
if ($changeOrganizationTier.paymentMethodId) {
41+
const card = await sdk.forConsole.billing.getPaymentMethod(
42+
$changeOrganizationTier.paymentMethodId
43+
);
44+
if (!card?.last4) {
45+
throw new Error(
46+
'The payment method you selected is not valid. Please select a different one.'
47+
);
48+
}
49+
} else {
50+
try {
51+
await submitStripeCard(name);
52+
const latestMethods = await sdk.forConsole.billing.listPaymentMethods();
53+
const paymentMethod = symmetricDifference(
54+
methods.paymentMethods,
55+
latestMethods.paymentMethods
56+
)[0] as PaymentMethodData;
57+
const card = await sdk.forConsole.billing.getPaymentMethod(paymentMethod.$id);
58+
if (card?.last4) {
59+
$changeOrganizationTier.paymentMethodId = paymentMethod.$id;
60+
} else {
61+
throw new Error(
62+
'The payment method you selected is not valid. Please select a different one.'
63+
);
64+
}
3965
40-
try {
41-
await submitStripeCard(name);
42-
const latestMethods = await sdk.forConsole.billing.listPaymentMethods();
43-
const paymentMethod = symmetricDifference(
44-
methods.paymentMethods,
45-
latestMethods.paymentMethods
46-
)[0] as PaymentMethodData;
47-
48-
$changeOrganizationTier.paymentMethodId = paymentMethod.$id;
49-
invalidate(Dependencies.PAYMENT_METHODS);
50-
} catch (e) {
51-
throw new Error(e.message);
66+
invalidate(Dependencies.PAYMENT_METHODS);
67+
} catch (e) {
68+
throw new Error(e.message);
69+
}
5270
}
5371
}
5472

0 commit comments

Comments
 (0)