概要
Next.js の最小ボイラープレートは、Dodo Payments がすでに接続されたスターターアプリです。API キーと product IDs を追加すると、checkout を開く料金ページ、payment events 用の webhook endpoint、および Customer Portal へのリンクを利用できます。このボイラープレートは、TypeScript、Tailwind CSS 4、および
@dodopayments/nextjs adaptor を使用する Next.js 16 App Router を採用しています。既存のアプリに同じ route handlers を追加する方法については、Next.js Adaptor を参照してください。特徴
ボイラープレートには次の機能が含まれています。- Quick Setup: クローンしてから約 5 分で稼働中の料金ページを用意できます。
- Checkout:
@dodopayments/nextjsを基盤に構築された、事前設定済みの checkout flow。 - Pricing Page: Tailwind CSS でスタイル設定されたダークテーマの料金ページ。
- Webhook Handler: 各 webhook signature を検証し、イベントに対してコードを実行する endpoint。
- Customer Portal: Customer Portal を開く header link。顧客はここで subscriptions を管理できます。
- TypeScript: 型付けされた product definitions と handlers。
- Pre-filled Checkout: 顧客の name と email を checkout に渡すため、顧客が再入力する必要はありません。
前提条件
始める前に、次のものが必要です。- Node.js 20.9 以降。Next.js 16 で必要です。
- Dodo Payments account。dashboard で API key と webhook signing secret を作成するために必要です。
クイックスタート
1
Clone the Repository
2
Install Dependencies
3
Get API Credentials
Dodo Payments にサインアップし、dashboard から credentials を取得します。
- API Key: Dashboard → Developer → API Keys で key を作成します。
- Webhook Key: Dashboard → Developer → Webhooks で endpoint を追加し、その signing secret をコピーします。endpoint URL は公開されており、HTTPS を使用する必要があります。マシンで events を受信する方法については、Webhook Events を参照してください。
4
Configure Environment Variables
example file をコピーして、root directory に Dodo Payments credentials に値を設定します。route handlers は次の variables を読み取ります。
.env file を作成します。DODO_PAYMENTS_API_KEYは checkout と Customer Portal handlers を authenticate します。DODO_PAYMENTS_WEBHOOK_KEYは webhook signatures を verify します。DODO_PAYMENTS_RETURN_URLは、payment 後に checkout が customer を送る先です。DODO_PAYMENTS_ENVIRONMENTはtest_modeまたはlive_modeです。
5
Add Your Products
src/lib/products.ts 内の sample products を自分のものに置き換えます。各 product_id に、dashboard の Products にある product の ID を設定します。name、description、price、および features を表示します。checkout では Dodo Payments の product に設定された price が請求されるため、price と product の値を同期してください。6
Run the Development Server
プロジェクト構成
checkout、Customer Portal、および webhook route handlers はsrc/app/api/ 配下にあります。
カスタマイズ
Product Information の更新
src/lib/products.ts を編集して、次を変更します。
- Dodo Payments dashboard の Products にある Product IDs
- Prices
- Features
- Descriptions
Customer Data の事前入力
src/app/components/ProductCard.tsx は、各 checkout request に hardcoded な name と email を送信します。これらをサインイン中の user の details に置き換えてください。
Customer Portal の更新
src/app/components/Header.tsx の Customer Portal link は、hardcoded な customer ID を使用して /api/customer-portal を開きます。これをサインイン中の user の Dodo Payments customer ID に置き換えてください。
Webhook Events
src/app/api/webhook/route.ts の handler は、DODO_PAYMENTS_WEBHOOK_KEY で各 request を verify してから、2 つの events を処理します。
- subscription が active になると
onSubscriptionActiveが実行されます(subscription.active)。 - payment が成功すると
onPaymentSucceededが実行されます(payment.succeeded)。
onSubscriptionCancelled などの handlers を追加します。Next.js Adaptor に、サポートされているすべての handlers が記載されています。
Dodo Payments から localhost にアクセスすることはできません。local development では、ngrok などの tunnel を使用して local server を公開し、その tunnel URL を webhook endpoint として使用してください。
デプロイ
Production 用に Build する
Vercel に Deploy する
[DODO_PAYMENTS_RETURN_URL を production URL に設定します。
Webhook URL の更新
deploy 後、Dodo Payments Dashboard に production webhook URL を追加します。example.com の箇所には自分の domain を指定してください。
DODO_PAYMENTS_WEBHOOK_KEY にコピーします。
トラブルシューティング
Module not found or build errors
Module not found or build errors
node_modules と package-lock.json を削除してから、dependencies を再インストールします。Checkout redirect fails
Checkout redirect fails
次の一般的な原因を確認してください。
- Product ID が Dodo Payments dashboard に存在しない。
.envの API key またはDODO_PAYMENTS_ENVIRONMENTが間違っている。test mode key はtest_modeでのみ動作します。
npm run dev を実行している terminal で error を確認してください。Webhooks not receiving events
Webhooks not receiving events
local testing では、ngrok を使用して server を公開します。Dodo dashboard で、ngrok HTTPS URL の後に
/api/webhook を付けた endpoint を追加します。その endpoint の signing secret を DODO_PAYMENTS_WEBHOOK_KEY として、.env file にコピーします。Customer portal link doesn't work
Customer portal link doesn't work
src/app/components/Header.tsx の hardcoded な CUSTOMER_ID を、Dodo Payments dashboard にある customer の ID に置き換えます。production では、代わりに authentication system と database から customer ID を取得してください。詳細情報
- Dodo Payments Documentation
- Checkout Sessions Documentation
- Webhooks Documentation
- Next.js Adaptor:
Checkout、CustomerPortal、およびWebhookshandlers の options
サポート
ボイラープレートについてサポートが必要な場合:- Discord community で質問してください。
- GitHub repository で issues を報告し、updates を確認してください。
- support team にメールしてください。