अवलोकन
Better Auth adaptor,@dodopayments/better-auth, एक Better Auth plugin है जो आपके users को Dodo Payments से जोड़ता है। यह सुविधाएँ प्रदान करता है:
- साइन-अप पर वैकल्पिक customer creation या email-आधारित customer linking
- product slug mapping के साथ checkout sessions, पसंदीदा checkout method
- Self-service Customer Portal
- usage-based billing के लिए usage ingestion और reporting endpoints
- signature verification के साथ webhook event processing
- हर endpoint के लिए TypeScript types
इस एकीकरण का उपयोग करने के लिए आपको Dodo Payments खाता और API कुंजियाँ चाहिए।
पूर्वापेक्षाएँ
- Node.js 16 या बाद का संस्करण
- अपने Dodo Payments dashboard तक पहुँच
- Better Auth 1.4 या बाद की 1.x release का उपयोग करने वाला मौजूदा project
स्थापना
1
Install Dependencies
अपने project root में यह command चलाएँ:
Adaptor, Dodo Payments SDK, Better Auth और Zod install हो गए हैं।
सेटअप
1
Configure Environment Variables
इन variables को अपनी
.env file में जोड़ें। Dashboard में Developer → API Keys के अंतर्गत API key बनाएँ। Webhook endpoint जोड़ते समय webhook secret मिलता है, जैसा कि इस page के Webhooks section में बताया गया है। BETTER_AUTH_SECRET कम-से-कम 32 characters वाली random string है।2
Set Up Server-Side Integration
src/lib/auth.ts बनाएँ या update करें:user table में dodoCustomerId field जोड़ता है, जिसमें प्रत्येक user की Dodo Payments customer ID store होती है। Plugin जोड़ने के बाद Better Auth CLI से अपना database schema update करें।3
Set Up Client-Side Integration
src/lib/auth-client.ts बनाएँ या update करें:Usage Examples
नई integrations के लिए
authClient.dodopayments.checkoutSession का उपयोग करें।
Legacy checkout method deprecated है और केवल backward
compatibility के लिए रखा गया है।Checkout Session बनाना (Preferred)
Configured slug या product cart से checkout session बनाएँ, फिर customer को लौटाए गए URL पर redirect करें:checkoutSession आपके लिए कुछ fields भर देता है:
- Billing address: पहले से आवश्यक नहीं है, क्योंकि checkout इसे customer से collect करता है। इसे पहले से भरने के लिए
billing_addresspass करें। - Customer: Signed-in user के लिए plugin उनके Better Auth session से email और name का उपयोग करता है और आपके द्वारा pass किए गए किसी भी
customerobject को अनदेखा करता है। Signed-in user न होने पर यहcustomerobject का उपयोग करता है। - Other fields: Argument Create Checkout Session endpoint के request body वाले समान fields स्वीकार करता है, साथ ही
slugऔरreferenceIdभी।
slug और न ही product_cart pass करते हैं, तो request 400 error के साथ विफल हो जाती है।
Return URL server plugin में configured
successUrl से आता है,
और आपके app के URL के आधार पर resolve होता है। Plugin client payload में मौजूद किसी भी
return_url को अनदेखा करता है।Legacy Checkout (Deprecated)
Legacy method के लिएbilling और customer आवश्यक हैं, और यह deprecated dynamic checkout flow के माध्यम से payment link बनाता है। customer में set किए गए fields session से प्राप्त email और name को override करते हैं।
Customer Portal तक पहुँचना
Portal endpoints के लिए verified email address वाले signed-in user की आवश्यकता होती है। यदि user के पास अभी Dodo Payments customer नहीं है, तो plugin email से customer खोजता है या उसे बनाता है।customer.portal() portal URL लौटाता है:
Customer Data की सूची बनाना
Signed-in customer की subscriptions और payments की सूची बनाएँ।page 1 से शुरू होता है, और status results को filter करता है:
Metered Usage को Track करना
Usage-based billing के लिए usage events रिकॉर्ड करने और customers को अपना usage दिखाने हेतु server परusage() plugin enable करें। दोनों methods के लिए verified email address वाले signed-in user की आवश्यकता होती है।
authClient.dodopayments.usage.ingestsigned-in user के लिए एक event रिकॉर्ड करता है।authClient.dodopayments.usage.meters.listsigned-in customer के usage events की सूची बनाता है। यहpage_number,page_size,event_name,meter_id,startऔरendquery parameters स्वीकार करता है।
meter_id omit करते हैं, तो list में customer के सभी usage events शामिल होते हैं। meter_id के साथ केवल उस meter से match करने वाले events शामिल होते हैं।
Webhooks
Webhooks plugin प्रत्येक Dodo Payments event के signature को verify करता है और
आपके handlers को call करता है। Default endpoint
/api/auth/dodopayments/webhooks है।1
Generate and Set Webhook Secret
Dashboard में Developer → Webhooks पर जाएँ और अपना endpoint URL जोड़ें, उदाहरण के लिए
https://<your-domain>/api/auth/dodopayments/webhooks। Endpoint का signing secret अपनी .env file में copy करें:2
Handle Webhook Events
हर उस event के लिए handler pass करें जिसे आप process करना चाहते हैं।
onPayload प्रत्येक event के लिए चलता है:{ received: true } लौटाता है।
Supported Webhook Event Handlers
प्रत्येक handler अपने event type के लिए verified payload प्राप्त करता है:Configuration Reference
Plugin Options
Plugin Options
- client (required): DodoPayments client instance
- createCustomerOnSignUp (optional): User के sign up करने पर Dodo Payments customer बनाएँ, या समान email वाले मौजूदा customer को link करें। User की details बदलने पर plugin customer को भी update करता है।
- use (required): Enable करने वाले plugins की array (checkout, portal, usage, webhooks)
- getCustomerParams (optional): ऐसा function जो Better Auth
Userप्राप्त करता है और creation तथा update के समय Dodo Payments customer से जोड़ने के लिए अतिरिक्त fields लौटाता है (जैसेmetadata,phone_number)। यह async हो सकता है।
Checkout Plugin Options
Checkout Plugin Options
- products:
{ productId, slug }objects की array, या उन्हें लौटाने वाला async function - successUrl: सफल payment के बाद redirect करने के लिए URL
- authenticatedUsersOnly: User authentication आवश्यक करें (default:
false)
Troubleshooting & Tips
Common Issues
Common Issues
- Invalid API key:
.envमेंDODO_PAYMENTS_API_KEYजाँचें और सुनिश्चित करें कि key का modeenvironmentसे match करता है। - Webhook signature mismatch: जाँचें कि webhook secret Dodo Payments dashboard में set किए गए secret से match करता है।
- Customer not created: जाँचें कि
createCustomerOnSignUpकोtrueपर set किया गया है। - Portal or usage requests return 401: User का email address verified नहीं है।
Best Practices
Best Practices
- सभी secrets और keys के लिए environment variables का उपयोग करें।
live_modeपर switch करने से पहलेtest_modeमें test करें।- Debugging और auditing के लिए webhook events log करें।