Stripe Java API 投射到客户时出现 Webhook 错误
Stripe Java API Webhook Error when casting to a Customer
我找到了这个 post: "" 它告诉你要从 StripeObject 中获取你必须转换的对象类型,比如
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 }
堆栈跟踪:
[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[=13=](StripeWebhook.java:43)
at spark.RouteImpl.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.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.run(QueuedThreadPool.java:626)
at java.base/java.lang.Thread.run(Thread.java:834)
我不明白为什么会这样说,因为它从来都不是订阅对象。
您的问题是,如果 webhook 描述了发票、收费或订阅(customer.subscription
描述的是订阅而不是客户 https://stripe.com/docs/api/events/types#event_types-customer.subscription.deleted),您的 if
语句的内容将会执行
在这种情况下,我怀疑您正在接收 customer.subscription.deleted
webhook,但随后试图将 Subscription
对象转换为 Customer
对象。
您需要重构代码以分别处理每个对象类型并相应地转换它们。或者,您可以使用 StripeObject
类型,它将自动进行转换:
StripeObject stripeObject = event.getData().getObject();
我找到了这个 post: "
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 }
堆栈跟踪:
[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[=13=](StripeWebhook.java:43)
at spark.RouteImpl.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.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.run(QueuedThreadPool.java:626)
at java.base/java.lang.Thread.run(Thread.java:834)
我不明白为什么会这样说,因为它从来都不是订阅对象。
您的问题是,如果 webhook 描述了发票、收费或订阅(customer.subscription
描述的是订阅而不是客户 https://stripe.com/docs/api/events/types#event_types-customer.subscription.deleted),您的 if
语句的内容将会执行
在这种情况下,我怀疑您正在接收 customer.subscription.deleted
webhook,但随后试图将 Subscription
对象转换为 Customer
对象。
您需要重构代码以分别处理每个对象类型并相应地转换它们。或者,您可以使用 StripeObject
类型,它将自动进行转换:
StripeObject stripeObject = event.getData().getObject();