You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node.js中基于收货地址动态设置Stripe配送费用

基于Stripe Checkout收货地址国家动态配置配送选项及费用

完全可以实现。Stripe Checkout支持为配送费率添加条件规则,根据用户选择的收货国家自动展示匹配的配送选项及对应费用。

你需要在定义配送费率时,通过shipping_rate_data里的金额、展示名称,搭配关键的condition字段来指定适用国家。针对你的需求(意大利本地免费配送,法国提供10欧元普通配送+20欧元加急配送),修改后的代码示例如下:

const session = await stripe.checkout.sessions.create({
    success_url: "http://localhost:4242/success.html?id={CHECKOUT_SESSION_ID}",
    cancel_url: "http://localhost:4242/cancel.html",
    payment_method_types: ["card"],
    mode: "payment",
    line_items: [
      {
        price: "price_1Nxx6ZDsK4pxN0rvXLjRy7bJ",
        quantity: req.body.quantity,
      },
    ],
    customer: "cus_Ol9Y0V0GCXTmSF",
    billing_address_collection: "required",
    shipping_address_collection: {
      allowed_countries: [
        "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", 
        "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", 
        "PL", "PT", "RO", "SK", "SI", "ES", "SE"
      ],
    },
    shipping_options: [
      // 意大利本地免费配送
      {
        shipping_rate_data: {
          type: 'fixed_amount',
          fixed_amount: {
            amount: 0,
            currency: 'eur',
          },
          display_name: '本地免费配送',
          delivery_estimate: {
            minimum: { unit: 'business_day', value: 2 },
            maximum: { unit: 'business_day', value: 3 },
          },
          condition: {
            country: 'IT',
          }
        },
      },
      // 法国普通配送(10欧元)
      {
        shipping_rate_data: {
          type: 'fixed_amount',
          fixed_amount: {
            amount: 1000, // 单位为分,10欧元=1000欧1在 Read复制结果风:帮补AGPostage,10欧元=1000欧分
            currency: 'eur',
          },
          display_name: '普通配送',
          delivery_estimate: {
            minimum: { unit: 'business_day', value: 5 },
            maximum: { unit: 'business_day', value: 7 },
          },
          condition: {
            country: 'FR',
          }
        },
      },
      // 法国加急配送(20欧元)
      {
        shipping_rate_data: {
          type: 'fixed_amount',
          fixed_amount: {
            amount: 2000, // 20欧元=2000欧分
            currency: 'eur',
          },
          display_name: '加急配送',
          delivery_estimate: {
            minimum: { unit: 'business_day', value: 2 },
            maximum: { unit: 'business_day', value: 3 },
          },
          condition: {
            country: 'FR',
          }
        },
      },
      // 可选:其他欧盟国家的统一配送选项
      {
        shipping_rate_data: {
          type: 'fixed_amount',
          fixed_amount: {
            amount: 1500,
            currency: 'eur',
          },
          display_name: '标准跨境配送',
          delivery_estimate: {
            minimum: { unit: 'business_day', value: 7 },
            maximum: { unit: 'business_day', value: 10 },
          },
          condition: {
            country: {
              in: ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", 
                   "DE", "GR", "HU", "IE", "LV", "LT", "LU", "MT", "NL", 
                   "PL", "PT", "RO", "SK", "SI", "ES", "SE"]
            }
          }
        },
      },
    ],
    customer_update: { shipping: "auto" },
    automatic_tax: { enabled: true },
    allow_promotion_codes: true,
});

关键说明

  • condition.country:指定配送费率适用的国家,支持单个国家代码(如'IT')或通过in操作符匹配多个国家数组。
  • 金额单位:Stripe API以最小货币单位计算金额,欧元下10欧元对应数值1000。
  • 自动展示逻辑:用户选择收货国家后,Stripe会自动过滤出符合条件的配送选项,仅展示对应国家的可用方式。

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:57:12