ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๐Ÿ“Œ NestJS ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹ค์ „๊นŒ์ง€ - NestJS ์™ธ๋ถ€ API ์—ฐ๋™ ์‹ค์ „ ๊ฐ€์ด๋“œ (Stripe, Firebase, SendGrid)

ํ˜„๋Œ€ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค๋Š” ๋‹ค์–‘ํ•œ ์™ธ๋ถ€ API์™€์˜ ํ†ตํ•ฉ์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค.
NestJS๋Š” ๊ตฌ์กฐ์ ์ธ ์•„ํ‚คํ…์ฒ˜์™€ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ ๋•๋ถ„์— Stripe, Firebase, SendGrid ๋“ฑ์˜ ์™ธ๋ถ€ ์„œ๋น„์Šค์™€์˜ ์—ฐ๋™์ด ๋งค์šฐ ๊ฐ„ํŽธํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์‹ค์ „์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์™ธ๋ถ€ ์„œ๋น„์Šค์™€ NestJS๋ฅผ ํ†ตํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๐Ÿš€


16.1 Stripe ์—ฐ๋™ – ๊ฒฐ์ œ ์‹œ์Šคํ…œ ๊ตฌํ˜„ํ•˜๊ธฐ

โœ… Stripe ํŒจํ‚ค์ง€ ์„ค์น˜

npm install stripe
npm install --save-dev @types/stripe

โœ… Stripe ์„œ๋น„์Šค ์ƒ์„ฑ

๐Ÿ“‚ libs/stripe/stripe.service.ts

import { Injectable } from '@nestjs/common';
import Stripe from 'stripe';

@Injectable()
export class StripeService {
  private stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
    apiVersion: '2023-10-16',
  });

  async createCheckoutSession(amount: number, currency = 'usd') {
    return this.stripe.checkout.sessions.create({
      payment_method_types: ['card'],
      line_items: [{
        price_data: {
          currency,
          product_data: { name: 'Test Item' },
          unit_amount: amount,
        },
        quantity: 1,
      }],
      mode: 'payment',
      success_url: 'https://yourdomain.com/success',
      cancel_url: 'https://yourdomain.com/cancel',
    });
  }
}

โœ… ์‚ฌ์šฉ ์˜ˆ (Controller)

@Controller('payments')
export class PaymentsController {
  constructor(private readonly stripeService: StripeService) {}

  @Post()
  async createPayment(@Body() body: { amount: number }) {
    const session = await this.stripeService.createCheckoutSession(body.amount);
    return { url: session.url };
  }
}

16.2 Firebase Admin SDK ์—ฐ๋™ – ์ธ์ฆ ๋ฐ ํ‘ธ์‹œ ์•Œ๋ฆผ

โœ… Firebase SDK ์„ค์น˜

npm install firebase-admin

โœ… Firebase ์„œ๋น„์Šค ๊ตฌ์„ฑ

๐Ÿ“‚ libs/firebase/firebase.service.ts

import * as admin from 'firebase-admin';
import { Injectable, OnModuleInit } from '@nestjs/common';

@Injectable()
export class FirebaseService implements OnModuleInit {
  onModuleInit() {
    admin.initializeApp({
      credential: admin.credential.cert({
        projectId: process.env.FIREBASE_PROJECT_ID,
        privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
        clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
      }),
    });
  }

  async sendPushNotification(token: string, title: string, body: string) {
    return admin.messaging().send({
      token,
      notification: {
        title,
        body,
      },
    });
  }
}

โœ… ์‚ฌ์šฉ ์˜ˆ (Controller)

@Post('push')
async notify(@Body() body: { token: string, title: string, message: string }) {
  return this.firebaseService.sendPushNotification(body.token, body.title, body.message);
}

๐Ÿ’ก ์ธ์ฆ, Firestore, Storage ๋“ฑ ๋‹ค์–‘ํ•œ Firebase ๊ธฐ๋Šฅ๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ


16.3 SendGrid ์—ฐ๋™ – ์ด๋ฉ”์ผ ๋ฐœ์†ก

โœ… SendGrid ์„ค์น˜

npm install @sendgrid/mail

โœ… SendGrid ์„œ๋น„์Šค ๊ตฌํ˜„

๐Ÿ“‚ libs/mailer/mailer.service.ts

import { Injectable } from '@nestjs/common';
import * as sgMail from '@sendgrid/mail';

@Injectable()
export class MailerService {
  constructor() {
    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
  }

  async sendEmail(to: string, subject: string, html: string) {
    return sgMail.send({
      to,
      from: 'noreply@yourdomain.com',
      subject,
      html,
    });
  }
}

๐Ÿ’ก HTML ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•˜๊ฑฐ๋‚˜, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋“ฑ์„ ํฌํ•จํ•œ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•๋„ ๊ฐ€๋Šฅ


16.4 ์™ธ๋ถ€ ์„œ๋น„์Šค ์—ฐ๋™ ์‹œ ์‹ค์ „ ํŒ

โœ… ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ด€๋ฆฌ๋Š” @nestjs/config๋ฅผ ํ†ตํ•ด ์ค‘์•™ํ™”
โœ… ๊ณตํ†ต ์œ ํ‹ธ์€ libs ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ , ์ „์—ญ์œผ๋กœ ๋ชจ๋“ˆ ๋“ฑ๋ก ๊ฐ€๋Šฅ
โœ… ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์‹œ HttpException, Logger ๋“ฑ์„ ์ ๊ทน ํ™œ์šฉ
โœ… ์ธ์ฆ์ด ํ•„์š”ํ•œ API๋Š” Retry, Timeout, Circuit Breaker ๋“ฑ์„ ๋„์ž…ํ•  ๊ฒƒ


16.5 ๊ฒฐ๋ก : NestJS๋Š” ์™ธ๋ถ€ ์„œ๋น„์Šค ์—ฐ๋™์— ๊ฐ•ํ•˜๋‹ค

โœ… NestJS๋Š” ๊ตฌ์กฐ์ ์œผ๋กœ ์™ธ๋ถ€ API ์—ฐ๋™์„ ๋ชจ๋“ˆํ™”ํ•˜๊ธฐ ์ข‹์Œ
โœ… Stripe๋กœ ๊ฒฐ์ œ ์‹œ์Šคํ…œ, Firebase๋กœ ์•Œ๋ฆผ, SendGrid๋กœ ์ด๋ฉ”์ผ ๋ฐœ์†ก ๊ตฌํ˜„
โœ… ์„œ๋น„์Šค๋ณ„ ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๋ฉด ์œ ์ง€๋ณด์ˆ˜์„ฑ๊ณผ ํ™•์žฅ์„ฑ์ด ํ–ฅ์ƒ๋จ

๋‹ค์Œ ๊ธ€์—์„œ๋Š” NestJS์˜ CQRS ํŒจํ„ด ์ ์šฉ ๋ฐ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜ ์„ค๊ณ„๋ฅผ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿš€


๐Ÿ” ๋‹ค์Œ ๊ธ€ ์˜ˆ๊ณ : NestJS CQRS์™€ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์„ค๊ณ„ ์ „๋žต

๐Ÿ“Œ ๋‹ค์Œ ํŽธ: 17. NestJS CQRS ํŒจํ„ด & ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์„ค๊ณ„


 

โ€ป ์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค.
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ
๋ฐ˜์‘ํ˜•