import { Resend } from 'resend'; import { escapeHtml } from '@/lib/security'; import type { ProductRow } from '@/lib/supabase/product-files'; const FROM = '쟁승메이드 '; const ADMIN_EMAIL = 'bgg8988@gmail.com'; const BANK_INFO = '케이뱅크 100-116-337157 (예금주: 박재오)'; function resend() { return new Resend(process.env.RESEND_API_KEY); } const won = (n: number) => `₩${n.toLocaleString('ko-KR')}`; /** 주문 접수: 고객 안내 + 관리자 알림 (실패해도 주문은 유효 — 호출부에서 try/catch) */ export async function sendOrderReceivedEmails(opts: { orderId: string; product: ProductRow; customerEmail: string; depositorName: string; }) { const { orderId, product, customerEmail, depositorName } = opts; // XSS 방지: 사용자 입력값 이스케이프 const safeDepositorName = escapeHtml(depositorName); const safeCustomerEmail = escapeHtml(customerEmail); const r = resend(); await r.emails.send({ from: FROM, to: [customerEmail], subject: `[쟁승메이드] 주문 접수 — ${product.name}`, html: `

주문이 접수되었습니다

${product.name} · ${won(product.price)}

아래 계좌로 입금해 주시면, 확인 후 마이페이지에서 바로 다운로드하실 수 있습니다.

${BANK_INFO}

입금자명: ${safeDepositorName}


주문번호 ${orderId} · 입금 확인은 영업시간 기준 최대 24시간 소요됩니다.

`, }); await r.emails.send({ from: FROM, to: [ADMIN_EMAIL], subject: `[쟁승메이드] 신규 주문(입금 대기) — ${product.name}`, html: `

신규 계좌이체 주문

상품: ${product.name} (${won(product.price)})

주문자 이메일: ${safeCustomerEmail} / 입금자명: ${safeDepositorName}

주문번호: ${orderId}

입금 확인 후 관리자 주문 페이지에서 [입금 확인]을 눌러주세요.

`, }); } /** 입금 확인: 고객에게 다운로드 활성화 안내 */ export async function sendOrderPaidEmail(opts: { product: ProductRow; customerEmail: string }) { const { product, customerEmail } = opts; await resend().emails.send({ from: FROM, to: [customerEmail], subject: `[쟁승메이드] 입금 확인 완료 — ${product.name} 다운로드 안내`, html: `

입금이 확인되었습니다

${product.name} 다운로드가 활성화되었습니다.

마이페이지 → 내 제품에서 바로 받으실 수 있습니다.

다운로드 링크는 클릭 시 4시간 동안 유효하며, 만료되면 다시 누르면 됩니다.

`, }); }