KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > thirdparty > verisign > PayflowPro


1 /*
2  * $Id: PayflowPro.java 6755 2006-02-16 23:36:54Z jaz $
3  *
4  * Copyright (c) 2001-2005 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.verisign;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import com.Verisign.payment.PFProAPI;
33
34 import org.apache.commons.collections.map.LinkedMap;
35 import org.ofbiz.accounting.payment.PaymentGatewayServices;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.StringUtil;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.base.util.UtilProperties;
40 import org.ofbiz.base.util.UtilValidate;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.service.DispatchContext;
43 import org.ofbiz.service.ServiceUtil;
44
45 /**
46  * PayflowPro - Verisign PayFlow Pro <=> OFBiz Service Module
47  *
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 6755 $
50  * @since 2.0
51  */

52 public class PayflowPro {
53
54     public static final String JavaDoc module = PayflowPro.class.getName();
55
56     /**
57      * Authorize credit card payment service. Service wrapper around PayFlow Pro API.
58      * @param dctx Service Engine DispatchContext.
59      * @param context Map context of parameters.
60      * @return Response map, including RESPMSG, and RESULT keys.
61      */

62     public static Map JavaDoc ccProcessor(DispatchContext dctx, Map JavaDoc context) {
63         GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
64         GenericValue authTrans = (GenericValue) context.get("authTrans");
65         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
66         String JavaDoc cvv2 = (String JavaDoc) context.get("cardSecurityCode");
67         Double JavaDoc processAmount = (Double JavaDoc) context.get("processAmount");
68         GenericValue party = (GenericValue) context.get("billToParty");
69         GenericValue cc = (GenericValue) context.get("creditCard");
70         GenericValue ps = (GenericValue) context.get("billingAddress");
71         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
72         if (configString == null) {
73             configString = "payment.properties";
74         }
75
76         if (authTrans == null){
77             authTrans = PaymentGatewayServices.getAuthTransaction(paymentPref);
78         }
79
80         // set the orderId as comment1 so we can query in PF Manager
81
boolean isReAuth = false;
82         Map JavaDoc data = UtilMisc.toMap("COMMENT1", orderId);
83         data.put("PONUM", orderId);
84         data.put("CUSTCODE", party.getString("partyId"));
85
86         // transaction type
87
if (UtilProperties.propertyValueEqualsIgnoreCase(configString, "payment.verisign.preAuth", "Y")) {
88             data.put("TRXTYPE", "A");
89             // only support re-auth for auth types; sale types don't do it
90
if (authTrans != null) {
91                 String JavaDoc refNum = authTrans.getString("referenceNum");
92                 data.put("ORIGID", refNum);
93                 isReAuth = true;
94             }
95         } else {
96             data.put("TRXTYPE", "S");
97         }
98
99         // credit card tender
100
data.put("TENDER", "C");
101
102         // card security code
103
if (UtilValidate.isNotEmpty(cvv2)) {
104             data.put("CVV2", cvv2);
105         }
106
107         // set the amount
108
data.put("AMT", processAmount.toString());
109
110         // get the payment information
111
data.put("ACCT", cc.getString("cardNumber"));
112
113         // name on card
114
String JavaDoc name = cc.getString("firstNameOnCard") + " " + cc.getString("lastNameOnCard");
115         data.put("FIRSTNAME", cc.getString("firstNameOnCard"));
116         data.put("LASTNAME", cc.getString("lastNameOnCard"));
117         data.put("COMMENT2", name);
118         if (cc.get("expireDate") != null) {
119             String JavaDoc exp = cc.getString("expireDate");
120             String JavaDoc expDate = exp.substring(0, 2);
121
122             expDate = expDate + exp.substring(exp.length() - 2);
123             data.put("EXPDATE", expDate);
124         }
125
126         // gather the address info
127
if (ps != null) {
128             String JavaDoc street = ps.getString("address1") +
129                 (ps.get("address2") != null && ps.getString("address2").length() > 0 ? " " +
130                     ps.getString("address2") : "");
131
132             data.put("STREET", street);
133             data.put("ZIP", ps.getString("postalCode"));
134         }
135
136         PFProAPI pn = init(configString);
137
138         // get the base params
139
StringBuffer JavaDoc params = makeBaseParams(configString);
140
141         // parse the context parameters
142
params.append("&" + parseContext(data));
143
144         // transmit the request
145
if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module);
146         String JavaDoc resp = pn.SubmitTransaction(params.toString());
147
148         if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module);
149
150         // reset for next use
151
pn.DestroyContext();
152
153         // check the response
154
Map JavaDoc result = ServiceUtil.returnSuccess();
155         parseAuthResponse(resp, result, configString, isReAuth);
156         result.put("processAmount", processAmount);
157         return result;
158     }
159
160     public static Map JavaDoc ccCapture(DispatchContext dctx, Map JavaDoc context) {
161         GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
162         GenericValue authTrans = (GenericValue) context.get("authTrans");
163         Double JavaDoc amount = (Double JavaDoc) context.get("captureAmount");
164         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
165         if (configString == null) {
166             configString = "payment.properties";
167         }
168
169         if (authTrans == null){
170             authTrans = PaymentGatewayServices.getAuthTransaction(paymentPref);
171         }
172
173         if (authTrans == null) {
174             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot capture");
175         }
176
177         // auth ref number
178
String JavaDoc refNum = authTrans.getString("referenceNum");
179         Map JavaDoc data = UtilMisc.toMap("ORIGID", refNum);
180
181         // tx type (Delayed Capture)
182
data.put("TRXTYPE", "D");
183
184         // credit card tender
185
data.put("TENDER", "C");
186
187         // get the orderID
188
String JavaDoc orderId = paymentPref.getString("orderId");
189         data.put("COMMENT1", orderId);
190
191         // amount to capture
192
data.put("AMT", amount.toString());
193
194         PFProAPI pn = init(configString);
195
196         // get the base params
197
StringBuffer JavaDoc params = makeBaseParams(configString);
198
199         // parse the context parameters
200
params.append("&" + parseContext(data));
201
202         // transmit the request
203
if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module);
204         String JavaDoc resp = pn.SubmitTransaction(params.toString());
205
206         if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module);
207
208         // reset for next use
209
pn.DestroyContext();
210
211         // check the response
212
Map JavaDoc result = ServiceUtil.returnSuccess();
213         parseCaptureResponse(resp, result, configString);
214         result.put("captureAmount", amount);
215         return result;
216     }
217
218     public static Map JavaDoc ccVoid(DispatchContext dctx, Map JavaDoc context) {
219         GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
220         GenericValue authTrans = (GenericValue) context.get("authTrans");
221         Double JavaDoc amount = (Double JavaDoc) context.get("releaseAmount");
222         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
223         if (configString == null) {
224             configString = "payment.properties";
225         }
226
227         if (authTrans == null){
228             authTrans = PaymentGatewayServices.getAuthTransaction(paymentPref);
229         }
230
231         if (authTrans == null) {
232             return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot capture");
233         }
234
235         // auth ref number
236
String JavaDoc refNum = authTrans.getString("referenceNum");
237         Map JavaDoc data = UtilMisc.toMap("ORIGID", refNum);
238
239         // tx type (Void)
240
data.put("TRXTYPE", "V");
241
242         // credit card tender
243
data.put("TENDER", "C");
244
245         // get the orderID
246
String JavaDoc orderId = paymentPref.getString("orderId");
247         data.put("COMMENT1", orderId);
248
249         // amount to capture
250
data.put("AMT", amount.toString());
251
252         PFProAPI pn = init(configString);
253
254         // get the base params
255
StringBuffer JavaDoc params = makeBaseParams(configString);
256
257         // parse the context parameters
258
params.append("&" + parseContext(data));
259
260         // transmit the request
261
if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module);
262         String JavaDoc resp = pn.SubmitTransaction(params.toString());
263
264         if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module);
265
266         // reset for next use
267
pn.DestroyContext();
268
269         // check the response
270
Map JavaDoc result = ServiceUtil.returnSuccess();
271         parseVoidResponse(resp, result, configString);
272         result.put("releaseAmount", amount);
273         return result;
274     }
275
276     public static Map JavaDoc ccRefund(DispatchContext dctx, Map JavaDoc context) {
277         GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
278         Double JavaDoc amount = (Double JavaDoc) context.get("refundAmount");
279         String JavaDoc configString = (String JavaDoc) context.get("paymentConfig");
280         if (configString == null) {
281             configString = "payment.properties";
282         }
283
284         GenericValue captureTrans = PaymentGatewayServices.getCaptureTransaction(paymentPref);
285
286
287         if (captureTrans == null) {
288             return ServiceUtil.returnError("No capture transaction found for the OrderPaymentPreference; cannot refund");
289         }
290
291         // auth ref number
292
String JavaDoc refNum = captureTrans.getString("referenceNum");
293         Map JavaDoc data = UtilMisc.toMap("ORIGID", refNum);
294
295         // tx type (Credit)
296
data.put("TRXTYPE", "C");
297
298         // credit card tender
299
data.put("TENDER", "C");
300
301         // get the orderID
302
String JavaDoc orderId = paymentPref.getString("orderId");
303         data.put("COMMENT1", orderId);
304
305         // amount to capture
306
data.put("AMT", amount.toString());
307
308         PFProAPI pn = init(configString);
309
310         // get the base params
311
StringBuffer JavaDoc params = makeBaseParams(configString);
312
313         // parse the context parameters
314
params.append("&" + parseContext(data));
315
316         // transmit the request
317
if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module);
318         String JavaDoc resp = pn.SubmitTransaction(params.toString());
319
320         if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module);
321
322         // reset for next use
323
pn.DestroyContext();
324
325         // check the response
326
Map JavaDoc result = ServiceUtil.returnSuccess();
327         parseRefundResponse(resp, result, configString);
328         result.put("refundAmount", amount);
329         return result;
330     }
331
332     private static void parseAuthResponse(String JavaDoc resp, Map JavaDoc result, String JavaDoc resource, boolean isReAuth) {
333         Debug.logInfo("Verisign response string: " + resp, module);
334         Map JavaDoc parameters = new LinkedMap();
335         List JavaDoc params = StringUtil.split(resp, "&");
336         Iterator JavaDoc i = params.iterator();
337
338         while (i.hasNext()) {
339             String JavaDoc str = (String JavaDoc) i.next();
340
341             if (str.length() > 0) {
342                 List JavaDoc kv = StringUtil.split(str, "=");
343                 Object JavaDoc k = kv.get(0);
344                 Object JavaDoc v = kv.get(1);
345
346                 if (k != null && v != null)
347                     parameters.put(k, v);
348             }
349         }
350
351         // txType
352
boolean isSale = !UtilProperties.propertyValueEqualsIgnoreCase(resource, "payment.verisign.preAuth", "Y");
353
354         // avs checking - ignore on re-auth
355
boolean avsCheckOkay = true;
356         String JavaDoc avsCode = null;
357         if (!isReAuth) {
358             boolean checkAvs = UtilProperties.propertyValueEqualsIgnoreCase(resource, "payment.verisign.checkAvs", "Y");
359             if (checkAvs && !isSale) {
360                 String JavaDoc addAvs = (String JavaDoc) parameters.get("AVSADDR");
361                 String JavaDoc zipAvs = (String JavaDoc) parameters.get("AVSZIP");
362                 avsCode = addAvs + zipAvs;
363                 if ("N".equals(addAvs) || "N".equals(zipAvs)) {
364                     avsCheckOkay = false;
365                 }
366             }
367         }
368
369         // cvv2 checking - ignore on re-auth
370
boolean cvv2CheckOkay = true;
371         String JavaDoc cvvCode = null;
372         if (!isReAuth) {
373             boolean checkCvv2 = UtilProperties.propertyValueEqualsIgnoreCase(resource, "payment.verisign.checkAvs", "Y");
374             if (checkCvv2 && !isSale) {
375                 cvvCode = (String JavaDoc) parameters.get("CVV2MATCH");
376                 if ("N".equals(cvvCode)) {
377                     cvv2CheckOkay = false;
378                 }
379             }
380         }
381
382         String JavaDoc respCode = (String JavaDoc) parameters.get("RESULT");
383         if (respCode.equals("0") && avsCheckOkay && cvv2CheckOkay) {
384             result.put("authResult", Boolean.TRUE);
385             result.put("authCode", parameters.get("AUTHCODE"));
386         } else {
387             result.put("authResult", Boolean.FALSE);
388         }
389         result.put("cvCode", cvvCode);
390         result.put("avsCode", avsCode);
391         result.put("authRefNum", parameters.get("PNREF"));
392         result.put("authFlag", parameters.get("RESULT"));
393         result.put("authMessage", parameters.get("RESPMSG"));
394     }
395
396     private static void parseCaptureResponse(String JavaDoc resp, Map JavaDoc result, String JavaDoc resource) {
397         Map JavaDoc parameters = new LinkedMap();
398         List JavaDoc params = StringUtil.split(resp, "&");
399         Iterator JavaDoc i = params.iterator();
400
401         while (i.hasNext()) {
402             String JavaDoc str = (String JavaDoc) i.next();
403
404             if (str.length() > 0) {
405                 List JavaDoc kv = StringUtil.split(str, "=");
406                 Object JavaDoc k = kv.get(0);
407                 Object JavaDoc v = kv.get(1);
408
409                 if (k != null && v != null)
410                     parameters.put(k, v);
411             }
412         }
413         String JavaDoc respCode = (String JavaDoc) parameters.get("RESULT");
414
415         if (respCode.equals("0")) {
416             result.put("captureResult", Boolean.TRUE);
417             result.put("captureCode", parameters.get("AUTHCODE"));
418         } else {
419             result.put("captureResult", Boolean.FALSE);
420         }
421         result.put("captureRefNum", parameters.get("PNREF"));
422         result.put("captureFlag", parameters.get("RESULT"));
423         result.put("captureMessage", parameters.get("RESPMSG"));
424     }
425
426     private static void parseVoidResponse(String JavaDoc resp, Map JavaDoc result, String JavaDoc resource) {
427         Map JavaDoc parameters = new LinkedMap();
428         List JavaDoc params = StringUtil.split(resp, "&");
429         Iterator JavaDoc i = params.iterator();
430
431         while (i.hasNext()) {
432             String JavaDoc str = (String JavaDoc) i.next();
433
434             if (str.length() > 0) {
435                 List JavaDoc kv = StringUtil.split(str, "=");
436                 Object JavaDoc k = kv.get(0);
437                 Object JavaDoc v = kv.get(1);
438
439                 if (k != null && v != null)
440                     parameters.put(k, v);
441             }
442         }
443         String JavaDoc respCode = (String JavaDoc) parameters.get("RESULT");
444
445         if (respCode.equals("0")) {
446             result.put("releaseResult", Boolean.TRUE);
447             result.put("releaseCode", parameters.get("AUTHCODE"));
448         } else {
449             result.put("releaseResult", Boolean.FALSE);
450         }
451         result.put("releaseRefNum", parameters.get("PNREF"));
452         result.put("releaseFlag", parameters.get("RESULT"));
453         result.put("releaseMessage", parameters.get("RESPMSG"));
454     }
455
456     private static void parseRefundResponse(String JavaDoc resp, Map JavaDoc result, String JavaDoc resource) {
457         Map JavaDoc parameters = new LinkedMap();
458         List JavaDoc params = StringUtil.split(resp, "&");
459         Iterator JavaDoc i = params.iterator();
460
461         while (i.hasNext()) {
462             String JavaDoc str = (String JavaDoc) i.next();
463
464             if (str.length() > 0) {
465                 List JavaDoc kv = StringUtil.split(str, "=");
466                 Object JavaDoc k = kv.get(0);
467                 Object JavaDoc v = kv.get(1);
468
469                 if (k != null && v != null)
470                     parameters.put(k, v);
471             }
472         }
473         String JavaDoc respCode = (String JavaDoc) parameters.get("RESULT");
474
475         if (respCode.equals("0")) {
476             result.put("refundResult", Boolean.TRUE);
477             result.put("refundCode", parameters.get("AUTHCODE"));
478         } else {
479             result.put("refundResult", Boolean.FALSE);
480         }
481         result.put("refundRefNum", parameters.get("PNREF"));
482         result.put("refundFlag", parameters.get("RESULT"));
483         result.put("refundMessage", parameters.get("RESPMSG"));
484     }
485     private static String JavaDoc parseContext(Map JavaDoc context) {
486         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
487         Set JavaDoc keySet = context.keySet();
488         Iterator JavaDoc i = keySet.iterator();
489
490         while (i.hasNext()) {
491             Object JavaDoc name = i.next();
492             Object JavaDoc value = context.get(name);
493
494             if (value != null && (value instanceof String JavaDoc) && ((String JavaDoc) value).length() == 0) continue;
495             buf.append(name + "=");
496             buf.append(value);
497             if (i.hasNext())
498                 buf.append("&");
499         }
500         return buf.toString();
501     }
502
503     private static StringBuffer JavaDoc makeBaseParams(String JavaDoc resource) {
504         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
505
506         try {
507             buf.append("PARTNER=");
508             buf.append(UtilProperties.getPropertyValue(resource, "payment.verisign.partner", "VeriSign"));
509             buf.append("&");
510             buf.append("VENDOR=");
511             buf.append(UtilProperties.getPropertyValue(resource, "payment.verisign.vendor", "nobody"));
512             buf.append("&");
513             buf.append("USER=");
514             buf.append(UtilProperties.getPropertyValue(resource, "payment.verisign.user", "nobody"));
515             buf.append("&");
516             buf.append("PWD=");
517             buf.append(UtilProperties.getPropertyValue(resource, "payment.verisign.pwd", "password"));
518         } catch (Exception JavaDoc e) {
519             Debug.logError(e, module);
520             return null;
521         }
522         return buf;
523     }
524
525     private static PFProAPI init(String JavaDoc resource) {
526         String JavaDoc certsPath = UtilProperties.getPropertyValue(resource, "payment.verisign.certsPath", "pfcerts");
527         String JavaDoc hostAddress = UtilProperties.getPropertyValue(resource, "payment.verisign.hostAddress", "test-payflow.verisign.com");
528         Integer JavaDoc hostPort = Integer.decode(UtilProperties.getPropertyValue(resource, "payment.verisign.hostPort", "443"));
529         Integer JavaDoc timeout = Integer.decode(UtilProperties.getPropertyValue(resource, "payment.verisign.timeout", "80"));
530         String JavaDoc proxyAddress = UtilProperties.getPropertyValue(resource, "payment.verisign.proxyAddress", "");
531         Integer JavaDoc proxyPort = Integer.decode(UtilProperties.getPropertyValue(resource, "payment.verisign.proxyPort", "80"));
532         String JavaDoc proxyLogon = UtilProperties.getPropertyValue(resource, "payment.verisign.proxyLogon", "");
533         String JavaDoc proxyPassword = UtilProperties.getPropertyValue(resource, "payment.verisign.proxyPassword", "");
534
535         PFProAPI pn = new PFProAPI();
536
537         // Set the certificate path
538
pn.SetCertPath(certsPath);
539         // Call the client.
540
pn.CreateContext(hostAddress, hostPort.intValue(), timeout.intValue(), proxyAddress, proxyPort.intValue(), proxyLogon, proxyPassword);
541         return pn;
542     }
543 }
544
545
Popular Tags