이제 추가 좌석을 나타내는 추가 기능을 만들어야 합니다. 이 추가 기능은 기본 구독에 연결되어 고객이 추가 좌석을 구매할 수 있도록 합니다.
우리가 만들고 있는 것: 좌석당 월 $2의 비용이 드는 추가 기능으로, 모든 기본 구독에 추가할 수 있습니다.
1
추가 기능으로 이동
Dodo Payments 대시보드에서 제품 섹션에 머무르세요.
추가 기능 탭을 클릭하세요.
추가 기능 만들기를 클릭하세요.
이렇게 하면 추가 기능 생성 양식이 열립니다.
2
추가 기능 세부정보 입력
좌석 추가 기능에 대해 다음 값을 입력하세요:추가 기능 이름: Additional Team Seat설명: Add extra team members to your workspace with full access to all features가격: 입력 → 2.00통화: 기본 구독 통화와 일치해야 합니다.세금 카테고리: 제품에 적합한 카테고리를 선택하세요.
API 키를 버전 관리에 커밋하지 마세요. .env을(를) .gitignore 파일에 추가하세요.
3
체크아웃 세션 생성 구현하기
다음 코드를 포함한 src/server.ts 파일을 생성하세요:
복사
// Add this new endpoint for dynamic seat quantitiesimport 'dotenv/config';import DodoPayments from 'dodopayments';import express, { Request, Response } from 'express';const app = express();// Initialize the Dodo Payments clientconst client = new DodoPayments({ bearerToken: process.env.DODO_PAYMENTS_API_KEY, environment: 'test_mode'});async function createCheckoutSession(seatCount: number) { try { const session = await client.checkoutSessions.create({ // Products to sell - use IDs from your Dodo Payments dashboard product_cart: [ { product_id: 'pdt_7Rl9OWT2Mz4wwUTKz74iZ', // Replace with your actual product ID quantity: 1, addons: [ { addon_id: 'adn_eKQbNakKrivDpaxmI8wKI', // Replace with your actual addon ID quantity: seatCount } ] } ], // Pre-fill customer information to reduce friction customer: { email: 'steve@example.com', name: 'Steve Irwin', }, // Where to redirect after successful payment return_url: 'https://example.com/checkout/success', }); // Redirect your customer to this URL to complete payment console.log('Checkout URL:', session.checkout_url); console.log('Session ID:', session.session_id); return session; } catch (error) { console.error('Failed to create checkout session:', error); throw error; }}// Example usage in an Express.js routeapp.post('/create-checkout/:seatCount', async (req: Request, res: Response) => { try { const seatCount = parseInt(req.params.seatCount); const session = await createCheckoutSession(seatCount); res.json({ checkout_url: session.checkout_url }); } catch (error) { res.status(500).json({ error: 'Failed to create checkout session' }); }});// Add this line after your other middlewareapp.use(express.static('public'));// Add this route to serve the demo pageapp.get('/', (req, res) => { res.sendFile(__dirname + '/../public/index.html');});app.listen(3000, () => { console.log('Server is running on port 3000');});
웹 인터페이스가 생성되었습니다! 이제 다양한 좌석 수량을 테스트할 수 있는 간단한 UI가 있습니다.
5
정적 파일 제공하기
HTML 파일을 제공하기 위해 src/server.ts에 이 코드를 추가하세요:
복사
// Add this line after your other middlewareapp.use(express.static('public'));// Add this route to serve the demo pageapp.get('/', (req, res) => { res.sendFile(__dirname + '/../public/index.html');});
정적 파일이 구성되었습니다! 데모 인터페이스를 보려면 http://localhost:3000를 방문하세요.
구독 및 좌석 변경 사항과 데이터베이스를 동기화하려면 Dodo Payments의 웹훅 이벤트를 수신해야 합니다. 웹훅은 고객이 체크아웃을 완료하거나 구독을 업데이트하거나 좌석 수를 변경할 때 백엔드에 알립니다.웹훅 엔드포인트를 설정하고 이벤트를 처리하는 방법에 대한 단계별 지침은 공식 Dodo Payments 웹훅 가이드를 참조하세요: