Checkout on credit add (#3555)

This commit is contained in:
Christian Byrne
2025-04-22 13:46:05 +08:00
committed by GitHub
parent bf4ad38e9b
commit e7fe2046ba
3 changed files with 89 additions and 3 deletions

View File

@@ -16,6 +16,16 @@ import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { useFirebaseAuth } from 'vuefire'
import { operations } from '@/types/comfyRegistryTypes'
type CreditPurchaseResponse =
operations['InitiateCreditPurchase']['responses']['201']['content']['application/json']
type CreditPurchasePayload =
operations['InitiateCreditPurchase']['requestBody']['content']['application/json']
// TODO: Switch to prod api based on environment (requires prod api to be ready)
const API_BASE_URL = 'https://stagingapi.comfy.org'
export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
// State
const loading = ref(false)
@@ -100,6 +110,39 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
return null
}
const addCredits = async (
requestBodyContent: CreditPurchasePayload
): Promise<CreditPurchaseResponse | null> => {
const token = await getIdToken()
if (!token) {
error.value = 'Cannot add credits: User not authenticated'
return null
}
const response = await fetch(`${API_BASE_URL}/customers/credit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify(requestBodyContent)
})
if (!response.ok) {
const errorData = await response.json()
error.value = `Failed to initiate credit purchase: ${errorData.message}`
return null
}
// TODO: start polling /listBalance until balance is updated or n retries fail or report no change
return response.json()
}
const initiateCreditPurchase = async (
requestBodyContent: CreditPurchasePayload
): Promise<CreditPurchaseResponse | null> =>
executeAuthAction((_) => addCredits(requestBodyContent))
return {
// State
loading,
@@ -118,6 +161,7 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
logout,
getIdToken,
loginWithGoogle,
loginWithGithub
loginWithGithub,
initiateCreditPurchase
}
})