|
1 | 1 | <script lang="ts"> |
| 2 | + import { onMount } from 'svelte'; |
2 | 3 | import { Box, CardGrid } from '$lib/components'; |
3 | 4 | import { Button } from '$lib/elements/forms'; |
4 | 5 | import { getChangePlanUrl, plansInfo } from '$lib/stores/billing'; |
5 | 6 | import { currentPlan, organization } from '$lib/stores/organization'; |
6 | 7 | import { page } from '$app/state'; |
7 | 8 | import { get } from 'svelte/store'; |
8 | | - import { invalidate } from '$app/navigation'; |
| 9 | + import { goto, invalidate } from '$app/navigation'; |
| 10 | + import { resolve } from '$app/paths'; |
9 | 11 | import { Dependencies } from '$lib/constants'; |
10 | 12 | import { addNotification } from '$lib/stores/notifications'; |
11 | 13 | import { sdk } from '$lib/stores/sdk'; |
|
21 | 23 | let showEnable = false; |
22 | 24 | let showDisable = false; |
23 | 25 | let reEnabling = false; |
24 | | - let cancelling = false; |
| 26 | + let refreshing = false; |
25 | 27 |
|
26 | 28 | $: planSupportsPremiumGeoDB = $currentPlan?.supportedAddons?.premiumGeoDB === true; |
27 | 29 | $: canUpgradeToPremiumGeoDB = |
|
45 | 47 | return false; |
46 | 48 | } |
47 | 49 |
|
48 | | - async function handleCancelAndRetry() { |
49 | | - cancelling = true; |
| 50 | + onMount(async () => { |
| 51 | + if (page.url.searchParams.get('type') === 'confirm-addon') { |
| 52 | + let addonId = page.url.searchParams.get('addonId'); |
| 53 | +
|
| 54 | + // Fall back to listing pending addons if addonId is missing |
| 55 | + if (!addonId || addonId === 'undefined') { |
| 56 | + try { |
| 57 | + const addonList = await sdk |
| 58 | + .forConsoleIn(page.params.region) |
| 59 | + .projects.listAddons({ projectId: page.params.project }); |
| 60 | + const pending = addonList.addons.find( |
| 61 | + (a) => a.key === 'premiumGeoDB' && a.status === 'pending' |
| 62 | + ); |
| 63 | + addonId = pending?.$id ?? null; |
| 64 | + } catch (e) { |
| 65 | + addNotification({ |
| 66 | + message: |
| 67 | + e?.message ?? |
| 68 | + 'Unable to verify Premium Geo DB addon status. Please retry.', |
| 69 | + type: 'error' |
| 70 | + }); |
| 71 | + addonId = null; |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + if (addonId) { |
| 76 | + await confirmAddon(addonId); |
| 77 | + } |
| 78 | +
|
| 79 | + const settingsUrl = resolve('/(console)/project-[region]-[project]/settings', { |
| 80 | + region: page.params.region, |
| 81 | + project: page.params.project |
| 82 | + }); |
| 83 | + await goto(settingsUrl, { replaceState: true }); |
| 84 | + } |
| 85 | + }); |
| 86 | +
|
| 87 | + async function confirmAddon(addonId: string) { |
50 | 88 | try { |
51 | | - await sdk.forConsoleIn(page.params.region).projects.deleteAddon({ |
| 89 | + await sdk.forConsoleIn(page.params.region).projects.confirmAddonPayment({ |
52 | 90 | projectId: page.params.project, |
53 | | - addonId: premiumGeoDBAddon.$id |
| 91 | + addonId |
54 | 92 | }); |
55 | | - await invalidate(Dependencies.ADDONS); |
56 | | - showEnable = true; |
57 | | - } catch (e) { |
| 93 | + await Promise.all([invalidate(Dependencies.ADDONS), invalidate(Dependencies.PROJECT)]); |
58 | 94 | addNotification({ |
59 | | - message: e.message, |
60 | | - type: 'error' |
| 95 | + message: 'Premium Geo DB addon has been enabled', |
| 96 | + type: 'success' |
61 | 97 | }); |
| 98 | + } catch (e) { |
| 99 | + // Webhook may have already activated it — sync state and assume success |
| 100 | + if (e?.type === 'addon_not_found' || e?.code === 404) { |
| 101 | + await Promise.all([ |
| 102 | + invalidate(Dependencies.ADDONS), |
| 103 | + invalidate(Dependencies.PROJECT) |
| 104 | + ]); |
| 105 | + addNotification({ |
| 106 | + message: 'Premium Geo DB addon has been enabled', |
| 107 | + type: 'success' |
| 108 | + }); |
| 109 | + } else if (e?.code === 402) { |
| 110 | + // Payment failed — the backend has cleaned up; offer to retry from scratch |
| 111 | + await invalidate(Dependencies.ADDONS); |
| 112 | + addNotification({ |
| 113 | + message: |
| 114 | + e?.message ?? |
| 115 | + 'Payment could not be authorized. Please try enabling the addon again.', |
| 116 | + type: 'error' |
| 117 | + }); |
| 118 | + } else { |
| 119 | + addNotification({ |
| 120 | + message: e.message, |
| 121 | + type: 'error' |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +
|
| 127 | + async function handleRefresh() { |
| 128 | + if (!premiumGeoDBAddon) return; |
| 129 | + refreshing = true; |
| 130 | + try { |
| 131 | + await confirmAddon(premiumGeoDBAddon.$id); |
62 | 132 | } finally { |
63 | | - cancelling = false; |
| 133 | + refreshing = false; |
64 | 134 | } |
65 | 135 | } |
66 | 136 |
|
|
88 | 158 |
|
89 | 159 | <CardGrid> |
90 | 160 | <svelte:fragment slot="title">Premium Geo DB</svelte:fragment> |
91 | | - Give your project richer location intelligence on every user. Tailor pricing by region, surface |
92 | | - the right currency and language, comply with local regulations, and spot suspicious sign-ins |
93 | | - before they cause damage — all without leaving Appwrite. |
| 161 | + Give your project richer location intelligence on every user. Tailor pricing by region, surface the |
| 162 | + right currency and language, comply with local regulations, and spot suspicious sign-ins before they |
| 163 | + cause damage — all without leaving Appwrite. |
94 | 164 | <svelte:fragment slot="aside"> |
95 | 165 | <Box> |
96 | 166 | <h6> |
|
116 | 186 | <Badge variant="secondary" type="warning" content="Payment pending" /> |
117 | 187 | </div> |
118 | 188 | <p class="text u-margin-block-start-8"> |
119 | | - A payment is awaiting confirmation. If the payment was interrupted, you can |
120 | | - cancel and retry. |
| 189 | + A payment is awaiting confirmation. If you've completed authentication, click |
| 190 | + refresh to check the payment status. |
121 | 191 | </p> |
122 | 192 | <Button |
123 | 193 | secondary |
124 | 194 | class="u-margin-block-start-16" |
125 | | - disabled={cancelling} |
126 | | - on:click={handleCancelAndRetry}> |
127 | | - <span class="text">Cancel & retry</span> |
| 195 | + disabled={refreshing} |
| 196 | + on:click={handleRefresh}> |
| 197 | + <span class="text">Refresh</span> |
128 | 198 | </Button> |
129 | 199 | {:else if isActive} |
130 | 200 | <div class="u-flex u-cross-center u-gap-8 u-margin-block-start-8"> |
|
0 commit comments