import { ZuploContext, environment } from "@zuplo/runtime"; export interface WalletTopUpResult { success: boolean; transaction?: any; error?: string; newBalance?: number; } /** * Adds credits to a user's wallet in Lago * @param userId - The Lago external_customer_id * @param amount - The amount to add in credits * @param context - The Zuplo context for logging * @param transactionName - Optional name for the transaction (defaults to "Prepaid Top-up") * @param isGranted - If true, credits are granted (free); if false, credits are paid (default: false) * @returns Result object with success status and transaction details */ export async function topUpWallet( userId: string, amount: number, context: ZuploContext, transactionName: string = "Prepaid Top-up", isGranted: boolean = false ): Promise { context.log.info(`Attempting to top up wallet for user ${userId} with amount $${amount.toFixed(2)}`); // Get wallet ID for this customer const checkWalletResponse = await fetch( `${environment.LAGO_API_BASE}/api/v1/wallets?external_customer_id=${userId}`, { method: "GET", headers: { "Authorization": `Bearer ${environment.LAGO_API_KEY}`, "Content-Type": "application/json" } } ); if (!checkWalletResponse.ok) { const error = await checkWalletResponse.text(); context.log.error(`Failed to fetch wallet from Lago: ${error}`); return { success: false, error: `Failed to fetch wallet information: ${error}` }; } const walletData = await checkWalletResponse.json(); const activeWallet = walletData.wallets?.find((w: any) => w.status === "active"); if (!activeWallet) { context.log.error("No active wallet found for customer"); return { success: false, error: "No active wallet found for user" }; } const wallet = activeWallet; const walletId = wallet.lago_id; context.log.info(`Found active wallet ID: ${walletId}`); try { // Create wallet transaction (top-up) const topUpResponse = await fetch( `${environment.LAGO_API_BASE}/api/v1/wallet_transactions`, { method: "POST", headers: { "Authorization": `Bearer ${environment.LAGO_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ wallet_transaction: { wallet_id: walletId, paid_credits: isGranted ? "0.0" : amount.toFixed(2), granted_credits: isGranted ? amount.toFixed(2) : "0.0", name: transactionName } }) } ); if (!topUpResponse.ok) { const error = await topUpResponse.text(); context.log.error(`Failed to create wallet transaction: Status ${topUpResponse.status}, Error: ${error}`); return { success: false, error: `Failed to create top-up transaction: ${error}` }; } const transactionData = await topUpResponse.json(); context.log.info(`Wallet transaction created: ${JSON.stringify(transactionData)}`); const transaction = transactionData.wallet_transactions?.[0]; if (!transaction) { context.log.error("No transaction returned from Lago"); return { success: false, error: "No transaction returned from Lago" }; } const currentBalance = parseFloat(wallet.credits_ongoing_balance || "0"); const newBalance = currentBalance + amount; return { success: true, transaction, newBalance }; } catch (error) { context.log.error(`Error creating wallet transaction: ${error}`); return { success: false, error: `Internal error: ${error}` }; } }