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
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ model Payment {
purchase_id Int @unique
status Status
method PaymentMethod
provider PaymentProvider
merchant_uid String @unique
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Expand Down
2 changes: 0 additions & 2 deletions src/purchases/dtos/purchase.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { PaymentProvider } from "@prisma/client";

export interface PurchaseHistoryItemDTO {
prompt_id: number;
title: string;
price: number;
seller_nickname: string;
pg: PaymentProvider | null;
}

export interface PurchaseHistoryResponseDTO {
Expand Down
4 changes: 1 addition & 3 deletions src/purchases/repositories/purchase.complete.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma, PaymentMethod, PaymentProvider, Status } from '@prisma/client';
import { Prisma, PaymentMethod, Status } from '@prisma/client';

type Tx = Prisma.TransactionClient;

Expand All @@ -17,7 +17,6 @@ export const PurchaseCompleteRepository = {
purchase_id: number;
merchant_uid: string;
method: PaymentMethod;
provider: PaymentProvider;
status: Status;
paymentId: string;
cash_receipt_url?: string | null;
Expand All @@ -29,7 +28,6 @@ export const PurchaseCompleteRepository = {
merchant_uid: data.merchant_uid,
imp_uid: data.paymentId,
method: data.method,
provider: data.provider,
status: data.status,
cash_receipt_url: data.cash_receipt_url,
cash_receipt_type: data.cash_receipt_type,
Expand Down
5 changes: 0 additions & 5 deletions src/purchases/repositories/purchase.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export const PurchaseRepository = {
user: { select: { nickname: true}},
},
},
payment: {
select: {
provider: true,
},
},
},
orderBy: { created_at: 'desc'},
})
Expand Down
1 change: 0 additions & 1 deletion src/purchases/services/purchase.complete.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const PurchaseCompleteService = {
paymentId: paymentId,
status: 'Succeed',
method: verifiedPayment.method,
provider: verifiedPayment.provider,
cash_receipt_url: verifiedPayment.cashReceipt?.url,
cash_receipt_type: verifiedPayment.cashReceipt?.type,
});
Expand Down
1 change: 0 additions & 1 deletion src/purchases/services/purchase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const PurchaseHistoryService = {
price: r.amount,
purchased_at: r.created_at.toISOString(),
seller_nickname: r.prompt.user.nickname,
pg: r.payment?.provider ?? null,
}));

return {
Expand Down
1 change: 0 additions & 1 deletion src/purchases/services/purchase.webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const WebhookService = {
purchase_id: purchase.purchase_id,
merchant_uid: paymentId,
method: verifiedPayment.method,
provider: verifiedPayment.provider,
cash_receipt_url: verifiedPayment.cashReceipt?.url,
cash_receipt_type: verifiedPayment.cashReceipt?.type,
status: 'Succeed',
Expand Down
39 changes: 6 additions & 33 deletions src/purchases/utils/payment.util.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
import { PaymentMethod, PaymentProvider } from '@prisma/client';
import { AppError } from '../../errors/AppError';

// 1. ๊ฒฐ์ œ ์ˆ˜๋‹จ ๋งคํ•‘
// ๊ฒฐ์ œ ์ˆ˜๋‹จ ๋งคํ•‘
export function normalizePaymentMethod(input: string): PaymentMethod {
const code = input.toUpperCase().replace(/\s+/g, '');

if (code === 'CARD' || code.includes('์นด๋“œ')) return 'CARD';
if (code === 'VIRTUAL_ACCOUNT' || code.includes('๊ฐ€์ƒ๊ณ„์ขŒ')) return 'VIRTUAL_ACCOUNT';
if (code === 'TRANSFER' || code.includes('๊ณ„์ขŒ์ด์ฒด')) return 'TRANSFER';
if (code === 'MOBILE' || code.includes('MOBILE_PHONE') || code.includes('ํœด๋Œ€ํฐ')) return 'MOBILE';
if (code === 'EASY_PAY' || code.includes('๊ฐ„ํŽธ๊ฒฐ์ œ')) return 'EASY_PAY';
if (code === 'PaymentMethodCard') return 'CARD';
if (code === 'PaymentMethodVirtualAccount') return 'VIRTUAL_ACCOUNT';
if (code === 'PaymentMethodEasyPay') return 'TRANSFER';
if (code === 'PaymentMethodTransfer') return 'MOBILE';
if (code === 'PaymentMethodMobile') return 'EASY_PAY';

throw new AppError(`์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฐ์ œ ์ˆ˜๋‹จ์ž…๋‹ˆ๋‹ค: ${input}`, 400, 'UnsupportedPaymentMethod');
}

// 2. ์ œ๊ณต์ž (Provider) ๋งคํ•‘
export function normalizePaymentProvider(input: string, method: PaymentMethod): PaymentProvider {
const code = (input || '').toUpperCase().replace(/\s+/g, '');

if (code.includes('KAKAO') || code.includes('์นด์นด์˜ค')) {
return 'KAKAOPAY';
}
if (code.includes('TOSSPAY') || code.includes('ํ† ์Šค')) {
return 'TOSSPAY';
}
if (
code.includes('NAVER') || code.includes('๋„ค์ด๋ฒ„') ||
code.includes('SAMSUNG') || code.includes('์‚ผ์„ฑ') ||
code.includes('APPLE') || code.includes('์• ํ”Œ') ||
code.includes('PAYCO') || code.includes('ํŽ˜์ด์ฝ”') ||
code.includes('LPAY') || code.includes('์—˜ํŽ˜์ด') ||
code.includes('SSG') || code.includes('์—์Šค์—์Šค์ง€') ||
code.includes('PINPAY') || code.includes('ํ•€ํŽ˜์ด')
) {
throw new AppError(`ํ˜„์žฌ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฐ„ํŽธ๊ฒฐ์ œ์‚ฌ์ž…๋‹ˆ๋‹ค: ${input}`, 400, 'ProviderNotSupported');
}
if (method !== 'EASY_PAY') {
return 'TOSSPAYMENTS';
}
throw new AppError(`์‹๋ณ„ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฐ์ œ ์ œ๊ณต์ž์ž…๋‹ˆ๋‹ค: ${input}`, 400, 'UnknownPaymentProvider');
}
23 changes: 3 additions & 20 deletions src/purchases/utils/portone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { AppError } from '../../errors/AppError';
import { PaymentMethod, PaymentProvider } from '@prisma/client'
import { normalizePaymentMethod, normalizePaymentProvider } from "./payment.util"
import { PaymentMethod } from '@prisma/client'
import { normalizePaymentMethod } from "./payment.util"

interface PortOnePaymentResponse {
id: string; // paymentId
Expand Down Expand Up @@ -43,7 +43,6 @@ export type PortonePaymentVerified = {
amount: number;
status: string;
method: PaymentMethod;
provider: PaymentProvider;
paidAt: Date;
customData: any;
cashReceipt?: {
Expand Down Expand Up @@ -101,24 +100,9 @@ export async function fetchAndVerifyPortonePayment(
}
}

// 5. PG Provider ์ถ”์ถœ
// 5. ๊ฒฐ์ œ ์ˆ˜๋‹จ ์ถ”์ถœ
const rawMethodType = payment.method?.type || '';
const method = normalizePaymentMethod(rawMethodType);
let rawProvider = '';

if (method === 'EASY_PAY') {
rawProvider = payment.method?.easyPay?.provider || '';
} else if (method === 'CARD') {
rawProvider = payment.method?.card?.publisher || '';
} else if (method === 'TRANSFER') {
rawProvider = payment.method?.transfer?.bank || '';
} else if (method === 'VIRTUAL_ACCOUNT') {
rawProvider = payment.method?.virtualAccount?.bank || '';
} else if (method === 'MOBILE') {
rawProvider = payment.method?.mobile?.carrier || 'MOBILE';
}

const provider = normalizePaymentProvider(rawProvider, method);

// 6. ํ˜„๊ธˆ์˜์ˆ˜์ฆ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
let cashReceiptInfo = null;
Expand All @@ -134,7 +118,6 @@ export async function fetchAndVerifyPortonePayment(
amount: payment.amount.total,
status: payment.status,
method: method,
provider: provider,
paidAt: payment.paidAt ? new Date(payment.paidAt) : new Date(),
customData: parsedCustomData,
cashReceipt: cashReceiptInfo
Expand Down