stripe java实现,如何在Java中从Stripe接收Webhook

编程知识 行业动态 更新时间:2024-06-13 00:22:27

I am trying to receive a webhook via a post request from Stripe Payments. The java method to process it looks like this:

@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:

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:

@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. 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.

So why can't I take in the correct type, a Stripe Event, as the parameter?

解决方案

Here's what I did:

The Java method still takes in the Event as a json String. Then I used Stripe's custom gson adapter and got the Event with:

Event event = Event.gson.fromJson(stripeJsonEvent, Event.class);

Where stripeJsonEvent is the string of json taken in by the webhook endpoint.

更多推荐

stripe java实现,如何在Java中从Stripe接收Webhook

本文发布于:2023-04-02 12:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/4f998798dbcb4383cc499a4ed2e3299f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   java   stripe   Webhook   Stripe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!