This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoogleCheckout.java
333 lines (289 loc) · 15.6 KB
/
GoogleCheckout.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package controllers;
import com.google.checkout.sdk.domain.*;
import com.google.checkout.sdk.notifications.Notification;
import models.GoogleCheckoutApiFactory;
import models.GoogleCheckoutInitializationException;
import models.PlayNotificationDispatcher;
import play.Logger;
import play.Play;
import play.mvc.Controller;
import play.mvc.Http;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class GoogleCheckout extends Controller {
public static void callback() {
try {
GoogleCheckoutApiFactory.build()
.handleNotification(new DelegatedNotificationDispatcher(request, response));
} catch (GoogleCheckoutInitializationException ex) {
Logger.error(ex, "Unable to create Google Checkout API");
error();
}
}
static class DelegatedNotificationDispatcher extends PlayNotificationDispatcher {
public DelegatedNotificationDispatcher(Http.Request request, Http.Response response) {
super(request, response);
}
@Override
protected boolean hasAlreadyHandled(String serialNumber, OrderSummary orderSummary, Notification notification) throws Exception {
Object result = NotificationHandler.alreadyHandled(serialNumber, orderSummary, notification);
return result != null && result.equals(true);
}
@Override
protected void rememberSerialNumber(String serialNumber, OrderSummary orderSummary, Notification notification) throws Exception {
NotificationHandler.rememberSerialNumber(serialNumber, orderSummary, notification);
}
@Override
protected void onNewOrderNotification(OrderSummary orderSummary, NewOrderNotification notification) throws Exception {
NotificationHandler.newOrder(orderSummary, notification);
}
@Override
protected void onOrderStateChangeNotification(OrderSummary orderSummary, OrderStateChangeNotification notification) throws Exception {
NotificationHandler.orderStateChange(orderSummary, notification);
}
@Override
protected void onChargeAmountNotification(OrderSummary orderSummary, ChargeAmountNotification notification) throws Exception {
NotificationHandler.chargeAmount(orderSummary, notification);
}
@Override
protected void startTransaction(String serialNumber, OrderSummary orderSummary, Notification notification) throws Exception {
NotificationHandler.startTransaction(serialNumber, orderSummary, notification);
}
@Override
protected void commitTransaction(String serialNumber, OrderSummary orderSummary, Notification notification) throws Exception {
NotificationHandler.commitTransaction(serialNumber, orderSummary, notification);
}
@Override
protected void rollBackTransaction(String serialNumber, OrderSummary orderSummary, Notification notification) throws Exception {
NotificationHandler.rollbackTransaction(serialNumber, orderSummary, notification);
}
@Override
protected void onAllNotifications(OrderSummary orderSummary, Notification notification) throws Exception {
NotificationHandler.all(orderSummary, notification);
}
@Override
protected void onRefundAmountNotification(OrderSummary orderSummary, RefundAmountNotification notification) throws Exception {
NotificationHandler.refundAmount(orderSummary, notification);
}
@Override
protected void onRiskInformationNotification(OrderSummary orderSummary, RiskInformationNotification notification) throws Exception {
NotificationHandler.riskInformation(orderSummary, notification);
}
@Override
protected void onChargebackAmountNotification(OrderSummary orderSummary, ChargebackAmountNotification notification) throws Exception {
NotificationHandler.chargebackAmount(orderSummary, notification);
}
@Override
protected void onAuthorizationAmountNotification(OrderSummary orderSummary, AuthorizationAmountNotification notification) throws Exception {
NotificationHandler.authorizationAmount(orderSummary, notification);
}
@Override
protected void onRiskAmountNotification(OrderSummary orderSummary, RefundAmountNotification notification) throws Exception {
NotificationHandler.riskAmount(orderSummary, notification);
}
}
public static class NotificationHandler {
/**
* With a read lock, determine whether this notification has already been
* handled.
* @param serialNumber The serial number of this notification, which is
* unique to this order/notification pair, but stable across each attempt
* to deliver the notification.
* @param orderSummary The state of the order whose notification is currently
* being handled.
* @param notification The parsed JAXB object of the notification itself.
* @return True if this notification has already been successfully handled and
* committed; otherwise false.
*/
static boolean alreadyHandled(String serialNumber, OrderSummary orderSummary, Notification notification) {
Object result = invokeOverride("alreadyHandled", serialNumber, orderSummary, notification);
return result != null && result.equals(true);
}
/**
* Save away the serialNumber so that if hasAlreadyHandled is called with this
* serial number in the future, it will return "true".
* @param serialNumber The serial number of this notification, which is
* unique to this order/notification pair, but stable across each attempt
* to deliver the notification.
* @param orderSummary The state of the order whose notification is currently
* being handled.
* @param notification The parsed JAXB object of the notification itself.
*/
static void rememberSerialNumber(String serialNumber, OrderSummary orderSummary, Notification notification) {
invokeOverride("rememberSerialNumber", serialNumber, orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled NewOrderNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void newOrder(OrderSummary orderSummary, NewOrderNotification notification) {
invokeOverride("newOrder", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled OrderStateChangeNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void orderStateChange(OrderSummary orderSummary, OrderStateChangeNotification notification) {
invokeOverride("orderStateChange", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled ChargeAmountNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void chargeAmount(OrderSummary orderSummary, ChargeAmountNotification notification) {
invokeOverride("chargeAmount", orderSummary, notification);
}
/**
* Start a new database transaction for handling this notification.
* @param serialNumber The serial number of this notification, which is
* unique to this order/notification pair, but stable across each attempt
* to deliver the notification.
* @param orderSummary The state of the order whose notification is currently
* being handled.
* @param notification The parsed JAXB object of the notification itself.
*/
static void startTransaction(String serialNumber, OrderSummary orderSummary, Notification notification) {
invokeOverride("startTransaction", serialNumber, orderSummary, notification);
}
/**
* The notification was successfully handled, so commit the current
* transaction.
* @param serialNumber The serial number of this notification, which is
* unique to this order/notification pair, but stable across each attempt
* to deliver the notification.
* @param orderSummary The state of the order whose notification is currently
* being handled.
* @param notification The parsed JAXB object of the notification itself.
*/
static void commitTransaction(String serialNumber, OrderSummary orderSummary, Notification notification) {
invokeOverride("commitTransaction", serialNumber, orderSummary, notification);
}
/**
* An error occurred while handling the notification, so roll back the current
* transaction.
* @param serialNumber The serial number of this notification, which is
* unique to this order/notification pair, but stable across each attempt
* to deliver the notification.
* @param orderSummary The state of the order whose notification is currently
* being handled.
* @param notification The parsed JAXB object of the notification itself.
*/
static void rollbackTransaction(String serialNumber, OrderSummary orderSummary, Notification notification) {
invokeOverride("rollbackTransaction", serialNumber, orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters any new notification from Checkout. This method will be called
* before the specific onFooNotification() methods.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void all(OrderSummary orderSummary, Notification notification) {
invokeOverride("all", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled RefundAmountNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void refundAmount(OrderSummary orderSummary, RefundAmountNotification notification) {
invokeOverride("refundAmount", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled RiskInformationNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void riskInformation(OrderSummary orderSummary, RiskInformationNotification notification) {
invokeOverride("riskInformation", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled ChargebackAmountNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void chargebackAmount(OrderSummary orderSummary, ChargebackAmountNotification notification) {
invokeOverride("chargebackAmount", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled AuthorizationAmountNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void authorizationAmount(OrderSummary orderSummary, AuthorizationAmountNotification notification) {
invokeOverride("authorizationAmount", orderSummary, notification);
}
/**
* Should be overridden with the behavior that should happen when your system
* encounters a not-yet-handled RefundAmountNotification.
* @param orderSummary The parsed OrderSummary object from the notification.
* @param notification The parsed JAXB object of the notification itself.
*/
static void riskAmount(OrderSummary orderSummary, RefundAmountNotification notification) {
invokeOverride("riskAmount", orderSummary, notification);
}
static Method[] methods;
/**
* Find a method in the user defined subclass
* @param name Method name to try to find
* @return Method if it exists, otherwise null
*/
static Method findMethod(String name) {
// try to find a subclass and grab all its methods
if (methods == null) {
List<Class> classes = Play.classloader.getAssignableClasses(NotificationHandler.class);
Class handler = classes.size() == 0 ? null : classes.get(0);
if (handler != null) {
methods = handler.getMethods();
} else {
Logger.warn("No user-defined implementation found for GoogleCheckout.NotificationHandler");
methods = new Method[0]; // we've tried to find some overridden methods, but can't
}
}
// iterate the overridden methods and try to find the one we're looking for
for (Method method : methods) {
if (method.getName().endsWith(name)) {
return method;
}
}
return null;
}
/**
* Invoke a method in the user defined subclass if it exists
* @param methodName Method to invoke
* @param args Arguments to pass to the overidden method
* @return The result of the method invocation, or null if it didn't exist (or is void)
*/
static Object invokeOverride(String methodName, Object... args) {
Method method = findMethod(methodName);
if (method == null) {
Logger.warn("No user-defined implementation found for "+methodName);
return null;
}
if (method.getParameterTypes().length != args.length) {
Logger.error("Overriden method signature is incorrect - "+methodName);
return null;
}
method.setAccessible(true);
try {
return method.invoke(null, args);
} catch (IllegalAccessException e) {
Logger.error(e, "Unable to access method");
} catch (InvocationTargetException e) {
Logger.error(e.getTargetException(), "Exception during callback execution");
}
return null;
}
}
}