From df026777c4daaddc8747b5c86a29a0c483ffe787 Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Fri, 14 Aug 2026 13:59:36 -0500 Subject: [PATCH] Acknowledge unacknowledged play tokens at receipt generation --- .../GooglePlayBillingManager.java | 49 ++++++++++--------- .../GooglePlayBillingManagerTest.java | 9 ++-- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManager.java index 88659ad9a..9a3d83a1b 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManager.java @@ -181,17 +181,10 @@ public class GooglePlayBillingManager implements SubscriptionPaymentProcessor, O .fromString(subscription.getAcknowledgementState()) .orElse(AcknowledgementState.UNSPECIFIED); - final boolean requiresAck = switch (acknowledgementState) { - case ACKNOWLEDGED -> false; - case PENDING -> true; - case UNSPECIFIED -> throw new UncheckedIOException( - new IOException("Invalid acknowledgement state " + subscription.getAcknowledgementState())); - }; - final SubscriptionPurchaseLineItem purchase = getLineItem(subscription); final ReceiptLevel level = productIdToLevel(purchase.getProductId()); - return new ValidatedToken(level.getValue(), purchase.getProductId(), purchaseToken, requiresAck); + return new ValidatedToken(level.getValue(), purchase.getProductId(), purchaseToken, requiresAcknowledgement(subscription)); } @@ -284,20 +277,6 @@ public class GooglePlayBillingManager implements SubscriptionPaymentProcessor, O public ReceiptItem getReceiptItem(String purchaseToken) throws RateLimitExceededException, SubscriptionNotFoundException, SubscriptionPaymentRequiredException { final SubscriptionPurchaseV2 subscription = lookupSubscription(purchaseToken); - final AcknowledgementState acknowledgementState = AcknowledgementState - .fromString(subscription.getAcknowledgementState()) - .orElse(AcknowledgementState.UNSPECIFIED); - if (acknowledgementState != AcknowledgementState.ACKNOWLEDGED) { - final SubscriptionPurchaseLineItem purchase = getLineItem(subscription); - // We should only ever generate receipts for a stored and acknowledged token. - logger.error("Tried to fetch receipt for purchase token that was never acknowledged. orderId: {} latestSuccessfulOrderId: {}, acknowledgementState: {}, canceledStateContext: {}, state: {} ", - subscription.getLatestOrderId(), - purchase.getLatestSuccessfulOrderId(), - subscription.getAcknowledgementState(), - subscription.getCanceledStateContext(), - subscription.getSubscriptionState()); - throw new IllegalStateException("Tried to fetch receipt for purchaseToken that was never acknowledged"); - } Metrics.counter(GET_RECEIPT_COUNTER_NAME, subscriptionTags(subscription)).increment(); @@ -312,6 +291,21 @@ public class GooglePlayBillingManager implements SubscriptionPaymentProcessor, O throw new SubscriptionPaymentRequiredException(); } + if (requiresAcknowledgement(subscription)) { + // We only generate receipts for previously stored tokens. Usually, we acknowledge tokens after storing them. + // However, it's possible that a client sent us a token, and we successfully stored it, but then failed to + // acknowledge it. If the client later attempts to create a receipt from that token, we can be confident that + // they are going to use their entitlement, so we can go ahead and acknowledge it + logger.info("Tried to fetch receipt for purchase token that was never acknowledged. Acknowledging. orderId: {} latestSuccessfulOrderId: {}, acknowledgementState: {}, canceledStateContext: {}, state: {} ", + subscription.getLatestOrderId(), + purchase.getLatestSuccessfulOrderId(), + subscription.getAcknowledgementState(), + subscription.getCanceledStateContext(), + subscription.getSubscriptionState()); + executeTokenOperation(pub -> pub.purchases().subscriptions() + .acknowledge(packageName, purchase.getProductId(), purchaseToken, new SubscriptionPurchasesAcknowledgeRequest())); + } + return new ReceiptItem( subscription.getLatestOrderId(), PaymentTime.periodEnds(expiration), @@ -450,6 +444,17 @@ public class GooglePlayBillingManager implements SubscriptionPaymentProcessor, O return lineItems.getFirst(); } + private static boolean requiresAcknowledgement(final SubscriptionPurchaseV2 subscription) { + return switch (AcknowledgementState + .fromString(subscription.getAcknowledgementState()) + .orElse(AcknowledgementState.UNSPECIFIED)) { + case ACKNOWLEDGED -> false; + case PENDING -> true; + case UNSPECIFIED -> throw new UncheckedIOException( + new IOException("Invalid acknowledgement state " + subscription.getAcknowledgementState())); + }; + } + private Tags subscriptionTags(final SubscriptionPurchaseV2 subscription) { final boolean expired = subscription.getLineItems().isEmpty() || getExpiration(getLineItem(subscription)).orElse(Instant.EPOCH).isBefore(clock.instant()); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManagerTest.java index 2b1903fbe..7a3bda832 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/GooglePlayBillingManagerTest.java @@ -128,7 +128,7 @@ class GooglePlayBillingManagerTest { assertThat(result.getLevel()).isEqualTo(201); assertThatNoException().isThrownBy(result::acknowledgePurchase); - verify(acknowledge, times(1)).execute(); + verify(acknowledge).execute(); } @ParameterizedTest @@ -203,15 +203,16 @@ class GooglePlayBillingManagerTest { } @Test - public void getReceiptUnacknowledged() throws IOException { + public void getReceiptUnacknowledged() + throws IOException, SubscriptionPaymentRequiredException, SubscriptionNotFoundException, RateLimitExceededException { when(subscriptionsv2Get.execute()).thenReturn(new SubscriptionPurchaseV2() .setAcknowledgementState(GooglePlayBillingManager.AcknowledgementState.PENDING.apiString()) .setSubscriptionState(GooglePlayBillingManager.SubscriptionState.ACTIVE.apiString()) .setLineItems(List.of(new SubscriptionPurchaseLineItem() .setExpiryTime(Instant.now().plus(Duration.ofDays(1)).toString()) .setProductId(SUBSCRIPTION_PRODUCT_ID)))); - assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> - googlePlayBillingManager.getReceiptItem(PURCHASE_TOKEN)); + googlePlayBillingManager.getReceiptItem(PURCHASE_TOKEN); + verify(acknowledge).execute(); } @Test