@dodopayments/fastify adaptor आपके Fastify ऐप को तीन route handlers देता है: Checkout checkout URLs लौटाता है, CustomerPortal ग्राहक को Customer Portal पर भेजता है, और Webhooks webhook requests को सत्यापित करके आपके event handlers को कॉल करता है।
Checkout Handler
अपने Fastify ऐप से payment links और checkout sessions बनाएँ।
Customer Portal
ग्राहकों को अपनी subscriptions और विवरण प्रबंधित करने दें।
Webhooks
Dodo Payments webhook events को सत्यापित और process करें।
Installation
1
Install the Package
अपने project root में निम्न command चलाएँ:इस package के लिए Fastify 5.4.0 या बाद का version आवश्यक है।
2
Set Up Environment Variables
अपने project root में एक Developer → API Keys के अंतर्गत API key बनाएँ। Developer → Webhooks के अंतर्गत अपना webhook endpoint जोड़ें और उसका signing secret
.env file बनाएँ:DODO_PAYMENTS_WEBHOOK_KEY में copy करें। Build करते समय, DODO_PAYMENTS_ENVIRONMENT=test_mode वाली test mode API key का उपयोग करें, क्योंकि test mode key केवल test mode पर काम करती है। DODO_PAYMENTS_RETURN_URL वैकल्पिक है।Route Handler Examples
इन examples में
Fastify() से बनाए गए Fastify instance पर routes register किए गए हैं। Webhook route को raw request body चाहिए, इसलिए इसके example में केवल webhook route वाले plugin के अंदर string body parser जोड़ा गया है।- Checkout Handler
- Customer Portal Handler
- Webhook Handler
Dodo Payments checkout को अपने Fastify ऐप में integrate करने के लिए इस handler का उपयोग करें। यह static (GET), dynamic (POST) और session (POST) payment flows को support करता है।
Checkout() static flow के लिए एक getHandler और dynamic तथा session flows के लिए एक postHandler लौटाता है। प्रत्येक POST flow को अपने अलग path पर register करें।Checkout Route Handler
Adaptor सभी तीन Dodo Payments checkout flows को support करता है। Route द्वारा serve किए जाने वाले flow को चुनने के लिए handler config में
type सेट करें। प्रत्येक flow JSON response देता है जिसमें ग्राहक के खोलने के लिए एक checkout_url होता है।- Static Payment Links:
type: "static", GET। query parameters से एक product के लिए payment link बनाता है, पहले यह जाँचकर कि product मौजूद है। - Dynamic Payment Links:
type: "dynamic", POST। Product recurring है या नहीं, इसके आधार पर payment link के साथ one-time payment या subscription बनाता है। - Checkout Sessions:
type: "session", POST। Product cart और customer details से checkout session बनाता है। नए integrations के लिए इस flow का उपयोग करें।
Checkout ये options लेता है:
Checkout दो handlers वाला object लौटाता है। जब type, static हो, तब GET के लिए getHandler register करें; और जब type, dynamic या session हो, तब POST के लिए postHandler register करें।
Static Checkout (GET)
Static Checkout (GET)
Supported Query Parameters
string
आवश्यक
Product identifier, उदाहरण के लिए
?productId=pdt_nZuwz45WAs64n3l07zpQR।integer
डिफ़ॉल्ट:"1"
Product की quantity।
string
Customer का पूरा नाम। यदि
firstName या lastName दिया गया हो, तो इसे अनदेखा किया जाता है।string
Customer का first name।
string
Customer का last name।
string
Customer का email address।
string
Customer का देश, ISO 3166-1 alpha-2 code के रूप में।
string
Customer का street address।
string
Customer का city।
string
Customer का state या province।
string
Customer का postal या ZIP code।
boolean
Full name field को disable करने के लिए
true पर सेट करें।boolean
First name field को disable करने के लिए
true पर सेट करें।boolean
Last name field को disable करने के लिए
true पर सेट करें।boolean
Email field को disable करने के लिए
true पर सेट करें।boolean
Country field को disable करने के लिए
true पर सेट करें।boolean
Address line field को disable करने के लिए
true पर सेट करें।boolean
City field को disable करने के लिए
true पर सेट करें।boolean
State field को disable करने के लिए
true पर सेट करें।boolean
ZIP code field को disable करने के लिए
true पर सेट करें।string
Payment currency, उदाहरण के लिए
USD।boolean
डिफ़ॉल्ट:"true"
Currency selector दिखाएँ या छिपाएँ।
number
Major currency units में charged amount तय करता है, उदाहरण के लिए $12.50 के लिए
12.5। केवल Pay What You Want products के साथ काम करता है और product की minimum price से कम होने पर अनदेखा किया जाता है।boolean
डिफ़ॉल्ट:"true"
Discounts section दिखाएँ या छिपाएँ।
string
metadata_ से शुरू होने वाला कोई भी query parameter checkout में metadata के रूप में पास किया जाता है, उदाहरण के लिए metadata_orderId=123।true हो और matching field में कोई value हो, उदाहरण के लिए disableEmail के साथ email। Handler इन parameters को static payment link में पास करता है।Response Format
Static checkout checkout URL के साथ JSON response लौटाता है:Dynamic Checkout (POST)
Dynamic Checkout (POST)
- Parameters को POST request में JSON body के रूप में भेजें।
- One-time और recurring दोनों payments को support करता है। Handler product retrieve करता है, फिर product recurring होने पर subscription और अन्यथा one-time payment बनाता है।
- Body में
billing(जिसमेंstreet,city,state,countryऔरzipcodeहों) औरcustomerआवश्यक हैं, साथ मेंproduct_id(वैकल्पिकquantityके साथ) याproduct_cartभी आवश्यक है। Subscriptions के लिएproduct_idआवश्यक है। - Handler
metadata,allowed_payment_method_types,billing_currency,discount_codes(या deprecateddiscount_code),return_url,show_saved_payment_methodsऔरtax_idको भी forward करता है। Subscriptions के लिए यहaddons,on_demandऔरtrial_period_daysको भी forward करता है। अन्य fields को अनदेखा करता है। - Field details के लिए देखें:
Response Format
Dynamic checkout payment link को checkout URL के रूप में JSON response लौटाता है:Checkout Sessions (POST)
Checkout Sessions (POST)
Checkout session payload को JSON body के रूप में भेजें। Handler एक checkout session बनाता है, जो one-time purchases और subscriptions के लिए पूरा payment flow संभालता है और अपना
checkout_url लौटाता है। product_cart आवश्यक है और इसमें कम-से-कम एक product होना चाहिए।प्रत्येक checkout_url एक बार काम करता है और 24 घंटे बाद expire हो जाता है, या confirm: true पास करने पर 15 मिनट बाद। payment_method_id के साथ बनाए गए session में कोई checkout_url नहीं होता, इसलिए handler 400 लौटाता है।अधिक विवरण और supported fields की पूरी सूची के लिए Checkout Sessions Integration Guide देखें।Response Format
Checkout sessions checkout URL के साथ JSON response लौटाते हैं:Customer Portal Route Handler
Customer Portal Route Handlercustomer_id में customer के लिए Customer Portal session बनाता है और request को portal link पर redirect करता है। CustomerPortal, Checkout की तरह ही bearerToken और environment options लेता है। यदि Dodo Payments session नहीं बना पाता, तो handler 500 लौटाता है।
Query Parameters
string
आवश्यक
Portal session के लिए customer ID, उदाहरण के लिए
?customer_id=cus_123।boolean
यदि
true पर set किया गया है, तो customer को portal link वाला email भेजता है।Webhook Route Handler
Webhook handler आपके webhook secret से प्रत्येक request को verify करता है, जिसेwebhookKey के रूप में pass किया जाता है, फिर आपके event handlers को call करता है।
- Method: केवल POST requests supported हैं। अन्य methods 405 लौटाते हैं।
- Signature Verification: Standard Webhooks specification का पालन करते हुए
webhookKeyसेwebhook-id,webhook-timestampऔरwebhook-signatureheaders को verify करता है। Verification विफल होने पर 401 लौटाता है। - Payload Validation: Zod से validated। Invalid payloads के लिए 400 लौटाता है।
- Error Handling:
- 401: Invalid signature
- 400: Invalid payload
- 500: Verification के दौरान internal error
- Event Routing: प्रत्येक event के लिए
onPayloadको call करता है, फिर event के type के handler को call करता है और उनके पूरा होने पर 200 लौटाता है। Handler आपके event handlers द्वारा throw की गई errors को catch नहीं करता।