Skip to content

Commit

Permalink
fix: add check for wrong cards
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmanNik committed Dec 19, 2023
1 parent 61bcc59 commit c38bb98
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
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

0 comments on commit c38bb98

Please sign in to comment.