Skip to content

Commit

Permalink
Use Braintree subscription status alongside transaction status to det…
Browse files Browse the repository at this point in the history
…ermine 204 and 402 responses for `/v1/subscription/{subscriberId}/receipt_credentials`
  • Loading branch information
katherine-signal authored Nov 16, 2023
1 parent c85ddae commit 216ac72
Showing 1 changed file with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,27 +523,35 @@ private CompletableFuture<Void> cancelSubscriptionAtEndOfCurrentPeriod(Subscript

@Override
public CompletableFuture<ReceiptItem> 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) {
Expand All @@ -553,12 +561,6 @@ private static Subscription getSubscription(Object subscriptionObj) {
return subscription;
}

public CompletableFuture<Optional<Transaction>> getLatestTransactionForSubscription(String subscriptionId) {
return getSubscription(subscriptionId)
.thenApply(BraintreeManager::getSubscription)
.thenApply(this::getLatestTransactionForSubscription);
}

private Optional<Transaction> getLatestTransactionForSubscription(Subscription subscription) {
return subscription.getTransactions().stream()
.max(Comparator.comparing(Transaction::getCreatedAt));
Expand Down

0 comments on commit 216ac72

Please sign in to comment.