问题描述
好吧,我已经解决了这个问题。真正的问题是,我没能以检索的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);
}
解决方法
在Java中实现条纹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,"discountable": true,"livemode": false,"metadata": {},"period": {
"end": 1473226524,"start": 1473140124
},"plan": {
"id": "aug 19 01","created": 1472448923,"interval": "day","interval_count": 1,"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
},"next_payment_attempt": null,"paid": true,"period_end": 1473140124,"period_start": 1473053724,"receipt_number": null,"starting_balance": 0,"subscription": "sub_95uFmJLQM3jFwP","subscription_proration_date": null,"subtotal": 4100,"tax": null,"tax_percent": null,"total": 4100,"webhooks_delivered_at": 1473140184
},"previous_attributes": null
},"pending_webhooks": 1,"request": null,"type": "invoice.payment_succeeded","user_id": null
}
我想要得到的值一样customer_id
,subscription_id
等等。但是,当我试图让使用事件对象中的数据,我不能简单地做的event.get....
。我将如何提取数据。