KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > thirdparty > gosoftware > RitaServices


1 /*
2  * $Id: RitaServices.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.accounting.thirdparty.gosoftware;
26
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.List JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.text.DecimalFormat JavaDoc;
32 import java.sql.Timestamp JavaDoc;
33
34 import org.ofbiz.service.DispatchContext;
35 import org.ofbiz.service.ServiceUtil;
36 import org.ofbiz.service.LocalDispatcher;
37 import org.ofbiz.service.GenericServiceException;
38 import org.ofbiz.base.util.GeneralException;
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.UtilMisc;
41 import org.ofbiz.base.util.StringUtil;
42 import org.ofbiz.base.util.UtilValidate;
43 import org.ofbiz.base.util.UtilProperties;
44 import org.ofbiz.base.util.UtilDateTime;
45 import org.ofbiz.entity.GenericValue;
46 import org.ofbiz.entity.GenericDelegator;
47 import org.ofbiz.entity.GenericEntityException;
48 import org.ofbiz.entity.util.EntityUtil;
49 import org.ofbiz.accounting.payment.PaymentGatewayServices;
50
51 /**
52  *
53  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
54  * @version $Rev: 5462 $
55  * @since 3.2
56  */

57 public class RitaServices {
58
59     public static final String JavaDoc module = RitaServices.class.getName();
60
61     public static Map JavaDoc ccAuth(DispatchContext dctx, Map JavaDoc context) {
62         Properties JavaDoc props = buildPccProperties(context);
63         RitaApi api = getApi(props, "CREDIT");
64         if (api == null) {
65             return ServiceUtil.returnError("RiTA is not configured properly");
66         }
67
68         try {
69             RitaServices.setCreditCardInfo(api, dctx.getDelegator(), context);
70         } catch (GeneralException e) {
71             return ServiceUtil.returnError(e.getMessage());
72         }
73
74         // basic tx info
75
api.set(RitaApi.TRANS_AMOUNT, getAmountString(context, "processAmount"));
76         api.set(RitaApi.INVOICE, context.get("orderId"));
77
78         // command setting
79
if ("1".equals(props.getProperty("autoBill"))) {
80             // sale
81
api.set(RitaApi.COMMAND, "SALE");
82         } else {
83             // pre-auth
84
api.set(RitaApi.COMMAND, "PRE_AUTH");
85         }
86
87         // send the transaction
88
RitaApi out = null;
89         try {
90             Debug.log("Sending request to RiTA", module);
91             out = api.send();
92         } catch (IOException JavaDoc e) {
93             Debug.logError(e, module);
94             return ServiceUtil.returnError(e.getMessage());
95         } catch (GeneralException e) {
96             Debug.logError(e, module);
97             return ServiceUtil.returnError(e.getMessage());
98         }
99
100         if (out != null) {
101             Map JavaDoc result = ServiceUtil.returnSuccess();
102             String JavaDoc resultCode = out.get(RitaApi.RESULT);
103             boolean passed = false;
104             if ("CAPTURED".equals(resultCode)) {
105                 result.put("authResult", new Boolean JavaDoc(true));
106                 result.put("captureResult", new Boolean JavaDoc(true));
107                 passed = true;
108             } else if ("APPROVED".equals(resultCode)) {
109                 result.put("authCode", out.get(RitaApi.AUTH_CODE));
110                 result.put("authResult", new Boolean JavaDoc(true));
111                 passed = true;
112             } else if ("PROCESSED".equals(resultCode)) {
113                 result.put("authResult", new Boolean JavaDoc(true));
114             } else {
115                 result.put("authResult", new Boolean JavaDoc(false));
116             }
117
118             result.put("authRefNum", out.get(RitaApi.INTRN_SEQ_NUM) != null ? out.get(RitaApi.INTRN_SEQ_NUM) : "");
119             result.put("processAmount", context.get("processAmount"));
120             result.put("authCode", out.get(RitaApi.AUTH_CODE));
121             result.put("authFlag", out.get(RitaApi.REFERENCE));
122             result.put("authMessage", out.get(RitaApi.RESULT));
123             result.put("cvCode", out.get(RitaApi.CVV2_CODE));
124             result.put("avsCode", out.get(RitaApi.AVS_CODE));
125
126             if (!passed) {
127                 String JavaDoc respMsg = out.get(RitaApi.RESULT) + " / " + out.get(RitaApi.INTRN_SEQ_NUM);
128                 result.put("customerRespMsgs", UtilMisc.toList(respMsg));
129             }
130
131             if (result.get("captureResult") != null) {
132                 result.put("captureCode", out.get(RitaApi.AUTH_CODE));
133                 result.put("captureFlag", out.get(RitaApi.REFERENCE));
134                 result.put("captureRefNum", out.get(RitaApi.INTRN_SEQ_NUM));
135                 result.put("captureMessage", out.get(RitaApi.RESULT));
136             }
137
138             return result;
139
140         } else {
141             return ServiceUtil.returnError("Receive a null result from RiTA");
142         }
143     }
144
145     public static Map JavaDoc ccCapture(DispatchContext dctx, Map JavaDoc context) {
146         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
147
148         //lets see if there is a auth transaction already in context
149
GenericValue authTransaction = (GenericValue) context.get("authTrans");
150
151         if(authTransaction == null){
152             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
153         }
154
155         if (authTransaction == null) {
156             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot capture");
157         }
158
159         // setup the RiTA Interface
160
Properties JavaDoc props = buildPccProperties(context);
161         RitaApi api = getApi(props, "CREDIT");
162         if (api == null) {
163             return ServiceUtil.returnError("RiTA is not configured properly");
164         }
165
166         api.set(RitaApi.ORIG_SEQ_NUM, authTransaction.getString("referenceNum"));
167         api.set(RitaApi.COMMAND, "COMPLETION");
168
169         // send the transaction
170
RitaApi out = null;
171         try {
172             out = api.send();
173         } catch (IOException JavaDoc e) {
174             Debug.logError(e, module);
175             return ServiceUtil.returnError(e.getMessage());
176         } catch (GeneralException e) {
177             Debug.logError(e, module);
178             return ServiceUtil.returnError(e.getMessage());
179         }
180
181         if (out != null) {
182             Map JavaDoc result = ServiceUtil.returnSuccess();
183             String JavaDoc resultCode = out.get(RitaApi.RESULT);
184             if ("CAPTURED".equals(resultCode)) {
185                 result.put("captureResult", new Boolean JavaDoc(true));
186             } else {
187                 result.put("captureResult", new Boolean JavaDoc(false));
188             }
189             result.put("captureAmount", context.get("captureAmount"));
190             result.put("captureRefNum", out.get(RitaApi.INTRN_SEQ_NUM) != null ? out.get(RitaApi.INTRN_SEQ_NUM) : "");
191             result.put("captureCode", out.get(RitaApi.AUTH_CODE));
192             result.put("captureFlag", out.get(RitaApi.REFERENCE));
193             result.put("captureMessage", out.get(RitaApi.RESULT));
194
195             return result;
196         } else {
197             return ServiceUtil.returnError("Receive a null result from RiTA");
198         }
199     }
200
201     public static Map JavaDoc ccVoidRelease(DispatchContext dctx, Map JavaDoc context) {
202         return ccVoid(dctx, context, false);
203     }
204
205     public static Map JavaDoc ccVoidRefund(DispatchContext dctx, Map JavaDoc context) {
206         return ccVoid(dctx, context, true);
207     }
208
209     private static Map JavaDoc ccVoid(DispatchContext dctx, Map JavaDoc context, boolean isRefund) {
210         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
211
212         //lets see if there is a auth transaction already in context
213
GenericValue authTransaction = (GenericValue) context.get("authTrans");
214
215         if(authTransaction == null){
216             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
217         }
218
219         if (authTransaction == null) {
220             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot release");
221         }
222
223         // setup the RiTA Interface
224
Properties JavaDoc props = buildPccProperties(context);
225         RitaApi api = getApi(props, "CREDIT");
226         if (api == null) {
227             return ServiceUtil.returnError("RiTA is not configured properly");
228         }
229
230         api.set(RitaApi.TRANS_AMOUNT, getAmountString(context, isRefund ? "refundAmount" : "releaseAmount"));
231         api.set(RitaApi.ORIG_SEQ_NUM, authTransaction.getString("referenceNum"));
232         api.set(RitaApi.COMMAND, "VOID");
233
234         // check to make sure we are configured for SALE mode
235
if (!"1".equals(props.getProperty("autoBill"))) {
236             return ServiceUtil.returnError("RiTA does not support releasing pre-auths.");
237         }
238
239         // send the transaction
240
RitaApi out = null;
241         try {
242             out = api.send();
243         } catch (IOException JavaDoc e) {
244             Debug.logError(e, module);
245             return ServiceUtil.returnError(e.getMessage());
246         } catch (GeneralException e) {
247             Debug.logError(e, module);
248             return ServiceUtil.returnError(e.getMessage());
249         }
250
251         if (out != null) {
252             Map JavaDoc result = ServiceUtil.returnSuccess();
253             String JavaDoc resultCode = out.get(RitaApi.RESULT);
254             if ("VOIDED".equals(resultCode)) {
255                 result.put(isRefund ? "refundResult" : "releaseResult", new Boolean JavaDoc(true));
256             } else {
257                 result.put(isRefund ? "refundResult" : "releaseResult", new Boolean JavaDoc(false));
258             }
259             result.put(isRefund ? "refundAmount" : "releaseAmount", context.get(isRefund ? "refundAmount" : "releaseAmount"));
260             result.put(isRefund ? "refundRefNum" : "releaseRefNum", out.get(RitaApi.INTRN_SEQ_NUM) != null ? out.get(RitaApi.INTRN_SEQ_NUM) : "");
261             result.put(isRefund ? "refundCode" : "releaseCode", out.get(RitaApi.AUTH_CODE));
262             result.put(isRefund ? "refundFlag" : "releaseFlag", out.get(RitaApi.REFERENCE));
263             result.put(isRefund ? "refundMessage" : "releaseMessage", out.get(RitaApi.RESULT));
264
265             return result;
266         } else {
267             return ServiceUtil.returnError("Receive a null result from RiTA");
268         }
269     }
270
271     public static Map JavaDoc ccCreditRefund(DispatchContext dctx, Map JavaDoc context) {
272         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
273
274         //lets see if there is a auth transaction already in context
275
GenericValue authTransaction = (GenericValue) context.get("authTrans");
276
277         if(authTransaction == null){
278             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
279         }
280
281         if (authTransaction == null) {
282             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot refund");
283         }
284
285         // setup the RiTA Interface
286
Properties JavaDoc props = buildPccProperties(context);
287         RitaApi api = getApi(props, "CREDIT");
288         if (api == null) {
289             return ServiceUtil.returnError("RiTA is not configured properly");
290         }
291
292         // set the required cc info
293
try {
294             RitaServices.setCreditCardInfo(api, dctx.getDelegator(), context);
295         } catch (GeneralException e) {
296             return ServiceUtil.returnError(e.getMessage());
297         }
298
299         api.set(RitaApi.TRANS_AMOUNT, getAmountString(context, "refundAmount"));
300         api.set(RitaApi.ORIG_SEQ_NUM, authTransaction.getString("referenceNum"));
301         api.set(RitaApi.COMMAND, "CREDIT");
302
303         // send the transaction
304
RitaApi out = null;
305         try {
306             out = api.send();
307         } catch (IOException JavaDoc e) {
308             Debug.logError(e, module);
309             return ServiceUtil.returnError(e.getMessage());
310         } catch (GeneralException e) {
311             Debug.logError(e, module);
312             return ServiceUtil.returnError(e.getMessage());
313         }
314
315         if (out != null) {
316             Map JavaDoc result = ServiceUtil.returnSuccess();
317             String JavaDoc resultCode = out.get(RitaApi.RESULT);
318             if ("CAPTURED".equals(resultCode)) {
319                 result.put("refundResult", new Boolean JavaDoc(true));
320             } else {
321                 result.put("refundResult", new Boolean JavaDoc(false));
322             }
323             result.put("refundAmount", context.get("refundAmount"));
324             result.put("refundRefNum", out.get(RitaApi.INTRN_SEQ_NUM) != null ? out.get(RitaApi.INTRN_SEQ_NUM) : "");
325             result.put("refundCode", out.get(RitaApi.AUTH_CODE));
326             result.put("refundFlag", out.get(RitaApi.REFERENCE));
327             result.put("refundMessage", out.get(RitaApi.RESULT));
328
329             return result;
330         } else {
331             return ServiceUtil.returnError("Receive a null result from RiTA");
332         }
333     }
334
335     public static Map JavaDoc ccRefund(DispatchContext dctx, Map JavaDoc context) {
336         LocalDispatcher dispatcher = dctx.getDispatcher();
337         GenericDelegator delegator = dctx.getDelegator();
338         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
339         GenericValue orderHeader = null;
340         try {
341             orderHeader = orderPaymentPreference.getRelatedOne("OrderHeader");
342         } catch (GenericEntityException e) {
343             Debug.logError(e, module);
344             return ServiceUtil.returnError("Unable to obtain order header information from payment preference");
345         }
346
347         if (orderHeader != null) {
348             String JavaDoc terminalId = orderHeader.getString("terminalId");
349             boolean isVoid = false;
350             if (terminalId != null) {
351                 Timestamp JavaDoc orderDate = orderHeader.getTimestamp("orderDate");
352                 GenericValue terminalState = null;
353                 try {
354                     List JavaDoc states = delegator.findByAnd("PosTerminalState", UtilMisc.toMap("posTerminalId", terminalId));
355                     states = EntityUtil.filterByDate(states, UtilDateTime.nowTimestamp(), "openedDate", "closedDate", true);
356                     terminalState = EntityUtil.getFirst(states);
357                 } catch (GenericEntityException e) {
358                     Debug.logError(e, module);
359                 }
360
361                 // this is the current opened terminal
362
if (terminalState != null) {
363                     Timestamp JavaDoc openDate = terminalState.getTimestamp("openedDate");
364                     // if the order date is after the open date of the current state
365
// the order happend within the current open/close of the terminal
366
if (orderDate.after(openDate)) {
367                         isVoid = true;
368                     }
369                 }
370             }
371
372             Map JavaDoc refundResp = null;
373             try {
374                 if (isVoid) {
375                     refundResp = dispatcher.runSync("ritaCCVoidRefund", context);
376                 } else {
377                     refundResp = dispatcher.runSync("ritaCCCreditRefund", context);
378                 }
379             } catch (GenericServiceException e) {
380                 Debug.logError(e, module);
381                 return ServiceUtil.returnError("Service invocation exception");
382             }
383             return refundResp;
384         } else {
385             return ServiceUtil.returnError("Unable to find order information; cannot complete the refund");
386         }
387     }
388
389     private static void setCreditCardInfo(RitaApi api, GenericDelegator delegator, Map JavaDoc context) throws GeneralException {
390         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
391         GenericValue creditCard = (GenericValue) context.get("creditCard");
392         if (creditCard == null) {
393             creditCard = delegator.findByPrimaryKey("CreditCard", UtilMisc.toMap("paymentMethodId", orderPaymentPreference.getString("paymentMethodId")));
394         }
395         if (creditCard != null) {
396             List JavaDoc expDateList = StringUtil.split(creditCard.getString("expireDate"), "/");
397             String JavaDoc month = (String JavaDoc) expDateList.get(0);
398             String JavaDoc year = (String JavaDoc) expDateList.get(1);
399             String JavaDoc y2d = year.substring(2);
400
401             String JavaDoc title = creditCard.getString("titleOnCard");
402             String JavaDoc fname = creditCard.getString("firstNameOnCard");
403             String JavaDoc mname = creditCard.getString("middleNameOnCard");
404             String JavaDoc lname = creditCard.getString("lastNameOnCard");
405             String JavaDoc sufix = creditCard.getString("suffixOnCard");
406             StringBuffer JavaDoc name = new StringBuffer JavaDoc();
407             if (UtilValidate.isNotEmpty(title)) {
408                 name.append(title + " ");
409             }
410             if (UtilValidate.isNotEmpty(fname)) {
411                 name.append(fname + " ");
412             }
413             if (UtilValidate.isNotEmpty(mname)) {
414                 name.append(mname + " ");
415             }
416             if (UtilValidate.isNotEmpty(lname)) {
417                 name.append(lname + " ");
418             }
419             if (UtilValidate.isNotEmpty(sufix)) {
420                 name.append(sufix);
421             }
422             String JavaDoc nameOnCard = name.toString().trim();
423             String JavaDoc acctNumber = creditCard.getString("cardNumber");
424             String JavaDoc cvNum = (String JavaDoc) context.get("cardSecurityCode");
425
426             api.set(RitaApi.ACCT_NUM, acctNumber);
427             api.set(RitaApi.EXP_MONTH, month);
428             api.set(RitaApi.EXP_YEAR, y2d);
429             api.set(RitaApi.CARDHOLDER, nameOnCard);
430             if (UtilValidate.isNotEmpty(cvNum)) {
431                 api.set(RitaApi.CVV2, cvNum);
432             }
433
434             // billing address information
435
GenericValue billingAddress = (GenericValue) context.get("billingAddress");
436             if (billingAddress != null) {
437                 api.set(RitaApi.CUSTOMER_STREET, billingAddress.getString("address1"));
438                 api.set(RitaApi.CUSTOMER_ZIP, billingAddress.getString("postalCode"));
439             } else {
440                 String JavaDoc zipCode = orderPaymentPreference.getString("billingPostalCode");
441                 if (UtilValidate.isNotEmpty(zipCode)) {
442                     api.set(RitaApi.CUSTOMER_ZIP, zipCode);
443                 }
444             }
445
446             // set the present flag
447
String JavaDoc presentFlag = orderPaymentPreference.getString("presentFlag");
448             if (presentFlag == null) {
449                 presentFlag = "N";
450             }
451             api.set(RitaApi.PRESENT_FLAG, presentFlag.equals("Y") ? "3" : "1"); // 1, no present, 2 present, 3 swiped
452
} else {
453             throw new GeneralException("No CreditCard object found");
454         }
455     }
456
457     private static RitaApi getApi(Properties JavaDoc props) {
458         if (props == null) {
459             Debug.logError("Cannot load API w/ null properties", module);
460             return null;
461         }
462         String JavaDoc host = props.getProperty("host");
463         int port = 0;
464         try {
465             port = Integer.parseInt(props.getProperty("port"));
466         } catch (Exception JavaDoc e) {
467             Debug.logError(e, module);
468         }
469         boolean ssl = props.getProperty("ssl", "N").equals("Y") ? true : false;
470
471         RitaApi api = null;
472         if (port > 0 && host != null) {
473             api = new RitaApi(host, port, ssl);
474         } else {
475             api = new RitaApi();
476         }
477
478         api.set(RitaApi.CLIENT_ID, props.getProperty("clientID"));
479         api.set(RitaApi.USER_ID, props.getProperty("userID"));
480         api.set(RitaApi.USER_PW, props.getProperty("userPW"));
481         api.set(RitaApi.FORCE_FLAG, props.getProperty("forceTx"));
482
483         return api;
484     }
485
486     private static RitaApi getApi(Properties JavaDoc props, String JavaDoc paymentType) {
487         RitaApi api = getApi(props);
488         api.set(RitaApi.FUNCTION_TYPE, "PAYMENT");
489         api.set(RitaApi.PAYMENT_TYPE, paymentType);
490         return api;
491     }
492
493     private static Properties JavaDoc buildPccProperties(Map JavaDoc context) {
494         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
495         if (configString == null) {
496             configString = "payment.properties";
497         }
498
499         String JavaDoc clientId = UtilProperties.getPropertyValue(configString, "payment.rita.clientID");
500         String JavaDoc userId = UtilProperties.getPropertyValue(configString, "payment.rita.userID");
501         String JavaDoc userPw = UtilProperties.getPropertyValue(configString, "payment.rita.userPW");
502         String JavaDoc host = UtilProperties.getPropertyValue(configString, "payment.rita.host");
503         String JavaDoc port = UtilProperties.getPropertyValue(configString, "payment.rita.port");
504         String JavaDoc ssl = UtilProperties.getPropertyValue(configString, "payment.rita.ssl", "N");
505         String JavaDoc autoBill = UtilProperties.getPropertyValue(configString, "payment.rita.autoBill", "0");
506         String JavaDoc forceTx = UtilProperties.getPropertyValue(configString, "payment.rita.forceTx", "0");
507
508         // some property checking
509
if (UtilValidate.isEmpty(clientId)) {
510             Debug.logWarning("The clientID property in [" + configString + "] is not configured", module);
511             return null;
512         }
513         if (UtilValidate.isEmpty(userId)) {
514             Debug.logWarning("The userID property in [" + configString + "] is not configured", module);
515             return null;
516         }
517         if (UtilValidate.isEmpty(userPw)) {
518             Debug.logWarning("The userPW property in [" + configString + "] is not configured", module);
519             return null;
520         }
521
522         // create some properties for CS Client
523
Properties JavaDoc props = new Properties JavaDoc();
524         props.put("clientID", clientId);
525         props.put("userID", userId);
526         props.put("userPW", userPw);
527         props.put("host", host);
528         props.put("port", port);
529         props.put("ssl", ssl);
530         props.put("autoBill", autoBill);
531         props.put("forceTx", forceTx);
532         Debug.log("Returning properties - " + props, module);
533
534         return props;
535     }
536
537     private static String JavaDoc getAmountString(Map JavaDoc context, String JavaDoc amountField) {
538         String JavaDoc currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
539         DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc(currencyFormat);
540         Double JavaDoc processAmount = (Double JavaDoc) context.get(amountField);
541         return formatter.format(processAmount);
542     }
543 }
544
Popular Tags