firebase 在条带中创建 checkout 会话时出现CORS错误

gmol1639  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(113)

我正在使用stripe进行开发。我创建了一个 checkout 会话,但出现了以下错误。如何解决此问题?

Access to fetch at 'https://xxx.cloudfunctions.net/createCheckoutSession' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

src/Purchase.ts

import React from "react";
import { httpsCallable } from "firebase/functions";
import { functions } from "../firebase";

function Purchase() {
  // ...
  const createCheckoutSession = httpsCallable(functions, "createCheckoutSession");
  createCheckoutSession({
    priceId: "price id",
  });
}

export default Purchase;

functions/indes.ts

import * as functions from "firebase-functions";

从“stripe”导入Stripe;

const stripe = new Stripe(functions.config().stripe.secret, {
    apiVersion: "2022-11-15",
});

export const createCheckoutSession = () => functions
    .region("asia-northeast1")
    .https.onCall(async (data) => {
      const priceId = data.priceId;

      const session = await stripe.checkout.sessions.create({
        line_items: [{
          price: priceId,
          quantity: 1,
        }],
        mode: "payment",
        success_url: "",
        cancel_url: "",
        payment_method_types: ["card"],
      });
      return session;
    });

src/firebase.ts

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
import { getAuth } from "firebase/auth";
import { getFunctions } from "firebase/functions";

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_APIKEY,
  authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID  ,
  appId: process.env.REACT_APP_FIREBASE_APP_ID,
};

const app = initializeApp(firebaseConfig);

export const db = getFirestore(app);
export const storage = getStorage(app);
export const auth = getAuth(app);
export const functions = getFunctions(app, "asia-northeast1");

export default app;
fnvucqvd

fnvucqvd1#

功能未展开。
之前

export const createCheckoutSession = () => functions

之后

export const createCheckoutSession = functions

相关问题