0
votes

I found this post: "Retrieve stripe data from stripe webhook event" and it tells you to get the object type you have to cast from StripeObject, like
Invoice invoice = (Invoice) event.getData().getObject();
Class: StripeWebhook

41    Event event = ApiResource.GSON.fromJson(request.body(), Event.class);
42    if (event.getType().equals("invoice.payment_failed") || event.getType().equals("charge.failed") || event.getType().equals("charge.refunded") || event.getType().equals("customer.subscription.deleted")) {
43 ->     Customer customer = (Customer)event.getData().getObject();`
44        Plan plan = (Plan)event.getData().getObject();
45    }

Stacktrace:

[qtp2043543300-35] ERROR spark.http.matching.GeneralError -
java.lang.ClassCastException: class com.stripe.model.Subscription cannot be cast to class com.stripe.model.Customer (com.stripe.model.Subscription and com.stripe.model.Customer are in unnamed module of loader 'app')
        at us.verif.bot.Stripe.StripeWebhook.lambda$startListener$0(StripeWebhook.java:43)
        at spark.RouteImpl$1.handle(RouteImpl.java:72)
        at spark.http.matching.Routes.execute(Routes.java:61)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:130)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1568)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
        at org.eclipse.jetty.server.Server.handle(Server.java:530)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
        at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
        at java.base/java.lang.Thread.run(Thread.java:834)

I don't understand why it would say this because it was never a Subscription object.

1

1 Answers

0
votes

Your problem is that the contents of your if statement will execute if the webhook describes an invoice, charge or subscription (customer.subscription describes a subscription instead of a customer https://stripe.com/docs/api/events/types#event_types-customer.subscription.deleted)

In this case, I suspect that you're receiving a customer.subscription.deleted webhook but are then trying to cast the Subscription object to a Customer object.

You need to refactor your code to handle each object type separately and cast them accordingly. Alternatively you could use the StripeObject type, which will do your casting automatically:

StripeObject stripeObject = event.getData().getObject();