【问题标题】:Retrieve stripe data from stripe webhook event从条带 webhook 事件中检索条带数据
【发布时间】:2017-01-13 12:18:08
【问题描述】:

在java中实现stripe webhook时,我成功获取了JSON格式的事件对象。问题是我无法获取嵌套 JSON 中的金额、订阅 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.... 那样做。我将如何提取数据。

【问题讨论】:

  • 我不是 Java 开发人员,所以对这段代码不太熟悉,但是当 Stripe 为您提供 ID 时,您需要进行另一个 API 调用以获取这些详细信息。
  • 问题是我无法获取发票 ID。

标签: java json stripe-payments webhooks


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。我很久以前就在看这样的东西。然后我想出了一个我在这篇文章中提到的替代解决方案作为答案。
  • Invoice invoice = event.getData().getObject(); 应该是 Invoice invoice = (Invoice) event.getData().getObject();。您仍然需要将其转换为 Invoice 对象。
【解决方案2】:

好吧,我已经解决了这个问题。真正的问题是我无法检索object id,在我的情况下是invoiceid (in_18qcFkElSMaq70BZy1US7o3g)。此 id 是发生事件的 id。这意味着如果它是 payment successful 事件,那么 object id 将是 charge id 。我必须将 event 对象转换为 map 然后获取所需的属性。下面是我为解决问题所做的完整代码 sn-p。

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

    // Converting event object to map
    ObjectMapper m = new ObjectMapper();
    @SuppressWarnings("unchecked")
    Map<String, Object> props = m.convertValue(event.getData(), Map.class);

    // Getting required data
    Object dataMap = props.get("object");

    @SuppressWarnings("unchecked")
    Map<String, String> objectMapper = m.convertValue(dataMap, Map.class);

    String invoiceId = objectMapper.get("id");

    System.out.println("invoideId : " + invoiceId);
}

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 2018-02-14
    • 1970-01-01
    • 2022-10-18
    • 2022-07-29
    • 1970-01-01
    • 2018-11-29
    • 2021-12-23
    • 2012-09-09
    相关资源
    最近更新 更多