2016-09-06 63 views
4

在java中實現條帶webhook時,我成功獲取了JSON格式的事件對象。問題是我無法獲取像嵌套JSON中的金額,subscription_id和屬性的詳細信息。從類對象獲取這些值也是不可用的。你能告訴我如何提取這些值從條帶webhook事件中恢復條帶數據

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 : <[email protected] 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....。我將如何提取數據。

謝謝你提前

+1

我不是一個Java開發使不熟悉這個代碼太多,但是當條紋讓你的ID,你需要做的另一個API調用來獲取這些細節。 – cyberwombat

+0

問題是我無法獲得發票編號。 – viper

回答

0

那麼我已經解決了這個問題。真正的問題是我無法檢索object id,在我的情況下是invoiceid(in_18qcFkElSMaq70BZy1US7o3g)。此ID是發生事件的ID。意思是如果它是payment successful事件,那麼object id將是charge id。我必須將event對象轉換爲map,然後獲取必需的屬性。以下是我爲解決問題所做的完整代碼片段。

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

條紋將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(); 
+0

謝謝你的回答。我很久以前就在看這樣的東西。然後我想出了一個我在這篇文章中提到的替代解決方案作爲答案。 – viper

+0

'Invoice invoice = event.getData()。getObject();'應該是'Invoice invoice =(Invoice)event.getData()。getObject();'。您仍然需要將其轉換爲「發票」對象。 – mrgrumpy22