实施 Stripe 时,如何使用 Express(或其他方法)重定向?

When implementing Stripe, how do I redirect using Express (or other method)?

我正在尝试使用 this tutorial 来集成 Stripe。

我的代码可以正常工作,但我现在想将用户重定向到 Stripe 返回的 URL。

这是 Stripe 提供的原始未修改 node.js 代码:

// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(SECRET_KEY_GOES_HERE);

// The price ID passed from the client
//   const {priceId} = req.body;
const priceId = '{{PRICE_ID}}';

const session = await stripe.checkout.sessions.create({
  mode: 'subscription',
  payment_method_types: ['card'],
  line_items: [
    {
      price: priceId,
      // For metered billing, do not pass quantity
      quantity: 1,
    },
  ],
  // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
  // the actual Session ID is returned in the query parameter when your customer
  // is redirected to the success page.
  success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/canceled.html',
});

// Redirect to the URL returned on the Checkout Session.
// With express, you can redirect with:
//   res.redirect(303, session.url);

底部说我可以使用 express 重定向到返回的 URL。

为了实现这一点,我尝试在顶部要求 express,然后取消对最后一行的注释。但是res没有定义,所以不行。

我是 node.jsexpress 的新手,所以我不确定如何进行设置。

这是我修改后的代码,由于未定义 res,所以无法正常工作:

const app = require('express')();
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(SECRET_KEY_GOES_HERE);

// The price ID passed from the client
//   const {priceId} = req.body;
const priceId = '{{PRICE_ID}}';

const session = await stripe.checkout.sessions.create({
  mode: 'subscription',
  payment_method_types: ['card'],
  line_items: [
    {
      price: priceId,
      // For metered billing, do not pass quantity
      quantity: 1,
    },
  ],
  // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
  // the actual Session ID is returned in the query parameter when your customer
  // is redirected to the success page.
  success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/canceled.html',
});

// Redirect to the URL returned on the Checkout Session.
// With express, you can redirect with:
res.redirect(303, session.url);

我如何让它工作?

如果您有其他有效的方法,则不必使用重定向 express

res 未定义的原因是因为它是传递到路由的回调中的参数。

这是来自 docs 的基本示例:

app.get('/', function (req, res) {
  res.send('Hello World!')
})

这里发生的事情是,当客户端请求主页时,他们将得到响应 'Hello World'。

您需要执行以下操作:

const express = require('express')
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(SECRET_KEY_GOES_HERE);
const app = express()
const port = 3000

app.get('/<the path your client will call to run stripe>', (req, res) => {
  
// The price ID passed from the client
//   const {priceId} = req.body;
const priceId = '{{PRICE_ID}}';

const session = await stripe.checkout.sessions.create({
  mode: 'subscription',
  payment_method_types: ['card'],
  line_items: [
    {
      price: priceId,
      // For metered billing, do not pass quantity
      quantity: 1,
    },
  ],
  // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
  // the actual Session ID is returned in the query parameter when your customer
  // is redirected to the success page.
  success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/canceled.html',
});

// Redirect to the URL returned on the Checkout Session.
// With express, you can redirect with:
res.redirect(303, session.url);
})

你看到回调中的参数 reqres 了吗?一个是请求,另一个是响应。如果您控制台记录该请求,您将看到对该路径发出的请求。

另外,请注意已调用 express 并将其分配给 app 变量,而您是在文件顶部直接调用它。

查看 Express 文档并阅读 Getting Started 指南可能会对您有所帮助 here