|
18 | 18 | let filteredMethods: PaymentMethodData[];
|
19 | 19 | let name: string;
|
20 | 20 | let budgetEnabled = false;
|
21 |
| -
|
| 21 | + let initialPaymentMethodId: string; |
22 | 22 | onMount(async () => {
|
23 | 23 | methods = await sdk.forConsole.billing.listPaymentMethods();
|
24 | 24 | filteredMethods = methods?.paymentMethods.filter((method) => !!method?.last4);
|
25 | 25 |
|
26 |
| - $changeOrganizationTier.paymentMethodId = |
| 26 | + initialPaymentMethodId = |
27 | 27 | $organization?.paymentMethodId ??
|
28 | 28 | $organization?.backupPaymentMethodId ??
|
29 | 29 | filteredMethods[0]?.$id ??
|
30 | 30 | null;
|
| 31 | + $changeOrganizationTier.paymentMethodId = initialPaymentMethodId; |
31 | 32 | $changeOrganizationTier.billingBudget = $organization?.billingBudget;
|
32 | 33 | budgetEnabled = !!$organization?.billingBudget;
|
33 | 34 | });
|
|
36 | 37 | if ($changeOrganizationTier.billingBudget < 0) {
|
37 | 38 | throw new Error('Budget cannot be negative');
|
38 | 39 | }
|
| 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 | + } |
39 | 65 |
|
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 | + } |
52 | 70 | }
|
53 | 71 | }
|
54 | 72 |
|
|
0 commit comments