Scope multi-attempt configuration in IssuedReceiptsManager to subscriptions only

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-08-07 14:39:13 -05:00
1 parent d6b80dfd16
commit 6e3106297b
13 files changed
+64 -26

No files matched your search

+1 -1
View File
@@ -107,7 +107,7 @@ dynamoDbTables:
tableName: Example_IssuedReceipts
expiration: P30D # Duration of time until rows expire
generator: abcdefg12345678= # random base64-encoded binary sequence
maxIssuedReceiptsPerPaymentId:
maxReceiptsPerSubscriptionPayment:
STRIPE: 1
BRAINTREE: 1
GOOGLE_PLAY_BILLING: 1
@@ -813,7 +813,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
config.getDynamoDbTables().getIssuedReceipts().getExpiration(),
dynamoDbClient,
config.getDynamoDbTables().getIssuedReceipts().getGenerator(),
config.getDynamoDbTables().getIssuedReceipts().getmaxIssuedReceiptsPerPaymentId());
config.getDynamoDbTables().getIssuedReceipts().getMaxReceiptsPerSubscriptionPayment());
OneTimeDonationsManager oneTimeDonationsManager = new OneTimeDonationsManager(
config.getDynamoDbTables().getOnetimeDonations().getTableName(), config.getDynamoDbTables().getOnetimeDonations().getExpiration(), dynamoDbClient);
DonationPermits donationPermits = new DonationPermits(
@@ -17,19 +17,17 @@ public class IssuedReceiptsTableConfiguration extends DynamoDbTables.TableWithEx
private final byte[] generator;
/**
* The maximum number of issued receipts the issued receipt manager should issue for a particular itemId
*/
private final EnumMap<PaymentProvider, Integer> maxIssuedReceiptsPerPaymentId;
/// The maximum number of receipts that may be issued for a single subscription payment.
private final EnumMap<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment;
public IssuedReceiptsTableConfiguration(
@JsonProperty("tableName") final String tableName,
@JsonProperty("expiration") final Duration expiration,
@JsonProperty("generator") final byte[] generator,
@JsonProperty("maxIssuedReceiptsPerPaymentId") final Map<PaymentProvider, Integer> maxIssuedReceiptsPerPaymentId) {
@JsonProperty("maxReceiptsPerSubscriptionPayment") final Map<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment) {
super(tableName, expiration);
this.generator = generator;
this.maxIssuedReceiptsPerPaymentId = EnumMapUtil.toCompleteEnumMap(PaymentProvider.class, maxIssuedReceiptsPerPaymentId);
this.maxReceiptsPerSubscriptionPayment = EnumMapUtil.toCompleteEnumMap(PaymentProvider.class, maxReceiptsPerSubscriptionPayment);
}
@NotEmpty
@@ -37,7 +35,7 @@ public class IssuedReceiptsTableConfiguration extends DynamoDbTables.TableWithEx
return generator;
}
public EnumMap<PaymentProvider, Integer> getmaxIssuedReceiptsPerPaymentId() {
return maxIssuedReceiptsPerPaymentId;
public EnumMap<PaymentProvider, Integer> getMaxReceiptsPerSubscriptionPayment() {
return maxReceiptsPerSubscriptionPayment;
}
}
@@ -384,7 +384,7 @@ public class OneTimeDonationController {
throw new BadRequestException("invalid receipt credential request", e);
}
try {
issuedReceiptsManager.recordIssuance(paymentDetails.id(), request.processor,
issuedReceiptsManager.recordOneTimeIssuance(paymentDetails.id(), request.processor,
receiptCredentialRequest, clock.instant());
} catch (WriteConflictException _) {
throw new WebApplicationException(Response.Status.CONFLICT);
@@ -304,7 +304,7 @@ public class OneTimeDonationsGrpcService extends SimpleOneTimeDonationsGrpc.OneT
}
try {
issuedReceiptsManager.recordIssuance(
issuedReceiptsManager.recordOneTimeIssuance(
paymentDetails.id(), processor, receiptCredentialRequest, clock.instant());
} catch (final WriteConflictException e) {
return CreateBoostReceiptCredentialsResponse.newBuilder()
@@ -44,34 +44,61 @@ public class IssuedReceiptsManager {
private final Duration expiration;
private final DynamoDbClient dynamoDbClient;
private final byte[] receiptTagGenerator;
private final EnumMap<PaymentProvider, Integer> maxIssuedReceiptsPerPaymentId;
private final EnumMap<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment;
public IssuedReceiptsManager(
@Nonnull final String table,
@Nonnull final Duration expiration,
@Nonnull final DynamoDbClient dynamoDbClient,
@Nonnull final byte[] receiptTagGenerator,
@Nonnull final EnumMap<PaymentProvider, Integer> maxIssuedReceiptsPerPaymentId) {
@Nonnull final EnumMap<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment) {
this.table = Objects.requireNonNull(table);
this.expiration = Objects.requireNonNull(expiration);
this.dynamoDbClient = Objects.requireNonNull(dynamoDbClient);
this.receiptTagGenerator = Objects.requireNonNull(receiptTagGenerator);
this.maxIssuedReceiptsPerPaymentId = Objects.requireNonNull(maxIssuedReceiptsPerPaymentId);
this.maxReceiptsPerSubscriptionPayment = Objects.requireNonNull(maxReceiptsPerSubscriptionPayment);
}
/// Records the issuance of a receipt credential for a one-time purchases.
///
/// Same as [#recordIssuance] except one-time purchases never allow multiple receipts on a single payment
public void recordOneTimeIssuance(
final String processorItemId,
final PaymentProvider processor,
final ReceiptCredentialRequest request,
final Instant now) throws WriteConflictException {
recordIssuance(processorItemId, processor, request, 1, now);
}
/// Returns normally if either this processor item was never issued a receipt credential
/// previously OR if it was issued a receipt credential previously for the exact same receipt credential request
/// enabling clients to retry in case they missed the original response.
///
/// If this item has already been used to issue another receipt, throws [WriteConflictException].
/// A subscription payment may be issued as many distinct receipts as `maxReceiptsPerSubscriptionPayment` allows for
/// its [PaymentProvider]. After that, a distinct receipt request throws [WriteConflictException].
///
/// For [PaymentProvider#STRIPE], item is expected to refer to an invoice line item (subscriptions) or a
/// payment intent (one-time).
/// For [PaymentProvider#STRIPE], item is expected to refer to an invoice line item (subscriptions) or a payment
/// intent (one-time).
///
/// @param processorItemId The identifier of the item within the processor
/// @param processor The processor used
/// @param request The [ReceiptCredentialRequest] to generate a receipt for. Subsequent retries for the same
/// `processorItemId` should use the same request
/// @param now The current time
public void recordIssuance(
final String processorItemId,
final PaymentProvider processor,
final ReceiptCredentialRequest request,
final Instant now) throws WriteConflictException {
recordIssuance(processorItemId, processor, request, maxReceiptsPerSubscriptionPayment.get(processor), now);
}
private void recordIssuance(
final String processorItemId,
final PaymentProvider processor,
final ReceiptCredentialRequest request,
final int maxReceipts,
final Instant now) throws WriteConflictException {
final AttributeValue key = dynamoDbKey(processor, processorItemId);
final byte[] tag = generateIssuedReceiptTag(request);
@@ -89,7 +116,7 @@ public class IssuedReceiptsManager {
":tag", b(tag),
":singletonTag", AttributeValue.fromBs(List.of(SdkBytes.fromByteArray(tag))),
":exp", n(now.plus(expiration).getEpochSecond()),
":maxTags", n(maxIssuedReceiptsPerPaymentId.get(processor))))
":maxTags", n(maxReceipts)))
.build();
try {
dynamoDbClient.updateItem(updateItemRequest);
@@ -90,7 +90,7 @@ public class LoginPurchaseManager {
}
try {
issuedReceiptsManager.recordIssuance(paymentDetails.id(), paymentProvider, receiptCredentialRequest,
issuedReceiptsManager.recordOneTimeIssuance(paymentDetails.id(), paymentProvider, receiptCredentialRequest,
clock.instant());
} catch (WriteConflictException _) {
throw new SubscriptionReceiptAlreadyRedeemedException();
@@ -381,7 +381,7 @@ public record CommandDependencies(
configuration.getDynamoDbTables().getIssuedReceipts().getExpiration(),
dynamoDbClient,
configuration.getDynamoDbTables().getIssuedReceipts().getGenerator(),
configuration.getDynamoDbTables().getIssuedReceipts().getmaxIssuedReceiptsPerPaymentId());
configuration.getDynamoDbTables().getIssuedReceipts().getMaxReceiptsPerSubscriptionPayment());
final ServerSecretParams zkSecretParams = new ServerSecretParams(configuration.getGroupsZkConfig().serverSecret().value());
final ServerZkReceiptOperations zkReceiptOperations = new ServerZkReceiptOperations(zkSecretParams);
@@ -345,7 +345,7 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest
PaymentStatus.SUCCEEDED,
Instant.now(),
null)));
doThrow(WriteConflictException.class).when(ISSUED_RECEIPTS_MANAGER).recordIssuance(any(), any(), any(), any());
doThrow(WriteConflictException.class).when(ISSUED_RECEIPTS_MANAGER).recordOneTimeIssuance(any(), any(), any(), any());
try (Response response = RESOURCE_EXTENSION.target("/v1/subscription/boost/receipt_credentials")
.request()
@@ -308,7 +308,7 @@ public class OneTimeDonationsGrpcServiceTest extends
Optional.of(new PaymentDetails("id", oneTimeDonationConfiguration.boost().level(),
PaymentStatus.SUCCEEDED, clock.instant(), null)));
doThrow(WriteConflictException.class).when(issuedReceiptsManager)
.recordIssuance(any(), any(), any(), any());
.recordOneTimeIssuance(any(), any(), any(), any());
final CreateBoostReceiptCredentialsResponse response =
unauthenticatedServiceStub().createBoostReceiptCredentials(
@@ -120,6 +120,19 @@ class IssuedReceiptsManagerTest {
.isExactlyInstanceOf(WriteConflictException.class);
}
@ParameterizedTest
@EnumSource(PaymentProvider.class)
void testSingleIssuance(final PaymentProvider processor) throws WriteConflictException {
final Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
final ReceiptCredentialRequest request = randomReceiptCredentialRequest();
issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, request, now);
// Retrying with the same request should still succeed
issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, request, now);
assertThatThrownBy(
() -> issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, randomReceiptCredentialRequest(), now))
.isExactlyInstanceOf(WriteConflictException.class);
}
private GetItemResponse getItem(final PaymentProvider processor, final String itemId) {
final DynamoDbClient client = DYNAMO_DB_EXTENSION.getDynamoDbClient();
@@ -94,7 +94,7 @@ class LoginPurchaseManagerTest {
.truncatedTo(ChronoUnit.DAYS)
.getEpochSecond());
verify(issuedReceiptsManager).recordIssuance(
verify(issuedReceiptsManager).recordOneTimeIssuance(
PURCHASE_ID, PROVIDER, receiptCredentialRequestContext.getRequest(), NOW);
}
@@ -149,7 +149,7 @@ class LoginPurchaseManagerTest {
when(paymentProcessor.claimOneTimePurchase(PURCHASE_ID))
.thenReturn(Optional.of(new PaymentDetails(PURCHASE_ID, LEVEL, PaymentStatus.SUCCEEDED, PURCHASED_AT, null)));
doThrow(new WriteConflictException())
.when(issuedReceiptsManager).recordIssuance(any(), any(), any(), any());
.when(issuedReceiptsManager).recordOneTimeIssuance(any(), any(), any(), any());
assertThatExceptionOfType(SubscriptionReceiptAlreadyRedeemedException.class).isThrownBy(() -> {
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest());
});
+1 -1
View File
@@ -113,7 +113,7 @@ dynamoDbTables:
tableName: issued_receipts_test
expiration: P30D # Duration of time until rows expire
generator: abcdefg12345678= # random base64-encoded binary sequence
maxIssuedReceiptsPerPaymentId:
maxReceiptsPerSubscriptionPayment:
STRIPE: 1
BRAINTREE: 1
GOOGLE_PLAY_BILLING: 1