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