2
2
3
3
4
4
import com .stripe .Stripe ;
5
+ import com .stripe .StripeClient ;
5
6
import com .stripe .exception .SignatureVerificationException ;
6
7
import com .stripe .exception .StripeException ;
7
- import com .stripe .model .Customer ;
8
8
import com .stripe .model .Event ;
9
9
import com .stripe .model .Invoice ;
10
- import com .stripe .model .Refund ;
11
10
import com .stripe .model .Subscription ;
12
11
import com .stripe .model .checkout .Session ;
13
- import com .stripe .net .Webhook ;
14
12
import com .stripe .param .CustomerUpdateParams ;
15
13
import com .stripe .param .RefundCreateParams ;
16
14
import com .stripe .param .SubscriptionCancelParams ;
32
30
*/
33
31
public class StripeApi {
34
32
33
+ private final StripeClient client ;
34
+
35
35
public StripeApi (@ NonNull String apiKey ) {
36
- Stripe . apiKey = apiKey ;
36
+ client = new StripeClient ( apiKey ) ;
37
37
}
38
38
39
39
/**
@@ -70,7 +70,7 @@ public Session createCheckoutSession(
70
70
String stripeCustomerId ,
71
71
Long trialPeriodDays
72
72
) throws StripeException {
73
- return Session .create (
73
+ return client . checkout (). sessions () .create (
74
74
new SessionCreateParams .Builder ()
75
75
.setSuccessUrl (successUrl )
76
76
.setCancelUrl (cancelUrl )
@@ -92,32 +92,32 @@ public Session createCheckoutSession(
92
92
}
93
93
94
94
/**
95
- * @see Webhook #constructEvent(String, String, String)
95
+ * @see StripeClient #constructEvent(String, String, String)
96
96
*/
97
97
@ NonNull
98
98
public Event decodeWebhookPayload (
99
99
@ NonNull String payload ,
100
100
@ NonNull String signature ,
101
101
@ NonNull String secret
102
102
) throws SignatureVerificationException {
103
- return Webhook .constructEvent (payload , signature , secret );
103
+ return client .constructEvent (payload , signature , secret );
104
104
}
105
105
106
106
/**
107
- * @see Subscription #retrieve(String)
107
+ * @see com.stripe.service.SubscriptionService #retrieve(String)
108
108
*/
109
109
@ NonNull
110
110
public Subscription getSubscription (@ NonNull String id ) throws StripeException {
111
- return Subscription .retrieve (id );
111
+ return client . subscriptions () .retrieve (id );
112
112
}
113
113
114
114
/**
115
115
* Marks an uncancelled subscription to be cancelled at the end of the current billing cycle.
116
116
*
117
117
* @param id id of the subscription to cancel.
118
118
* @throws StripeException on Stripe API errors.
119
- * @see Subscription #update(SubscriptionUpdateParams)
120
- * @see Subscription #cancel(SubscriptionCancelParams)
119
+ * @see com.stripe.service.SubscriptionService #update(String, SubscriptionUpdateParams)
120
+ * @see com.stripe.service.SubscriptionService #cancel(String, SubscriptionCancelParams)
121
121
*/
122
122
public void cancelSubscription (@ NonNull String id ) throws StripeException {
123
123
val subscription = getSubscription (id );
@@ -126,8 +126,10 @@ public void cancelSubscription(@NonNull String id) throws StripeException {
126
126
}
127
127
128
128
if (!requireNonNullElse (subscription .getCancelAtPeriodEnd (), false )) {
129
- subscription .update (
130
- SubscriptionUpdateParams .builder ()
129
+ client .subscriptions ()
130
+ .update (
131
+ id ,
132
+ SubscriptionUpdateParams .builder ()
131
133
.setCancelAtPeriodEnd (true )
132
134
.build ());
133
135
}
@@ -140,7 +142,7 @@ public void cancelSubscription(@NonNull String id) throws StripeException {
140
142
* @throws StripeException on Stripe API errors.
141
143
*/
142
144
public void refundSubscription (@ NonNull String id ) throws StripeException {
143
- val subscription = Subscription .retrieve (
145
+ val subscription = client . subscriptions () .retrieve (
144
146
id ,
145
147
SubscriptionRetrieveParams .builder ()
146
148
.addExpand ("latest_invoice" )
@@ -154,7 +156,7 @@ public void refundSubscription(@NonNull String id) throws StripeException {
154
156
155
157
if (charge != null ) {
156
158
try {
157
- Refund .create (
159
+ client . refunds () .create (
158
160
RefundCreateParams .builder ()
159
161
.setCharge (charge )
160
162
.build ());
@@ -166,19 +168,19 @@ public void refundSubscription(@NonNull String id) throws StripeException {
166
168
}
167
169
168
170
if (!"canceled" .equals (subscription .getStatus ())) {
169
- subscription . cancel ();
171
+ client . subscriptions (). cancel (id );
170
172
}
171
173
}
172
174
173
175
/**
174
- * @see com.stripe.model .billingportal.Session #create(com.stripe.param.billingportal.SessionCreateParams)
176
+ * @see com.stripe.service .billingportal.SessionService #create(com.stripe.param.billingportal.SessionCreateParams)
175
177
*/
176
178
@ NonNull
177
179
public com .stripe .model .billingportal .Session createCustomerPortalSession (
178
180
@ NonNull String customerId ,
179
181
String returnUrl
180
182
) throws StripeException {
181
- return com . stripe . model . billingportal . Session .create (
183
+ return client . billingPortal (). sessions () .create (
182
184
com .stripe .param .billingportal .SessionCreateParams .builder ()
183
185
.setCustomer (customerId )
184
186
.setReturnUrl (returnUrl )
@@ -193,7 +195,8 @@ public com.stripe.model.billingportal.Session createCustomerPortalSession(
193
195
* @throws StripeException on upstream errors.
194
196
*/
195
197
public void resetCustomerNameAndEmail (@ NonNull String customerId ) throws StripeException {
196
- Customer .retrieve (customerId ).update (
198
+ client .customers ().update (
199
+ customerId ,
197
200
CustomerUpdateParams .builder ()
198
201
.setName (EmptyParam .EMPTY )
199
202
.setEmail (EmptyParam .EMPTY )
0 commit comments