【问题标题】:net::ERR_CONNECTION_REFUSED error when trying to create a new Stripe subscription checkout session尝试创建新的 Stripe 订阅结帐会话时出现 net::ERR_CONNECTION_REFUSED 错误
【发布时间】:2020-11-28 14:56:10
【问题描述】:

我正在尝试模仿 Stripe 在他们的 Github 中的内容,以通过我的网站创建一个新的订阅会话。当我单击触发事件处理程序的按钮时,我在 Google Chrome 的控制台中收到以下错误:

script.js:3 POST http://127.0.0.1:3000/create-checkout-session net::ERR_CONNECTION_REFUSED createCheckoutSession@script.js:3 (匿名)@script.js:39

Uncaught (in promise) TypeError: Failed to fetch Promise.then(异步)
(匿名)@script.js:39

VS Code 中的终端向我显示此错误(显示部分消息):

{ 错误:缺少必需的参数:line_items[0][currency].

我假设 script.js 文件中的某些内容不正确,但我不确定是什么?这是该文件中的代码:

// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function (priceId) {
  return fetch('/create-checkout-session', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      priceId: priceId,
    }),
  }).then(function (result) {
    return result.json();
  });
};

// Handle any errors returned from Checkout
var handleResult = function (result) {
  if (result.error) {
    var displayError = document.getElementById('error-message');
    displayError.textContent = result.error.message;
  }
};

/* Get your Stripe publishable key to initialize Stripe.js */
fetch('/setup')
  .then(function (result) {
    return result.json();
  })
  .then(function (json) {
    var publishableKey = json.publishableKey;
    var basicPriceId = json.basicPrice;

    var stripe = Stripe(publishableKey);
    // Setup event handler to create a Checkout Session when button is clicked
    document
      .getElementById('basic-plan-btn')
      .addEventListener('click', function (evt) {
        console.log('This is working');
        createCheckoutSession(basicPriceId).then(function (data) {
          // Call Stripe.js method to redirect to the new Checkout page
          stripe
            .redirectToCheckout({
              sessionId: data.sessionId,
            })
            .then(handleResult);
        });
      });
  });

如有任何帮助,我们将不胜感激!

【问题讨论】:

    标签: javascript node.js stripe-payments


    【解决方案1】:

    服务器端处理程序接收这个前端请求实际上是什么create the session?这将在您的后端使用密钥完成。

    如果我假设您的 fetch 大致映射到该 create 调用,这取决于您如何使用传递的参数,您可能需要添加一些层。例如,确保您的价格 ID 位于 line_items array 的订单项对象中:

    stripe.checkout.sessions.create(
      {
        success_url: 'https://example.com/success',
        cancel_url: 'https://example.com/cancel',
        payment_method_types: ['card'],
        line_items: [
          {
            price: priceId,
            quantity: 2,
          },
        ],
        mode: 'payment',
      },
    )
    

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2020-11-26
      • 2021-06-24
      • 2021-07-13
      • 2017-02-15
      • 2022-09-28
      • 2019-12-02
      • 2022-01-05
      • 2019-11-19
      相关资源
      最近更新 更多