部署Stripe密钥至Firebase Cloud Functions失败求助
问题描述
部署Firebase Cloud Functions时持续失败,错误日志显示无法在us-central1区域创建函数,尝试过改用JavaScript重写代码、重新初始化项目,但问题依旧。同时需要将函数部署到加拿大多伦多所在的东海岸区域。
执行过的配置命令:
firebase functions:config:set stripe.testkey="YOUR KEY HERE"
最初的TypeScript代码(index.ts):
/* eslint-disable */ import * as functions from "firebase-functions"; //firebase functions:config:set stripe.livekey="YOUR KEY HERE" const stripe = require('stripe')(functions.config().stripe.testkey); exports.createPaymentIntent = functions.https.onRequest(async (req, res) => { stripe.paymentIntents.create( { amount: req.query.amount, currency: req.query.currency, payment_method: req.query.pm_id, confirmation_method: 'automatic', confirm: true, description: req.query.description, }, function(err, paymentIntent) { if (err !== null){ console.log('Error payment Intent: ', err); res.send('error');} else { console.log('Created paymentintent: ', paymentIntent); res.json({ paymentIntent: paymentIntent }); }}); });
改用JavaScript后的代码(index.js):
const functions = require("firebase-functions"); const stripe = require("stripe")("my key here"); exports.stripePaymentIntentRequest = functions.https.onRequest(async (req, res) => { try { let customerId; const customerList = await stripe.customers.list({ email: req.body.email, limit: 1 }); if (customerList.data.length !== 0) { customerId = customerList.data[0].id; } else { const customer = await stripe.customers.create({ email: req.body.email }); customerId = customer.data.id; } const ephemeralKey = await stripe.ephemeralKeys.create( { customer: customerId }, { apiVersion: '2020-08-27' } ); const paymentIntent = await stripe.paymentIntents.create({ amount: parseInt(req.body.amount), currency: 'cad', customer: customerId, }) res.status(200).send({ paymentIntent: paymentIntent.client_secret, ephemeralKey: ephemeralKey.secret, customer: customerId, success: true, }) } catch (error) { res.status(404).send({ success: false, error: error.message }) } });
两次部署的核心错误日志:
[debug] [xxxxxx] Error: Failed to create function [函数名] in region us-central1
...
[error] Error: There was an error deploying functions
解决方案
一、排查部署失败的常见原因
修正Stripe密钥使用方式
- 执行
firebase functions:config:get确认stripe.testkey配置存在且正确。 - JS代码中不要硬编码密钥,改为读取配置:
const stripe = require("stripe")(functions.config().stripe.testkey)。
- 执行
修复代码逻辑错误
- JS代码中,
stripe.customers.create返回的直接是客户对象,应使用customer.id而非customer.data.id,否则会触发运行时错误导致部署失败。 - 回调函数改用
await语法,避免异步逻辑导致的响应未正确发送(示例参考下方TS代码)。
- JS代码中,
检查版本兼容性
- 在
package.json中指定符合Firebase要求的Node.js版本(推荐18或20):"engines": { "node": "18" } - 执行
npm install -g firebase-tools更新Firebase CLI到最新版本。
- 在
二、设置多伦多区域(northamerica-northeast1)
多伦多对应的Firebase Functions区域为northamerica-northeast1,有两种设置方式:
单个函数指定区域
在定义函数时通过.region()方法指定:// TS示例 exports.createPaymentIntent = functions.region('northamerica-northeast1').https.onRequest(async (req, res) => { try { const paymentIntent = await stripe.paymentIntents.create({ amount: req.query.amount, currency: req.query.currency, payment_method: req.query.pm_id, confirmation_method: 'automatic', confirm: true, description: req.query.description, }); console.log('Created paymentintent: ', paymentIntent); res.json({ paymentIntent }); } catch (err) { console.log('Error payment Intent: ', err); res.send('error'); } });// JS示例 exports.stripePaymentIntentRequest = functions.region('northamerica-northeast1').https.onRequest(async (req, res) => { // 修正后的函数逻辑 });全局默认区域
在项目根目录的firebase.json中添加配置,统一设置所有函数的默认区域:{ "functions": { "region": "northamerica-northeast1" } }
内容的提问来源于stack exchange,提问作者Julia Pak
相关产品推荐
相关产品推荐

