投射给客户时出现条带化Java API Webhook错误

时间:2019-03-05 03:00:32

标签: java json stripe-payments webhooks

我发现了这篇文章:“ Retrieve stripe data from stripe webhook event”,它告诉您必须从StripeObject强制转换为对象类型,例如
Invoice invoice = (Invoice) event.getData().getObject();
类: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)

我不明白为什么这么说,因为它从来不是Subscription对象。

1 个答案:

答案 0 :(得分:0)

您的问题是,如果Webhook描述发票,费用或订阅(if描述订阅而不是客户https://stripe.com/docs/api/events/types#event_types-customer.subscription.deleted),您的customer.subscription语句的内容就会执行

在这种情况下,我怀疑您正在接收customer.subscription.deleted Webhook,但随后尝试将Subscription对象转换为Customer对象。

您需要重构代码以分别处理每种对象类型,并进行相应的转换。或者,您可以使用StripeObject类型,它将自动进行投射:

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