我正在尝试为用户设置一个按钮,以管理他与Stripe客户门户的订阅以及他们的Firebase集成。
问题是,该文档对旧版本的Firebase(8(有效,而我使用的是新版本的Firebase9。
我们在客户端,这是React.js.
这是他们调用函数的js代码(你可以在这里看到完整的代码:https://github.com/stripe-samples/firebase-subscription-payments/blob/master/public/javascript/app.js):
const functionLocation = 'us-east1';
const functionRef = firebase
.app()
.functions(functionLocation)
.httpsCallable('ext-firestore-stripe-subscriptions-createPortalLink');
const { data } = await functionRef({ returnUrl: window.location.origin });
window.location.assign(data.url);
});
Firebase 9应该是这样的:
const functions = getFunctions(firebaseApp)
export async function goToBillingPortal() {
const functionRef = httpsCallable(functions, 'ext-firestore-stripe-subscriptions-createPortalLink');
const { data } = await functionRef({ returnUrl: window.location.origin });
window.location.assign(data.url);
}
控制台显示3个错误:
Access to fetch at 'https://us-central1-xxxxxxxxd76.cloudfunctions.net/ext-firestore-stripe-subscriptions-createPortalLink' 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.
service.ts:206 POST https://us-central1-xxxxxxx76.cloudfunctions.net/ext-firestore-stripe-subscriptions-createPortalLink net::ERR_FAILED
Uncaught (in promise) FirebaseError: internal
显然,问题是我需要指定服务器的位置(例如us-central1(,但我不明白如何使用Firebase 9。所有指南均适用于Firebase 8。
我尝试过不同的东西,比如:
const functions = getFunctions(firebaseApp).location("us-central1")
或
const functionRef = httpsCallable(functions, 'ext-firestore-stripe-subscriptions-createPortalLink').location("us-central1")
但它不起作用。
想法?
如文档中所示,您应该将区域作为getFunctions()
方法的第二个参数:
const functions = getFunctions(firebaseApp, "europe-west1");
还可以定义.region()
的部署位置,我认为它甚至可以接受一个列表:
const CLOUD_FUNCTIONS_REGION = "us-east1";
exports.billing_portal = functions.region( CLOUD_FUNCTIONS_REGION ).https.onCall(
async (data, context: CallableContext) => {
}
);