Skip to content

Commit a2bdb87

Browse files
committed
fix refund issue implementation in stripe api adapter
1 parent cd0ffc7 commit a2bdb87

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/main/java/com/trynoice/api/subscription/upstream/StripeApi.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.stripe.exception.StripeException;
77
import com.stripe.model.Customer;
88
import com.stripe.model.Event;
9+
import com.stripe.model.Invoice;
910
import com.stripe.model.Refund;
1011
import com.stripe.model.Subscription;
1112
import com.stripe.model.checkout.Session;
@@ -20,6 +21,8 @@
2021
import lombok.NonNull;
2122
import lombok.val;
2223

24+
import java.util.Optional;
25+
2326
import static java.util.Objects.requireNonNullElse;
2427

2528
/**
@@ -138,14 +141,21 @@ public void refundSubscription(@NonNull String id) throws StripeException {
138141
.build(),
139142
null);
140143

141-
try {
142-
Refund.create(
143-
RefundCreateParams.builder()
144-
.setCharge(subscription.getLatestInvoiceObject().getCharge())
145-
.build());
146-
} catch (StripeException e) {
147-
if (!"charge_already_refunded".equals(e.getCode())) {
148-
throw e;
144+
// charge may be null if the latest invoice is for a trial period.
145+
val charge = Optional.ofNullable(subscription.getLatestInvoiceObject())
146+
.map(Invoice::getCharge)
147+
.orElse(null);
148+
149+
if (charge != null) {
150+
try {
151+
Refund.create(
152+
RefundCreateParams.builder()
153+
.setCharge(charge)
154+
.build());
155+
} catch (StripeException e) {
156+
if (!"charge_already_refunded".equals(e.getCode())) {
157+
throw e;
158+
}
149159
}
150160
}
151161

0 commit comments

Comments
 (0)