简体   繁体   English

从条带webhook事件中检索条带数据

[英]Retrieve stripe data from stripe webhook event

When implementing stripe webhook in java, I am successful in getting the event object in JSON format. 在java中实现条带webhook时,我成功地以JSON格式获取事件对象。 The problem is I am not able to get the details like the amount, subscription_id, attributes which are in nested JSON. 问题是我无法获得嵌套JSON中的amount,subscription_id,属性等详细信息。 Getting these values from the class object is also not available. 从类对象获取这些值也不可用。 Could you please tell me how to extract these values 你能告诉我如何提取这些价值观吗?

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

}

And I get the following response :- 我收到以下回复: -

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
}

I want to get the values like the customer_id , subscription_id , etc. But when I try to get the data using the event object, I couldn't simply do as event.get.... . 我想得到像customer_idsubscription_id等的值。但是当我尝试使用事件对象获取数据时,我不能简单地做为event.get.... How would I extract the data. 我如何提取数据。

Stripe sends event objects to your webhook handler. Stripe将事件对象发送到您的webhook处理程序。 Each event object carries another object in its data.object attribute. 每个事件对象在其data.object属性中携带另一个对象。 The type of that object depends on the type of the event: for charge.* events, it'll be a charge object , for invoice.* event, it'll be an invoice object , etc. 该对象的类型取决于事件的类型:对于charge.*事件,它将是一个充电对象 ,对于invoice.* event,它将是一个发票对象等。

With Stripe's Java bindings , you can automatically get an object of the correct type: 使用Stripe的Java绑定 ,您可以自动获取正确类型的对象:

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

stripeObject will be automatically cast to the correct type. stripeObject将自动转换为正确的类型。

Alternatively, you can do the casting yourself: 或者,您可以自己进行投射:

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

Well I had solved this problem. 好吧,我已经解决了这个问题。 The real issue was that I was not able to retrive the object id , in my case the invoiceid (in_18qcFkElSMaq70BZy1US7o3g). 真正的问题是我无法检索object id ,在我的例子中是invoiceid (in_18qcFkElSMaq70BZy1US7o3g)。 This id is the id for the event occured. 此id是发生事件的ID。 Meaning if it is a payment successful event, then the object id will be the charge id . 这意味着如果它是payment successful事件,那么object id将是charge id I had to convert the event object to map then get the required attribute. 我必须将event对象转换为map然后获取所需的属性。 Below is the complete code snippet of what I had done to solve the problem. 下面是我为解决问题所做的完整代码片段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM