Skip to content

Commit

Permalink
migrate to StripeClient from Stripe Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed Jun 30, 2024
1 parent 0d1d2c6 commit cb963a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/main/java/com/trynoice/api/subscription/upstream/StripeApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@


import com.stripe.Stripe;
import com.stripe.StripeClient;
import com.stripe.exception.SignatureVerificationException;
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;
import com.stripe.net.Webhook;
import com.stripe.param.CustomerUpdateParams;
import com.stripe.param.RefundCreateParams;
import com.stripe.param.SubscriptionCancelParams;
Expand All @@ -32,8 +30,10 @@
*/
public class StripeApi {

private final StripeClient client;

public StripeApi(@NonNull String apiKey) {
Stripe.apiKey = apiKey;
client = new StripeClient(apiKey);
}

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ public Session createCheckoutSession(
String stripeCustomerId,
Long trialPeriodDays
) throws StripeException {
return Session.create(
return client.checkout().sessions().create(
new SessionCreateParams.Builder()
.setSuccessUrl(successUrl)
.setCancelUrl(cancelUrl)
Expand All @@ -92,32 +92,32 @@ public Session createCheckoutSession(
}

/**
* @see Webhook#constructEvent(String, String, String)
* @see StripeClient#constructEvent(String, String, String)
*/
@NonNull
public Event decodeWebhookPayload(
@NonNull String payload,
@NonNull String signature,
@NonNull String secret
) throws SignatureVerificationException {
return Webhook.constructEvent(payload, signature, secret);
return client.constructEvent(payload, signature, secret);
}

/**
* @see Subscription#retrieve(String)
* @see com.stripe.service.SubscriptionService#retrieve(String)
*/
@NonNull
public Subscription getSubscription(@NonNull String id) throws StripeException {
return Subscription.retrieve(id);
return client.subscriptions().retrieve(id);
}

/**
* Marks an uncancelled subscription to be cancelled at the end of the current billing cycle.
*
* @param id id of the subscription to cancel.
* @throws StripeException on Stripe API errors.
* @see Subscription#update(SubscriptionUpdateParams)
* @see Subscription#cancel(SubscriptionCancelParams)
* @see com.stripe.service.SubscriptionService#update(String, SubscriptionUpdateParams)
* @see com.stripe.service.SubscriptionService#cancel(String, SubscriptionCancelParams)
*/
public void cancelSubscription(@NonNull String id) throws StripeException {
val subscription = getSubscription(id);
Expand All @@ -126,8 +126,10 @@ public void cancelSubscription(@NonNull String id) throws StripeException {
}

if (!requireNonNullElse(subscription.getCancelAtPeriodEnd(), false)) {
subscription.update(
SubscriptionUpdateParams.builder()
client.subscriptions()
.update(
id,
SubscriptionUpdateParams.builder()
.setCancelAtPeriodEnd(true)
.build());
}
Expand All @@ -140,7 +142,7 @@ public void cancelSubscription(@NonNull String id) throws StripeException {
* @throws StripeException on Stripe API errors.
*/
public void refundSubscription(@NonNull String id) throws StripeException {
val subscription = Subscription.retrieve(
val subscription = client.subscriptions().retrieve(
id,
SubscriptionRetrieveParams.builder()
.addExpand("latest_invoice")
Expand All @@ -154,7 +156,7 @@ public void refundSubscription(@NonNull String id) throws StripeException {

if (charge != null) {
try {
Refund.create(
client.refunds().create(
RefundCreateParams.builder()
.setCharge(charge)
.build());
Expand All @@ -166,19 +168,19 @@ public void refundSubscription(@NonNull String id) throws StripeException {
}

if (!"canceled".equals(subscription.getStatus())) {
subscription.cancel();
client.subscriptions().cancel(id);
}
}

/**
* @see com.stripe.model.billingportal.Session#create(com.stripe.param.billingportal.SessionCreateParams)
* @see com.stripe.service.billingportal.SessionService#create(com.stripe.param.billingportal.SessionCreateParams)
*/
@NonNull
public com.stripe.model.billingportal.Session createCustomerPortalSession(
@NonNull String customerId,
String returnUrl
) throws StripeException {
return com.stripe.model.billingportal.Session.create(
return client.billingPortal().sessions().create(
com.stripe.param.billingportal.SessionCreateParams.builder()
.setCustomer(customerId)
.setReturnUrl(returnUrl)
Expand All @@ -193,7 +195,8 @@ public com.stripe.model.billingportal.Session createCustomerPortalSession(
* @throws StripeException on upstream errors.
*/
public void resetCustomerNameAndEmail(@NonNull String customerId) throws StripeException {
Customer.retrieve(customerId).update(
client.customers().update(
customerId,
CustomerUpdateParams.builder()
.setName(EmptyParam.EMPTY)
.setEmail(EmptyParam.EMPTY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
UPDATE subscription_plan
SET provided_id = 'price_1PXNj4SEeVq01jORrGAuBGFQ'
WHERE provided_id = 'price_1Ko0DFSEeVq01jORuGMXnINT';

UPDATE subscription_plan
SET provided_id = 'price_1PXNj4SEeVq01jORx6Pv3Fe5'
WHERE provided_id = 'price_1Ko0DFSEeVq01jORt7saWcXM';

UPDATE subscription_plan
SET provided_id = 'price_1PXNj4SEeVq01jORJ98swlrA'
WHERE provided_id = 'price_1Ko0DFSEeVq01jOR9aCG7EM8';

UPDATE subscription_plan
SET provided_id = 'price_1PXNj4SEeVq01jORDhHqsaEK'
WHERE provided_id = 'price_1Ko0DFSEeVq01jORgHlVkTEN';

0 comments on commit cb963a4

Please sign in to comment.