fix: add check for wrong cards

This commit is contained in:
Arman
2023-12-19 17:51:20 +01:00
parent 61bcc59953
commit c38bb98347
2 changed files with 60 additions and 21 deletions
@@ -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);
}
}
}
@@ -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;
});
@@ -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);
}
}
}