KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > thirdparty > cybersource > IcsPaymentServices


1 /*
2  * $Id: IcsPaymentServices.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 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.cybersource;
26
27 import java.text.DecimalFormat JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import com.cybersource.ws.client.Client;
35 import com.cybersource.ws.client.ClientException;
36 import com.cybersource.ws.client.FaultException;
37
38 import org.ofbiz.accounting.payment.PaymentGatewayServices;
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.SSLUtil;
41 import org.ofbiz.base.util.StringUtil;
42 import org.ofbiz.base.util.UtilMisc;
43 import org.ofbiz.base.util.UtilProperties;
44 import org.ofbiz.base.util.UtilValidate;
45 import org.ofbiz.entity.GenericEntityException;
46 import org.ofbiz.entity.GenericValue;
47 import org.ofbiz.service.DispatchContext;
48 import org.ofbiz.service.ServiceUtil;
49
50 /**
51  * CyberSource WS Integration Services
52  *
53  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
54  * @version $Rev: 5462 $
55  * @since 3.0
56  */

57 public class IcsPaymentServices {
58
59     public static final String JavaDoc module = IcsPaymentServices.class.getName();
60
61     // load the JSSE properties
62
static {
63         SSLUtil.loadJsseProperties();
64     }
65
66     public static Map JavaDoc ccAuth(DispatchContext dctx, Map JavaDoc context) {
67         // generate the request/properties
68
Properties JavaDoc props = buildCsProperties(context);
69         if (props == null) {
70             return ServiceUtil.returnError("ERROR: Getting Cybersource property configuration");
71         }
72
73         Map JavaDoc request = buildAuthRequest(context);
74         request.put("merchantID", props.get("merchantID"));
75
76         // transmit the request
77
Map JavaDoc reply = null;
78         try {
79             reply = Client.runTransaction(request, props);
80         } catch (FaultException e) {
81             Debug.logError(e, "ERROR: Fault from CyberSource", module);
82             Debug.logError(e, "Fault : " + e.getFaultString(), module);
83             return ServiceUtil.returnError("Unable to communicate with CyberSource");
84         } catch (ClientException e) {
85             Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
86             return ServiceUtil.returnError("Unable to communicate with CyberSource");
87         }
88
89         // process the reply
90
Map JavaDoc result = ServiceUtil.returnSuccess();
91         processAuthResult(reply, result);
92         return result;
93     }
94
95     public static Map JavaDoc ccReAuth(DispatchContext dctx, Map JavaDoc context) {
96         return ServiceUtil.returnSuccess();
97     }
98
99     public static Map JavaDoc ccCapture(DispatchContext dctx, Map JavaDoc context) {
100         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
101
102         //lets see if there is a auth transaction already in context
103
GenericValue authTransaction = (GenericValue) context.get("authTrans");
104
105         if (authTransaction == null){
106             authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
107         }
108
109         if (authTransaction == null) {
110             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot capture");
111         }
112
113         // generate the request/properties
114
Properties JavaDoc props = buildCsProperties(context);
115         if (props == null) {
116             return ServiceUtil.returnError("ERROR: Getting Cybersource property configuration");
117         }
118
119         Map JavaDoc request = buildCaptureRequest(context, authTransaction);
120         request.put("merchantID", props.get("merchantID"));
121
122         // transmit the request
123
Map JavaDoc reply = null;
124         try {
125             reply = Client.runTransaction(request, props);
126         } catch (FaultException e) {
127             Debug.logError(e, "ERROR: Fault from CyberSource", module);
128             return ServiceUtil.returnError("Unable to communicate with CyberSource");
129         } catch (ClientException e) {
130             Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
131             return ServiceUtil.returnError("Unable to communicate with CyberSource");
132         }
133
134         // process the reply
135
Map JavaDoc result = ServiceUtil.returnSuccess();
136         processCaptureResult(reply, result);
137         return result;
138     }
139
140     public static Map JavaDoc ccRelease(DispatchContext dctx, Map JavaDoc context) {
141         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
142         GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
143         if (authTransaction == null) {
144             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot release");
145         }
146
147         // generate the request/properties
148
Properties JavaDoc props = buildCsProperties(context);
149         if (props == null) {
150             return ServiceUtil.returnError("ERROR: Getting Cybersource property configuration");
151         }
152
153         Map JavaDoc request = buildReleaseRequest(context, authTransaction);
154         request.put("merchantID", props.get("merchantID"));
155
156         // transmit the request
157
Map JavaDoc reply = null;
158         try {
159             reply = Client.runTransaction(request, props);
160         } catch (FaultException e) {
161             Debug.logError(e, "ERROR: Fault from CyberSource", module);
162             return ServiceUtil.returnError("Unable to communicate with CyberSource");
163         } catch (ClientException e) {
164             Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
165             return ServiceUtil.returnError("Unable to communicate with CyberSource");
166         }
167
168         // process the reply
169
Map JavaDoc result = ServiceUtil.returnSuccess();
170         processReleaseResult(reply, result);
171         return result;
172     }
173
174     public static Map JavaDoc ccRefund(DispatchContext dctx, Map JavaDoc context) {
175         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
176         GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
177         if (authTransaction == null) {
178             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot refund");
179         }
180
181         // generate the request/properties
182
Properties JavaDoc props = buildCsProperties(context);
183         if (props == null) {
184             return ServiceUtil.returnError("ERROR: Getting Cybersource property configuration");
185         }
186
187         Map JavaDoc request = buildRefundRequest(context, authTransaction);
188         request.put("merchantID", props.get("merchantID"));
189
190         // transmit the request
191
Map JavaDoc reply = null;
192         try {
193             reply = Client.runTransaction(request, props);
194         } catch (FaultException e) {
195             Debug.logError(e, "ERROR: Fault from CyberSource", module);
196             return ServiceUtil.returnError("Unable to communicate with CyberSource");
197         } catch (ClientException e) {
198             Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
199             return ServiceUtil.returnError("Unable to communicate with CyberSource");
200         }
201
202         // process the reply
203
Map JavaDoc result = ServiceUtil.returnSuccess();
204         processRefundResult(reply, result);
205         return result;
206     }
207
208     public static Map JavaDoc ccCredit(DispatchContext dctx, Map JavaDoc context) {
209         // generate the request/properties
210
Properties JavaDoc props = buildCsProperties(context);
211         if (props == null) {
212             return ServiceUtil.returnError("ERROR: Getting Cybersource property configuration");
213         }
214
215         Map JavaDoc request = buildCreditRequest(context);
216         request.put("merchantID", props.get("merchantID"));
217
218         // transmit the request
219
Map JavaDoc reply = null;
220         try {
221             reply = Client.runTransaction(request, props);
222         } catch (FaultException e) {
223             Debug.logError(e, "ERROR: Fault from CyberSource", module);
224             return ServiceUtil.returnError("Unable to communicate with CyberSource");
225         } catch (ClientException e) {
226             Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
227             return ServiceUtil.returnError("Unable to communicate with CyberSource");
228         }
229
230         // process the reply
231
Map JavaDoc result = ServiceUtil.returnSuccess();
232         processCreditResult(reply, result);
233         return result;
234     }
235
236     private static Properties JavaDoc buildCsProperties(Map JavaDoc context) {
237         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
238         if (configString == null) {
239             configString = "payment.properties";
240         }
241
242         String JavaDoc merchantId = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantID");
243         String JavaDoc targetApi = UtilProperties.getPropertyValue(configString, "payment.cybersource.api.version");
244         String JavaDoc production = UtilProperties.getPropertyValue(configString, "payment.cybersource.production");
245         String JavaDoc enableLog = UtilProperties.getPropertyValue(configString, "payment.cybersource.log");
246         String JavaDoc logSize = UtilProperties.getPropertyValue(configString, "payment.cybersource.log.size");
247         String JavaDoc logFile = UtilProperties.getPropertyValue(configString, "payment.cybersource.log.file");
248         String JavaDoc logDir = UtilProperties.getPropertyValue(configString, "payment.cybersource.log.dir");
249
250         String JavaDoc keysPath = UtilProperties.getPropertyValue(configString, "payment.cybersource.keysDir");
251         String JavaDoc keysFile = UtilProperties.getPropertyValue(configString, "payment.cybersource.keysFile");
252
253         // some property checking
254
if (UtilValidate.isEmpty(merchantId)) {
255             Debug.logWarning("The merchantId property in [" + configString + "] is not configured", module);
256             return null;
257         }
258         if (UtilValidate.isEmpty(keysPath)) {
259             Debug.logWarning("The keysDir property in [" + configString + "] is not configured", module);
260             return null;
261         }
262
263         // create some properties for CS Client
264
Properties JavaDoc props = new Properties JavaDoc();
265         props.put("merchantID", merchantId);
266         props.put("keysDirectory", keysPath);
267         props.put("targetAPIVersion", targetApi);
268         props.put("sendToProduction", production);
269         props.put("enableLog", enableLog);
270         props.put("logDirectory", logDir);
271         props.put("logFilename", logFile);
272         props.put("logMaximumSize", logSize);
273
274         if (keysFile != null && keysFile.length() > 0) {
275             props.put("alternateKeyFilename", keysFile);
276         }
277         //Debug.logInfo("Created CyberSource Properties : " + props, module);
278

279         return props;
280     }
281
282     private static Map JavaDoc buildAuthRequest(Map JavaDoc context) {
283         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
284         String JavaDoc currency = (String JavaDoc) context.get("currency");
285         if (configString == null) {
286             configString = "payment.properties";
287         }
288
289         // make the request map
290
String JavaDoc capture = UtilProperties.getPropertyValue(configString, "payment.cybersource.autoBill", "false");
291         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
292
293         Map JavaDoc request = new HashMap JavaDoc();
294         request.put("ccAuthService_run", "true"); // run auth service
295
request.put("ccCaptureService_run", capture); // run capture service (i.e. sale)
296
request.put("merchantReferenceCode", orderId); // set the order ref number
297
request.put("purchaseTotals_currency", currency); // set the order currency
298
appendFullBillingInfo(request, context); // add in all address info
299
appendItemLineInfo(request, context, "processAmount"); // add in the item info
300
appendAvsRules(request, context); // add in the AVS flags and decline codes
301

302         return request;
303     }
304
305     private static Map JavaDoc buildCaptureRequest(Map JavaDoc context, GenericValue authTransaction) {
306         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
307         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
308         String JavaDoc currency = (String JavaDoc) context.get("currency");
309         if (configString == null) {
310             configString = "payment.properties";
311         }
312         String JavaDoc merchantDesc = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantDescr", null);
313         String JavaDoc merchantCont = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantContact", null);
314
315         Map JavaDoc request = new HashMap JavaDoc();
316         request.put("ccCaptureService_run", "true");
317         request.put("ccCaptureService_authRequestID", authTransaction.getString("referenceNum"));
318         request.put("item_0_unitPrice", getAmountString(context, "captureAmount"));
319         request.put("merchantReferenceCode", orderPaymentPreference.getString("orderId"));
320         request.put("purchaseTotals_currency", currency);
321
322         // TODO: add support for verbal authorizations
323
//request.put("ccCaptureService_authType", null); -- should be 'verbal'
324
//request.put("ccCaptureService_verbalAuthCode", null); -- code from verbal auth
325

326         if (merchantDesc != null) {
327             request.put("invoiceHeader_merchantDescriptor", merchantDesc); // merchant description
328
}
329         if (merchantCont != null) {
330             request.put("invoiceHeader_merchantDescriptorContact", merchantCont); // merchant contact info
331
}
332
333         return request;
334     }
335
336     private static Map JavaDoc buildReleaseRequest(Map JavaDoc context, GenericValue authTransaction) {
337         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
338         String JavaDoc currency = (String JavaDoc) context.get("currency");
339         Map JavaDoc request = new HashMap JavaDoc();
340         request.put("ccAuthReversalService_run", "true");
341         request.put("ccAuthReversalService_authRequestID", authTransaction.getString("referenceNum"));
342         request.put("item_0_unitPrice", getAmountString(context, "releaseAmount"));
343         request.put("merchantReferenceCode", orderPaymentPreference.getString("orderId"));
344         request.put("purchaseTotals_currency", currency);
345         return request;
346     }
347
348     private static Map JavaDoc buildRefundRequest(Map JavaDoc context, GenericValue authTransaction) {
349         GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
350         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
351         String JavaDoc currency = (String JavaDoc) context.get("currency");
352         if (configString == null) {
353             configString = "payment.properties";
354         }
355         String JavaDoc merchantDesc = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantDescr", null);
356         String JavaDoc merchantCont = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantContact", null);
357
358         Map JavaDoc request = new HashMap JavaDoc();
359         request.put("ccCreditService_run", "true");
360         request.put("ccCreditService_captureRequestID", authTransaction.getString("referenceNum"));
361         request.put("item_0_unitPrice", getAmountString(context, "refundAmount"));
362         request.put("merchantReferenceCode", orderPaymentPreference.getString("orderId"));
363         request.put("purchaseTotals_currency", currency);
364
365         if (merchantDesc != null) {
366             request.put("invoiceHeader_merchantDescriptor", merchantDesc); // merchant description
367
}
368         if (merchantCont != null) {
369             request.put("invoiceHeader_merchantDescriptorContact", merchantCont); // merchant contact info
370
}
371
372         return request;
373     }
374
375     private static Map JavaDoc buildCreditRequest(Map JavaDoc context) {
376         String JavaDoc refCode = (String JavaDoc) context.get("referenceCode");
377         Map JavaDoc request = new HashMap JavaDoc();
378         request.put("ccCreditService_run", "true"); // run credit service
379
request.put("merchantReferenceCode", refCode); // set the ref number could be order id
380
appendFullBillingInfo(request, context); // add in all address info
381
appendItemLineInfo(request, context, "creditAmount"); // add in the item info
382
return request;
383     }
384
385     private static void appendAvsRules(Map JavaDoc request, Map JavaDoc context) {
386         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
387         if (configString == null) {
388             configString = "payment.properties";
389         }
390         String JavaDoc avsCodes = UtilProperties.getPropertyValue(configString, "payment.cybersource.avsDeclineCodes", null);
391
392         GenericValue party = (GenericValue) context.get("billToParty");
393         if (party != null) {
394             GenericValue avsOverride = null;
395
396             try {
397                 avsOverride = party.getDelegator().findByPrimaryKey("PartyIcsAvsOverride",
398                         UtilMisc.toMap("partyId", party.getString("partyId")));
399             } catch (GenericEntityException e) {
400                 Debug.logError(e, module);
401             }
402             if (avsOverride != null && avsOverride.get("avsDeclineString") != null) {
403                 String JavaDoc overrideString = avsOverride.getString("avsDeclineString");
404                 if (overrideString != null && overrideString.length() > 0) {
405                     avsCodes = overrideString;
406                 }
407             }
408         }
409
410         if (avsCodes != null && avsCodes.length() > 0) {
411             request.put("businessRules_declineAVSFlags", avsCodes);
412         }
413
414         String JavaDoc avsIgnore = UtilProperties.getPropertyValue(configString, "payment.cybersource.avsDeclineCodes", "false");
415         request.put("businessRules_ignoreAVS", avsIgnore);
416     }
417
418     private static void appendFullBillingInfo(Map JavaDoc request, Map JavaDoc context) {
419         // person info
420
GenericValue party = (GenericValue) context.get("billToParty");
421
422         // contact info
423
GenericValue email = (GenericValue) context.get("billToEmail");
424         if (email != null) {
425             request.put("billTo_email", email.getString("infoString"));
426         } else {
427             Debug.logWarning("Email not defined; Cybersource will fail.", module);
428         }
429
430         // phone number seems to not be used; possibly only for reporting.
431

432         // CC payment info
433
GenericValue creditCard = (GenericValue) context.get("creditCard");
434         if (creditCard != null) {
435             List JavaDoc expDateList = StringUtil.split(creditCard.getString("expireDate"), "/");
436
437             request.put("billTo_firstName", creditCard.getString("firstNameOnCard"));
438             request.put("billTo_lastName", creditCard.getString("lastNameOnCard"));
439             request.put("card_accountNumber", creditCard.getString("cardNumber"));
440             request.put("card_expirationMonth", expDateList.get(0));
441             request.put("card_expirationYear", expDateList.get(1));
442         } else {
443             Debug.logWarning("CreditCard not defined; Cybersource will fail.", module);
444         }
445
446         // CCV info
447
String JavaDoc cvNum = (String JavaDoc) context.get("cardSecurityCode");
448         String JavaDoc cvSet = UtilValidate.isEmpty(cvNum) ? "1" : "0";
449         request.put("card_cvIndicator", cvSet);
450         if ("1".equals(cvNum)) {
451             request.put("card_cvNumber", cvNum);
452         }
453
454         // payment contact info
455
GenericValue billingAddress = (GenericValue) context.get("billingAddress");
456
457         if (billingAddress != null) {
458             request.put("billTo_street1", billingAddress.getString("address1"));
459             if (billingAddress.get("address2") != null) {
460                 request.put("billTo_street2", billingAddress.getString("address2"));
461             }
462             request.put("billTo_city", billingAddress.getString("city"));
463             String JavaDoc bCountry = billingAddress.get("countryGeoId") != null ? billingAddress.getString("countryGeoId") : "USA";
464
465             request.put("billTo_country", bCountry);
466             request.put("billTo_postalCode", billingAddress.getString("postalCode"));
467             if (billingAddress.get("stateProvinceGeoId") != null) {
468                 request.put("billTo_state", billingAddress.getString("stateProvinceGeoId"));
469             }
470         } else {
471             Debug.logWarning("BillingAddress not defined; Cybersource will fail.", module);
472         }
473
474         // order shipping information
475
GenericValue shippingAddress = (GenericValue) context.get("shippingAddress");
476         if (shippingAddress != null) {
477             if (creditCard != null) {
478                 // TODO: this is just a kludge since we don't have a firstName and lastName on the PostalAddress entity, that needs to be done
479
request.put("shipTo_firstName", creditCard.getString("firstNameOnCard"));
480                 request.put("shipTo_lastName", creditCard.getString("lastNameOnCard"));
481             }
482
483             request.put("shipTo_street1", shippingAddress.getString("address1"));
484             if (shippingAddress.get("address2") != null) {
485                 request.put("shipTo_street2", shippingAddress.getString("address2"));
486             }
487             request.put("shipTo_city", shippingAddress.getString("city"));
488             String JavaDoc sCountry = shippingAddress.get("countryGeoId") != null ? shippingAddress.getString("countryGeoId") : "USA";
489
490             request.put("shipTo_country", sCountry);
491             request.put("shipTo_postalCode", shippingAddress.getString("postalCode"));
492             if (shippingAddress.get("stateProvinceGeoId") != null) {
493                 request.put("shipTo_state", shippingAddress.getString("stateProvinceGeoId"));
494             }
495         }
496     }
497
498     private static void appendItemLineInfo(Map JavaDoc request, Map JavaDoc context, String JavaDoc amountField) {
499         // send over a line item total offer w/ the total for billing; don't trust CyberSource for calc
500
String JavaDoc currency = (String JavaDoc) context.get("currency");
501
502         int lineNumber = 0;
503         request.put("item_" + lineNumber + "_unitPrice", getAmountString(context, amountField));
504
505         // the currency
506
request.put("purchaseTotals_currency", currency);
507
508         // create the offers (one for each line item)
509
List JavaDoc orderItems = (List JavaDoc) context.get("orderItems");
510         if (orderItems != null) {
511             Iterator JavaDoc itemIterator = orderItems.iterator();
512
513             while (itemIterator.hasNext()) {
514                 lineNumber++;
515                 GenericValue item = (GenericValue) itemIterator.next();
516                 GenericValue product = null;
517                 try {
518                     product = item.getRelatedOne("Product");
519                 } catch (GenericEntityException e) {
520                     Debug.logError(e, "ERROR: Unable to get Product from OrderItem, not passing info to CyberSource");
521                 }
522
523                 if (product != null) {
524                     request.put("item_" + lineNumber + "_productName", product.getString("productName"));
525                     request.put("item_" + lineNumber + "_productSKU", product.getString("productId"));
526                 } else {
527                     // no product; just send the item description -- non product items
528
request.put("item_" + lineNumber + "_productName", item.getString("description"));
529                 }
530
531                 // get the quantity..
532
Double JavaDoc quantity = item.getDouble("quantity");
533
534                 // test quantity if INT pass as is; if not pass as 1
535
long roundQ = Math.round(quantity.doubleValue());
536                 Double JavaDoc rounded = new Double JavaDoc(new Long JavaDoc(roundQ).toString());
537
538                 if (rounded.doubleValue() != quantity.doubleValue()) {
539                     request.put("item_" + lineNumber + "_quantity", "1");
540                 } else {
541                     request.put("", new Integer JavaDoc(quantity.intValue()).toString());
542                 }
543
544                 // set the amount to 0.0000 -- we will send a total too.
545
request.put("item_" + lineNumber + "_unitPrice", "0.0000");
546             }
547         }
548     }
549
550     private static String JavaDoc getAmountString(Map JavaDoc context, String JavaDoc amountField) {
551         String JavaDoc currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
552         DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc(currencyFormat);
553         Double JavaDoc processAmount = (Double JavaDoc) context.get(amountField);
554         return formatter.format(processAmount);
555     }
556
557     private static void processAuthResult(Map JavaDoc reply, Map JavaDoc result) {
558         String JavaDoc decision = getDecision(reply);
559         if ("ACCEPT".equalsIgnoreCase(decision)) {
560             result.put("authCode", reply.get("ccAuthReply_authorizationCode"));
561             result.put("authResult", new Boolean JavaDoc(true));
562         } else {
563             result.put("authCode", decision);
564             result.put("authResult", new Boolean JavaDoc(false));
565         }
566
567         if (reply.get("ccAuthReply_amount") != null) {
568             result.put("processAmount", new Double JavaDoc((String JavaDoc) reply.get("ccAuthReply_amount")));
569         } else {
570             result.put("processAmount", new Double JavaDoc(0.00));
571         }
572
573         result.put("authRefNum", reply.get("requestID"));
574         result.put("authFlag", reply.get("ccAuthReply_reasonCode"));
575         result.put("authMessage", reply.get("ccAuthReply_processorResponse"));
576         result.put("cvCode", reply.get("ccAuthReply_cvCode"));
577         result.put("avsCode", reply.get("ccAuthReply_avsCode"));
578         result.put("scoreCode", reply.get("ccAuthReply_authFactorCode"));
579         result.put("captureRefNum", reply.get("requestID")); // maybe use something else here?
580
result.put("captureCode", reply.get("ccCaptureReply_reconciliationID"));
581     }
582
583     private static void processCaptureResult(Map JavaDoc reply, Map JavaDoc result) {
584         String JavaDoc decision = getDecision(reply);
585         if ("ACCEPT".equalsIgnoreCase(decision)) {
586             result.put("captureResult", new Boolean JavaDoc(true));
587         } else {
588             result.put("captureResult", new Boolean JavaDoc(false));
589         }
590
591         if (reply.get("ccCaptureReply_amount") != null) {
592             result.put("captureAmount", new Double JavaDoc((String JavaDoc) reply.get("ccCaptureReply_amount")));
593         } else {
594             result.put("captureAmount", new Double JavaDoc(0.00));
595         }
596
597         result.put("captureRefNum", reply.get("requestID"));
598         result.put("captureCode", reply.get("ccCaptureReply_reconciliationID"));
599         result.put("captureFlag", reply.get("ccCaptureReply_reasonCode"));
600         result.put("captureMessage", reply.get("decision"));
601     }
602
603     private static void processReleaseResult(Map JavaDoc reply, Map JavaDoc result) {
604         String JavaDoc decision = getDecision(reply);
605         if ("ACCEPT".equalsIgnoreCase(decision)) {
606             result.put("releaseResult", new Boolean JavaDoc(true));
607         } else {
608             result.put("releaseResult", new Boolean JavaDoc(false));
609         }
610
611         if (reply.get("ccAuthReversalReply_amount") != null) {
612             result.put("releaseAmount", new Double JavaDoc((String JavaDoc) reply.get("ccAuthReversalReply_amount")));
613         } else {
614             result.put("releaseAmount", new Double JavaDoc(0.00));
615         }
616
617         result.put("releaseRefNum", reply.get("requestID"));
618         result.put("releaseCode", reply.get("ccAuthReversalReply_authorizationCode"));
619         result.put("releaseFlag", reply.get("ccAuthReversalReply_reasonCode"));
620         result.put("releaseMessage", reply.get("decision"));
621     }
622
623     private static void processRefundResult(Map JavaDoc reply, Map JavaDoc result) {
624         String JavaDoc decision = getDecision(reply);
625         if ("ACCEPT".equalsIgnoreCase(decision)) {
626             result.put("refundResult", new Boolean JavaDoc(true));
627         } else {
628             result.put("refundResult", new Boolean JavaDoc(false));
629         }
630
631         if (reply.get("ccCreditReply_amount") != null) {
632             result.put("refundAmount", new Double JavaDoc((String JavaDoc) reply.get("ccCreditReply_amount")));
633         } else {
634             result.put("refundAmount", new Double JavaDoc(0.00));
635         }
636
637         result.put("refundRefNum", reply.get("requestID"));
638         result.put("refundCode", reply.get("ccCreditReply_reconciliationID"));
639         result.put("refundFlag", reply.get("ccCreditReply_reasonCode"));
640         result.put("refundMessage", reply.get("decision"));
641     }
642
643     private static void processCreditResult(Map JavaDoc reply, Map JavaDoc result) {
644         String JavaDoc decision = (String JavaDoc) reply.get("decision");
645         if ("ACCEPT".equalsIgnoreCase(decision)) {
646             result.put("creditResult", new Boolean JavaDoc(true));
647         } else {
648             result.put("creditResult", new Boolean JavaDoc(false));
649         }
650
651         if (reply.get("ccCreditReply_amount") != null) {
652             result.put("creditAmount", new Double JavaDoc((String JavaDoc) reply.get("ccCreditReply_amount")));
653         } else {
654             result.put("creditAmount", new Double JavaDoc(0.00));
655         }
656
657         result.put("creditRefNum", reply.get("requestID"));
658         result.put("creditCode", reply.get("ccCreditReply_reconciliationID"));
659         result.put("creditFlag", reply.get("ccCreditReply_reasonCode"));
660         result.put("creditMessage", reply.get("decision"));
661     }
662
663     private static String JavaDoc getDecision(Map JavaDoc reply) {
664         String JavaDoc decision = (String JavaDoc) reply.get("decision");
665         String JavaDoc reasonCode = (String JavaDoc) reply.get("reasonCode");
666         if (!"ACCEPT".equalsIgnoreCase(decision)) {
667             Debug.logInfo("CyberSource : " + decision + " (" + reasonCode + ")", module);
668             Debug.logInfo("Reply Dump : " + reply, module);
669         }
670         return decision;
671     }
672 }
673
Popular Tags