★★★★★
#앱별 실전 패턴 · claude-code
[buildit] 토스 Webhook 처리 (결제 완료 자동화)
빌드잇 앱: 토스 결제 후 자동으로 의뢰 상태 업데이트.
#buildit#payment#toss#webhook
빌드잇 앱: 토스 결제 후 자동으로 의뢰 상태 업데이트.
Toss Payments API 문서: https://docs.tosspayments.com
구현 (Cloud Functions):
```typescript
exports.handleTossWebhook = functions.https.onRequest(async (req, res) => {
if (req.method !== 'POST') {
return res.status(405).send('Method not allowed');
}
const { orderId, paymentKey, amount, status } = req.body;
// 결제 검증 (Toss API)
const response = await fetch('https://api.tosspayments.com/v1/payments/confirm', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + Buffer.from(TOSS_SECRET_KEY).toString('base64'),
},
body: JSON.stringify({
paymentKey,
amount,
orderId,
}),
});
const paymentData = await response.json();
if (paymentData.status === 'DONE') {
// 결제 완료 → Firestore 업데이트
await db.collection('projects').doc(orderId).update({
paymentStatus: 'completed',
paymentKey,
paidAt: new Date(),
});
// 의뢰자에게 알림
const projectDoc = await db.collection('projects').doc(orderId).get();
const clientId = projectDoc.data().clientId;
await db.collection('notifications').add({
userId: clientId,
title: '결제 완료',
message: '결제가 완료되었습니다. 시공자를 기다려주세요.',
createdAt: new Date(),
});
}
res.status(200).json({ success: true });
});
```
결제 시작 (Flutter):
```dart
class PaymentService {
static Future<void> initiateTossPayment({
required String projectId,
required int amount,
required String clientName,
}) async {
// Toss 결제 창 열기
final response = await launchUrl(Uri.parse(
'https://payment.tosspayments.com/checkout/payment?'
'clientKey=$TOSS_CLIENT_KEY'
'&orderId=$projectId'
'&amount=$amount'
'&orderName=${Uri.encodeComponent(clientName)}s Project'
'&successUrl=https://buildit-app.com/payment/success'
'&failUrl=https://buildit-app.com/payment/fail',
));
}
}
```
구현해줄까?
쓰는 법 — 위 칸에 본인 값을 채우면 아래 프롬프트에 바로 반영돼요. 복사 버튼을 누르면 채워진 그대로 클립보드에 담기니까, claude-code(또는 본인이 쓰는 AI)에 붙여넣기만 하면 됩니다. 변수가 없으면 그냥 복사해서 쓰세요.

