如何解决条纹Webhook的Firebase功能不适用于async-await
我正在使用出色的stripe documentation构建一个Webhook来响应条带化成功付款。该代码可以正常工作,但是一旦我引入了async-await,就会收到500 Internal server error。
我正在通过条带网站上的send test webhook
测试firebase onRequest Webhook函数。
我的代码有效期为here
我想念什么?
带有async-await的Firebase功能不起作用:
exports.stripeEvents = functions.https.onRequest(async (request,response) =>
{
try
{
let stripesignature = request.headers['stripe-signature'] as string;
let stripeevent:Stripe.Event= stripe.webhooks.constructEvent(request.rawBody.toString('utf8'),stripesignature,config.stripewebhooksecretkey);
if(stripeevent.type === 'payment_intent.succeeded')
{
let payment_intent1 = stripeevent.data.object as Stripe.PaymentIntent;
let datafirstelement=null;
if(payment_intent1?.charges?.data && payment_intent1?.charges?.data.length>0)
{
datafirstelement=payment_intent1?.charges?.data[0];
}
let userid='someuserid';
let documentreference=config.db.collection('users').doc(userid);
await documentreference.update({payments: admin.firestore.FieldValue.arrayUnion(datafirstelement)});
response.status(200).send({'result':true});
}
}
catch(error)
{
//log error into sentry
throw error;
}
});
无需异步等待的firebase函数可以工作:
exports.stripeEvents = functions.https.onRequest((request,config.stripewebhooksecretkey);
if(stripeevent.type === 'payment_intent.succeeded')
{
let payment_intent1 = stripeevent.data.object as Stripe.PaymentIntent;
let datafirstelement=null;
if(payment_intent1?.charges?.data && payment_intent1?.charges?.data.length>0)
{
datafirstelement=payment_intent1?.charges?.data[0];
}
let userid='someuserid';
let documentreference=config.db.collection('users').doc(userid);
//await documentreference.update({payments: //admin.firestore.FieldValue.arrayUnion(datafirstelement)});
response.status(200).send({'result':true});
}
}
catch(error)
{
//log error into sentry
throw error;
}
});
更新1:我正在通过条纹网站上的send test webhook
测试firebase onRequest Webhook函数。
不使用异步等待时成功进行条带支付,使用异步等待时发生内部服务器错误:
不使用async-await时,webhook成功日志(使用async-await时,没有日志):
更新2:firebase功能日志:
stripeEvents
Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
at Object.validateResourcePath (/workspace/node_modules/@google-cloud/firestore/build/src/path.js:403:15)
at CollectionReference.doc (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:1988:20)
at exports.stripeEvents.functions.https.onRequest (/workspace/lib/index.js:115:85)
at cloudFunction (/workspace/node_modules/firebase-functions/lib/providers/https.js:51:16)
at process.nextTick (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:99:17)
at process._tickCallback (internal/process/next_tick.js:61:11)
更新3:firebase功能日志:
我将await documentreference.update({payments: //admin.firestore.FieldValue.arrayUnion(datafirstelement)});
行更改为await documentreference.set({payments: admin.firestore.FieldValue.arrayUnion(datafirstelement)},{merge:true});
,应用程序仍然引发以下错误。该代码存在here。
12:02:30.464 AM
stripeEvents
Function execution started
12:02:54.174 AM
stripeEvents
Function execution started
12:02:54.901 AM
stripeEvents
This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
12:02:54.905 AM
stripeEvents
Function execution took 732 ms,finished with status: 'crash'
12:03:30.467 AM
stripeEvents
Function execution took 60004 ms,finished with status: 'timeout'
解决方法
从上面的评论以及从您得到的错误(Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
)中,您在测试Cloud Function时传递给doc()
方法的值似乎是一个空字符串。
您需要在测试中模拟数据,以便将正确的值传递给doc()
方法。
请注意,我不确定“从Web应用程序”进行测试(如您在上面的评论中所述)会有所改变,因为Stripe Webhook与您的Web应用程序无关(Stripe后端调用您的Web应用程序)后端,即您的Cloud Function)。您可以将userId作为付款元数据发送到Stripe,并且(1)将其作为查询字符串参数添加到webhook调用的URL,然后(2)解析并在您的Cloud Function中使用此值。
还请注意,无效的“异步版本”与有效的“非异步版本”之间的区别与异步/等待无关,只是您将产生错误的行注释掉了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。