条纹已处理,现在怎么办?

Stripe processed, now what?

我正在尝试构建一个无服务器 NuxtJS 应用程序,使用 firebase 进行身份验证,使用 netlify 进行部署(和功能)并使用 stripe 进行支付。

netlify 上的整个支付过程和无服务器功能对我来说都是全新的,所以这可能是一个菜鸟问题。

我遵循了多个文档和指南,并完成了一个具有 firebase 身份验证、netlify 部署和无服务器功能的应用程序,使我能够处理条纹支付 - 现在我只是想不出下一步。我的 stripe success_url 通向包含成功消息的 /pages/succes/index.js 路由 -> 虽然在这里我需要来自 Stripe 的一些数据响应,使我能够展示购买的商品并将产品 ID 作为"bought-product" firebase 中用户对象的条目(产品本质上是对用户配置文件的升级)。

点击"buy product"函数

async buyProduct(sku, qty) {
  const data = {
    sku: sku,
    quantity: qty,
  };

  const response = await fetch('/.netlify/functions/create-checkout', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(data),
  }).then((res) => res.json());

  console.log(response);

  const stripe = await loadStripe(response.publishableKey);
  const { error } = await stripe.redirectToCheckout({
    sessionId: response.sessionId,
  });

  if (error) {
    console.error(error);
  }
}

创建结帐 Netlify 函数

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const inventory = require('./data/products.json');

exports.handler = async (event) => {
  const { sku, quantity } = JSON.parse(event.body);
  const product = inventory.find((p) => p.sku === sku);
  const validatedQuantity = quantity > 0 && quantity < 11 ? quantity : 1;

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    billing_address_collection: 'auto',
    shipping_address_collection: {
      allowed_countries: ['US', 'CA'],
    },
    success_url: `${process.env.URL}/success`,
    cancel_url: process.env.URL,
    line_items: [
      {
        name: product.name,
        description: product.description,
        images: [product.image],
        amount: product.amount,
        currency: product.currency,
        quantity: validatedQuantity,
      },
    ],
  });

  return {
    statusCode: 200,
    body: JSON.stringify({
      sessionId: session.id,
      publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
    }),
  };
};

如果您需要更多信息,或者有什么不明白的地方,请告诉我!

tldr; 我已经使用 Netlify 功能在无服务器应用程序中处理了 Stripe 付款,并希望在成功页面上能够访问购买的商品和用户信息。

当您创建结帐会话并定义您的 success_url 时,您可以将 session_id={CHECKOUT_SESSION_ID} 作为模板附加,Stripe 将在其中自动填写会话 ID。参见 https://stripe.com/docs/payments/checkout/accept-a-payment#create-checkout-session

在你的情况下你会这样做:

success_url: `${process.env.URL}/success?session_id={CHECKOUT_SESION_ID}`,

然后当您的用户被重定向到您的成功时 URL 您可以使用会话 ID 检索会话并执行任何购买操作。