|
| 1 | +<template> |
| 2 | + <div class="flex flex-col p-6"> |
| 3 | + <div |
| 4 | + class="flex items-center gap-2" |
| 5 | + :class="{ 'text-red-500': isInsufficientCredits }" |
| 6 | + > |
| 7 | + <i |
| 8 | + :class="[ |
| 9 | + 'text-2xl', |
| 10 | + isInsufficientCredits ? 'pi pi-exclamation-triangle' : '' |
| 11 | + ]" |
| 12 | + /> |
| 13 | + <h2 class="text-2xl font-semibold"> |
| 14 | + {{ |
| 15 | + $t( |
| 16 | + isInsufficientCredits |
| 17 | + ? 'credits.topUp.insufficientTitle' |
| 18 | + : 'credits.topUp.title' |
| 19 | + ) |
| 20 | + }} |
| 21 | + </h2> |
| 22 | + </div> |
| 23 | + |
| 24 | + <!-- Error Message --> |
| 25 | + <p v-if="isInsufficientCredits" class="text-lg text-muted mt-6"> |
| 26 | + {{ $t('credits.topUp.insufficientMessage') }} |
| 27 | + </p> |
| 28 | + |
| 29 | + <!-- Balance Section --> |
| 30 | + <div class="flex justify-between items-center mt-8"> |
| 31 | + <div class="flex flex-col gap-2"> |
| 32 | + <div class="text-muted">{{ $t('credits.yourCreditBalance') }}</div> |
| 33 | + <div class="flex items-center gap-2"> |
| 34 | + <Tag |
| 35 | + severity="secondary" |
| 36 | + icon="pi pi-dollar" |
| 37 | + rounded |
| 38 | + class="text-amber-400 p-1" |
| 39 | + /> |
| 40 | + <span class="text-2xl">{{ formattedBalance }}</span> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + <Button |
| 44 | + text |
| 45 | + severity="secondary" |
| 46 | + :label="$t('credits.creditsHistory')" |
| 47 | + icon="pi pi-arrow-up-right" |
| 48 | + @click="handleSeeDetails" |
| 49 | + /> |
| 50 | + </div> |
| 51 | + |
| 52 | + <!-- Amount Input Section --> |
| 53 | + <div class="flex flex-col gap-2 mt-8"> |
| 54 | + <div> |
| 55 | + <span class="text-muted">{{ $t('credits.topUp.addCredits') }}</span> |
| 56 | + <span class="text-muted text-sm ml-1">{{ |
| 57 | + $t('credits.topUp.maxAmount') |
| 58 | + }}</span> |
| 59 | + </div> |
| 60 | + <div class="flex items-center gap-2"> |
| 61 | + <Tag |
| 62 | + severity="secondary" |
| 63 | + icon="pi pi-dollar" |
| 64 | + rounded |
| 65 | + class="text-amber-400 p-1" |
| 66 | + /> |
| 67 | + <InputNumber |
| 68 | + v-model="amount" |
| 69 | + :min="1" |
| 70 | + :max="1000" |
| 71 | + :step="1" |
| 72 | + mode="currency" |
| 73 | + currency="USD" |
| 74 | + show-buttons |
| 75 | + @blur="handleBlur" |
| 76 | + @input="handleInput" |
| 77 | + /> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + <div class="flex justify-end mt-8"> |
| 81 | + <ProgressSpinner v-if="loading" class="w-8 h-8" /> |
| 82 | + <Button |
| 83 | + v-else |
| 84 | + severity="primary" |
| 85 | + :label="$t('credits.topUp.buyNow')" |
| 86 | + :disabled="!amount || amount > 1000" |
| 87 | + :pt="{ |
| 88 | + root: { class: 'px-8' } |
| 89 | + }" |
| 90 | + @click="handleBuyNow" |
| 91 | + /> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | +</template> |
| 95 | + |
| 96 | +<script setup lang="ts"> |
| 97 | +import Button from 'primevue/button' |
| 98 | +import InputNumber from 'primevue/inputnumber' |
| 99 | +import ProgressSpinner from 'primevue/progressspinner' |
| 100 | +import Tag from 'primevue/tag' |
| 101 | +import { computed, onBeforeUnmount, ref } from 'vue' |
| 102 | +
|
| 103 | +import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore' |
| 104 | +import { formatMetronomeCurrency, usdToMicros } from '@/utils/formatUtil' |
| 105 | +
|
| 106 | +defineProps<{ |
| 107 | + isInsufficientCredits?: boolean |
| 108 | +}>() |
| 109 | +
|
| 110 | +const authStore = useFirebaseAuthStore() |
| 111 | +const amount = ref<number>(9.99) |
| 112 | +const didClickBuyNow = ref(false) |
| 113 | +const loading = computed(() => authStore.loading) |
| 114 | +
|
| 115 | +const handleBlur = (e: any) => { |
| 116 | + if (e.target.value) { |
| 117 | + amount.value = parseFloat(e.target.value) |
| 118 | + } |
| 119 | +} |
| 120 | +
|
| 121 | +const handleInput = (e: any) => { |
| 122 | + amount.value = e.value |
| 123 | +} |
| 124 | +
|
| 125 | +const formattedBalance = computed(() => { |
| 126 | + if (!authStore.balance) return '0.000' |
| 127 | + return formatMetronomeCurrency(authStore.balance.amount_micros, 'usd') |
| 128 | +}) |
| 129 | +
|
| 130 | +const handleSeeDetails = async () => { |
| 131 | + const response = await authStore.accessBillingPortal() |
| 132 | + if (!response?.billing_portal_url) return |
| 133 | + window.open(response.billing_portal_url, '_blank') |
| 134 | +} |
| 135 | +
|
| 136 | +const handleBuyNow = async () => { |
| 137 | + if (!amount.value) return |
| 138 | +
|
| 139 | + const response = await authStore.initiateCreditPurchase({ |
| 140 | + amount_micros: usdToMicros(amount.value), |
| 141 | + currency: 'usd' |
| 142 | + }) |
| 143 | +
|
| 144 | + if (!response?.checkout_url) return |
| 145 | +
|
| 146 | + didClickBuyNow.value = true |
| 147 | +
|
| 148 | + // Go to Stripe checkout page |
| 149 | + window.open(response.checkout_url, '_blank') |
| 150 | +} |
| 151 | +
|
| 152 | +onBeforeUnmount(() => { |
| 153 | + if (didClickBuyNow.value) { |
| 154 | + // If clicked buy now, then returned back to the dialog and closed, fetch the balance |
| 155 | + void authStore.fetchBalance() |
| 156 | + } |
| 157 | +}) |
| 158 | +</script> |
0 commit comments