[英]How to Receive Webhook from Stripe in Java
I am trying to receive a webhook via a post request from Stripe Payments.我正在尝试通过来自 Stripe Payments 的帖子请求接收网络钩子。 The java method to process it looks like this:
处理它的java方法如下所示:
@ResponseBody
@RequestMapping( consumes="application/json",
produces="application/json",
method=RequestMethod.POST,
value="stripeWebhookEndpoint")
public String stripeWebhookEndpoint(Event event){
logger.info("\n\n" + event.toString());
logger.info("\n\n" + event.getId());
return null;
}
But the Stripe Event always comes back with all null values:但是Stripe 事件总是返回所有空值:
<com.stripe.model.Event@315899720 id=null> JSON: {
"id": null,
"type": null,
"user_id": null,
"livemode": null,
"created": null,
"data": null,
"pending_webhooks": null
}
If the method receives a String instead,and using @RequestBody:如果该方法接收一个字符串,并使用@RequestBody:
@ResponseBody
@RequestMapping( consumes="application/json",
produces="application/json",
method=RequestMethod.POST,
value="stripeWebhookEndpoint")
public String stripeWebhookEndpoint(@RequestBody String json){
logger.info(json);
return null;
}
Here, it prints the json without null values.在这里,它打印没有空值的 json。 Here's part of the request being printed:
这是正在打印的请求的一部分:
{
"created": 1326853478,
"livemode": false,
"id": "evt_00000000000000",
"type": "charge.succeeded",
"object": "event",
"request": null,
"data": {
"object": {
"id": "ch_00000000000000",
"object": "charge",
"created": 1389985862,
"livemode": false,
"paid": true,
"amount": 2995,
"currency": "usd",
...
}
But using @RequestBody with a Stripe Event parameter gives a 400: bad syntax.但是将 @RequestBody 与Stripe Event参数一起使用会产生 400: 错误的语法。
So why can't I take in the correct type, a Stripe Event , as the parameter?那么为什么我不能采用正确的类型Stripe Event作为参数呢?
Here's what I did:这是我所做的:
The Java method still takes in the Event as a json String. Java 方法仍将事件作为 json 字符串接收。 Then I used Stripe's custom gson adapter and got the Event with:
然后我使用了 Stripe 的自定义 gson 适配器并获得了事件:
Event event = Event.gson.fromJson(stripeJsonEvent, Event.class);
Where stripeJsonEvent is the string of json taken in by the webhook endpoint.其中stripeJsonEvent是 webhook 端点接收的 json 字符串。
I have been looking for the same answer, so after looking at their own code, here is how they actually do it:我一直在寻找相同的答案,所以在查看了他们自己的代码后,他们实际上是这样做的:
String rawJson = IOUtils.toString(request.getInputStream());
Event event = APIResource.GSON.fromJson(rawJson, Event.class);
APIResource comes from their library (I am using 1.6.5) APIResource 来自他们的库(我使用的是 1.6.5)
public String stripeWebhookEndpoint(@RequestBody String json, HttpServletRequest request) {
String header = request.getHeader("Stripe-Signature");
String endpointSecret = "your stripe webhook secret";
try {
event = Webhook.constructEvent(json, header, endpointSecret);
System.err.println(event);
} catch (SignatureVerificationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
enter code here
return "";
}
In order to abstract all of the deserialization logic out of the controller I did the following:为了从控制器中抽象出所有反序列化逻辑,我执行了以下操作:
Created a custom deserializer创建了一个自定义解串器
public class StripeEventDeserializer extends JsonDeserializer<Event> {
private ObjectMapper mapper;
public StripeEventDeserializer(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override
public Event deserialize(JsonParser jp, DeserializationContext context) throws IOException {
ObjectNode root = mapper.readTree(jp);
Event event = ApiResource.GSON.fromJson(root.toString(), Event.class);
return event;
}
}
I then needed to add that deserializer to my ObjectMapper
config:然后我需要将该反序列化器添加到我的
ObjectMapper
配置中:
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Event.class, new StripeEventDeserializer(mapper));
mapper.registerModule(simpleModule);
I could then use @RequestBody
correctly on the Spring rest controller:然后我可以在 Spring rest 控制器上正确使用
@RequestBody
:
@PostMapping("/webhook")
public void webhook(@RequestBody Event stripeEvent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.