跳转到主要内容
会话有效性:默认情况下,Checkout 会话有效期为 24 小时。如果您在请求中传递 confirm=true,则会话仅在 15 分钟内有效。

前提条件

1

Dodo Payments 账户

您需要一个具有 API 访问权限的有效 Dodo Payments 商户账户。
2

API 凭证

从 Dodo Payments 仪表板生成您的 API 凭证:
3

产品设置

在实现结账会话之前,请在 Dodo Payments 仪表板中创建您的产品。

创建您的第一个 Checkout 会话

import DodoPayments from 'dodopayments';

// Initialize the Dodo Payments client
const 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 route
app.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' });
  }
});

API 响应

上述所有方法返回相同的响应结构:
{
  "session_id": "cks_Gi6KGJ2zFJo9rq9Ukifwa",
  "checkout_url": "https://test.checkout.dodopayments.com/session/cks_Gi6KGJ2zFJo9rq9Ukifwa"
}
1

获取结账 URL

从 API 响应中提取 checkout_url
2

重定向您的客户

将您的客户引导到结账 URL 以完成购买。
// Redirect immediately
window.location.href = session.checkout_url;

// Or open in new window
window.open(session.checkout_url, '_blank');
替代集成选项:您可以使用 Overlay Checkout(模态覆盖)或 Inline Checkout(完全嵌入)直接在您的页面中嵌入结账,而不是重定向。两种选项都使用相同的结账会话 URL。
3

处理返回

付款后,客户将被重定向到您的 return_url,并附加付款状态的额外查询参数。

请求体

必填字段

每个结账会话所需的基本字段

可选字段

自定义您的结账体验的附加配置

必填字段

product_cart
array
必填
要包含在结账会话中的产品数组。每个产品必须具有来自 Dodo Payments 仪表板的有效 product_id
混合结账:您可以在同一结账会话中结合一次性付款产品和订阅产品。这使得强大的用例成为可能,例如与订阅相关的设置费用、与 SaaS 的硬件捆绑等。
查找您的产品 ID:您可以在 Dodo Payments 仪表板的产品 → 查看详细信息中找到产品 ID,或使用 列出产品 API

可选字段

配置这些字段以自定义结账体验并为您的支付流程添加业务逻辑。
customer
object
客户信息。您可以使用其 ID 附加现有客户,或在结账期间创建新客户记录。
使用其 ID 将现有客户附加到结账会话。
billing_address
object
账单地址信息,用于准确的税收计算、防欺诈和合规。
confirm 设置为 true 时,所有账单地址字段在成功创建会话时变为必填。
allowed_payment_method_types
array
控制在结账期间可用的支付方式。这有助于针对特定市场或业务需求进行优化。可用选项: credit, debit, upi_collect, apple_pay, google_pay, amazon_pay, klarna, affirm, afterpay_clearpay, cashapp, multibanco, bancontact_card, eps, ideal, przelewy24, paypal
重要:始终包括 creditdebit 作为后备选项,以防止在首选支付方式不可用时结账失败。
示例
["apple_pay", "google_pay", "credit", "debit"]
billing_currency
string
用固定的账单货币覆盖默认货币选择。使用 ISO 4217 货币代码。支持的货币USDEURGBPCADAUDINR,等等。示例"USD" 表示美元,"EUR" 表示欧元
此字段仅在启用自适应定价时有效。如果禁用自适应定价,将使用产品的默认货币。
show_saved_payment_methods
boolean
默认值:"false"
为回头客显示以前保存的支付方式,提高结账速度和用户体验。
return_url
string
成功付款或取消后重定向客户的 URL。
confirm
boolean
默认值:"false"
如果为 true,则立即完成所有会话详细信息。如果缺少必需数据,API 将抛出错误。
discount_code
string
将折扣代码应用于结账会话。
metadata
object
自定义键值对以存储有关会话的附加信息。
force_3ds
boolean
覆盖此会话的商户默认 3DS 行为。
minimal_address
boolean
默认值:"false"
启用最小地址收集模式。启用时,结账仅收集:
  • 国家:始终需要用于税收确定
  • 邮政编码:仅在需要销售税、增值税或商品及服务税计算的地区
这显著减少了结账摩擦,消除了不必要的表单字段。
启用最小地址以加快结账完成速度。对于需要完整账单详细信息的企业,仍然可以收集完整地址。
customization
object
自定义结账界面的外观和行为。
feature_flags
object
配置结账会话的特定功能和行为。
subscription_data
object
包含订阅产品的结账会话的附加配置。

使用示例

以下是 10 个全面的示例,展示了不同结账会话配置的各种业务场景:

1. 简单单产品结账

const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_ebook_guide',
      quantity: 1
    }
  ],
  customer: {
    email: 'customer@example.com',
    name: 'John Doe'
  },
  return_url: 'https://yoursite.com/success'
});

2. 多产品购物车

const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_laptop',
      quantity: 1
    },
    {
      product_id: 'prod_mouse',
      quantity: 2
    },
    {
      product_id: 'prod_warranty',
      quantity: 1
    }
  ],
  customer: {
    email: 'customer@example.com',
    name: 'John Doe',
    phone_number: '+1234567890'
  },
  billing_address: {
    street: '123 Tech Street',
    city: 'San Francisco',
    state: 'CA',
    country: 'US',
    zipcode: '94102'
  },
  return_url: 'https://electronics-store.com/order-confirmation'
});

3. 带试用期的订阅

const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_monthly_plan',
      quantity: 1
    }
  ],
  subscription_data: {
    trial_period_days: 14
  },
  customer: {
    email: 'user@startup.com',
    name: 'Jane Smith'
  },
  return_url: 'https://saas-app.com/onboarding',
  metadata: {
    plan_type: 'professional',
    referral_source: 'google_ads'
  }
});

4. 预确认结账

confirm 设置为 true 时,客户将直接进入结账页面,跳过任何确认步骤。
const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_premium_course',
      quantity: 1
    }
  ],
  customer: {
    email: 'student@university.edu',
    name: 'Alex Johnson',
    phone_number: '+1555123456'
  },
  billing_address: {
    street: '456 University Ave',
    city: 'Boston',
    state: 'MA',
    country: 'US',
    zipcode: '02134'
  },
  confirm: true,
  return_url: 'https://learning-platform.com/course-access',
  metadata: {
    course_category: 'programming',
    enrollment_date: '2024-01-15'
  }
});

5. 带货币覆盖的结账

billing_currency 覆盖仅在您的帐户设置中启用自适应货币时生效。如果禁用自适应货币,则此参数将无效。
const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_consulting_service',
      quantity: 1
    }
  ],
  customer: {
    email: 'client@company.co.uk',
    name: 'Oliver Williams'
  },
  billing_address: {
    street: '789 Business Park',
    city: 'London',
    state: 'England',
    country: 'GB',
    zipcode: 'SW1A 1AA'
  },
  billing_currency: 'GBP',
  feature_flags: {
    allow_currency_selection: true,
    allow_tax_id: true
  },
  return_url: 'https://consulting-firm.com/service-confirmation'
});

6. 为回头客提供保存的支付方式

const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_monthly_subscription',
      quantity: 1
    }
  ],
  customer: {
    email: 'returning.customer@example.com',
    name: 'Robert Johnson',
    phone_number: '+1555987654'
  },
  show_saved_payment_methods: true,
  feature_flags: {
    allow_phone_number_collection: true,
    always_create_new_customer: false
  },
  return_url: 'https://subscription-service.com/welcome-back',
  metadata: {
    customer_tier: 'premium',
    account_age: 'returning'
  }
});

7. B2B 结账与税号收集

const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_enterprise_license',
      quantity: 5
    }
  ],
  customer: {
    email: 'procurement@enterprise.com',
    name: 'Sarah Davis',
    phone_number: '+1800555000'
  },
  billing_address: {
    street: '1000 Corporate Blvd',
    city: 'New York',
    state: 'NY',
    country: 'US',
    zipcode: '10001'
  },
  feature_flags: {
    allow_tax_id: true,
    allow_phone_number_collection: true,
    always_create_new_customer: false
  },
  return_url: 'https://enterprise-software.com/license-delivery',
  metadata: {
    customer_type: 'enterprise',
    contract_id: 'ENT-2024-001',
    sales_rep: 'john.sales@company.com'
  }
});

8. 带折扣代码的深色主题结账

const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_gaming_chair',
      quantity: 1
    }
  ],
  discount_code: 'BLACKFRIDAY2024',
  customization: {
    theme: 'dark',
    show_order_details: true,
    show_on_demand_tag: false
  },
  feature_flags: {
    allow_discount_code: true
  },
  customer: {
    email: 'gamer@example.com',
    name: 'Mike Chen'
  },
  return_url: 'https://gaming-store.com/order-complete'
});

9. 区域支付方式(印度的 UPI)

有关UPI配置和测试的详细信息,请参见印度支付方式页面。
const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_online_course_hindi',
      quantity: 1
    }
  ],
  allowed_payment_method_types: [
    'upi_collect',
    'credit',
    'debit'
  ],
  customer: {
    email: 'student@example.in',
    name: 'Priya Sharma',
    phone_number: '+919876543210'
  },
  billing_address: {
    street: 'MG Road',
    city: 'Bangalore',
    state: 'Karnataka',
    country: 'IN',
    zipcode: '560001'
  },
  billing_currency: 'INR',
  return_url: 'https://education-platform.in/course-access',
  metadata: {
    region: 'south_india',
    language: 'hindi'
  }
});

10. BNPL(先买后付)结账

有关BNPL配置和测试的详细信息,请参见先买后付(BNPL)页面。
const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_luxury_watch',
      quantity: 1
    }
  ],
  allowed_payment_method_types: [
    'klarna',
    'afterpay_clearpay',
    'credit',
    'debit'
  ],
  customer: {
    email: 'fashion.lover@example.com',
    name: 'Emma Thompson',
    phone_number: '+1234567890'
  },
  billing_address: {
    street: '555 Fashion Ave',
    city: 'Los Angeles',
    state: 'CA',
    country: 'US',
    zipcode: '90210'
  },
  feature_flags: {
    allow_phone_number_collection: true
  },
  return_url: 'https://luxury-store.com/purchase-confirmation',
  metadata: {
    product_category: 'luxury',
    price_range: 'high_value'
  }
});

11. 使用现有支付方式进行即时结账

使用客户保存的支付方式创建一个立即处理的结账会话,跳过支付方式收集:
const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_premium_plan',
      quantity: 1
    }
  ],
  customer: {
    customer_id: 'cus_123'  // Required when using payment_method_id
  },
  payment_method_id: 'pm_abc123',  // Use customer's saved payment method
  confirm: true,  // Required when using payment_method_id
  return_url: 'https://yourapp.com/success'
});
使用payment_method_id时,confirm必须设置为true,并且必须提供现有的customer_id。该支付方式将进行资格验证以确认支付货币的合规性。
支付方式必须属于客户并与支付货币兼容。这使得对于回头客户而言,一键购买成为可能。

12. 更清晰支付URL的短链接

生成简化的、可分享的支付链接,并附带自定义短标识:
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 link
console.log(session.checkout_url);  // e.g., https://checkout.dodopayments.com/buy/abc123
短链接非常适合SMS、电子邮件或社交媒体分享。它们比长URL更容易记住,并能建立更多客户信任。

13. 跳过支付成功页面并立即重定向

在支付完成后立即重定向客户,绕过默认的成功页面:
const session = await client.checkoutSessions.create({
  product_cart: [
    {
      product_id: 'prod_digital_product',
      quantity: 1
    }
  ],
  feature_flags: {
    redirect_immediately: true  // Skip success page, redirect immediately
  },
  return_url: 'https://yourapp.com/success',
  customer: {
    email: 'customer@example.com',
    name: 'Jane Smith'
  }
});
当您拥有提供优于默认支付成功页面的自定义成功页面时,请使用redirect_immediately: true。这对于移动应用和嵌入式结账流程尤其有用。
当启用redirect_immediately时,客户在支付完成后将立即重定向到您的return_url,完全跳过默认的成功页面。

预览结账会话

在创建结账会话之前,您可以预览包括税费、折扣和总额的定价细目。这对于在客户继续结账之前显示准确的价格非常有用。
const preview = await client.checkoutSessions.preview({
  product_cart: [
    { product_id: 'prod_123', quantity: 1 }
  ],
  billing_address: {
    country: 'US',
    state: 'CA',
    zipcode: '94102'
  },
  discount_code: 'SAVE20'
});

console.log('Subtotal:', preview.subtotal);
console.log('Tax:', preview.tax);
console.log('Discount:', preview.discount);
console.log('Total:', preview.total);

预览API参考

查看完整的预览端点文档。

从动态链接迁移到结账会话

主要区别

以前,在使用动态链接创建支付链接时,您需要提供客户的完整账单地址。 使用结账会话,这已不再必要。您只需传递您拥有的任何信息,我们会处理其余的。例如:
  • 如果您只知道客户的账单国家,只需提供该信息。
  • 结账流程将自动收集缺失的详细信息,然后将客户转移到支付页面。
  • 另一方面,如果您已经拥有所有所需的信息并希望直接跳转到支付页面,您可以传递完整的数据集并在请求体中包含confirm=true

迁移过程

从动态链接迁移到结账会话非常简单:
1

更新您的集成

更新您的集成以使用新的API或SDK方法。
2

调整请求负载

根据结账会话格式调整请求负载。
3

就这样!

是的。您不需要额外的处理或特殊的迁移步骤。