Skip to main content

अवलोकन

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 है।
API कुंजियों या सीक्रेट्स को वर्शन कंट्रोल में कभी कमिट न करें।
2

Set Up Server-Side Integration

src/lib/auth.ts बनाएँ या update करें:
Plugin Better Auth की user table में dodoCustomerId field जोड़ता है, जिसमें प्रत्येक user की Dodo Payments customer ID store होती है। Plugin जोड़ने के बाद Better Auth CLI से अपना database schema update करें।
Production के लिए environment को live_mode पर set करें।
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_address pass करें।
  • Customer: Signed-in user के लिए plugin उनके Better Auth session से email और name का उपयोग करता है और आपके द्वारा pass किए गए किसी भी customer object को अनदेखा करता है। Signed-in user न होने पर यह customer object का उपयोग करता है।
  • Other fields: Argument Create Checkout Session endpoint के request body वाले समान fields स्वीकार करता है, साथ ही slug और referenceId भी।
यदि slug configured नहीं है, या आप न तो slug और न ही product_cart pass करते हैं, तो request 400 error के साथ विफल हो जाती है।
Return URL server plugin में configured successUrl से आता है, और आपके app के URL के आधार पर resolve होता है। Plugin client payload में मौजूद किसी भी return_url को अनदेखा करता है।

Legacy Checkout (Deprecated)

authClient.dodopayments.checkout method deprecated है। नई implementations के लिए इसके बजाय checkoutSession का उपयोग करें।
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.ingest signed-in user के लिए एक event रिकॉर्ड करता है।
  • authClient.dodopayments.usage.meters.list signed-in customer के usage events की सूची बनाता है। यह page_number, page_size, event_name, meter_id, start और end query parameters स्वीकार करता है।
Dodo Payments ऐसे events को reject करता है जिनके timestamps एक घंटे से अधिक पुराने या पाँच मिनट से अधिक भविष्य के हों।
यदि आप 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 के लिए चलता है:
यदि signature verification विफल होती है या कोई handler error throw करता है, तो endpoint 400 के साथ respond करता है। आपके handlers के समाप्त होने के बाद यह { received: true } लौटाता है।

Supported Webhook Event Handlers

प्रत्येक handler अपने event type के लिए verified payload प्राप्त करता है:

Configuration Reference

  • 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 हो सकता है।
  • products: { productId, slug } objects की array, या उन्हें लौटाने वाला async function
  • successUrl: सफल payment के बाद redirect करने के लिए URL
  • authenticatedUsersOnly: User authentication आवश्यक करें (default: false)

Troubleshooting & Tips

  • Invalid API key: .env में DODO_PAYMENTS_API_KEY जाँचें और सुनिश्चित करें कि key का mode environment से 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 नहीं है।
  • सभी secrets और keys के लिए environment variables का उपयोग करें।
  • live_mode पर switch करने से पहले test_mode में test करें।
  • Debugging और auditing के लिए webhook events log करें।

LLMs के लिए Prompt

अपने project में adaptor जोड़ने के लिए इस prompt को अपने AI coding assistant में copy करें। अपने agent को Dodo Payments docs और skills देने के लिए Agent Plugin भी install करें।
अंतिम संशोधन 26 सितंबर 2026