簡體   English   中英

帶節點的條帶引發錯誤 400:“無法讀取未定義的屬性‘會話’”

[英]Stripe with Node throws Error 400: "Cannot read property 'sessions' of undefined"

我正在嘗試將條帶集成到我組織的應用程序中,但是,按照條帶自己的文檔,使用 nodeJS 進行集成的方法是這樣做:

https://stripe.com/docs/billing/subscriptions/checkout#create-session

我幾乎嘗試了所有方法,但服務器總是拋出 404 錯誤並顯示以下消息:“無法讀取未定義的屬性‘會話’”

問題是,當我在后端控制條帶對象時,我發現實際上條帶沒有“結帳”屬性,但條帶文檔說它確實有它,這讓我認為我一定做錯了什么.

這是我的“去結帳”按鈕的前端代碼

    const createCheckoutSession = (priceId:any) => {
        return axios.post("/create-checkout-session", {
          method: "POST",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify({
            priceId: priceId
          })
        }).then(function(result) {
          return result.data();
        });
      };
    
    const handleClick = async (event:any) => {
        // Get Stripe.js instance
        const stripe = await stripePromise;
    
        // Call your backend to create the Checkout Session
        //const response = await fetch('/create-checkout-session', { method: 'POST' });
        createCheckoutSession('price_1J7UmZFYFs5GcbAXvPronXSX').then(function(data) {
            // Call Stripe.js method to redirect to the new Checkout page
            stripe
              .redirectToCheckout({
                sessionId: data.sessionId
              })
              .then(()=>{console.log('handleResult')});
          })
          };

這是我的 server.js 代碼

//STRIPE CODE
const stripeKey = 'sk_test_51J7UfrFYFs5GcbAX45lXy6w2TV1hPqvUbqu3hdB4QRAXFR3QYmTgcNKXcVG7tL9vwaanjdYWGvkfiQ6Bd41dK4V7004yMs3Cbh'
const stripe = Stripe(stripeKey);

app.post('/create-checkout-session', async (req, res) => {
  //console.log("--------STRIPE----------", stripe);
  const { priceId } = req.body;

  try {
    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://localhost:3000/success?session_id={CHECKOUT_SESSION_ID}',
      cancel_url: 'https://localhost:3000/canceled',
    });

    res.send({
      sessionId: session.id,
    });
  } catch (e) {
    res.status(400);
    return res.send({
      error: {
        message: e.message,
      }
    });
  }
});

聽起來您使用的是不支持結帳會話的舊版 Stripe 節點庫。 如果可能,您應該檢查您使用的庫的 版本升級到最新版本,或者至少是v6.20.0 (添加 Checkout 支持的第一個版本)。

好吧,所以我想通了,我的包中已經安裝了條帶,但是每次我嘗試更新它時,它都會說我使用的是最新版本。

無論如何,我卸載然后使用npm i <package>重新安裝了所有與 stripe 相關的東西,它突然工作得很好

我卸載了條紋,重新安裝 - 沒有骰子。

確保根據文檔:

該軟件包需要使用您帳戶的密鑰進行配置,該密鑰可在 Stripe Dashboard 中找到。 要求它帶有鍵的值:

喜歡

const stripe = require('stripe')(process.env.STRIPE_PRIVATE_KEY);

確保之后,一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM