Firebase条带扩展: checkout 按钮未重定向到条带实时模式

uoifb46i  于 2023-01-02  发布在  其他
关注(0)|答案(1)|浏览(130)

我目前在将stripe集成到我的网站上时遇到问题。我在此电子商务网站上工作了一段时间,遇到了stripe在单击结帐按钮时没有响应的问题。我多次尝试联系stripe解决此问题,但唯一有用的信息是该stripe。redirectToCheckout已弃用。另外,我正在尝试弄清楚如何使用firestore的stripe扩展。如果有人能帮我解决这个问题,我将不胜感激。我附上了我的checkout.js(按钮功能)和index.js(无服务器功能)

    • checkout . js**
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "*****",
  authDomain: "*****",
  projectId: "*****",
  storageBucket: "*****",
  messagingSenderId: "*****",
  appId: "*****",
  measurementId: "*****"
};


 // Initialize Firebase
 const app = initializeApp(firebaseConfig);
 const analytics = getAnalytics(app);
 const db = getFirestore(app);

 const checkoutButton = document.querySelector('#checkout-button')
 const createStripeCheckout = firebase.functions().httpsCallable('createStripeCheckout')
 const stripe = require("stripe")(functions.config().stripe.push)
  checkoutButton.addEventListener('click', () => {
    createStripeCheckout()
       .then(response => {
         const sessionId = response.data.id
         stripe.redirectToCheckout({ sessionId: sessionId})
    })
 })
    • 索引. js**
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.createStripeCheckout = functions.https.onCall( async (data, context) => {
  //stripe init
  const docRef = await db
    .collection("products")
    .doc(`${products.uid}`)
    .collection("checkout_sessions")
    .add({
      price: priceId,
      allow_promotion_codes: true,
      success_url: window.location.origin,
      cancel_url: window.location.origin,
    });

// Wait for the CheckoutSession to get attached by the extension
  onSnapshot(docRef, (snap) => {
    const data = snap.data();
    let error;
    let url;
    if (data) {
      error = data.error;
      url = data.url;
    }
    if (error) {
      // Show an error to your customer
      alert(`An error occurred: ${error.message}`);
    }
    if (url) {
      // We have a Stripe Checkout URL, let's redirect.
      window.location.assign(url);
    }
  });
});
    • 索引. html**
<!-- Shopping Cart -->
            <div class="cart-box">
                <div class="cart-icon">
                    <i class="fa-solid fa-cart-arrow-down"></i>
                </div>
                <div class="whole-cart-window hide">
                    <h2>Shopping Bag</h2>
                     <div class="cart-wrapper">
                         <!-- Item 1 -->
                </div> 
                <div class="subtotal">Subtotal: $0.00</div>
                <button type="submit" id="checkout-button">Checkout</button>
                </div>
        </div>

每次我点击 checkout 按钮时,没有任何动作发生,但是当它围绕属性时,它会重定向一个404 not found作为响应。

2q5ifsrm

2q5ifsrm1#

我不是一个真正的Maven在firebase扩展,但我注意到这里的几件事:

  • 正如您提到的,stripe.redirectToCheckout已过时,不应使用
  • 另外,当调用window.location.assign(url)时,您将在firebase函数中处理重定向

所以我将从删除createStripeCheckout()之后的.then处理程序开始,看看这是否有帮助。

checkoutButton.addEventListener('click', () => {
  createStripeCheckout()
})

如果这也没有帮助,我强烈建议检查并从这个官方的sample中获得灵感

相关问题