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

如何在支付成功后获取Stripe订单金额用于Google Ads动态转化

如何在Stripe支付成功后获取订单金额用于Google Ads动态转化

方案一:通过成功页URL传递金额(快速实现)

这种方式简单直接,适合前端直接触发转化的场景。

1. 修改后端代码,传递金额到成功页

在创建Stripe Checkout Session时,把订单总金额(Stripe返回的amount_total,单位为分)添加到success_url的查询参数里:

app.post('/create-checkout-session', async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    line_items: [
      {
        price: 'price_mypriceID',
        quantity: 1,
      },
    ],
    mode: 'payment',
    // 新增amount参数,传递订单总金额(单位:分)
    success_url: `${YOUR_DOMAIN}/dashboard?success=true&amount=${session.amount_total}`,
    cancel_url: `${YOUR_DOMAIN}/dashboard?canceled=true`,
  });

  res.json({url: session.url}) 
});

2. 前端解析URL参数,传入Google Ads转化代码

在成功页的前端代码中,从URL里提取amount参数,转换为美元金额(除以100),再填入Google Ads的转化事件:

// 解析URL查询参数
const urlParams = new URLSearchParams(window.location.search);
const isSuccess = urlParams.get('success') === 'true';
const rawAmount = urlParams.get('amount');

if (isSuccess && rawAmount) {
  // 转换为美元金额(Stripe的amount_total是以分为单位的)
  const orderValue = parseFloat(rawAmount) / 100;
  
  window.gtag('config', 'AW-myaccount');
  window.gtag('event', 'conversion', {
    'send_to': 'AW-myaccount',
    'value': orderValue,
    'currency': 'USD'
  });
}

方案二:通过Stripe Webhook获取金额(更安全可靠)

如果担心前端URL参数被篡改,或者需要服务器端验证订单有效性,可以用Stripe Webhook:

1. 配置Stripe Webhook

在Stripe后台配置checkout.session.completed事件的Webhook端点,比如你的后端/stripe-webhook。

2. 后端接收Webhook,存储订单金额

app.post('/stripe-webhook', express.raw({type: 'application/json'}), async (req, res) => {
  const sig = req.headers['stripe-signature'];
  let event;

  try {
    event = stripe.webhooks.constructEvent(req.body, sig, process.env.STRIPE_WEBHOOK_SECRET);
  } catch (err) {
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  // 处理checkout.session.completed事件
  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;
    // 获取订单金额(单位:分)
    const orderAmount = session.amount_total;
    // 把金额和订单ID关联存储到数据库,生成带订单ID的成功页URL
    // success_url示例:`${YOUR_DOMAIN}/dashboard?success=true&order_id=${session.id}`
  }

  res.json({received: true});
});

3. 前端通过订单ID获取金额

前端成功页拿到order_id后,调用后端接口获取对应的订单金额,再传入Google Ads转化代码:

const urlParams = new URLSearchParams(window.location.search);
const isSuccess = urlParams.get('success') === 'true';
const orderId = urlParams.get('order_id');

if (isSuccess && orderId) {
  // 调用后端接口获取订单金额
  fetch(`/get-order-amount?order_id=${orderId}`)
    .then(res => res.json())
    .then(data => {
      const orderValue = data.amount / 100;
      window.gtag('config', 'AW-myaccount');
      window.gtag('event', 'conversion', {
        'send_to': 'AW-myaccount',
        'value': orderValue,
        'currency': 'USD'
      });
    });
}

注意事项

  • Stripe的amount_total是以分为单位的,需除以100得到实际货币金额(比如1000分 = 10美元)
  • 方案一可对URL参数做简单校验,避免恶意传入无效值
  • 方案二更适合需要严格验证订单有效性的场景,防止虚假转化上报

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:37:43