KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PcChargeServices.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.text.DecimalFormat JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 import org.ofbiz.service.DispatchContext;
34 import org.ofbiz.service.ServiceUtil;
35 import org.ofbiz.base.util.UtilProperties;
36 import org.ofbiz.base.util.UtilValidate;
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.StringUtil;
39 import org.ofbiz.base.util.GeneralException;
40 import org.ofbiz.base.util.UtilMisc;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.accounting.payment.PaymentGatewayServices;
43
44 /**
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 3.2
49  */

50 public class PcChargeServices {
51
52     public static final String JavaDoc module = PcChargeServices.class.getName();
53
54     public static Map JavaDoc ccAuth(DispatchContext dctx, Map JavaDoc context) {
55         Properties JavaDoc props = buildPccProperties(context);
56         PcChargeApi api = getApi(props);
57         if (api == null) {
58             return ServiceUtil.returnError("PCCharge is not configured properly");
59         }
60
61         try {
62             PcChargeServices.setCreditCardInfo(api, context);
63         } catch (GeneralException e) {
64             return ServiceUtil.returnError(e.getMessage());
65         }
66
67         // basic tx info
68
api.set(PcChargeApi.TRANS_AMOUNT, getAmountString(context, "processAmount"));
69         api.set(PcChargeApi.TICKET_NUM, context.get("orderId"));
70         api.set(PcChargeApi.MANUAL_FLAG, "0");
71         api.set(PcChargeApi.PRESENT_FLAG, "1");
72
73         // command setting
74
if ("1".equals(props.getProperty("autoBill"))) {
75             // sale
76
api.set(PcChargeApi.COMMAND, "1");
77         } else {
78             // pre-auth
79
api.set(PcChargeApi.COMMAND, "4");
80         }
81
82         // send the transaction
83
PcChargeApi out = null;
84         try {
85             out = api.send();
86         } catch (IOException JavaDoc e) {
87             Debug.logError(e, module);
88             return ServiceUtil.returnError(e.getMessage());
89         } catch (GeneralException e) {
90             Debug.logError(e, module);
91             return ServiceUtil.returnError(e.getMessage());
92         }
93
94         if (out != null) {
95             Map JavaDoc result = ServiceUtil.returnSuccess();
96             String JavaDoc resultCode = out.get(PcChargeApi.RESULT);
97             boolean passed = false;
98             if ("CAPTURED".equals(resultCode)) {
99                 result.put("authResult", new Boolean JavaDoc(true));
100                 result.put("captureResult", new Boolean JavaDoc(true));
101                 passed = true;
102             } else if ("APPROVED".equals(resultCode)) {
103                 result.put("authCode", out.get(PcChargeApi.AUTH_CODE));
104                 result.put("authResult", new Boolean JavaDoc(true));
105                 passed = true;
106             } else if ("PROCESSED".equals(resultCode)) {
107                 result.put("authResult", new Boolean JavaDoc(true));
108             } else {
109                 result.put("authResult", new Boolean JavaDoc(false));
110             }
111
112             result.put("authRefNum", out.get(PcChargeApi.TROUTD) != null ? out.get(PcChargeApi.TROUTD) : "");
113             result.put("processAmount", context.get("processAmount"));
114             result.put("authCode", out.get(PcChargeApi.AUTH_CODE));
115             result.put("authFlag", out.get(PcChargeApi.REFERENCE));
116             result.put("authMessage", out.get(PcChargeApi.RESULT));
117             result.put("cvCode", out.get(PcChargeApi.CVV2_CODE));
118             result.put("avsCode", out.get(PcChargeApi.AVS_CODE));
119
120             if (!passed) {
121                 String JavaDoc respMsg = out.get(PcChargeApi.RESULT) + " / " + out.get(PcChargeApi.AUTH_CODE);
122                 String JavaDoc refNum = out.get(PcChargeApi.TROUTD);
123                 result.put("customerRespMsgs", UtilMisc.toList(respMsg, refNum));
124             }
125
126             if (result.get("captureResult") != null) {
127                 result.put("captureCode", out.get(PcChargeApi.AUTH_CODE));
128                 result.put("captureFlag", out.get(PcChargeApi.REFERENCE));
129                 result.put("captureRefNum", out.get(PcChargeApi.TROUTD));
130                 result.put("captureMessage", out.get(PcChargeApi.RESULT));
131             }
132
133             return result;
134
135         } else {
136             return ServiceUtil.returnError("Receive a null result from PcCharge");
137         }
138     }
139
140     public static Map JavaDoc ccCapture(DispatchContext dctx, Map JavaDoc context) {
141         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
142
143         //lets see if there is a auth transaction already in context
144
GenericValue authTransaction = (GenericValue) context.get("authTrans");
145
146         if(authTransaction == null){
147             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
148         }
149
150         if (authTransaction == null) {
151             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot capture");
152         }
153
154         // setup the PCCharge Interface
155
Properties JavaDoc props = buildPccProperties(context);
156         PcChargeApi api = getApi(props);
157         if (api == null) {
158             return ServiceUtil.returnError("PCCharge is not configured properly");
159         }
160
161         api.set(PcChargeApi.TROUTD, authTransaction.getString("referenceNum"));
162         api.set(PcChargeApi.COMMAND, "5");
163
164         // send the transaction
165
PcChargeApi out = null;
166         try {
167             out = api.send();
168         } catch (IOException JavaDoc e) {
169             Debug.logError(e, module);
170             return ServiceUtil.returnError(e.getMessage());
171         } catch (GeneralException e) {
172             Debug.logError(e, module);
173             return ServiceUtil.returnError(e.getMessage());
174         }
175
176         if (out != null) {
177             Map JavaDoc result = ServiceUtil.returnSuccess();
178             String JavaDoc resultCode = out.get(PcChargeApi.RESULT);
179             if ("CAPTURED".equals(resultCode)) {
180                 result.put("captureResult", new Boolean JavaDoc(true));
181             } else {
182                 result.put("captureResult", new Boolean JavaDoc(false));
183             }
184             result.put("captureAmount", context.get("captureAmount"));
185             result.put("captureRefNum", out.get(PcChargeApi.TROUTD) != null ? out.get(PcChargeApi.TROUTD) : "");
186             result.put("captureCode", out.get(PcChargeApi.AUTH_CODE));
187             result.put("captureFlag", out.get(PcChargeApi.REFERENCE));
188             result.put("captureMessage", out.get(PcChargeApi.RESULT));
189
190             return result;
191         } else {
192             return ServiceUtil.returnError("Receive a null result from PcCharge");
193         }
194     }
195
196     public static Map JavaDoc ccRelease(DispatchContext dctx, Map JavaDoc context) {
197         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
198
199         //lets see if there is a auth transaction already in context
200
GenericValue authTransaction = (GenericValue) context.get("authTrans");
201
202         if(authTransaction == null){
203             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
204         }
205
206         if (authTransaction == null) {
207             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot release");
208         }
209
210         // setup the PCCharge Interface
211
Properties JavaDoc props = buildPccProperties(context);
212         PcChargeApi api = getApi(props);
213         if (api == null) {
214             return ServiceUtil.returnError("PCCharge is not configured properly");
215         }
216
217         api.set(PcChargeApi.TROUTD, authTransaction.getString("referenceNum"));
218         api.set(PcChargeApi.COMMAND, "3");
219
220         // check to make sure we are configured for SALE mode
221
if (!"true".equalsIgnoreCase(props.getProperty("autoBill"))) {
222             return ServiceUtil.returnError("PCCharge does not support releasing pre-auths.");
223         }
224
225         // send the transaction
226
PcChargeApi out = null;
227         try {
228             out = api.send();
229         } catch (IOException JavaDoc e) {
230             Debug.logError(e, module);
231             return ServiceUtil.returnError(e.getMessage());
232         } catch (GeneralException e) {
233             Debug.logError(e, module);
234             return ServiceUtil.returnError(e.getMessage());
235         }
236
237         if (out != null) {
238             Map JavaDoc result = ServiceUtil.returnSuccess();
239             String JavaDoc resultCode = out.get(PcChargeApi.RESULT);
240             if ("VOIDED".equals(resultCode)) {
241                 result.put("releaseResult", new Boolean JavaDoc(true));
242             } else {
243                 result.put("releaseResult", new Boolean JavaDoc(false));
244             }
245             result.put("releaseAmount", context.get("releaseAmount"));
246             result.put("releaseRefNum", out.get(PcChargeApi.TROUTD) != null ? out.get(PcChargeApi.TROUTD) : "");
247             result.put("releaseCode", out.get(PcChargeApi.AUTH_CODE));
248             result.put("releaseFlag", out.get(PcChargeApi.REFERENCE));
249             result.put("releaseMessage", out.get(PcChargeApi.RESULT));
250
251             return result;
252         } else {
253             return ServiceUtil.returnError("Receive a null result from PcCharge");
254         }
255     }
256
257     public static Map JavaDoc ccRefund(DispatchContext dctx, Map JavaDoc context) {
258         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
259
260         //lets see if there is a auth transaction already in context
261
GenericValue authTransaction = (GenericValue) context.get("authTrans");
262
263         if(authTransaction == null){
264             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
265         }
266
267         if (authTransaction == null) {
268             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot refund");
269         }
270
271         // setup the PCCharge Interface
272
Properties JavaDoc props = buildPccProperties(context);
273         PcChargeApi api = getApi(props);
274         if (api == null) {
275             return ServiceUtil.returnError("PCCharge is not configured properly");
276         }
277
278         api.set(PcChargeApi.TROUTD, authTransaction.getString("referenceNum"));
279         api.set(PcChargeApi.COMMAND, "2");
280
281         // send the transaction
282
PcChargeApi out = null;
283         try {
284             out = api.send();
285         } catch (IOException JavaDoc e) {
286             Debug.logError(e, module);
287             return ServiceUtil.returnError(e.getMessage());
288         } catch (GeneralException e) {
289             Debug.logError(e, module);
290             return ServiceUtil.returnError(e.getMessage());
291         }
292
293         if (out != null) {
294             Map JavaDoc result = ServiceUtil.returnSuccess();
295             String JavaDoc resultCode = out.get(PcChargeApi.RESULT);
296             if ("CAPTURED".equals(resultCode)) {
297                 result.put("refundResult", new Boolean JavaDoc(true));
298             } else {
299                 result.put("refundResult", new Boolean JavaDoc(false));
300             }
301             result.put("refundAmount", context.get("releaseAmount"));
302             result.put("refundRefNum", out.get(PcChargeApi.TROUTD) != null ? out.get(PcChargeApi.TROUTD) : "");
303             result.put("refundCode", out.get(PcChargeApi.AUTH_CODE));
304             result.put("refundFlag", out.get(PcChargeApi.REFERENCE));
305             result.put("refundMessage", out.get(PcChargeApi.RESULT));
306
307             return result;
308         } else {
309             return ServiceUtil.returnError("Receive a null result from PcCharge");
310         }
311     }
312
313     private static void setCreditCardInfo(PcChargeApi api, Map JavaDoc context) throws GeneralException {
314         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
315         GenericValue creditCard = (GenericValue) context.get("creditCard");
316         if (creditCard != null) {
317             List JavaDoc expDateList = StringUtil.split(creditCard.getString("expireDate"), "/");
318             String JavaDoc month = (String JavaDoc) expDateList.get(0);
319             String JavaDoc year = (String JavaDoc) expDateList.get(1);
320             String JavaDoc y2d = year.substring(2);
321             String JavaDoc expDate = month + y2d;
322
323             String JavaDoc title = creditCard.getString("titleOnCard");
324             String JavaDoc fname = creditCard.getString("firstNameOnCard");
325             String JavaDoc mname = creditCard.getString("middleNameOnCard");
326             String JavaDoc lname = creditCard.getString("lastNameOnCard");
327             String JavaDoc sufix = creditCard.getString("suffixOnCard");
328             StringBuffer JavaDoc name = new StringBuffer JavaDoc();
329             if (UtilValidate.isNotEmpty(title)) {
330                 name.append(title + " ");
331             }
332             if (UtilValidate.isNotEmpty(fname)) {
333                 name.append(fname + " ");
334             }
335             if (UtilValidate.isNotEmpty(mname)) {
336                 name.append(mname + " ");
337             }
338             if (UtilValidate.isNotEmpty(lname)) {
339                 name.append(lname + " ");
340             }
341             if (UtilValidate.isNotEmpty(sufix)) {
342                 name.append(sufix);
343             }
344             String JavaDoc nameOnCard = name.toString().trim();
345             String JavaDoc acctNumber = "F" + creditCard.getString("cardNumber");
346             String JavaDoc cvNum = (String JavaDoc) context.get("cardSecurityCode");
347
348             api.set(PcChargeApi.ACCT_NUM, acctNumber);
349             api.set(PcChargeApi.EXP_DATE, expDate);
350             api.set(PcChargeApi.CARDHOLDER, nameOnCard);
351             if (UtilValidate.isNotEmpty(cvNum)) {
352                 api.set(PcChargeApi.CVV2, cvNum);
353             }
354
355             // billing address information
356
GenericValue billingAddress = (GenericValue) context.get("billingAddress");
357             if (billingAddress != null) {
358                 api.set(PcChargeApi.STREET, billingAddress.getString("address1"));
359                 api.set(PcChargeApi.ZIP_CODE, billingAddress.getString("postalCode"));
360             } else {
361                 String JavaDoc zipCode = orderPaymentPreference.getString("billingPostalCode");
362                 if (UtilValidate.isNotEmpty(zipCode)) {
363                     api.set(PcChargeApi.ZIP_CODE, zipCode);
364                 }
365             }
366         } else {
367             throw new GeneralException("No CreditCard object found");
368         }
369     }
370
371     private static PcChargeApi getApi(Properties JavaDoc props) {
372         if (props == null) {
373             Debug.logError("Cannot load API w/ null properties", module);
374             return null;
375         }
376         String JavaDoc host = props.getProperty("host");
377         int port = 0;
378         try {
379             port = Integer.parseInt(props.getProperty("port"));
380         } catch (Exception JavaDoc e) {
381             Debug.logError(e, module);
382         }
383         PcChargeApi api = null;
384         if (port > 0 && host != null) {
385             api = new PcChargeApi(host, port);
386         } else {
387             api = new PcChargeApi();
388         }
389
390         api.set(PcChargeApi.PROCESSOR_ID, props.getProperty("processorID"));
391         api.set(PcChargeApi.MERCH_NUM, props.getProperty("merchantID"));
392         api.set(PcChargeApi.USER_ID, props.getProperty("userID"));
393         return api;
394     }
395
396     private static Properties JavaDoc buildPccProperties(Map JavaDoc context) {
397         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
398         if (configString == null) {
399             configString = "payment.properties";
400         }
401
402         String JavaDoc processorId = UtilProperties.getPropertyValue(configString, "payment.pccharge.processorID");
403         String JavaDoc merchantId = UtilProperties.getPropertyValue(configString, "payment.pccharge.merchantID");
404         String JavaDoc userId = UtilProperties.getPropertyValue(configString, "payment.pccharge.userID");
405         String JavaDoc host = UtilProperties.getPropertyValue(configString, "payment.pccharge.host");
406         String JavaDoc port = UtilProperties.getPropertyValue(configString, "payment.pccharge.port");
407         String JavaDoc autoBill = UtilProperties.getPropertyValue(configString, "payment.pccharge.autoBill", "true");
408
409         // some property checking
410
if (UtilValidate.isEmpty(processorId)) {
411             Debug.logWarning("The processorID property in [" + configString + "] is not configured", module);
412             return null;
413         }
414         if (UtilValidate.isEmpty(merchantId)) {
415             Debug.logWarning("The merchantID property in [" + configString + "] is not configured", module);
416             return null;
417         }
418         if (UtilValidate.isEmpty(userId)) {
419             Debug.logWarning("The userID property in [" + configString + "] is not configured", module);
420             return null;
421         }
422
423         // create some properties for CS Client
424
Properties JavaDoc props = new Properties JavaDoc();
425         props.put("processorID", processorId);
426         props.put("merchantID", merchantId);
427         props.put("userID", userId);
428         props.put("host", host);
429         props.put("port", port);
430         props.put("autoBill", autoBill);
431         Debug.log("Returning properties - " + props, module);
432
433         return props;
434     }
435
436     private static String JavaDoc getAmountString(Map JavaDoc context, String JavaDoc amountField) {
437         String JavaDoc currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
438         DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc(currencyFormat);
439         Double JavaDoc processAmount = (Double JavaDoc) context.get(amountField);
440         return formatter.format(processAmount);
441     }
442
443 }
444
Popular Tags