Skip to content

Commit

Permalink
fix refund issue implementation in stripe api adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed Aug 25, 2022
1 parent cd0ffc7 commit a2bdb87
Showing 1 changed file with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import com.stripe.model.Event;
import com.stripe.model.Invoice;
import com.stripe.model.Refund;
import com.stripe.model.Subscription;
import com.stripe.model.checkout.Session;
Expand All @@ -20,6 +21,8 @@
import lombok.NonNull;
import lombok.val;

import java.util.Optional;

import static java.util.Objects.requireNonNullElse;

/**
Expand Down Expand Up @@ -138,14 +141,21 @@ public void refundSubscription(@NonNull String id) throws StripeException {
.build(),
null);

try {
Refund.create(
RefundCreateParams.builder()
.setCharge(subscription.getLatestInvoiceObject().getCharge())
.build());
} catch (StripeException e) {
if (!"charge_already_refunded".equals(e.getCode())) {
throw e;
// charge may be null if the latest invoice is for a trial period.
val charge = Optional.ofNullable(subscription.getLatestInvoiceObject())
.map(Invoice::getCharge)
.orElse(null);

if (charge != null) {
try {
Refund.create(
RefundCreateParams.builder()
.setCharge(charge)
.build());
} catch (StripeException e) {
if (!"charge_already_refunded".equals(e.getCode())) {
throw e;
}
}
}

Expand Down

0 comments on commit a2bdb87

Please sign in to comment.