import DodoPayments from 'dodopayments';// Initialize the Dodo Payments clientconst client = new DodoPayments({ bearerToken: process.env.DODO_PAYMENTS_API_KEY, environment: 'test_mode', // defaults to 'live_mode'});async function createCheckoutSession() { try { const session = await client.checkoutSessions.create({ // Products to sell - use IDs from your Dodo Payments dashboard product_cart: [ { product_id: 'prod_123', // Replace with your actual product ID quantity: 1 } ], // Pre-fill customer information to reduce friction customer: { email: '[email protected]', name: 'John Doe', phone_number: '+1234567890' }, // Billing address for tax calculation and compliance billing_address: { street: '123 Main St', city: 'San Francisco', state: 'CA', country: 'US', // Required: ISO 3166-1 alpha-2 country code zipcode: '94102' }, // Where to redirect after successful payment return_url: 'https://yoursite.com/checkout/success', // Custom data for your internal tracking metadata: { order_id: 'order_123', source: 'web_app' } }); // Redirect your customer to this URL to complete payment console.log('Checkout URL:', session.checkout_url); console.log('Session ID:', session.session_id); return session; } catch (error) { console.error('Failed to create checkout session:', error); throw error; }}// Example usage in an Express.js routeapp.post('/create-checkout', async (req, res) => { try { const session = await createCheckoutSession(); res.json({ checkout_url: session.checkout_url }); } catch (error) { res.status(500).json({ error: 'Failed to create checkout session' }); }});
نسخ
import osfrom dodopayments import DodoPayments# Initialize the Dodo Payments clientclient = DodoPayments( bearer_token=os.environ.get("DODO_PAYMENTS_API_KEY"), # This is the default and can be omitted environment="test_mode", # defaults to "live_mode")def create_checkout_session(): """ Create a checkout session for a single product with customer data pre-filled. Returns the session object containing checkout_url for customer redirection. """ try: session = client.checkout_sessions.create( # Products to sell - use IDs from your Dodo Payments dashboard product_cart=[ { "product_id": "prod_123", # Replace with your actual product ID "quantity": 1 } ], # Pre-fill customer information to reduce checkout friction customer={ "email": "[email protected]", "name": "John Doe", "phone_number": "+1234567890" }, # Billing address for tax calculation and compliance billing_address={ "street": "123 Main St", "city": "San Francisco", "state": "CA", "country": "US", # Required: ISO 3166-1 alpha-2 country code "zipcode": "94102" }, # Where to redirect after successful payment return_url="https://yoursite.com/checkout/success", # Custom data for your internal tracking metadata={ "order_id": "order_123", "source": "web_app" } ) # Redirect your customer to this URL to complete payment print(f"Checkout URL: {session.checkout_url}") print(f"Session ID: {session.session_id}") return session except Exception as error: print(f"Failed to create checkout session: {error}") raise error# Example usage in a Flask routefrom flask import Flask, jsonify, requestapp = Flask(__name__)@app.route('/create-checkout', methods=['POST'])def create_checkout(): try: session = create_checkout_session() return jsonify({"checkout_url": session.checkout_url}) except Exception as error: return jsonify({"error": "Failed to create checkout session"}), 500
نسخ
// Direct API call using fetch - useful for any JavaScript environmentasync function createCheckoutSession() { try { const response = await fetch('https://test.dodopayments.com/checkouts', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.DODO_PAYMENTS_API_KEY}` }, body: JSON.stringify({ // Products to sell - use IDs from your Dodo Payments dashboard product_cart: [ { product_id: 'prod_123', // Replace with your actual product ID quantity: 1 } ], // Pre-fill customer information to reduce checkout friction customer: { email: '[email protected]', name: 'John Doe', phone_number: '+1234567890' }, // Billing address for tax calculation and compliance billing_address: { street: '123 Main St', city: 'San Francisco', state: 'CA', country: 'US', // Required: ISO 3166-1 alpha-2 country code zipcode: '94102' }, // Where to redirect after successful payment return_url: 'https://yoursite.com/checkout/success', // Custom data for your internal tracking metadata: { order_id: 'order_123', source: 'web_app' } }) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const session = await response.json(); // Redirect your customer to this URL to complete payment console.log('Checkout URL:', session.checkout_url); console.log('Session ID:', session.session_id); return session; } catch (error) { console.error('Failed to create checkout session:', error); throw error; }}// Example: Redirect user to checkoutcreateCheckoutSession().then(session => { window.location.href = session.checkout_url;});
المبلغ الذي يدفعه العميل إذا تم تمكين pay_what_you_want. إذا تم تعطيله، سيتم تجاهل هذا الحقل.التنسيق: يتم تمثيله بأدنى فئة من العملة (مثل السنتات للدولار الأمريكي). على سبيل المثال، لتحصيل 1.00 دولار، مرر 100.
ابحث عن معرفات منتجاتك: يمكنك العثور على معرفات المنتجات في لوحة معلومات دودي للدفع تحت المنتجات → عرض التفاصيل، أو باستخدام API قائمة المنتجات.
رمز الدولة المكون من حرفين (ISO 3166-1 alpha-2). هذا الحقل مطلوب دائمًا عند تقديم عنوان الفواتير.أمثلة: "US" (الولايات المتحدة)، "CA" (كندا)، "GB" (المملكة المتحدة)، "DE" (ألمانيا)
تحكم في طرق الدفع المتاحة للعملاء أثناء الدفع. يساعد ذلك في تحسين الأسواق أو متطلبات الأعمال المحددة.الخيارات المتاحة: credit، debit، upi_collect، upi_intent، apple_pay، google_pay، amazon_pay، klarna، affirm، afterpay_clearpay، sepa، ach، cashapp، multibanco، bancontact_card، eps، ideal، przelewy24، paypal
حرج: تأكد دائمًا من تضمين credit و debit كخيارات احتياطية لمنع فشل الدفع عند عدم توفر طرق الدفع المفضلة.
سعر المنتج للشحنة الأولية للعميل. إذا لم يتم تحديده، سيتم استخدام السعر المخزن للمنتج.التنسيق: يتم تمثيله بأدنى فئة من العملة (مثل السنتات للدولار الأمريكي). على سبيل المثال، لتحصيل 1.00 دولار، مرر 100.
سابقًا، عند إنشاء رابط دفع باستخدام الروابط الديناميكية، كان يتعين عليك تقديم عنوان الفواتير الكامل للعميل.مع جلسات الدفع، لم يعد هذا ضروريًا. يمكنك ببساطة تمرير أي معلومات لديك، وسنتولى الباقي. على سبيل المثال:
إذا كنت تعرف فقط بلد الفواتير للعميل، فقط قدم ذلك.
ستجمع عملية الدفع تلقائيًا التفاصيل المفقودة قبل نقل العميل إلى صفحة الدفع.
من ناحية أخرى، إذا كان لديك بالفعل جميع المعلومات المطلوبة وترغب في تخطي مباشرة إلى صفحة الدفع، يمكنك تمرير مجموعة البيانات الكاملة وتضمين confirm=true في جسم طلبك.