From cb963a44c23059689894912fc72c98ddbc96f21f Mon Sep 17 00:00:00 2001 From: Ashutosh Gangwar Date: Sun, 30 Jun 2024 19:07:33 +0530 Subject: [PATCH] migrate to StripeClient from Stripe Java API --- .../api/subscription/upstream/StripeApi.java | 41 ++++++++++--------- ...1__Update_test_mode_subscription_plans.sql | 15 +++++++ 2 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 src/main/resources/db/migration/development/V4.1__Update_test_mode_subscription_plans.sql diff --git a/src/main/java/com/trynoice/api/subscription/upstream/StripeApi.java b/src/main/java/com/trynoice/api/subscription/upstream/StripeApi.java index c7d4484..e01bcb2 100644 --- a/src/main/java/com/trynoice/api/subscription/upstream/StripeApi.java +++ b/src/main/java/com/trynoice/api/subscription/upstream/StripeApi.java @@ -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; @@ -32,8 +30,10 @@ */ public class StripeApi { + private final StripeClient client; + public StripeApi(@NonNull String apiKey) { - Stripe.apiKey = apiKey; + client = new StripeClient(apiKey); } /** @@ -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) @@ -92,7 +92,7 @@ public Session createCheckoutSession( } /** - * @see Webhook#constructEvent(String, String, String) + * @see StripeClient#constructEvent(String, String, String) */ @NonNull public Event decodeWebhookPayload( @@ -100,15 +100,15 @@ public Event decodeWebhookPayload( @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); } /** @@ -116,8 +116,8 @@ public Subscription getSubscription(@NonNull String id) throws StripeException { * * @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); @@ -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()); } @@ -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") @@ -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()); @@ -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) @@ -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) diff --git a/src/main/resources/db/migration/development/V4.1__Update_test_mode_subscription_plans.sql b/src/main/resources/db/migration/development/V4.1__Update_test_mode_subscription_plans.sql new file mode 100644 index 0000000..0a7ea54 --- /dev/null +++ b/src/main/resources/db/migration/development/V4.1__Update_test_mode_subscription_plans.sql @@ -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';