如何在Java中从Stripe接收Webhook [英] How to Receive Webhook from Stripe in Java

查看:1263
本文介绍了如何在Java中从Stripe接收Webhook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Stripe Payments的发帖请求来接收Webhook.处理它的java方法如下:

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;
}

但是 Stripe事件总是返回所有空值:

But the Stripe Event always comes back with all null values:

<com.stripe.model.Event@315899720 id=null> JSON: {
  "id": null,
  "type": null,
  "user_id": null,
  "livemode": null,
  "created": null,
  "data": null,
  "pending_webhooks": null
}

如果该方法改为接收字符串,并使用@RequestBody:

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;
}

在这里,它会打印不包含空值的json.这是正在打印的请求的一部分:

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",
...
}

但是将@RequestBody与 Stripe Event 参数一起使用会产生400错误的语法.

But using @RequestBody with a Stripe Event parameter gives a 400: bad syntax.

那我为什么不能接受正确的类型,即 Stripe Event 作为参数?

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

推荐答案

这就是我所做的:

Java方法仍然将Event作为json字符串接收.然后,我使用了Stripe的自定义gson适配器,并获得了带有以下内容的事件:

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);

其中 stripeJsonEvent 是webhook端点接受的json字符串.

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

这篇关于如何在Java中从Stripe接收Webhook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆