1 25 package org.ofbiz.accounting.thirdparty.verisign; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 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 52 public class PayflowPro { 53 54 public static final String module = PayflowPro.class.getName(); 55 56 62 public static Map ccProcessor(DispatchContext dctx, Map context) { 63 GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); 64 GenericValue authTrans = (GenericValue) context.get("authTrans"); 65 String orderId = (String ) context.get("orderId"); 66 String cvv2 = (String ) context.get("cardSecurityCode"); 67 Double processAmount = (Double ) 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 configString = (String ) 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 boolean isReAuth = false; 82 Map data = UtilMisc.toMap("COMMENT1", orderId); 83 data.put("PONUM", orderId); 84 data.put("CUSTCODE", party.getString("partyId")); 85 86 if (UtilProperties.propertyValueEqualsIgnoreCase(configString, "payment.verisign.preAuth", "Y")) { 88 data.put("TRXTYPE", "A"); 89 if (authTrans != null) { 91 String refNum = authTrans.getString("referenceNum"); 92 data.put("ORIGID", refNum); 93 isReAuth = true; 94 } 95 } else { 96 data.put("TRXTYPE", "S"); 97 } 98 99 data.put("TENDER", "C"); 101 102 if (UtilValidate.isNotEmpty(cvv2)) { 104 data.put("CVV2", cvv2); 105 } 106 107 data.put("AMT", processAmount.toString()); 109 110 data.put("ACCT", cc.getString("cardNumber")); 112 113 String 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 exp = cc.getString("expireDate"); 120 String expDate = exp.substring(0, 2); 121 122 expDate = expDate + exp.substring(exp.length() - 2); 123 data.put("EXPDATE", expDate); 124 } 125 126 if (ps != null) { 128 String 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 StringBuffer params = makeBaseParams(configString); 140 141 params.append("&" + parseContext(data)); 143 144 if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module); 146 String resp = pn.SubmitTransaction(params.toString()); 147 148 if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module); 149 150 pn.DestroyContext(); 152 153 Map result = ServiceUtil.returnSuccess(); 155 parseAuthResponse(resp, result, configString, isReAuth); 156 result.put("processAmount", processAmount); 157 return result; 158 } 159 160 public static Map ccCapture(DispatchContext dctx, Map context) { 161 GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); 162 GenericValue authTrans = (GenericValue) context.get("authTrans"); 163 Double amount = (Double ) context.get("captureAmount"); 164 String configString = (String ) 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 String refNum = authTrans.getString("referenceNum"); 179 Map data = UtilMisc.toMap("ORIGID", refNum); 180 181 data.put("TRXTYPE", "D"); 183 184 data.put("TENDER", "C"); 186 187 String orderId = paymentPref.getString("orderId"); 189 data.put("COMMENT1", orderId); 190 191 data.put("AMT", amount.toString()); 193 194 PFProAPI pn = init(configString); 195 196 StringBuffer params = makeBaseParams(configString); 198 199 params.append("&" + parseContext(data)); 201 202 if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module); 204 String resp = pn.SubmitTransaction(params.toString()); 205 206 if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module); 207 208 pn.DestroyContext(); 210 211 Map result = ServiceUtil.returnSuccess(); 213 parseCaptureResponse(resp, result, configString); 214 result.put("captureAmount", amount); 215 return result; 216 } 217 218 public static Map ccVoid(DispatchContext dctx, Map context) { 219 GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); 220 GenericValue authTrans = (GenericValue) context.get("authTrans"); 221 Double amount = (Double ) context.get("releaseAmount"); 222 String configString = (String ) 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 String refNum = authTrans.getString("referenceNum"); 237 Map data = UtilMisc.toMap("ORIGID", refNum); 238 239 data.put("TRXTYPE", "V"); 241 242 data.put("TENDER", "C"); 244 245 String orderId = paymentPref.getString("orderId"); 247 data.put("COMMENT1", orderId); 248 249 data.put("AMT", amount.toString()); 251 252 PFProAPI pn = init(configString); 253 254 StringBuffer params = makeBaseParams(configString); 256 257 params.append("&" + parseContext(data)); 259 260 if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module); 262 String resp = pn.SubmitTransaction(params.toString()); 263 264 if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module); 265 266 pn.DestroyContext(); 268 269 Map result = ServiceUtil.returnSuccess(); 271 parseVoidResponse(resp, result, configString); 272 result.put("releaseAmount", amount); 273 return result; 274 } 275 276 public static Map ccRefund(DispatchContext dctx, Map context) { 277 GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); 278 Double amount = (Double ) context.get("refundAmount"); 279 String configString = (String ) 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 String refNum = captureTrans.getString("referenceNum"); 293 Map data = UtilMisc.toMap("ORIGID", refNum); 294 295 data.put("TRXTYPE", "C"); 297 298 data.put("TENDER", "C"); 300 301 String orderId = paymentPref.getString("orderId"); 303 data.put("COMMENT1", orderId); 304 305 data.put("AMT", amount.toString()); 307 308 PFProAPI pn = init(configString); 309 310 StringBuffer params = makeBaseParams(configString); 312 313 params.append("&" + parseContext(data)); 315 316 if (Debug.verboseOn()) Debug.logVerbose("Sending to Verisign: " + params.toString(), module); 318 String resp = pn.SubmitTransaction(params.toString()); 319 320 if (Debug.verboseOn()) Debug.logVerbose("Response from Verisign: " + resp, module); 321 322 pn.DestroyContext(); 324 325 Map result = ServiceUtil.returnSuccess(); 327 parseRefundResponse(resp, result, configString); 328 result.put("refundAmount", amount); 329 return result; 330 } 331 332 private static void parseAuthResponse(String resp, Map result, String resource, boolean isReAuth) { 333 Debug.logInfo("Verisign response string: " + resp, module); 334 Map parameters = new LinkedMap(); 335 List params = StringUtil.split(resp, "&"); 336 Iterator i = params.iterator(); 337 338 while (i.hasNext()) { 339 String str = (String ) i.next(); 340 341 if (str.length() > 0) { 342 List kv = StringUtil.split(str, "="); 343 Object k = kv.get(0); 344 Object v = kv.get(1); 345 346 if (k != null && v != null) 347 parameters.put(k, v); 348 } 349 } 350 351 boolean isSale = !UtilProperties.propertyValueEqualsIgnoreCase(resource, "payment.verisign.preAuth", "Y"); 353 354 boolean avsCheckOkay = true; 356 String avsCode = null; 357 if (!isReAuth) { 358 boolean checkAvs = UtilProperties.propertyValueEqualsIgnoreCase(resource, "payment.verisign.checkAvs", "Y"); 359 if (checkAvs && !isSale) { 360 String addAvs = (String ) parameters.get("AVSADDR"); 361 String zipAvs = (String ) parameters.get("AVSZIP"); 362 avsCode = addAvs + zipAvs; 363 if ("N".equals(addAvs) || "N".equals(zipAvs)) { 364 avsCheckOkay = false; 365 } 366 } 367 } 368 369 boolean cvv2CheckOkay = true; 371 String cvvCode = null; 372 if (!isReAuth) { 373 boolean checkCvv2 = UtilProperties.propertyValueEqualsIgnoreCase(resource, "payment.verisign.checkAvs", "Y"); 374 if (checkCvv2 && !isSale) { 375 cvvCode = (String ) parameters.get("CVV2MATCH"); 376 if ("N".equals(cvvCode)) { 377 cvv2CheckOkay = false; 378 } 379 } 380 } 381 382 String respCode = (String ) 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 resp, Map result, String resource) { 397 Map parameters = new LinkedMap(); 398 List params = StringUtil.split(resp, "&"); 399 Iterator i = params.iterator(); 400 401 while (i.hasNext()) { 402 String str = (String ) i.next(); 403 404 if (str.length() > 0) { 405 List kv = StringUtil.split(str, "="); 406 Object k = kv.get(0); 407 Object v = kv.get(1); 408 409 if (k != null && v != null) 410 parameters.put(k, v); 411 } 412 } 413 String respCode = (String ) 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 resp, Map result, String resource) { 427 Map parameters = new LinkedMap(); 428 List params = StringUtil.split(resp, "&"); 429 Iterator i = params.iterator(); 430 431 while (i.hasNext()) { 432 String str = (String ) i.next(); 433 434 if (str.length() > 0) { 435 List kv = StringUtil.split(str, "="); 436 Object k = kv.get(0); 437 Object v = kv.get(1); 438 439 if (k != null && v != null) 440 parameters.put(k, v); 441 } 442 } 443 String respCode = (String ) 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 resp, Map result, String resource) { 457 Map parameters = new LinkedMap(); 458 List params = StringUtil.split(resp, "&"); 459 Iterator i = params.iterator(); 460 461 while (i.hasNext()) { 462 String str = (String ) i.next(); 463 464 if (str.length() > 0) { 465 List kv = StringUtil.split(str, "="); 466 Object k = kv.get(0); 467 Object v = kv.get(1); 468 469 if (k != null && v != null) 470 parameters.put(k, v); 471 } 472 } 473 String respCode = (String ) 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 parseContext(Map context) { 486 StringBuffer buf = new StringBuffer (); 487 Set keySet = context.keySet(); 488 Iterator i = keySet.iterator(); 489 490 while (i.hasNext()) { 491 Object name = i.next(); 492 Object value = context.get(name); 493 494 if (value != null && (value instanceof String ) && ((String ) 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 makeBaseParams(String resource) { 504 StringBuffer buf = new StringBuffer (); 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 e) { 519 Debug.logError(e, module); 520 return null; 521 } 522 return buf; 523 } 524 525 private static PFProAPI init(String resource) { 526 String certsPath = UtilProperties.getPropertyValue(resource, "payment.verisign.certsPath", "pfcerts"); 527 String hostAddress = UtilProperties.getPropertyValue(resource, "payment.verisign.hostAddress", "test-payflow.verisign.com"); 528 Integer hostPort = Integer.decode(UtilProperties.getPropertyValue(resource, "payment.verisign.hostPort", "443")); 529 Integer timeout = Integer.decode(UtilProperties.getPropertyValue(resource, "payment.verisign.timeout", "80")); 530 String proxyAddress = UtilProperties.getPropertyValue(resource, "payment.verisign.proxyAddress", ""); 531 Integer proxyPort = Integer.decode(UtilProperties.getPropertyValue(resource, "payment.verisign.proxyPort", "80")); 532 String proxyLogon = UtilProperties.getPropertyValue(resource, "payment.verisign.proxyLogon", ""); 533 String proxyPassword = UtilProperties.getPropertyValue(resource, "payment.verisign.proxyPassword", ""); 534 535 PFProAPI pn = new PFProAPI(); 536 537 pn.SetCertPath(certsPath); 539 pn.CreateContext(hostAddress, hostPort.intValue(), timeout.intValue(), proxyAddress, proxyPort.intValue(), proxyLogon, proxyPassword); 541 return pn; 542 } 543 } 544 545 | Popular Tags |