diff --git a/service/config/sample.yml b/service/config/sample.yml index a0fcf8a00..e099df509 100644 --- a/service/config/sample.yml +++ b/service/config/sample.yml @@ -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 diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index d6ab833a3..d3dd8723e 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -813,7 +813,7 @@ public class WhisperServerService extends Application maxIssuedReceiptsPerPaymentId; + /// The maximum number of receipts that may be issued for a single subscription payment. + private final EnumMap maxReceiptsPerSubscriptionPayment; public IssuedReceiptsTableConfiguration( @JsonProperty("tableName") final String tableName, @JsonProperty("expiration") final Duration expiration, @JsonProperty("generator") final byte[] generator, - @JsonProperty("maxIssuedReceiptsPerPaymentId") final Map maxIssuedReceiptsPerPaymentId) { + @JsonProperty("maxReceiptsPerSubscriptionPayment") final Map 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 getmaxIssuedReceiptsPerPaymentId() { - return maxIssuedReceiptsPerPaymentId; + public EnumMap getMaxReceiptsPerSubscriptionPayment() { + return maxReceiptsPerSubscriptionPayment; } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java index fcead5cad..85cdfae68 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java @@ -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); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcService.java index d8148b347..7a15f367d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcService.java @@ -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() diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java index 8bee5d966..de9fdc1a2 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManager.java @@ -44,34 +44,61 @@ public class IssuedReceiptsManager { private final Duration expiration; private final DynamoDbClient dynamoDbClient; private final byte[] receiptTagGenerator; - private final EnumMap maxIssuedReceiptsPerPaymentId; + private final EnumMap maxReceiptsPerSubscriptionPayment; public IssuedReceiptsManager( @Nonnull final String table, @Nonnull final Duration expiration, @Nonnull final DynamoDbClient dynamoDbClient, @Nonnull final byte[] receiptTagGenerator, - @Nonnull final EnumMap maxIssuedReceiptsPerPaymentId) { + @Nonnull final EnumMap 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); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManager.java index e00825549..ff6588343 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManager.java @@ -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(); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java index a4c14b582..d7217d188 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/CommandDependencies.java @@ -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); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java index bfab15691..754cded88 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java @@ -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() diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcServiceTest.java index 524b7b409..d136278b7 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/OneTimeDonationsGrpcServiceTest.java @@ -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( diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManagerTest.java index 18e626462..6b261954b 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/IssuedReceiptsManagerTest.java @@ -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(); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManagerTest.java index 9112b8259..af3fbb16d 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/subscriptions/LoginPurchaseManagerTest.java @@ -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()); }); diff --git a/service/src/test/resources/config/test.yml b/service/src/test/resources/config/test.yml index e39e42cdb..ee314ccb6 100644 --- a/service/src/test/resources/config/test.yml +++ b/service/src/test/resources/config/test.yml @@ -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