云功能停止执行

时间:2019-06-07 00:18:34

标签: javascript firebase google-cloud-firestore google-cloud-functions paypal-rest-sdk

因此,我创建了一个用于处理Paypal即时付款通知(IPN)的功能。一切顺利,收到了通知,当我请求退回Paypal进行验证时,我也收到了“ VERIFIED”消息。问题是云功能不是或停止在验证功能回调内执行。

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);
    })

1 个答案:

答案 0 :(得分:0)

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