Skip to content

Commit 1c34fa4

Browse files
ralyodioqwencoder
andcommitted
fix: add auth headers to topup request + fix payment-status to use Supabase
- handleTopup() was missing authHeaders() so /api/usage/topup returned 'Not authenticated' - payment-status/[paymentId] was still querying CoinPay API, now queries credit_deposits from Supabase Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent 7a244d4 commit 1c34fa4

2 files changed

Lines changed: 18 additions & 23 deletions

File tree

src/app/api/usage/payment-status/[paymentId]/route.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
3-
const COINPAY_BASE_URL = (process.env.COINPAYPORTAL_API_URL || "https://coinpayportal.com").replace(/\/$/, '') + '/api';
4-
const COINPAY_API_KEY = process.env.COINPAYPORTAL_API_KEY;
2+
import { getSupabaseAdmin } from "@/lib/supabase";
53

64
export async function GET(
75
_req: NextRequest,
@@ -14,32 +12,29 @@ export async function GET(
1412
return NextResponse.json({ error: "paymentId required" }, { status: 400 });
1513
}
1614

17-
if (!COINPAY_API_KEY) {
18-
return NextResponse.json({ error: "Not configured" }, { status: 503 });
19-
}
15+
const admin = getSupabaseAdmin();
2016

21-
const res = await fetch(`${COINPAY_BASE_URL}/payments/${paymentId}`, {
22-
headers: {
23-
Authorization: `Bearer ${COINPAY_API_KEY}`,
24-
},
25-
cache: "no-store",
26-
});
17+
// Check the credit_deposits table for this payment
18+
const { data: deposit, error } = await admin
19+
.from("credit_deposits")
20+
.select("*")
21+
.eq("coinpay_payment_id", paymentId)
22+
.single();
2723

28-
if (!res.ok) {
29-
const text = await res.text();
30-
console.error("[payment-status] CoinPay error:", res.status, text);
24+
if (error && error.code !== "PGRST116") {
25+
console.error("[payment-status] DB error:", error);
3126
return NextResponse.json({ error: "Failed to fetch payment status" }, { status: 502 });
3227
}
3328

34-
const data = await res.json();
35-
const payment = data.payment || data;
29+
if (!deposit) {
30+
return NextResponse.json({ status: "pending", payment_id: paymentId });
31+
}
3632

3733
return NextResponse.json({
38-
status: payment.status || "pending",
39-
amount_usd: payment.amount_usd,
40-
currency: payment.currency,
41-
payment_address: payment.payment_address,
42-
tx_hash: payment.tx_hash,
34+
status: deposit.status,
35+
amount_usd: deposit.amount_usd,
36+
currency: "usdc_sol",
37+
payment_id: deposit.coinpay_payment_id,
4338
});
4439
} catch (error) {
4540
console.error("[payment-status] Error:", error);

src/app/usage/usage-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export default function UsageContent() {
115115
try {
116116
const res = await fetch("/api/usage/topup", {
117117
method: "POST",
118-
headers: { "Content-Type": "application/json" },
118+
headers: { ...authHeaders(), "Content-Type": "application/json" },
119119
body: JSON.stringify({ amount_usd: topupAmount, currency: topupCurrency }),
120120
});
121121
const result = await res.json();

0 commit comments

Comments
 (0)