java - 从 stripe webhook 事件中检索 stripe 数据

标签 java json stripe-payments webhooks

在java中实现stripe webhook时,我成功获取了JSON格式的事件对象。问题是我无法获取嵌套 JSON 中的数量、subscription_id、属性等详细信息。从类对象中获取这些值也是不可用的。你能告诉我如何提取这些值吗

public void handle(HttpServletRequest request) {

    Stripe.apiKey = sk_test_XXXXXXXXXXXXXXXXXXXX;

    String rawJson = "";

    try {
            rawJson = IOUtils.toString(request.getInputStream());
        } 
        catch (IOException ex) {
            System.out.println("Error extracting json value : " + ex.getMessage());
         }
    Event event = APIResource.GSON.fromJson(rawJson, Event.class);
    System.out.println("Webhook event : " + event);

}

我得到以下回复:-

Webhook event : <com.stripe.model.Event@1462134034 id=evt_18qdEBElSMaq70BZlEwdDJG3> JSON: {
  "id": "evt_18qdEBElSMaq70BZlEwdDJG3",
  "api_version": "2016-07-06",
  "created": 1473143919,
  "data": {
    "object": {
      "id": "in_18qcFkElSMaq70BZy1US7o3g",
      "amount_due": 4100,
      "application_fee": null,
      "attempt_count": 1,
      "attempted": true,
      "charge": "ch_18qdEBElSMaq70BZIEQvJTPe",
      "closed": true,
      "created": null,
      "currency": "usd",
      "customer": "cus_95uFN7q2HzHN7j",
      "date": 1473140172,
      "description": null,
      "discount": null,
      "ending_balance": 0,
      "forgiven": false,
      "lines": {
        "data": [
          {
            "id": "sub_95uFmJLQM3jFwP",
            "amount": 4100,
            "currency": "usd",
            "description": null,
            "discountable": true,
            "livemode": false,
            "metadata": {},
            "period": {
              "end": 1473226524,
              "start": 1473140124
            },
            "plan": {
              "id": "aug 19 01",
              "amount": 4100,
              "created": 1472448923,
              "currency": "usd",
              "interval": "day",
              "interval_count": 1,
              "livemode": false,
              "metadata": {},
              "name": "Aug 19 plan. Better than paypal",
              "statement_descriptor": null,
              "trial_period_days": null,
              "statement_description": null
            },
            "proration": false,
            "quantity": 1,
            "subscription": null,
            "type": "subscription"
          }
        ],
        "total_count": 1,
        "has_more": false,
        "request_options": null,
        "request_params": null,
        "url": "/v1/invoices/in_18qcFkElSMaq70BZy1US7o3g/lines",
        "count": null
      },
      "livemode": false,
      "metadata": {},
      "next_payment_attempt": null,
      "paid": true,
      "period_end": 1473140124,
      "period_start": 1473053724,
      "receipt_number": null,
      "starting_balance": 0,
      "statement_descriptor": null,
      "subscription": "sub_95uFmJLQM3jFwP",
      "subscription_proration_date": null,
      "subtotal": 4100,
      "tax": null,
      "tax_percent": null,
      "total": 4100,
      "webhooks_delivered_at": 1473140184
    },
    "previous_attributes": null
  },
  "livemode": false,
  "pending_webhooks": 1,
  "request": null,
  "type": "invoice.payment_succeeded",
  "user_id": null
}

我想获取 customer_idsubscription_id 等值。但是当我尝试使用事件对象获取数据时,我不能简单地做作为 event.get.... 。我将如何提取数据。

最佳答案

Stripe 发送event objects到您的 webhook 处理程序。每个事件对象在其 data.object 中携带另一个对象属性。该对象的类型取决于事件的类型:对于 charge.*事件,它将是一个 charge object , 对于 invoice.*事件,它将是一个 invoice object

Stripe's Java bindings ,你可以自动得到一个正确类型的对象:

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

stripeObject 将自动转换为正确的类型。

或者,您可以自己进行转换:

if (event.getType().equals("invoice.payment_failed")) {
    Invoice invoice = event.getData().getObject();

关于java - 从 stripe webhook 事件中检索 stripe 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342251/

相关文章:

java - session 和事务的线程使用导致内存泄漏?

java - MinHeap 有 2 个字段...removeMin 并删除给定的键

python - ModelSerializer 是否应该按 View 编写?

json - 从 postgres 中的复杂嵌套结构中检索具有特定键名的 json 元素

php - 如何将Android与PHP和MySQL连接?

javascript - 无法从 StripePaymentIntent 对象检索元数据

java - 使 Struts2 redirectAction 使用相对路径而不是绝对路径

ruby-on-rails - 使用 Stripe for rails 检查充电是否成功

stripe-payments - Stripe API 不返回源,尽管根据仪表板有一些

java - 如何从输入生成随机值?