Google Billing API:购买订阅后不久,将触发订阅过期,然后直接续订 [英] Google Billing API: Shortly after subscription purchase, subscription expired is triggered, directly followed by subscription renewed

本文介绍了Google Billing API:购买订阅后不久,将触发订阅过期,然后直接续订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase Cloud Functions处理应用程序内购买的后端流程.如果我购买了一个订阅(在Flutter/Android客户端上),则购买流程可以正常工作.但是,当第一次续订发生时,我首先收到订阅已过期的实时开发人员通知,大约50秒后,我收到了有关续订的新通知.

I am using Firebase Cloud Functions to handle the back-end process for In-App-Purchases. If I purchase a subscription (on Flutter/Android client), the purchase flow works without problems. However, when the first Renewal happens, I get first a Real Time Developer Notification that the Subscription has Expired, and around 50 Seconds later I get a new notification that the subscription got renewed.

详细过程如下:

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

1. Purchase subscription on Client (at the moment in internal testing Mode)

--- Google Cloud日志记录活动的开始(hh:mm:ss):

--- Start of Google Cloud Logging Events (hh:mm:ss):

2.00:00:00 -已触发购买通知订阅(空白功能,仅打印到控制台)

2. 00:00:00 - Notification Subscription Purchased Triggered (empty function, only prints to console)

3.在客户端上,InAppPurchaseConnection PurchaseUpdatedStream(Flutter)被触发,客户端将数据发送到后端,并且云功能verifyPurchase被触发.

3. On Client, InAppPurchaseConnection purchaseUpdatedStream (Flutter) is triggered, Client sends data to back-end and Cloud Function verifyPurchase is triggered.

4.00:00:03 -verifyPurchase函数开始执行

4. 00:00:03 - verifyPurchase function execution started

5.00:00:05 -收到状态200回复​​

5. 00:00:05 - Status 200 response received

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

6. 00:00:06 - Acknowledge Response received (Status 204)

7. 直到这一切都很好,我才能在Firestore DB中看到正确的数据

8.00:04:08 -通知订阅过期已触发(后端撤销了订阅,使用户没有购买任何其他服务)
订阅状态响应:

8. 00:04:08 - Notification Subscription Expired Triggered (back-end revokes subscription, leaving the user without any additional services she/he purchased)
Subscription Status response:

{"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,
种类":"androidpublisher#subscriptionPurchase",
"obfuscatedExternalAccountId":"q [.. 3]"
}

{ "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 -通知订阅已更新已触发从这里开始,一切都会按预期进行,并且用户会收到他/他所支付的服务.

9. 00:05:01 - Notification Subscription Renewed Triggered From here on everything works as expected and the user receives the services she/he paid for.

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

10. ~ 16. 00:10:01 ~ 00:35:20 - Notification Subscription Renewed Triggered 4x times, then cancelled and expired (expected behaviour)

--- Google Cloud日志记录活动结束.

--- End of Google Cloud Logging Events.


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步(过期).我将不胜感激.

So I don't exactly understand why step 8 (expired) happens. I would appreciate any help.

推荐答案

我相信您的问题是,如

I believe your issue is that you are calling the subscriptionPurchase API before acknowledging the purchase, as stated in this comment.

尝试相应地更改代码.

这篇关于Google Billing API:购买订阅后不久,将触发订阅过期,然后直接续订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆