Acknowledge unacknowledged play tokens at receipt generation

This commit is contained in:
Ravi Khadiwala authored and Jon Chambers committed 2026-08-18 11:23:11 -04:00
1 parent c7bcaac310
commit df026777c4
2 files changed
+32 -26

No files matched your search

@@ -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());
@@ -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