Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions apps/web/app/[locale]/checkout/toss/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useEffect, useRef, useState } from "react";
import { useSearchParams } from "next/navigation";
import { useLocale } from "next-intl";
import { loadTossPayments } from "@tosspayments/tosspayments-sdk";
import { loadTossPayments, ANONYMOUS } from "@tosspayments/tosspayments-sdk";

export default function TossCheckoutPage() {
const params = useSearchParams();
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function TossCheckoutPage() {
try {
const tossPayments = await loadTossPayments(clientKey);
const widgets = tossPayments.widgets({
customerKey: `guest_${orderId}`,
customerKey: ANONYMOUS,
});
widgetsRef.current = widgets;

Expand All @@ -52,8 +52,9 @@ export default function TossCheckoutPage() {

setReady(true);
} catch (err) {
console.error("[toss] widget init error", err);
setError("결제 위젯을 불러오지 못했습니다. 새로고침해 주세요.");
const msg = err instanceof Error ? err.message : String(err);
console.error("[toss] widget init error", msg, err);
setError(`결제 위젯을 불러오지 못했습니다: ${msg}`);
}
})();
}, [orderId, amount]);
Expand Down
12 changes: 10 additions & 2 deletions apps/web/app/[locale]/hooks/usePaywall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ export function usePaywall(options: UsePaywallOptions) {
body: JSON.stringify(checkoutBody),
});

if (!res.ok) throw new Error(t("createFail"));
if (!res.ok) {
const errData = await res.json().catch(() => null);
const serverMsg = errData?.error?.message;
throw new Error(serverMsg || t("createFail"));
}
const data = await res.json();
const checkoutUrl = data.data?.checkoutUrl;
if (!checkoutUrl) throw new Error(t("noCheckoutUrl"));
Expand All @@ -137,7 +141,11 @@ export function usePaywall(options: UsePaywallOptions) {
body: JSON.stringify(checkoutBody),
});

if (!res.ok) throw new Error(t("createFail"));
if (!res.ok) {
const errData = await res.json().catch(() => null);
const serverMsg = errData?.error?.message;
throw new Error(serverMsg || t("createFail"));
}
const data = await res.json();
const orderId = data.data?.order?.orderId ?? data.order?.orderId;
if (!orderId) throw new Error(t("noOrderId"));
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/checkout/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ export async function POST(req: Request) {

return NextResponse.json({ ok: true, data: { order: orderSummary } });
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err);
logger.error('[checkout/create]', { error: err });
return NextResponse.json(
{ ok: false, error: { code: 'INTERNAL_ERROR', message: 'Checkout creation failed.' } },
{ ok: false, error: { code: 'INTERNAL_ERROR', message: `Checkout creation failed: ${errMsg}` } },
{ status: 500 }
);
}
Expand Down
33 changes: 31 additions & 2 deletions apps/web/app/api/checkout/paddle/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logger } from '../../../../../lib/logger';

function getPaddle() {
const key = process.env.PADDLE_API_KEY;
if (!key) throw new Error('PADDLE_API_KEY is not set');
if (!key) return null;
const isProd = process.env.PADDLE_ENVIRONMENT === 'production';
return new Paddle(key, { environment: isProd ? Environment.production : Environment.sandbox });
}
Expand Down Expand Up @@ -207,6 +207,34 @@ export async function POST(req: Request) {
});

const paddle = getPaddle();
if (!paddle) {
// Paddle not configured — fallback to Toss checkout
const tossRes = await fetch(new URL('/api/checkout/create', req.url).href, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const tossData = await tossRes.json();
if (!tossData.ok) {
return NextResponse.json(tossData, { status: tossRes.status });
}
const tossOrder = tossData.data?.order;
const origin = req.headers.get('origin') ?? process.env.NEXT_PUBLIC_BASE_URL ?? 'http://localhost:3000';
const localePath = `/${locale}`;
const tossParams = new URLSearchParams({
orderId: tossOrder.orderId,
amount: String(tossOrder.amount),
orderName: tossOrder.orderName,
product: PRODUCT_TO_LOADING[body.productCode] ?? 'saju',
});
return NextResponse.json({
ok: true,
data: {
checkoutUrl: `${origin}${localePath}/checkout/toss?${tossParams.toString()}`,
orderId: tossOrder.orderId,
},
});
}
const origin =
req.headers.get('origin') ?? process.env.NEXT_PUBLIC_BASE_URL ?? 'http://localhost:3000';
const productNames = PRODUCT_NAMES[body.productCode] ?? PRODUCT_NAMES.full!;
Expand Down Expand Up @@ -274,9 +302,10 @@ export async function POST(req: Request) {
},
});
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err);
logger.error('[paddle/create]', { error: err });
return NextResponse.json(
{ ok: false, error: { code: 'PADDLE_ERROR', message: 'Failed to create Paddle checkout.' } },
{ ok: false, error: { code: 'PADDLE_ERROR', message: `Paddle checkout failed: ${errMsg}` } },
{ status: 500 }
);
}
Expand Down
4 changes: 3 additions & 1 deletion apps/web/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const nextConfig: NextConfig = {
"'self'",
"'unsafe-inline'",
"https://cdn.paddle.com",
"https://js.tosspayments.com",
"https://www.googletagmanager.com",
...(isDev ? ["'unsafe-eval'"] : []),
].join(" ");
Expand All @@ -21,6 +22,7 @@ const nextConfig: NextConfig = {
"'self'",
"https://api.paddle.com",
"https://checkout.paddle.com",
"https://*.tosspayments.com",
"https://*.googleapis.com",
"https://www.google-analytics.com",
"https://*.google-analytics.com",
Expand All @@ -42,7 +44,7 @@ const nextConfig: NextConfig = {
"img-src 'self' data: blob: https:",
"font-src 'self' data: https://cdn.jsdelivr.net https://fonts.gstatic.com",
`connect-src ${connectSrc}`,
"frame-src 'self' https://buy.paddle.com https://checkout.paddle.com",
"frame-src 'self' https://buy.paddle.com https://checkout.paddle.com https://*.tosspayments.com",
].join("; "),
},
];
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/config/countries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const COUNTRY_CONFIGS: Record<string, CountryConfig> = {
tarot: { premium: 2900 },
},
priceLabel: "₩5,900",
paymentProvider: "paddle",
paymentProvider: "toss",
shareChannels: ["kakao", "copy", "twitter"],
enabledServices: ["saju", "compatibility", "name", "palm", "tarot"],
comingSoonServices: ["face"],
Expand Down
Loading