diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/BraintreeManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/BraintreeManager.java index 4c30a4de8..223d53a36 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/BraintreeManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/subscriptions/BraintreeManager.java @@ -523,27 +523,35 @@ private CompletableFuture cancelSubscriptionAtEndOfCurrentPeriod(Subscript @Override public CompletableFuture getReceiptItem(String subscriptionId) { + return getSubscription(subscriptionId) + .thenApply(BraintreeManager::getSubscription) + .thenApply(subscription -> getLatestTransactionForSubscription(subscription) + .map(transaction -> { + if (!getPaymentStatus(transaction.getStatus()).equals(PaymentStatus.SUCCEEDED)) { + final SubscriptionStatus subscriptionStatus = getSubscriptionStatus(subscription.getStatus()); + if (subscriptionStatus.equals(SubscriptionStatus.ACTIVE) || subscriptionStatus.equals(SubscriptionStatus.PAST_DUE)) { + throw new WebApplicationException(Response.Status.NO_CONTENT); + } - return getLatestTransactionForSubscription(subscriptionId).thenApply(maybeTransaction -> maybeTransaction.map(transaction -> { - - if (!getPaymentStatus(transaction.getStatus()).equals(PaymentStatus.SUCCEEDED)) { - throw new WebApplicationException(Response.Status.PAYMENT_REQUIRED); - } - - final Instant expiration = transaction.getSubscriptionDetails().getBillingPeriodEndDate().toInstant(); - final Plan plan = braintreeGateway.plan().find(transaction.getPlanId()); + throw new WebApplicationException(Response.status(Response.Status.PAYMENT_REQUIRED) + .entity(Map.of("chargeFailure", createChargeFailure(transaction))) + .build()); + } - final BraintreePlanMetadata metadata; - try { - metadata = SystemMapper.jsonMapper().readValue(plan.getDescription(), BraintreePlanMetadata.class); + final Instant expiration = transaction.getSubscriptionDetails().getBillingPeriodEndDate().toInstant(); + final Plan plan = braintreeGateway.plan().find(transaction.getPlanId()); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } + final BraintreePlanMetadata metadata; + try { + metadata = SystemMapper.jsonMapper().readValue(plan.getDescription(), BraintreePlanMetadata.class); - return new ReceiptItem(transaction.getId(), expiration, metadata.level()); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } - }).orElseThrow(() -> new WebApplicationException(Response.Status.NO_CONTENT))); + return new ReceiptItem(transaction.getId(), expiration, metadata.level()); + }) + .orElseThrow(() -> new WebApplicationException(Response.Status.NO_CONTENT))); } private static Subscription getSubscription(Object subscriptionObj) { @@ -553,12 +561,6 @@ private static Subscription getSubscription(Object subscriptionObj) { return subscription; } - public CompletableFuture> getLatestTransactionForSubscription(String subscriptionId) { - return getSubscription(subscriptionId) - .thenApply(BraintreeManager::getSubscription) - .thenApply(this::getLatestTransactionForSubscription); - } - private Optional getLatestTransactionForSubscription(Subscription subscription) { return subscription.getTransactions().stream() .max(Comparator.comparing(Transaction::getCreatedAt));