GitHub Repository
Minimal Go + Dodo Payments ボイラープレート
概要
このGo boilerplateは、pricing pageからDodo Payments productsを販売する最小限のGoサーバーです。checkout sessionを作成し、webhookを検証・処理して、Customer Portalを開きます。独自のGo backendの出発点としてクローンしてください。このboilerplateにはGo 1.24.4以降が必要で、
go.modに設定されたバージョンを使用します。cmd、internal、templatesのレイアウトを使用し、Go HTML templatesでpricing pageをレンダリングして、dodopayments-go SDK経由でDodo Payments APIを呼び出します。特徴
- Quick Setup: repositoryをクローンし、
.envにAPI keysを追加して、make runでserverを起動します。 - Payment Integration:
dodopayments-goSDKでcheckout sessionを作成するcheckout flowです。 - Modern UI: Go HTML templatesとTailwind CSSで構築したダークテーマのpricing pageです。
- Webhook Handling: eventを処理する前に、各webhookのsignatureを検証します。
- Customer Portal: Customer Portalを通じたセルフサービスのsubscription managementです。
- Go Best Practices:
cmd、internal、templatesを使用した整理されたproject layoutです。 - Pre-filled Checkout: customer’s nameとemailをcheckoutに渡すため、customerは再入力する必要がありません。
前提条件
開始する前に、次のものが必要です:- Go 1.24.4以降。
go versionでversionを確認します。 - Dodo Payments account。dashboardでAPI keyとwebhook signing keyを作成します。
- 少なくとも1つのproduct。dashboardのProductsで作成します。
クイックスタート
1
Clone the Repository
2
Install Dependencies
make installはgo mod downloadを実行し、その後go mod tidyを実行します。makeなしでmodulesをダウンロードするには、次を実行します:3
Get API Credentials
Dodo Paymentsに登録し、dashboardから両方のkeyをコピーします:
- API Key: Developer → API Keys
- Webhook Key: Developer → Webhooks。各webhook endpointには独自のsigning keyがあります。local serverに到達するendpointを作成する方法は、LocallyでのWebhookのテストを参照してください。
4
Configure Environment Variables
templateから、project rootにserverはstartup時にこれらのvariablesを読み込みます:
.env fileを作成します:.envに次の値を設定します:.env
いずれかのrequired keyが欠けている場合、serverはstartup時に終了します。
.env.exampleはPORTとDODO_PAYMENTS_RETURN_URLをport 8080に設定します。このpageではport 8000を使用するため、表示されているように両方を8000に設定するか、このpageのcommands内の8000を8080に置き換えます。5
Add Your Products
internal/lib/products.goのsample productを自分のproductsに置き換えます。dashboardのProductsから各product IDをコピーします:Priceは、pricing pageに表示するpriceだけを、最小のcurrency unitで設定します。9999は$99.99として表示されます。checkoutではDodo Paymentsのproduct priceが請求されます。6
Run the Development Server
make runはserverをbin/serverにbuildして起動します。binaryを先にbuildせずserverを実行するには、次を実行します:productsが一覧表示され、購入可能なダークテーマのpricing pageが表示されます。
Project Structure
repositoryのlayoutは次のとおりです:API Endpoints
boilerplateには、次のpre-configured endpointsが含まれています:Customization
Update Product Information
internal/lib/products.goを編集して、次を変更します:
- Product IDs(Dodo Payments dashboardのProductsから取得)
- Names
- pricing pageに表示されるpricing
- Features
- Descriptions
/mo suffixを追加し、Priceが100000以上の場合はpriceの代わりにCustomを表示します。変更するにはtemplates/index.htmlを編集します。
Pre-fill Customer Data
.envでは、handleCheckout functionがhardcoded customer dataを/api/checkoutに送信します。signed-in userのdataに置き換えてください:
handlePortal functionはこのcustomer dataを再利用し、同じsample nameとemailにfallbackします。production appでは、両方のfunctionにauthentication systemからこれらのvaluesをinjectしてください。
Webhook Events
internal/api/webhook.goは、client.Webhooks.UnwrapとDODO_PAYMENTS_WEBHOOK_KEYのkeyで各requestを検証し、typeによってeventをrouteします。これらのeventsにはhandlerがあり、各handlerはevent dataをlogに記録します:
handlerは
subscription.on_hold、subscription.failed、subscription.expired、subscription.plan_changedもactionなしで受け付け、その他すべてのevent typeをunhandledとしてlogに記録します。検証済みのすべてのeventに対して200で応答します。すべてのevent typeについては、Webhook Event Guideを参照してください。
handler functionsにbusiness logicを追加して、次を実行します:
- databaseのuser permissionsを更新する
- confirmation emailsを送信する
- digital productsへのaccessをprovisionする
- analyticsとmetricsをtrackする
Testing Webhooks Locally
Dodo Paymentsはlocalhostに到達できません。development中にwebhookを受信するには、ngrokなどのtunnelでlocal serverを公開します:
/api/webhookを付けたURLでendpointを追加します:
DODO_PAYMENTS_WEBHOOK_KEYにコピーし、serverを再起動します。
Deployment
Build for Production
make buildはserverをbin/serverにcompileします:
makeなしでbinaryをbuildして起動するには、次を実行します:
Deploy to Vercel
[.env fileのvariablesをVercel project settingsに追加します。.envはrepositoryに含まれていないためです。その後、dashboardのwebhook endpointをhttps://yourdomain.com/api/webhookに設定します。
Docker
project rootにDockerfileを作成します。build stageではgo.modに合わせてGo 1.24.4以降を使用する必要があります:
templates/をbinaryの隣にcopyします。serverがworking directoryからtemplatesをloadするためです。imageをbuildして実行します:
.envのPORT valueでlistenするため、port mappingと一致するようPORT=8000を維持します。
Production Considerations
Troubleshooting
Build errors or missing dependencies
Build errors or missing dependencies
go versionがGo 1.24.4以降を示していることを確認し、modulesを再度downloadします:Checkout session creation fails
Checkout session creation fails
Common causes:
- product IDが無効です。API keyと同じmodeのProductsに存在することを確認してください。
.envのAPI keyまたはDODO_PAYMENTS_ENVIRONMENTが間違っています。test mode keyにはtest_modeが必要です。- 正確なerrorについてはserver logsを確認してください。handlerは
500を返す前に、失敗したすべてのrequestをlogに記録します。
Webhooks not receiving events
Webhooks not receiving events
local testingでは、ngrokでserverを公開します:Dodo Payments dashboardのwebhook URLをngrok URLに設定します。次に
.envのDODO_PAYMENTS_WEBHOOK_KEYを、そのendpointのsigning keyに設定します。serverがwebhook verification failedをlogに記録する場合、keyがendpointと一致していません。Templates not loading
Templates not loading
serverはworking directoryから
templates/base.htmlとtemplates/index.htmlをloadします。project rootからserverを起動するか、cmd/server/main.goのtemplate pathsを変更してください。Learn More
Go SDK
Go SDKの完全なdocumentation
Webhooks Documentation
すべてのwebhook eventsとbest practicesを学ぶ
Checkout Sessions
checkout session configurationを詳しく確認する
API Reference
Dodo Payments APIの完全なdocumentation
Support
boilerplateについてサポートが必要な場合:- Discord communityで質問します。
- GitHub repositoryでissuesとupdatesを確認します。
- support teamに連絡します。