File tree Expand file tree Collapse file tree
src/main/java/com/trynoice/api/subscription/upstream Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66import com .stripe .exception .StripeException ;
77import com .stripe .model .Customer ;
88import com .stripe .model .Event ;
9+ import com .stripe .model .Invoice ;
910import com .stripe .model .Refund ;
1011import com .stripe .model .Subscription ;
1112import com .stripe .model .checkout .Session ;
2021import lombok .NonNull ;
2122import lombok .val ;
2223
24+ import java .util .Optional ;
25+
2326import 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
You can’t perform that action at this time.
0 commit comments