Google Billing API: Shortly after subscription purchase, subscription expired is triggered, directly followed by subscription renewed
W

0

2

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.

Detailed Process is as follows:


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

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

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

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 function execution started

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

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

7. Until here all is good and I can see the correct data in Firestore DB

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

-> Cancel Reason here states: 0. User canceled the subscription (which is not the case)

-> AcknowledgementState is 1: Acknowledged

Please see: REST API REF Resource: SubscriptionPurchase

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 - Notification Subscription Renewed Triggered 4x times, then cancelled and expired (expected behaviour)

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

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

Winthrop answered 20/3, 2021 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.