ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ NestJS ๋ฐฑ์๋ ๊ฐ๋ฐ: ๊ธฐ์ด๋ถํฐ ์ค์ ๊น์ง - NestJS ์ธ๋ถ API ์ฐ๋ ์ค์ ๊ฐ์ด๋ (Stripe, Firebase, SendGrid)
octo54 2025. 3. 25. 11:20๐ 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 ํจํด & ๋ง์ดํฌ๋ก์๋น์ค ์ค๊ณ
'study > ๋ฐฑ์๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- kotlin
- rag
- ๊ด๋ฆฌ์
- AI์ฑ๋ด
- REACT
- nodejs
- PostgreSQL
- ๋ฐฑ์๋๊ฐ๋ฐ
- ์ค๋งํธ ์ปจํธ๋ํธ
- gatsbyjs
- NestJS
- AI ์๋ํ
- ํ๋ก ํธ์๋
- Webpack
- Ktor
- Next.js
- fastapi
- LangChain
- App Router
- llm
- nextJS
- CI/CD
- ์น๊ฐ๋ฐ
- github
- Docker
- SEO์ต์ ํ
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- seo ์ต์ ํ 10๊ฐ
- Prisma
- ๋ฐฑ์๋
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |