forked from PingPlusPlus/pingpp-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhooks.java
More file actions
69 lines (61 loc) · 2.27 KB
/
Webhooks.java
File metadata and controls
69 lines (61 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.pingplusplus.model;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.pingplusplus.net.APIResource;
/**
* Created by sunkai on 15/5/11.
*/
public class Webhooks {
class InnerObject {
String type;
}
/**
* 解析event 中的object
*
* @param eventStr
* @return
*/
public static Object parseEvnet(String eventStr) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
InnerObject innerObject = gson.fromJson(eventStr, InnerObject.class);
if (null == innerObject) {
return null;
}
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(eventStr).getAsJsonObject();
String eventData = jsonObject.get("data").getAsJsonObject().get("object").getAsJsonObject().toString();
if ("summary.daily.available".equals(innerObject.type)) {
return gson.fromJson(eventData, Summary.class);
} else if ("summary.weekly.available".equals(innerObject.type)) {
return gson.fromJson(eventData, Summary.class);
} else if ("summary.monthly.available".equals(innerObject.type)) {
return gson.fromJson(eventData, Summary.class);
} else if ("charge.succeeded".equals(innerObject.type)) {
return APIResource.GSON.fromJson(eventData, Charge.class);
} else if ("refund.succeeded".equals(innerObject.type)) {
return gson.fromJson(eventData, Refund.class);
}
return null;
}
/**
* 解析event,返回Event对象
*
* @param eventStr
* @return
*/
public static Event eventParse(String eventStr) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
InnerObject innerObject = gson.fromJson(eventStr, InnerObject.class);
if (innerObject == null || innerObject.type == null || innerObject.type.isEmpty()) {
return null;
}
return gson.fromJson(eventStr, Event.class);
}
}