1 24 package org.ofbiz.accounting.payment; 25 26 import java.sql.Timestamp ; 27 import java.util.HashMap ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.base.util.StringUtil; 34 import org.ofbiz.base.util.UtilDateTime; 35 import org.ofbiz.base.util.UtilMisc; 36 import org.ofbiz.base.util.UtilValidate; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.ofbiz.entity.GenericValue; 40 import org.ofbiz.entity.util.EntityUtil; 41 import org.ofbiz.security.Security; 42 import org.ofbiz.service.DispatchContext; 43 import org.ofbiz.service.ModelService; 44 import org.ofbiz.service.ServiceUtil; 45 46 53 public class PaymentMethodServices { 54 55 public final static String module = PaymentMethodServices.class.getName(); 56 57 64 public static Map deletePaymentMethod(DispatchContext ctx, Map context) { 65 Map result = new HashMap (); 66 GenericDelegator delegator = ctx.getDelegator(); 67 Security security = ctx.getSecurity(); 68 GenericValue userLogin = (GenericValue) context.get("userLogin"); 69 70 Timestamp now = UtilDateTime.nowTimestamp(); 71 72 String paymentMethodId = (String ) context.get("paymentMethodId"); 74 GenericValue paymentMethod = null; 75 76 try { 77 paymentMethod = delegator.findByPrimaryKey("PaymentMethod", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 78 } catch (GenericEntityException e) { 79 Debug.logWarning(e.toString(), module); 80 return ServiceUtil.returnError("ERROR: Could not find Payment Method to delete (read failure: " + e.getMessage() + ")"); 81 } 82 83 if (paymentMethod == null) { 84 return ServiceUtil.returnError("ERROR: Could not find Payment Method to delete (read failure)"); 85 } 86 87 if (paymentMethod.get("partyId") == null || !paymentMethod.getString("partyId").equals(userLogin.getString("partyId"))) { 89 if (!security.hasEntityPermission("PAY_INFO", "_DELETE", userLogin)) { 90 return ServiceUtil.returnError("You do not have permission to delete Payment Method for this partyId"); 91 } 92 } 93 94 paymentMethod.set("thruDate", now); 95 try { 96 paymentMethod.store(); 97 } catch (GenericEntityException e) { 98 Debug.logWarning(e.toString(), module); 99 return ServiceUtil.returnError("ERROR: Could not delete Payment Method (write failure): " + e.getMessage()); 100 } 101 102 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 103 return result; 104 } 105 106 public static Map makeExpireDate(DispatchContext ctx, Map context) { 107 Map result = new HashMap (); 108 String expMonth = (String ) context.get("expMonth"); 109 String expYear = (String ) context.get("expYear"); 110 111 StringBuffer expDate = new StringBuffer (); 112 expDate.append(expMonth); 113 expDate.append("/"); 114 expDate.append(expYear); 115 result.put("expireDate", expDate.toString()); 116 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 117 return result; 118 } 119 120 127 public static Map createCreditCard(DispatchContext ctx, Map context) { 128 Map result = new HashMap (); 129 GenericDelegator delegator = ctx.getDelegator(); 130 Security security = ctx.getSecurity(); 131 GenericValue userLogin = (GenericValue) context.get("userLogin"); 132 133 Timestamp now = UtilDateTime.nowTimestamp(); 134 135 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE"); 136 137 if (result.size() > 0) return result; 138 139 List messages = new LinkedList (); 141 142 context.put("cardNumber", StringUtil.removeSpaces((String ) context.get("cardNumber"))); 144 if (!UtilValidate.isCardMatch((String ) context.get("cardType"), (String ) context.get("cardNumber"))) 145 messages.add( 146 (String ) context.get("cardNumber") 147 + UtilValidate.isCreditCardPrefixMsg 148 + (String ) context.get("cardType") 149 + UtilValidate.isCreditCardSuffixMsg 150 + " (It appears to be a " 151 + UtilValidate.getCardType((String ) context.get("cardNumber")) 152 + " credit card number)"); 153 if (!UtilValidate.isDateAfterToday((String ) context.get("expireDate"))) 154 messages.add("The expiration date " + (String ) context.get("expireDate") + " is before today."); 155 if (messages.size() > 0) { 156 return ServiceUtil.returnError(messages); 157 } 158 159 List toBeStored = new LinkedList (); 160 GenericValue newPm = delegator.makeValue("PaymentMethod", null); 161 162 toBeStored.add(newPm); 163 GenericValue newCc = delegator.makeValue("CreditCard", null); 164 165 toBeStored.add(newCc); 166 167 String newPmId = null; 168 try { 169 newPmId = delegator.getNextSeqId("PaymentMethod"); 170 } catch (IllegalArgumentException e) { 171 return ServiceUtil.returnError("ERROR: Could not create credit card (id generation failure)"); 172 173 } 174 175 newPm.set("partyId", partyId); 176 newPm.set("description",context.get("description")); 177 newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now)); 178 newPm.set("thruDate", context.get("thruDate")); 179 newCc.set("companyNameOnCard", context.get("companyNameOnCard")); 180 newCc.set("titleOnCard", context.get("titleOnCard")); 181 newCc.set("firstNameOnCard", context.get("firstNameOnCard")); 182 newCc.set("middleNameOnCard", context.get("middleNameOnCard")); 183 newCc.set("lastNameOnCard", context.get("lastNameOnCard")); 184 newCc.set("suffixOnCard", context.get("suffixOnCard")); 185 newCc.set("cardType", context.get("cardType")); 186 newCc.set("cardNumber", context.get("cardNumber")); 187 newCc.set("expireDate", context.get("expireDate")); 188 189 newPm.set("paymentMethodId", newPmId); 190 newPm.set("paymentMethodTypeId", "CREDIT_CARD"); 191 newCc.set("paymentMethodId", newPmId); 192 193 GenericValue newPartyContactMechPurpose = null; 194 String contactMechId = (String ) context.get("contactMechId"); 195 196 if (contactMechId != null && contactMechId.length() > 0 && !contactMechId.equals("_NEW_")) { 197 newCc.set("contactMechId", context.get("contactMechId")); 199 String contactMechPurposeTypeId = "BILLING_LOCATION"; 201 202 GenericValue tempVal = null; 203 204 try { 205 List allPCMPs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", 206 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId), null), true); 207 208 tempVal = EntityUtil.getFirst(allPCMPs); 209 } catch (GenericEntityException e) { 210 Debug.logWarning(e.getMessage(), module); 211 tempVal = null; 212 } 213 214 if (tempVal == null) { 215 newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", 217 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now)); 218 } 219 } 220 221 if (newPartyContactMechPurpose != null) toBeStored.add(newPartyContactMechPurpose); 222 223 try { 224 delegator.storeAll(toBeStored); 225 } catch (GenericEntityException e) { 226 Debug.logWarning(e.getMessage(), module); 227 return ServiceUtil.returnError("ERROR: Could not create credit card (write failure): " + e.getMessage()); 228 } 229 230 result.put("paymentMethodId", newCc.getString("paymentMethodId")); 231 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 232 return result; 233 } 234 235 242 public static Map updateCreditCard(DispatchContext ctx, Map context) { 243 Map result = new HashMap (); 244 GenericDelegator delegator = ctx.getDelegator(); 245 Security security = ctx.getSecurity(); 246 GenericValue userLogin = (GenericValue) context.get("userLogin"); 247 248 Timestamp now = UtilDateTime.nowTimestamp(); 249 250 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE"); 251 252 if (result.size() > 0) return result; 253 254 List toBeStored = new LinkedList (); 255 boolean isModified = false; 256 257 GenericValue paymentMethod = null; 258 GenericValue newPm = null; 259 GenericValue creditCard = null; 260 GenericValue newCc = null; 261 String paymentMethodId = (String ) context.get("paymentMethodId"); 262 263 try { 264 creditCard = delegator.findByPrimaryKey("CreditCard", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 265 paymentMethod = delegator.findByPrimaryKey("PaymentMethod", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 266 } catch (GenericEntityException e) { 267 Debug.logWarning(e.getMessage(), module); 268 return ServiceUtil.returnError( 269 "ERROR: Could not get credit card to update (read error): " + e.getMessage()); 270 } 271 272 if (creditCard == null || paymentMethod == null) { 273 return ServiceUtil.returnError("ERROR: Could not find credit card to update with payment method id " + paymentMethodId); 274 } 275 if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin)) { 276 return ServiceUtil.returnError("Party Id [" + partyId + "] is not the owner of payment method [" + paymentMethodId + "] and does not have permission to change it."); 277 } 278 279 List messages = new LinkedList (); 281 282 String updatedCardNumber = StringUtil.removeSpaces((String ) context.get("cardNumber")); 284 if (updatedCardNumber.startsWith("*")) { 285 String origCardNumber = creditCard.getString("cardNumber"); 287 Debug.log(origCardNumber); 288 String origMaskedNumber = ""; 289 int cardLength = origCardNumber.length() - 4; 290 for (int i = 0; i < cardLength; i++) { 291 origMaskedNumber = origMaskedNumber + "*"; 292 } 293 origMaskedNumber = origMaskedNumber + origCardNumber.substring(cardLength); 294 Debug.log(origMaskedNumber); 295 296 if (updatedCardNumber.equals(origMaskedNumber)) { 298 updatedCardNumber = origCardNumber; 299 } 300 } 301 context.put("cardNumber", updatedCardNumber); 302 303 if (!UtilValidate.isCardMatch((String ) context.get("cardType"), (String ) context.get("cardNumber"))) 304 messages.add((String ) context.get("cardNumber") 305 + UtilValidate.isCreditCardPrefixMsg + (String ) context.get("cardType") + UtilValidate.isCreditCardSuffixMsg 306 + " (It appears to be a " + UtilValidate.getCardType((String ) context.get("cardNumber")) + " credit card number)"); 307 if (!UtilValidate.isDateAfterToday((String ) context.get("expireDate"))) 308 messages.add("The expiration date " + (String ) context.get("expireDate") + " is before today."); 309 if (messages.size() > 0) { 310 return ServiceUtil.returnError(messages); 311 } 312 313 newPm = GenericValue.create(paymentMethod); 314 toBeStored.add(newPm); 315 newCc = GenericValue.create(creditCard); 316 toBeStored.add(newCc); 317 318 String newPmId = null; 319 try { 320 newPmId = delegator.getNextSeqId("PaymentMethod"); 321 } catch (IllegalArgumentException e) { 322 return ServiceUtil.returnError("ERROR: Could not update credit card info (id generation failure)"); 323 324 } 325 326 newPm.set("partyId", partyId); 327 newPm.set("fromDate", context.get("fromDate"), false); 328 newPm.set("description",context.get("description")); 329 if (newPm.get("thruDate") == null) { 331 newPm.set("thruDate", context.get("thruDate")); 332 } 333 newCc.set("companyNameOnCard", context.get("companyNameOnCard")); 334 newCc.set("titleOnCard", context.get("titleOnCard")); 335 newCc.set("firstNameOnCard", context.get("firstNameOnCard")); 336 newCc.set("middleNameOnCard", context.get("middleNameOnCard")); 337 newCc.set("lastNameOnCard", context.get("lastNameOnCard")); 338 newCc.set("suffixOnCard", context.get("suffixOnCard")); 339 340 newCc.set("cardType", context.get("cardType")); 341 newCc.set("cardNumber", context.get("cardNumber")); 342 newCc.set("expireDate", context.get("expireDate")); 343 344 GenericValue newPartyContactMechPurpose = null; 345 String contactMechId = (String ) context.get("contactMechId"); 346 347 if (contactMechId != null && contactMechId.length() > 0 && !contactMechId.equals("_NEW_")) { 348 newCc.set("contactMechId", contactMechId); 350 } 351 352 if (!newCc.equals(creditCard) || !newPm.equals(paymentMethod)) { 353 newPm.set("paymentMethodId", newPmId); 354 newCc.set("paymentMethodId", newPmId); 355 356 newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now)); 357 isModified = true; 358 } 359 360 if (contactMechId != null && contactMechId.length() > 0 && !contactMechId.equals("_NEW_")) { 361 362 String contactMechPurposeTypeId = "BILLING_LOCATION"; 364 365 GenericValue tempVal = null; 366 367 try { 368 List allPCMPs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", 369 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId), null), true); 370 371 tempVal = EntityUtil.getFirst(allPCMPs); 372 } catch (GenericEntityException e) { 373 Debug.logWarning(e.getMessage(), module); 374 tempVal = null; 375 } 376 377 if (tempVal == null) { 378 newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", 380 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now)); 381 } 382 } 383 384 if (isModified) { 385 if (newPartyContactMechPurpose != null) toBeStored.add(newPartyContactMechPurpose); 387 388 paymentMethod.set("thruDate", now); 390 toBeStored.add(paymentMethod); 391 392 try { 393 delegator.storeAll(toBeStored); 394 } catch (GenericEntityException e) { 395 Debug.logWarning(e.getMessage(), module); 396 return ServiceUtil.returnError("ERROR: Could not update credit card (write failure): " + e.getMessage()); 397 } 398 } else { 399 result.put("newPaymentMethodId", paymentMethodId); 400 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 401 if (contactMechId == null || !contactMechId.equals("_NEW_")) { 402 result.put(ModelService.SUCCESS_MESSAGE, "No changes made, not updating credit card"); 403 } 404 405 return result; 406 } 407 408 result.put("newPaymentMethodId", newCc.getString("paymentMethodId")); 409 410 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 411 return result; 412 } 413 414 public static Map createGiftCard(DispatchContext ctx, Map context) { 415 Map result = new HashMap (); 416 GenericDelegator delegator = ctx.getDelegator(); 417 Security security = ctx.getSecurity(); 418 GenericValue userLogin = (GenericValue) context.get("userLogin"); 419 420 Timestamp now = UtilDateTime.nowTimestamp(); 421 422 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE"); 423 424 if (result.size() > 0) 425 return result; 426 427 List toBeStored = new LinkedList (); 428 GenericValue newPm = delegator.makeValue("PaymentMethod", null); 429 toBeStored.add(newPm); 430 GenericValue newGc = delegator.makeValue("GiftCard", null); 431 toBeStored.add(newGc); 432 433 String newPmId = null; 434 try { 435 newPmId = delegator.getNextSeqId("PaymentMethod"); 436 } catch (IllegalArgumentException e) { 437 return ServiceUtil.returnError("ERROR: Could not create GiftCard (id generation failure)"); 438 } 439 440 newPm.set("partyId", partyId); 441 newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now)); 442 newPm.set("thruDate", context.get("thruDate")); 443 newPm.set("description",context.get("description")); 444 445 newGc.set("cardNumber", context.get("cardNumber")); 446 newGc.set("pinNumber", context.get("pinNumber")); 447 newGc.set("expireDate", context.get("expireDate")); 448 449 newPm.set("paymentMethodId", newPmId); 450 newPm.set("paymentMethodTypeId", "GIFT_CARD"); 451 newGc.set("paymentMethodId", newPmId); 452 453 try { 454 delegator.storeAll(toBeStored); 455 } catch (GenericEntityException e) { 456 Debug.logWarning(e.getMessage(), module); 457 return ServiceUtil.returnError("ERROR: Could not create GiftCard (write failure): " + e.getMessage()); 458 } 459 460 result.put("paymentMethodId", newGc.getString("paymentMethodId")); 461 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 462 return result; 463 } 464 465 public static Map updateGiftCard(DispatchContext ctx, Map context) { 466 Map result = new HashMap (); 467 GenericDelegator delegator = ctx.getDelegator(); 468 Security security = ctx.getSecurity(); 469 GenericValue userLogin = (GenericValue) context.get("userLogin"); 470 471 Timestamp now = UtilDateTime.nowTimestamp(); 472 473 String partyId = 474 ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE"); 475 476 if (result.size() > 0) 477 return result; 478 479 List toBeStored = new LinkedList (); 480 boolean isModified = false; 481 482 GenericValue paymentMethod = null; 483 GenericValue newPm = null; 484 GenericValue giftCard = null; 485 GenericValue newGc = null; 486 String paymentMethodId = (String ) context.get("paymentMethodId"); 487 488 try { 489 giftCard = delegator.findByPrimaryKey("GiftCard", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 490 paymentMethod = delegator.findByPrimaryKey("PaymentMethod", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 491 } catch (GenericEntityException e) { 492 Debug.logWarning(e.getMessage(), module); 493 return ServiceUtil.returnError("ERROR: Could not get GiftCard to update (read error): " + e.getMessage()); 494 } 495 496 if (giftCard == null || paymentMethod == null) { 497 return ServiceUtil.returnError("ERROR: Could not find GiftCard to update with id " + paymentMethodId); 498 } 499 if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin)) { 500 return ServiceUtil.returnError("Party Id [" + partyId + "] is not the owner of payment method [" + paymentMethodId + "] and does not have permission to change it."); 501 } 502 503 504 String cardNumber = StringUtil.removeSpaces((String ) context.get("cardNumber")); 506 if (cardNumber.startsWith("*")) { 507 String origCardNumber = giftCard.getString("cardNumber"); 509 String origMaskedNumber = ""; 511 int cardLength = origCardNumber.length() - 4; 512 if (cardLength > 0) { 513 for (int i = 0; i < cardLength; i++) { 514 origMaskedNumber = origMaskedNumber + "*"; 515 } 516 origMaskedNumber = origMaskedNumber + origCardNumber.substring(cardLength); 517 } else { 518 origMaskedNumber = origCardNumber; 519 } 520 521 if (cardNumber.equals(origMaskedNumber)) { 523 cardNumber = origCardNumber; 524 } 525 } 526 context.put("cardNumber", cardNumber); 527 528 newPm = GenericValue.create(paymentMethod); 529 toBeStored.add(newPm); 530 newGc = GenericValue.create(giftCard); 531 toBeStored.add(newGc); 532 533 String newPmId = null; 534 try { 535 newPmId = delegator.getNextSeqId("PaymentMethod"); 536 } catch (IllegalArgumentException e) { 537 return ServiceUtil.returnError("ERROR: Could not update GiftCard info (id generation failure)"); 538 } 539 540 newPm.set("partyId", partyId); 541 newPm.set("fromDate", context.get("fromDate"), false); 542 newPm.set("thruDate", context.get("thruDate")); 543 newPm.set("description",context.get("description")); 544 545 newGc.set("cardNumber", context.get("cardNumber")); 546 newGc.set("pinNumber", context.get("pinNumber")); 547 newGc.set("expireDate", context.get("expireDate")); 548 549 if (!newGc.equals(giftCard) || !newPm.equals(paymentMethod)) { 550 newPm.set("paymentMethodId", newPmId); 551 newGc.set("paymentMethodId", newPmId); 552 553 newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now)); 554 isModified = true; 555 } 556 557 if (isModified) { 558 paymentMethod.set("thruDate", now); 560 toBeStored.add(paymentMethod); 561 562 try { 563 delegator.storeAll(toBeStored); 564 } catch (GenericEntityException e) { 565 Debug.logWarning(e.getMessage(), module); 566 return ServiceUtil.returnError( 567 "ERROR: Could not update EFT Account (write failure): " + e.getMessage()); 568 } 569 } else { 570 result.put("newPaymentMethodId", paymentMethodId); 571 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 572 result.put(ModelService.SUCCESS_MESSAGE, "No changes made, not updating EFT Account"); 573 574 return result; 575 } 576 577 result.put("newPaymentMethodId", newGc.getString("paymentMethodId")); 578 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 579 return result; 580 } 581 582 589 public static Map createEftAccount(DispatchContext ctx, Map context) { 590 Map result = new HashMap (); 591 GenericDelegator delegator = ctx.getDelegator(); 592 Security security = ctx.getSecurity(); 593 GenericValue userLogin = (GenericValue) context.get("userLogin"); 594 595 Timestamp now = UtilDateTime.nowTimestamp(); 596 597 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE"); 598 599 if (result.size() > 0) return result; 600 601 List toBeStored = new LinkedList (); 602 GenericValue newPm = delegator.makeValue("PaymentMethod", null); 603 604 toBeStored.add(newPm); 605 GenericValue newEa = delegator.makeValue("EftAccount", null); 606 607 toBeStored.add(newEa); 608 609 String newPmId = null; 610 try { 611 newPmId = delegator.getNextSeqId("PaymentMethod"); 612 } catch (IllegalArgumentException e) { 613 return ServiceUtil.returnError("ERROR: Could not create credit card (id generation failure)"); 614 } 615 616 newPm.set("partyId", partyId); 617 newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now)); 618 newPm.set("thruDate", context.get("thruDate")); 619 newPm.set("description",context.get("description")); 620 newEa.set("bankName", context.get("bankName")); 621 newEa.set("routingNumber", context.get("routingNumber")); 622 newEa.set("accountType", context.get("accountType")); 623 newEa.set("accountNumber", context.get("accountNumber")); 624 newEa.set("nameOnAccount", context.get("nameOnAccount")); 625 newEa.set("companyNameOnAccount", context.get("companyNameOnAccount")); 626 newEa.set("contactMechId", context.get("contactMechId")); 627 628 newPm.set("paymentMethodId", newPmId); 629 newPm.set("paymentMethodTypeId", "EFT_ACCOUNT"); 630 newEa.set("paymentMethodId", newPmId); 631 632 GenericValue newPartyContactMechPurpose = null; 633 String contactMechId = (String ) context.get("contactMechId"); 634 635 if (contactMechId != null && contactMechId.length() > 0) { 636 String contactMechPurposeTypeId = "BILLING_LOCATION"; 638 639 GenericValue tempVal = null; 640 try { 641 List allPCMPs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", 642 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId), null), true); 643 644 tempVal = EntityUtil.getFirst(allPCMPs); 645 } catch (GenericEntityException e) { 646 Debug.logWarning(e.getMessage(), module); 647 tempVal = null; 648 } 649 650 if (tempVal == null) { 651 newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", 653 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now)); 654 } 655 } 656 657 if (newPartyContactMechPurpose != null) 658 toBeStored.add(newPartyContactMechPurpose); 659 660 try { 661 delegator.storeAll(toBeStored); 662 } catch (GenericEntityException e) { 663 Debug.logWarning(e.getMessage(), module); 664 return ServiceUtil.returnError("ERROR: Could not create credit card (write failure): " + e.getMessage()); 665 } 666 667 result.put("paymentMethodId", newEa.getString("paymentMethodId")); 668 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 669 return result; 670 } 671 672 679 public static Map updateEftAccount(DispatchContext ctx, Map context) { 680 Map result = new HashMap (); 681 GenericDelegator delegator = ctx.getDelegator(); 682 Security security = ctx.getSecurity(); 683 GenericValue userLogin = (GenericValue) context.get("userLogin"); 684 685 Timestamp now = UtilDateTime.nowTimestamp(); 686 687 String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE"); 688 689 if (result.size() > 0) return result; 690 691 List toBeStored = new LinkedList (); 692 boolean isModified = false; 693 694 GenericValue paymentMethod = null; 695 GenericValue newPm = null; 696 GenericValue eftAccount = null; 697 GenericValue newEa = null; 698 String paymentMethodId = (String ) context.get("paymentMethodId"); 699 700 try { 701 eftAccount = delegator.findByPrimaryKey("EftAccount", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 702 paymentMethod = 703 delegator.findByPrimaryKey("PaymentMethod", UtilMisc.toMap("paymentMethodId", paymentMethodId)); 704 } catch (GenericEntityException e) { 705 Debug.logWarning(e.getMessage(), module); 706 return ServiceUtil.returnError( 707 "ERROR: Could not get EFT Account to update (read error): " + e.getMessage()); 708 } 709 710 if (eftAccount == null || paymentMethod == null) { 711 return ServiceUtil.returnError("ERROR: Could not find EFT Account to update with id " + paymentMethodId); 712 } 713 if (!paymentMethod.getString("partyId").equals(partyId) && !security.hasEntityPermission("PAY_INFO", "_UPDATE", userLogin)) { 714 return ServiceUtil.returnError("Party Id [" + partyId + "] is not the owner of payment method [" + paymentMethodId + "] and does not have permission to change it."); 715 } 716 717 newPm = GenericValue.create(paymentMethod); 718 toBeStored.add(newPm); 719 newEa = GenericValue.create(eftAccount); 720 toBeStored.add(newEa); 721 722 String newPmId = null; 723 try { 724 newPmId = delegator.getNextSeqId("PaymentMethod"); 725 } catch (IllegalArgumentException e) { 726 return ServiceUtil.returnError("ERROR: Could not update EFT Account info (id generation failure)"); 727 } 728 729 newPm.set("partyId", partyId); 730 newPm.set("fromDate", context.get("fromDate"), false); 731 newPm.set("thruDate", context.get("thruDate")); 732 newPm.set("description",context.get("description")); 733 newEa.set("bankName", context.get("bankName")); 734 newEa.set("routingNumber", context.get("routingNumber")); 735 newEa.set("accountType", context.get("accountType")); 736 newEa.set("accountNumber", context.get("accountNumber")); 737 newEa.set("nameOnAccount", context.get("nameOnAccount")); 738 newEa.set("companyNameOnAccount", context.get("companyNameOnAccount")); 739 newEa.set("contactMechId", context.get("contactMechId")); 740 741 if (!newEa.equals(eftAccount) || !newPm.equals(paymentMethod)) { 742 newPm.set("paymentMethodId", newPmId); 743 newEa.set("paymentMethodId", newPmId); 744 newPm.set("fromDate", (context.get("fromDate") != null ? context.get("fromDate") : now)); 745 isModified = true; 746 } 747 748 GenericValue newPartyContactMechPurpose = null; 749 String contactMechId = (String ) context.get("contactMechId"); 750 751 if (contactMechId != null && contactMechId.length() > 0) { 752 String contactMechPurposeTypeId = "BILLING_LOCATION"; 754 755 GenericValue tempVal = null; 756 757 try { 758 List allPCMPs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", 759 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId",contactMechPurposeTypeId), null), true); 760 tempVal = EntityUtil.getFirst(allPCMPs); 761 } catch (GenericEntityException e) { 762 Debug.logWarning(e.getMessage(), module); 763 tempVal = null; 764 } 765 766 if (tempVal == null) { 767 newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose", 769 UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now)); 770 } 771 } 772 773 if (isModified) { 774 if (newPartyContactMechPurpose != null) 776 toBeStored.add(newPartyContactMechPurpose); 777 778 paymentMethod.set("thruDate", now); 780 toBeStored.add(paymentMethod); 781 782 try { 783 delegator.storeAll(toBeStored); 784 } catch (GenericEntityException e) { 785 Debug.logWarning(e.getMessage(), module); 786 return ServiceUtil.returnError( 787 "ERROR: Could not update EFT Account (write failure): " + e.getMessage()); 788 } 789 } else { 790 result.put("newPaymentMethodId", paymentMethodId); 791 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 792 result.put(ModelService.SUCCESS_MESSAGE, "No changes made, not updating EFT Account"); 793 794 return result; 795 } 796 797 result.put("newPaymentMethodId", newEa.getString("paymentMethodId")); 798 799 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 800 return result; 801 } 802 } 803 | Popular Tags |