首页 > 解决方案 > 云功能停止执行

问题描述

所以我创建了一个函数来处理 Paypal 即时付款通知(IPN)。收到通知一切顺利,当我请求返回 Paypal 进行验证时,我也收到了“已验证”消息。问题是云函数在验证函数回调中没有或停止执行。

exports.ipnHandler = functions.https.onRequest((req, res) => {
  console.log("IPN Notification Event Received");

  if (req.method !== "POST") {
      console.error("Request method not allowed.");
      res.status(405).send("Method Not Allowed");
  } else {
      // Return empty 200 response to acknowledge IPN post success.
      console.log("IPN Notification Event received successfully.");

      // JSON object of the IPN message consisting of transaction details.
      let ipnTransactionMessage = req.body;
      // Convert JSON ipn data to a query string since Google Cloud Function does not expose raw request data.
      let formUrlEncodedBody = querystring.stringify(ipnTransactionMessage);
      // Build the body of the verification post message by prefixing 'cmd=_notify-validate'.
      let verificationBody = `cmd=_notify-validate&${formUrlEncodedBody}`;

      console.log(`Verifying IPN: ${verificationBody}`);

      let options = {
        method: "POST",
        url: getPaypalURI(),
        body: verificationBody,
      };

    requestPromise(options)
        .then(body => {
            // This will log "VERIFIED"
            console.log(body);   // Cloud function stops here

            if (body === "VERIFIED") {
                console.log("Manage user subscription");

                const transactionType = ipnTransactionMessage.txn_type;
                const invoice = ipnTransactionMessage.invoice;
                const docRef = firestore.collection("users");

                console.log("Transaction type: " + transactionType);
                console.log("Invoice " + invoice);

                if (transactionType === "subscr_payment") {
                    console.log("About to subscribe user: " + invoice);
                    return docRef.where("invoice", "==", invoice).get();
                }
            }

            return console.log("Request completed");
        })
        .then(snapshots => {
            return console.log("Return snapshots " + snapshots);
        })
        .catch(error => {
            console.log(error);
        })

    res.status(200).end();
}

当我添加这一行时,它开始出现异常。return docRef.where("invoice", "==", invoice).get();据说它会在下面的回调中返回快照数据。

.then(snapshots => {
        return console.log("Return snapshots " + snapshots);
    })

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionspaypal-rest-sdk

解决方案


我已经解决了这个问题。似乎该函数已达到超时,默认为 60 秒。我只是将超时更改为 5 分钟。


推荐阅读