diff --git a/src/app/api/gigs/[id]/invoice/route.ts b/src/app/api/gigs/[id]/invoice/route.ts index e0f5414c..85e81cd7 100644 --- a/src/app/api/gigs/[id]/invoice/route.ts +++ b/src/app/api/gigs/[id]/invoice/route.ts @@ -313,16 +313,6 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{ ); } - const selectedCurrency = preferredCoinToPaymentCurrency(payment_currency || null); - const selectedAddress = merchant_wallet_address?.trim() || ""; - - if (!selectedCurrency || !selectedAddress) { - return NextResponse.json( - { error: "Select a CoinPay receiving wallet before sending the invoice" }, - { status: 400 } - ); - } - const workerCoinpayToken = await getConnectedCoinpayAccessToken(workerId); if (!workerCoinpayToken) { return NextResponse.json( @@ -353,6 +343,24 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{ ); } + let selectedCurrency = preferredCoinToPaymentCurrency(payment_currency || null); + let selectedAddress = merchant_wallet_address?.trim() || ""; + + if (!selectedCurrency || !selectedAddress) { + const gigCoin = preferredCoinToPaymentCurrency(gig.payment_coin || null); + const preferred = workerWallets.find((w) => w.currency === gigCoin) || workerWallets[0]; + + if (preferred) { + selectedCurrency = preferred.currency; + selectedAddress = preferred.address; + } else { + return NextResponse.json( + { error: "Select a CoinPay receiving wallet before sending the invoice" }, + { status: 400 } + ); + } + } + const selectedWallet = findCoinpayGlobalWallet( workerWallets, selectedCurrency, diff --git a/src/components/gigs/InvoiceButton.tsx b/src/components/gigs/InvoiceButton.tsx index 1d097bb2..91145a3c 100644 --- a/src/components/gigs/InvoiceButton.tsx +++ b/src/components/gigs/InvoiceButton.tsx @@ -234,12 +234,19 @@ export function InvoiceButton({ setIsCreating(true); setError(null); - const selectedWallet = - wallets.find((wallet) => walletKey(wallet) === selectedWalletKey) || null; - if (!selectedWallet) { - setError("Select a CoinPay receiving wallet"); - setIsCreating(false); - return; + let selectedWalletCurrency: string | undefined; + let selectedWalletAddress: string | undefined; + + if (isWorker) { + const selectedWallet = + wallets.find((wallet) => walletKey(wallet) === selectedWalletKey) || null; + if (!selectedWallet) { + setError("Select a CoinPay receiving wallet"); + setIsCreating(false); + return; + } + selectedWalletCurrency = selectedWallet.currency; + selectedWalletAddress = selectedWallet.address; } try { @@ -251,8 +258,8 @@ export function InvoiceButton({ items: lineItems, amount: total, currency: "USD", - payment_currency: selectedWallet.currency, - merchant_wallet_address: selectedWallet.address, + payment_currency: selectedWalletCurrency, + merchant_wallet_address: selectedWalletAddress, notes: notes || undefined, due_date: dueDate || undefined, pr_links: prLinks.length > 0 ? prLinks : undefined, @@ -844,7 +851,8 @@ function InvoiceForm({ /> -
{error}
}