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.text.DecimalFormat ; 31 import java.io.IOException ; 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 50 public class PcChargeServices { 51 52 public static final String module = PcChargeServices.class.getName(); 53 54 public static Map ccAuth(DispatchContext dctx, Map context) { 55 Properties 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 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 if ("1".equals(props.getProperty("autoBill"))) { 75 api.set(PcChargeApi.COMMAND, "1"); 77 } else { 78 api.set(PcChargeApi.COMMAND, "4"); 80 } 81 82 PcChargeApi out = null; 84 try { 85 out = api.send(); 86 } catch (IOException 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 result = ServiceUtil.returnSuccess(); 96 String resultCode = out.get(PcChargeApi.RESULT); 97 boolean passed = false; 98 if ("CAPTURED".equals(resultCode)) { 99 result.put("authResult", new Boolean (true)); 100 result.put("captureResult", new Boolean (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 (true)); 105 passed = true; 106 } else if ("PROCESSED".equals(resultCode)) { 107 result.put("authResult", new Boolean (true)); 108 } else { 109 result.put("authResult", new Boolean (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 respMsg = out.get(PcChargeApi.RESULT) + " / " + out.get(PcChargeApi.AUTH_CODE); 122 String 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 ccCapture(DispatchContext dctx, Map context) { 141 GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); 142 143 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 Properties 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 PcChargeApi out = null; 166 try { 167 out = api.send(); 168 } catch (IOException 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 result = ServiceUtil.returnSuccess(); 178 String resultCode = out.get(PcChargeApi.RESULT); 179 if ("CAPTURED".equals(resultCode)) { 180 result.put("captureResult", new Boolean (true)); 181 } else { 182 result.put("captureResult", new Boolean (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 ccRelease(DispatchContext dctx, Map context) { 197 GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); 198 199 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 Properties 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 if (!"true".equalsIgnoreCase(props.getProperty("autoBill"))) { 222 return ServiceUtil.returnError("PCCharge does not support releasing pre-auths."); 223 } 224 225 PcChargeApi out = null; 227 try { 228 out = api.send(); 229 } catch (IOException 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 result = ServiceUtil.returnSuccess(); 239 String resultCode = out.get(PcChargeApi.RESULT); 240 if ("VOIDED".equals(resultCode)) { 241 result.put("releaseResult", new Boolean (true)); 242 } else { 243 result.put("releaseResult", new Boolean (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 ccRefund(DispatchContext dctx, Map context) { 258 GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); 259 260 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 Properties 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 PcChargeApi out = null; 283 try { 284 out = api.send(); 285 } catch (IOException 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 result = ServiceUtil.returnSuccess(); 295 String resultCode = out.get(PcChargeApi.RESULT); 296 if ("CAPTURED".equals(resultCode)) { 297 result.put("refundResult", new Boolean (true)); 298 } else { 299 result.put("refundResult", new Boolean (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 context) throws GeneralException { 314 GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); 315 GenericValue creditCard = (GenericValue) context.get("creditCard"); 316 if (creditCard != null) { 317 List expDateList = StringUtil.split(creditCard.getString("expireDate"), "/"); 318 String month = (String ) expDateList.get(0); 319 String year = (String ) expDateList.get(1); 320 String y2d = year.substring(2); 321 String expDate = month + y2d; 322 323 String title = creditCard.getString("titleOnCard"); 324 String fname = creditCard.getString("firstNameOnCard"); 325 String mname = creditCard.getString("middleNameOnCard"); 326 String lname = creditCard.getString("lastNameOnCard"); 327 String sufix = creditCard.getString("suffixOnCard"); 328 StringBuffer name = new StringBuffer (); 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 nameOnCard = name.toString().trim(); 345 String acctNumber = "F" + creditCard.getString("cardNumber"); 346 String cvNum = (String ) 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 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 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 props) { 372 if (props == null) { 373 Debug.logError("Cannot load API w/ null properties", module); 374 return null; 375 } 376 String host = props.getProperty("host"); 377 int port = 0; 378 try { 379 port = Integer.parseInt(props.getProperty("port")); 380 } catch (Exception 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 buildPccProperties(Map context) { 397 String configString = (String ) context.get("paymentConfig"); 398 if (configString == null) { 399 configString = "payment.properties"; 400 } 401 402 String processorId = UtilProperties.getPropertyValue(configString, "payment.pccharge.processorID"); 403 String merchantId = UtilProperties.getPropertyValue(configString, "payment.pccharge.merchantID"); 404 String userId = UtilProperties.getPropertyValue(configString, "payment.pccharge.userID"); 405 String host = UtilProperties.getPropertyValue(configString, "payment.pccharge.host"); 406 String port = UtilProperties.getPropertyValue(configString, "payment.pccharge.port"); 407 String autoBill = UtilProperties.getPropertyValue(configString, "payment.pccharge.autoBill", "true"); 408 409 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 Properties props = new Properties (); 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 getAmountString(Map context, String amountField) { 437 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 438 DecimalFormat formatter = new DecimalFormat (currencyFormat); 439 Double processAmount = (Double ) context.get(amountField); 440 return formatter.format(processAmount); 441 } 442 443 } 444 | Popular Tags |