2

我正在使用 Firebase Cloud Functions 来处理应用内购买的后端流程。如果我购买订阅(在 Flutter/Android 客户端上),购买流程没有问题。但是,当第一次续订发生时,我首先收到订阅已过期的实时开发人员通知,大约 50 秒后,我收到订阅已续订的新通知。

详细流程如下:


1.在客户端购买订阅(目前处于内部测试模式)

--- Google Cloud Logging 事件的开始 (hh:mm:ss):

2. 00:00:00 - 通知订阅已触发(空函数,仅打印到控制台)

3. 在Client,触发InAppPurchaseConnection purchaseUpdatedStream(Flutter),Client向后端发送数据,触发Cloud Function verifyPurchase。

4. 00:00:03 - verifyPurchase 函数执行开始

5. 00:00:05 - 收到状态 200 响应

6. 00:00:06 - 收到确认响应(状态 204)

7. 直到这里一切都好,我可以在 Firestore DB 中看到正确的数据

8. 00:04:08 - Notification Subscription Expired Triggered(后端撤销订阅,使用户没有购买任何其他服务)
订阅状态响应:

{ “startTimeMillis”:“1616129098091”,
“expiryTimeMillis”:“1616130591517”,
“autoRenewing”:false,
“priceCurrencyCode”:“KRW”,
“priceAmountMicros”:“2900000000”,
“countryCode”:“KR”,
“developerPayload” :"",
"cancelReason":0,
"userCancellationTimeMillis":"1616130398607",
"orderId":"GPA.3391-[...].3",
"purchaseType":0,
"acknowledgementState":1,
"kind ":"androidpublisher#subscriptionPurchase",
"obfuscatedExternalAccountId":"q[..]3"
}

-> 此处的取消​​原因说明:0. 用户取消了订阅(事实并非如此)

-> AcknowledgementState 为 1:已确认

请参阅:REST API REF 资源:SubscriptionPurchase

9. 00:05:01 - 通知订阅更新触发从这里开始一切都按预期工作,用户收到她/他支付的服务。

10. ~ 16. 00:10:01 ~ 00:35:20 - 通知订阅续订 触发 4 次,然后取消并过期(预期行为)

--- Google Cloud Logging 事件结束。


export const verifyPurchase = functions.region("asia-northeast3").https.onCall(async (data, context) => {
  const {sku, purchaseToken, packageName, purchaseType} = data;
  const userId: string = context.auth?.uid ?? "";
  functions.logger.log(`USER ID: ${userId}`);

  let result = 0;

  try {
    await authClient.authorize();
    functions.logger.log("START TO CHECK SUBSCRIPTION OR ONE TIME PURCHASE");
    if (purchaseType === PurchaseType.subscription) {
      const subscription = await playDeveloperApiClient.purchases.subscriptions.get({
        packageName: packageName,
        subscriptionId: sku,
        token: purchaseToken,
      });
      functions.logger.log("SUBSCRIPTION BEING PROCESSED");
      if (subscription.status === 200 && subscription?.data?.paymentState === 1 || subscription?.data?.paymentState === 2) {
        functions.logger.log(`SUBSCRIPTION STATUS: ${subscription.status}`);

        if (!subscription.data.linkedPurchaseToken) {
          const subscriptionData: any = {
            "transactionId": purchaseToken,
            "userId": userId,
            "expirationDateMs": subscription.data.expiryTimeMillis,
            "disabled": false,
            "platform": "android",
            "receipt": data,
            "rawData": subscription.data,
          };

          await admin.firestore().collection("subscriptions").add(subscriptionData).then(
              async (value) => {
               [...user Data being updated on DB...]
          );

          const acknowdlegeResponse = await playDeveloperApiClient.purchases.subscriptions.acknowledge({
            packageName: packageName,
            subscriptionId: sku,
            token: purchaseToken,
            requestBody: {
            },
          });
          functions.logger.info(`ACKNOWLEDGE RESPONSE SUBSCRIPTION: ${JSON.stringify(acknowdlegeResponse)}`);**
        } else {
          await subscriptionChange(purchaseToken, userId, data, subscription);
        }

        result = subscription.status;
      }
    } else {
      const oneTimePurchase = await playDeveloperApiClient.purchases.products.get({
      [...part for one time purchases...]
  } catch (e) {
    // handle error
    functions.logger.error("verifyPurchase ERROR");
    functions.logger.error(e);
  }
  functions.logger.log(`RETURN RESULT: ${result}`);

  return result;
});

所以我不完全理解为什么第 8 步(过期)会发生。我将不胜感激任何帮助。

4

1 回答 1

0

我相信您的问题是您在确认购买之前调用了 subscriptionPurchase API,如本评论所述。

尝试相应地更改您的代码。

于 2021-04-27T10:40:05.713 回答