콘텐츠로 이동

@gj-kit/toss-payments-nestjs 레시피

각 코드 블록은 패키지 README에서 이미 타입 검사하는 public-contract 예제를 재사용합니다.

1. typed payment provider 하나 등록하기

섹션 제목: “1. typed payment provider 하나 등록하기”

먼저 core configuration을 만든 다음 typed injection을 위해 module을 한 번 등록합니다.

완료 상태: typed payment kit을 주입하는 Nest provider 하나를 만듭니다.

앱 소유 경계: core payment config을 만들고 한 번만 등록하며, webhook route에서는 원본 request bytes를 보존합니다.

import { orThrow } from '@gj-kit/toss-payments';
import { defineTossPaymentsConfig, parseApiSecretKey } from '@gj-kit/toss-payments/server';
import { TossPaymentsModule } from '@gj-kit/toss-payments-nestjs';
declare const apiSecretFromEnv: string; // Read this once from your server environment.
const config = defineTossPaymentsConfig({
secretKey: orThrow(parseApiSecretKey(apiSecretFromEnv)),
});
export const payments = TossPaymentsModule.forRoot(config);

2. configuration이 배선한 flow만 주입하기

섹션 제목: “2. configuration이 배선한 flow만 주입하기”

configuration별 type을 복구해 배선하지 않은 billing flow에는 property가 없게 합니다.

주석 처리한 줄은 실제 TS2339입니다. 이 config에는 BillingKeyStore가 배선되어 있지 않으므로, 타입을 싣지 않는 DI token을 거쳐 주입된 kit에도 billing 프로퍼티 자체가 존재하지 않습니다.

import { Injectable } from '@nestjs/common';
import { orThrow } from '@gj-kit/toss-payments';
import { defineTossPaymentsConfig, parseApiSecretKey, type OrderStore } from '@gj-kit/toss-payments/server';
import { InjectTossPayments, TossPaymentsModule, type TossPaymentsFor } from '@gj-kit/toss-payments-nestjs';
declare const SECRET: string; // the app owns this (process.env)
declare const orders: OrderStore; // the app owns this (its own DB adapter)
export const tossConfig = defineTossPaymentsConfig({ secretKey: orThrow(parseApiSecretKey(SECRET)), orders });
export type AppToss = TossPaymentsFor<typeof tossConfig>;
export const tossModule = TossPaymentsModule.forRoot(tossConfig);
@Injectable()
export class PaymentsService {
constructor(@InjectTossPayments() private readonly toss: AppToss) {}
order = () => this.toss.confirm.createOrder({ amount: 9_900, orderName: 'Pro' });
// this.toss.billing — TS2339: the config wired no BillingKeyStore, so there is no property.
}

전체 API reference에서 모든 export와 import path를 확인하세요.