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: 'customer@example.com', 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": "customer@example.com", "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: 'customer@example.com', 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;});
وجه عميلك إلى عنوان URL للدفع لإكمال عملية الشراء.
نسخ
// Redirect immediatelywindow.location.href = session.checkout_url;// Or open in new windowwindow.open(session.checkout_url, '_blank');
خيارات تكامل بديلة: بدلاً من إعادة التوجيه، يمكنك تضمين الدفع مباشرة في صفحتك باستخدام الدفع عبر Overlay (تراكب نافذة) أو الدفع المضمن (مضمن بالكامل). كلا الخيارين يستخدمان نفس عنوان URL لجلسة الدفع.
3
التعامل مع العودة
بعد الدفع، يتم إعادة توجيه العملاء إلى return_url الخاص بك مع معلمات استعلام إضافية لحالة الدفع.
مصفوفة من المنتجات التي يجب تضمينها في جلسة الدفع. يجب أن تحتوي كل منتج على product_id صالح من لوحة معلومات مدفوعات دودو الخاصة بك.
دفع مختلط: يمكنك دمج منتجات الدفع لمرة واحدة ومنتجات الاشتراك في نفس جلسة الدفع. هذا يمكّن حالات استخدام قوية مثل رسوم الإعداد مع الاشتراكات، وحزم الأجهزة مع SaaS، والمزيد.
المبلغ الذي يدفعه العميل إذا تم تمكين pay_what_you_want. إذا تم تعطيله، سيتم تجاهل هذا الحقل.التنسيق: يتم تمثيله بأدنى فئة من العملة (مثل السنتات للدولار الأمريكي). على سبيل المثال، لتحصيل 1.00 دولار، مرر 100.
ابحث عن معرفات منتجاتك: يمكنك العثور على معرفات المنتجات في لوحة معلومات مدفوعات دودو تحت المنتجات → عرض التفاصيل، أو باستخدام API قائمة المنتجات.
رمز الدولة المكون من حرفين (ISO 3166-1 alpha-2). هذا الحقل مطلوب دائمًا عند تقديم عنوان الفواتير.أمثلة: "US" (الولايات المتحدة)، "CA" (كندا)، "GB" (المملكة المتحدة)، "DE" (ألمانيا)
تحكم في طرق الدفع المتاحة للعملاء أثناء الدفع. يساعد ذلك في تحسين الأسواق أو متطلبات الأعمال المحددة.الخيارات المتاحة: credit, debit, upi_collect, apple_pay, google_pay, amazon_pay, klarna, affirm, afterpay_clearpay, cashapp, multibanco, bancontact_card, eps, ideal, przelewy24, paypal
حرج: تأكد دائمًا من تضمين credit وdebit كخيارات احتياطية لمنع فشل الدفع عند عدم توفر طرق الدفع المفضلة.
سعر المنتج للشحنة الأولية للعميل. إذا لم يتم تحديده، سيتم استخدام السعر المخزن للمنتج.التنسيق: يتم تمثيله بأدنى فئة من العملة (مثل السنتات للدولار الأمريكي). على سبيل المثال، لتحصيل 1.00 دولار، مرر 100.
قم بإنشاء روابط دفع مختصرة وقابلة للمشاركة مع شفرات مخصصة:
نسخ
const session = await client.checkoutSessions.create({ product_cart: [ { product_id: 'prod_monthly_subscription', quantity: 1 } ], short_link: true, // Generate a shortened payment link return_url: 'https://yourapp.com/success', customer: { email: 'customer@example.com', name: 'John Doe' }});// The checkout_url will be a shortened, cleaner linkconsole.log(session.checkout_url); // e.g., https://checkout.dodopayments.com/buy/abc123
تعد الروابط القصيرة مثالية للمشاركة عبر SMS أو البريد الإلكتروني أو وسائل التواصل الاجتماعي. فهي أسهل في التذكر وتبني ثقة أكبر لدى العملاء مقارنةً بالروابط الطويلة.
استخدم redirect_immediately: true عندما يكون لديك صفحة نجاح مخصصة توفر تجربة مستخدم أفضل مقارنة بصفحة النجاح الافتراضية للدفع. هذا مفيد بشكل خاص لتطبيقات الجوال وعمليات الدفع المدمجة.
عند تمكين redirect_immediately، يتم إعادة توجيه العملاء إلى return_url الخاصة بك على الفور بعد إتمام الدفع، متجاوزين صفحة النجاح الافتراضية تمامًا.
سابقًا، عند إنشاء رابط دفع باستخدام الروابط الديناميكية، كان يُطلب منك تقديم عنوان الفوترة الكامل للعميل.مع جلسات الدفع، لم يعد هذا ضروريًا. يمكنك ببساطة تمرير أي معلومات لديك، وسنتولى الباقي. على سبيل المثال:
إذا كنت تعرف فقط بلد الفوترة للعميل، قدم ذلك فقط.
ستجمع عملية الدفع تلقائيًا التفاصيل المفقودة قبل نقل العميل إلى صفحة الدفع.
من ناحية أخرى، إذا كان لديك جميع المعلومات المطلوبة وترغب في الانتقال مباشرةً إلى صفحة الدفع، يمكنك تمرير مجموعة البيانات الكاملة وإدراج confirm=true في جسم الطلب الخاص بك.