0001: /*
0002: * $Id: PaymentServices.java,v 1.6 2004/02/06 00:31:27 jonesde Exp $
0003: *
0004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
0005: *
0006: * Permission is hereby granted, free of charge, to any person obtaining a
0007: * copy of this software and associated documentation files (the "Software"),
0008: * to deal in the Software without restriction, including without limitation
0009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010: * and/or sell copies of the Software, and to permit persons to whom the
0011: * Software is furnished to do so, subject to the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be included
0014: * in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
0021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
0022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024: package org.ofbiz.accounting.payment;
0025:
0026: import java.sql.Timestamp;
0027: import java.util.HashMap;
0028: import java.util.LinkedList;
0029: import java.util.List;
0030: import java.util.Map;
0031:
0032: import org.ofbiz.base.util.Debug;
0033: import org.ofbiz.base.util.StringUtil;
0034: import org.ofbiz.base.util.UtilDateTime;
0035: import org.ofbiz.base.util.UtilMisc;
0036: import org.ofbiz.base.util.UtilValidate;
0037: import org.ofbiz.entity.GenericDelegator;
0038: import org.ofbiz.entity.GenericEntityException;
0039: import org.ofbiz.entity.GenericValue;
0040: import org.ofbiz.entity.util.EntityUtil;
0041: import org.ofbiz.security.Security;
0042: import org.ofbiz.service.DispatchContext;
0043: import org.ofbiz.service.ModelService;
0044: import org.ofbiz.service.ServiceUtil;
0045:
0046: /**
0047: * Services for Payment maintenance
0048: *
0049: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
0050: * @version $Revision: 1.6 $
0051: * @since 2.0
0052: */
0053: public class PaymentServices {
0054:
0055: public final static String module = PaymentServices.class.getName();
0056:
0057: /**
0058: * Deletes a PaymentMethod entity according to the parameters passed in the context
0059: * <b>security check</b>: userLogin partyId must equal paymentMethod partyId, or must have PAY_INFO_DELETE permission
0060: * @param ctx The DispatchContext that this service is operating in
0061: * @param context Map containing the input parameters
0062: * @return Map with the result of the service, the output parameters
0063: */
0064: public static Map deletePaymentMethod(DispatchContext ctx,
0065: Map context) {
0066: Map result = new HashMap();
0067: GenericDelegator delegator = ctx.getDelegator();
0068: Security security = ctx.getSecurity();
0069: GenericValue userLogin = (GenericValue) context
0070: .get("userLogin");
0071:
0072: Timestamp now = UtilDateTime.nowTimestamp();
0073:
0074: // never delete a PaymentMethod, just put a to date on the link to the party
0075: String paymentMethodId = (String) context
0076: .get("paymentMethodId");
0077: GenericValue paymentMethod = null;
0078:
0079: try {
0080: paymentMethod = delegator.findByPrimaryKey("PaymentMethod",
0081: UtilMisc.toMap("paymentMethodId", paymentMethodId));
0082: } catch (GenericEntityException e) {
0083: Debug.logWarning(e.toString(), module);
0084: return ServiceUtil
0085: .returnError("ERROR: Could not find Payment Method to delete (read failure: "
0086: + e.getMessage() + ")");
0087: }
0088:
0089: if (paymentMethod == null) {
0090: return ServiceUtil
0091: .returnError("ERROR: Could not find Payment Method to delete (read failure)");
0092: }
0093:
0094: // <b>security check</b>: userLogin partyId must equal paymentMethod partyId, or must have PAY_INFO_DELETE permission
0095: if (paymentMethod.get("partyId") == null
0096: || !paymentMethod.getString("partyId").equals(
0097: userLogin.getString("partyId"))) {
0098: if (!security.hasEntityPermission("PAY_INFO", "_DELETE",
0099: userLogin)) {
0100: return ServiceUtil
0101: .returnError("You do not have permission to delete Payment Method for this partyId");
0102: }
0103: }
0104:
0105: paymentMethod.set("thruDate", now);
0106: try {
0107: paymentMethod.store();
0108: } catch (GenericEntityException e) {
0109: Debug.logWarning(e.toString(), module);
0110: return ServiceUtil
0111: .returnError("ERROR: Could not delete Payment Method (write failure): "
0112: + e.getMessage());
0113: }
0114:
0115: result.put(ModelService.RESPONSE_MESSAGE,
0116: ModelService.RESPOND_SUCCESS);
0117: return result;
0118: }
0119:
0120: public static Map makeExpireDate(DispatchContext ctx, Map context) {
0121: Map result = new HashMap();
0122: String expMonth = (String) context.get("expMonth");
0123: String expYear = (String) context.get("expYear");
0124:
0125: StringBuffer expDate = new StringBuffer();
0126: expDate.append(expMonth);
0127: expDate.append("/");
0128: expDate.append(expYear);
0129: result.put("expireDate", expDate.toString());
0130: result.put(ModelService.RESPONSE_MESSAGE,
0131: ModelService.RESPOND_SUCCESS);
0132: return result;
0133: }
0134:
0135: /**
0136: * Creates CreditCard and PaymentMethod entities according to the parameters passed in the context
0137: * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_CREATE permission
0138: * @param ctx The DispatchContext that this service is operating in
0139: * @param context Map containing the input parameters
0140: * @return Map with the result of the service, the output parameters
0141: */
0142: public static Map createCreditCard(DispatchContext ctx, Map context) {
0143: Map result = new HashMap();
0144: GenericDelegator delegator = ctx.getDelegator();
0145: Security security = ctx.getSecurity();
0146: GenericValue userLogin = (GenericValue) context
0147: .get("userLogin");
0148:
0149: Timestamp now = UtilDateTime.nowTimestamp();
0150:
0151: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0152: security, context, result, "PAY_INFO", "_CREATE");
0153:
0154: if (result.size() > 0)
0155: return result;
0156:
0157: // do some more complicated/critical validation...
0158: List messages = new LinkedList();
0159:
0160: // first remove all spaces from the credit card number
0161: context.put("cardNumber", StringUtil
0162: .removeSpaces((String) context.get("cardNumber")));
0163: if (!UtilValidate.isCardMatch((String) context.get("cardType"),
0164: (String) context.get("cardNumber")))
0165: messages.add((String) context.get("cardNumber")
0166: + UtilValidate.isCreditCardPrefixMsg
0167: + (String) context.get("cardType")
0168: + UtilValidate.isCreditCardSuffixMsg
0169: + " (It appears to be a "
0170: + UtilValidate.getCardType((String) context
0171: .get("cardNumber"))
0172: + " credit card number)");
0173: if (!UtilValidate.isDateAfterToday((String) context
0174: .get("expireDate")))
0175: messages.add("The expiration date "
0176: + (String) context.get("expireDate")
0177: + " is before today.");
0178: if (messages.size() > 0) {
0179: return ServiceUtil.returnError(messages);
0180: }
0181:
0182: List toBeStored = new LinkedList();
0183: GenericValue newPm = delegator.makeValue("PaymentMethod", null);
0184:
0185: toBeStored.add(newPm);
0186: GenericValue newCc = delegator.makeValue("CreditCard", null);
0187:
0188: toBeStored.add(newCc);
0189:
0190: Long newPmId = delegator.getNextSeqId("PaymentMethod");
0191:
0192: if (newPmId == null) {
0193: return ServiceUtil
0194: .returnError("ERROR: Could not create credit card (id generation failure)");
0195: }
0196: newPm.set("partyId", partyId);
0197: newPm.set("fromDate",
0198: (context.get("fromDate") != null ? context
0199: .get("fromDate") : now));
0200: newPm.set("thruDate", context.get("thruDate"));
0201: newCc.set("nameOnCard", context.get("nameOnCard"));
0202: newCc
0203: .set("companyNameOnCard", context
0204: .get("companyNameOnCard"));
0205: newCc.set("cardType", context.get("cardType"));
0206: newCc.set("cardNumber", context.get("cardNumber"));
0207: newCc.set("expireDate", context.get("expireDate"));
0208:
0209: newPm.set("paymentMethodId", newPmId.toString());
0210: newPm.set("paymentMethodTypeId", "CREDIT_CARD");
0211: newCc.set("paymentMethodId", newPmId.toString());
0212:
0213: GenericValue newPartyContactMechPurpose = null;
0214: String contactMechId = (String) context.get("contactMechId");
0215:
0216: if (contactMechId != null && contactMechId.length() > 0
0217: && !contactMechId.equals("_NEW_")) {
0218: // set the contactMechId on the credit card
0219: newCc.set("contactMechId", context.get("contactMechId"));
0220: // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
0221: String contactMechPurposeTypeId = "BILLING_LOCATION";
0222:
0223: GenericValue tempVal = null;
0224:
0225: try {
0226: List allPCMPs = EntityUtil
0227: .filterByDate(
0228: delegator
0229: .findByAnd(
0230: "PartyContactMechPurpose",
0231: UtilMisc
0232: .toMap(
0233: "partyId",
0234: partyId,
0235: "contactMechId",
0236: contactMechId,
0237: "contactMechPurposeTypeId",
0238: contactMechPurposeTypeId),
0239: null), true);
0240:
0241: tempVal = EntityUtil.getFirst(allPCMPs);
0242: } catch (GenericEntityException e) {
0243: Debug.logWarning(e.getMessage(), module);
0244: tempVal = null;
0245: }
0246:
0247: if (tempVal == null) {
0248: // no value found, create a new one
0249: newPartyContactMechPurpose = delegator.makeValue(
0250: "PartyContactMechPurpose", UtilMisc.toMap(
0251: "partyId", partyId, "contactMechId",
0252: contactMechId,
0253: "contactMechPurposeTypeId",
0254: contactMechPurposeTypeId, "fromDate",
0255: now));
0256: }
0257: }
0258:
0259: if (newPartyContactMechPurpose != null)
0260: toBeStored.add(newPartyContactMechPurpose);
0261:
0262: try {
0263: delegator.storeAll(toBeStored);
0264: } catch (GenericEntityException e) {
0265: Debug.logWarning(e.getMessage(), module);
0266: return ServiceUtil
0267: .returnError("ERROR: Could not create credit card (write failure): "
0268: + e.getMessage());
0269: }
0270:
0271: result.put("paymentMethodId", newCc
0272: .getString("paymentMethodId"));
0273: result.put(ModelService.RESPONSE_MESSAGE,
0274: ModelService.RESPOND_SUCCESS);
0275: return result;
0276: }
0277:
0278: /**
0279: * Updates CreditCard and PaymentMethod entities according to the parameters passed in the context
0280: * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
0281: * @param ctx The DispatchContext that this service is operating in
0282: * @param context Map containing the input parameters
0283: * @return Map with the result of the service, the output parameters
0284: */
0285: public static Map updateCreditCard(DispatchContext ctx, Map context) {
0286: Map result = new HashMap();
0287: GenericDelegator delegator = ctx.getDelegator();
0288: Security security = ctx.getSecurity();
0289: GenericValue userLogin = (GenericValue) context
0290: .get("userLogin");
0291:
0292: Timestamp now = UtilDateTime.nowTimestamp();
0293:
0294: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0295: security, context, result, "PAY_INFO", "_UPDATE");
0296:
0297: if (result.size() > 0)
0298: return result;
0299:
0300: List toBeStored = new LinkedList();
0301: boolean isModified = false;
0302:
0303: GenericValue paymentMethod = null;
0304: GenericValue newPm = null;
0305: GenericValue creditCard = null;
0306: GenericValue newCc = null;
0307: String paymentMethodId = (String) context
0308: .get("paymentMethodId");
0309:
0310: try {
0311: creditCard = delegator.findByPrimaryKey("CreditCard",
0312: UtilMisc.toMap("paymentMethodId", paymentMethodId));
0313: paymentMethod = delegator.findByPrimaryKey("PaymentMethod",
0314: UtilMisc.toMap("paymentMethodId", paymentMethodId));
0315: } catch (GenericEntityException e) {
0316: Debug.logWarning(e.getMessage(), module);
0317: return ServiceUtil
0318: .returnError("ERROR: Could not get credit card to update (read error): "
0319: + e.getMessage());
0320: }
0321:
0322: if (creditCard == null || paymentMethod == null) {
0323: return ServiceUtil
0324: .returnError("ERROR: Could not find credit card to update with payment method id "
0325: + paymentMethodId);
0326: }
0327:
0328: // do some more complicated/critical validation...
0329: List messages = new LinkedList();
0330:
0331: // first remove all spaces from the credit card number
0332: String updatedCardNumber = StringUtil
0333: .removeSpaces((String) context.get("cardNumber"));
0334: if (updatedCardNumber.startsWith("*")) {
0335: // get the masked card number from the db
0336: String origCardNumber = creditCard.getString("cardNumber");
0337: Debug.log(origCardNumber);
0338: String origMaskedNumber = "";
0339: int cardLength = origCardNumber.length() - 4;
0340: for (int i = 0; i < cardLength; i++) {
0341: origMaskedNumber = origMaskedNumber + "*";
0342: }
0343: origMaskedNumber = origMaskedNumber
0344: + origCardNumber.substring(cardLength);
0345: Debug.log(origMaskedNumber);
0346:
0347: // compare the two masked numbers
0348: if (updatedCardNumber.equals(origMaskedNumber)) {
0349: updatedCardNumber = origCardNumber;
0350: }
0351: }
0352: context.put("cardNumber", updatedCardNumber);
0353:
0354: if (!UtilValidate.isCardMatch((String) context.get("cardType"),
0355: (String) context.get("cardNumber")))
0356: messages.add((String) context.get("cardNumber")
0357: + UtilValidate.isCreditCardPrefixMsg
0358: + (String) context.get("cardType")
0359: + UtilValidate.isCreditCardSuffixMsg
0360: + " (It appears to be a "
0361: + UtilValidate.getCardType((String) context
0362: .get("cardNumber"))
0363: + " credit card number)");
0364: if (!UtilValidate.isDateAfterToday((String) context
0365: .get("expireDate")))
0366: messages.add("The expiration date "
0367: + (String) context.get("expireDate")
0368: + " is before today.");
0369: if (messages.size() > 0) {
0370: return ServiceUtil.returnError(messages);
0371: }
0372:
0373: newPm = new GenericValue(paymentMethod);
0374: toBeStored.add(newPm);
0375: newCc = new GenericValue(creditCard);
0376: toBeStored.add(newCc);
0377:
0378: Long newPmId = delegator.getNextSeqId("PaymentMethod");
0379:
0380: if (newPmId == null) {
0381: return ServiceUtil
0382: .returnError("ERROR: Could not update credit card info (id generation failure)");
0383: }
0384:
0385: newPm.set("partyId", partyId);
0386: newPm.set("fromDate", context.get("fromDate"), false);
0387: newPm.set("thruDate", context.get("thruDate"));
0388: newCc.set("nameOnCard", context.get("nameOnCard"));
0389: newCc
0390: .set("companyNameOnCard", context
0391: .get("companyNameOnCard"));
0392: newCc.set("cardType", context.get("cardType"));
0393: newCc.set("cardNumber", context.get("cardNumber"));
0394: newCc.set("expireDate", context.get("expireDate"));
0395:
0396: GenericValue newPartyContactMechPurpose = null;
0397: String contactMechId = (String) context.get("contactMechId");
0398:
0399: if (contactMechId != null && contactMechId.length() > 0
0400: && !contactMechId.equals("_NEW_")) {
0401: // set the contactMechId on the credit card
0402: newCc.set("contactMechId", contactMechId);
0403: }
0404:
0405: if (!newCc.equals(creditCard) || !newPm.equals(paymentMethod)) {
0406: newPm.set("paymentMethodId", newPmId.toString());
0407: newCc.set("paymentMethodId", newPmId.toString());
0408:
0409: newPm.set("fromDate",
0410: (context.get("fromDate") != null ? context
0411: .get("fromDate") : now));
0412: isModified = true;
0413: }
0414:
0415: if (contactMechId != null && contactMechId.length() > 0
0416: && !contactMechId.equals("_NEW_")) {
0417:
0418: // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
0419: String contactMechPurposeTypeId = "BILLING_LOCATION";
0420:
0421: GenericValue tempVal = null;
0422:
0423: try {
0424: List allPCMPs = EntityUtil
0425: .filterByDate(
0426: delegator
0427: .findByAnd(
0428: "PartyContactMechPurpose",
0429: UtilMisc
0430: .toMap(
0431: "partyId",
0432: partyId,
0433: "contactMechId",
0434: contactMechId,
0435: "contactMechPurposeTypeId",
0436: contactMechPurposeTypeId),
0437: null), true);
0438:
0439: tempVal = EntityUtil.getFirst(allPCMPs);
0440: } catch (GenericEntityException e) {
0441: Debug.logWarning(e.getMessage(), module);
0442: tempVal = null;
0443: }
0444:
0445: if (tempVal == null) {
0446: // no value found, create a new one
0447: newPartyContactMechPurpose = delegator.makeValue(
0448: "PartyContactMechPurpose", UtilMisc.toMap(
0449: "partyId", partyId, "contactMechId",
0450: contactMechId,
0451: "contactMechPurposeTypeId",
0452: contactMechPurposeTypeId, "fromDate",
0453: now));
0454: }
0455: }
0456:
0457: if (isModified) {
0458: // Debug.logInfo("yes, is modified", module);
0459: if (newPartyContactMechPurpose != null)
0460: toBeStored.add(newPartyContactMechPurpose);
0461:
0462: // set thru date on old paymentMethod
0463: paymentMethod.set("thruDate", now);
0464: toBeStored.add(paymentMethod);
0465:
0466: try {
0467: delegator.storeAll(toBeStored);
0468: } catch (GenericEntityException e) {
0469: Debug.logWarning(e.getMessage(), module);
0470: return ServiceUtil
0471: .returnError("ERROR: Could not update credit card (write failure): "
0472: + e.getMessage());
0473: }
0474: } else {
0475: result.put("newPaymentMethodId", paymentMethodId);
0476: result.put(ModelService.RESPONSE_MESSAGE,
0477: ModelService.RESPOND_SUCCESS);
0478: if (contactMechId == null || !contactMechId.equals("_NEW_")) {
0479: result.put(ModelService.SUCCESS_MESSAGE,
0480: "No changes made, not updating credit card");
0481: }
0482:
0483: return result;
0484: }
0485:
0486: result.put("newPaymentMethodId", newCc
0487: .getString("paymentMethodId"));
0488:
0489: result.put(ModelService.RESPONSE_MESSAGE,
0490: ModelService.RESPOND_SUCCESS);
0491: return result;
0492: }
0493:
0494: public static Map createGiftCard(DispatchContext ctx, Map context) {
0495: Map result = new HashMap();
0496: GenericDelegator delegator = ctx.getDelegator();
0497: Security security = ctx.getSecurity();
0498: GenericValue userLogin = (GenericValue) context
0499: .get("userLogin");
0500:
0501: Timestamp now = UtilDateTime.nowTimestamp();
0502:
0503: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0504: security, context, result, "PAY_INFO", "_CREATE");
0505:
0506: if (result.size() > 0)
0507: return result;
0508:
0509: List toBeStored = new LinkedList();
0510: GenericValue newPm = delegator.makeValue("PaymentMethod", null);
0511: toBeStored.add(newPm);
0512: GenericValue newGc = delegator.makeValue("GiftCard", null);
0513: toBeStored.add(newGc);
0514:
0515: Long newPmId = delegator.getNextSeqId("PaymentMethod");
0516: if (newPmId == null) {
0517: return ServiceUtil
0518: .returnError("ERROR: Could not create GiftCard (id generation failure)");
0519: }
0520: newPm.set("partyId", partyId);
0521: newPm.set("fromDate",
0522: (context.get("fromDate") != null ? context
0523: .get("fromDate") : now));
0524: newPm.set("thruDate", context.get("thruDate"));
0525:
0526: newGc.set("cardNumber", context.get("cardNumber"));
0527: newGc.set("pinNumber", context.get("pinNumber"));
0528: newGc.set("expireDate", context.get("expireDate"));
0529:
0530: newPm.set("paymentMethodId", newPmId.toString());
0531: newPm.set("paymentMethodTypeId", "GIFT_CARD");
0532: newGc.set("paymentMethodId", newPmId.toString());
0533:
0534: try {
0535: delegator.storeAll(toBeStored);
0536: } catch (GenericEntityException e) {
0537: Debug.logWarning(e.getMessage(), module);
0538: return ServiceUtil
0539: .returnError("ERROR: Could not create GiftCard (write failure): "
0540: + e.getMessage());
0541: }
0542:
0543: result.put("paymentMethodId", newGc
0544: .getString("paymentMethodId"));
0545: result.put(ModelService.RESPONSE_MESSAGE,
0546: ModelService.RESPOND_SUCCESS);
0547: return result;
0548: }
0549:
0550: public static Map updateGiftCard(DispatchContext ctx, Map context) {
0551: Map result = new HashMap();
0552: GenericDelegator delegator = ctx.getDelegator();
0553: Security security = ctx.getSecurity();
0554: GenericValue userLogin = (GenericValue) context
0555: .get("userLogin");
0556:
0557: Timestamp now = UtilDateTime.nowTimestamp();
0558:
0559: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0560: security, context, result, "PAY_INFO", "_UPDATE");
0561:
0562: if (result.size() > 0)
0563: return result;
0564:
0565: List toBeStored = new LinkedList();
0566: boolean isModified = false;
0567:
0568: GenericValue paymentMethod = null;
0569: GenericValue newPm = null;
0570: GenericValue giftCard = null;
0571: GenericValue newGc = null;
0572: String paymentMethodId = (String) context
0573: .get("paymentMethodId");
0574:
0575: try {
0576: giftCard = delegator.findByPrimaryKey("GiftCard", UtilMisc
0577: .toMap("paymentMethodId", paymentMethodId));
0578: paymentMethod = delegator.findByPrimaryKey("PaymentMethod",
0579: UtilMisc.toMap("paymentMethodId", paymentMethodId));
0580: } catch (GenericEntityException e) {
0581: Debug.logWarning(e.getMessage(), module);
0582: return ServiceUtil
0583: .returnError("ERROR: Could not get GiftCard to update (read error): "
0584: + e.getMessage());
0585: }
0586:
0587: if (giftCard == null || paymentMethod == null) {
0588: return ServiceUtil
0589: .returnError("ERROR: Could not find GiftCard to update with id "
0590: + paymentMethodId);
0591: }
0592:
0593: // card number (masked)
0594: String cardNumber = StringUtil.removeSpaces((String) context
0595: .get("cardNumber"));
0596: if (cardNumber.startsWith("*")) {
0597: // get the masked card number from the db
0598: String origCardNumber = giftCard.getString("cardNumber");
0599: //Debug.log(origCardNumber);
0600: String origMaskedNumber = "";
0601: int cardLength = origCardNumber.length() - 4;
0602: if (cardLength > 0) {
0603: for (int i = 0; i < cardLength; i++) {
0604: origMaskedNumber = origMaskedNumber + "*";
0605: }
0606: origMaskedNumber = origMaskedNumber
0607: + origCardNumber.substring(cardLength);
0608: } else {
0609: origMaskedNumber = origCardNumber;
0610: }
0611:
0612: // compare the two masked numbers
0613: if (cardNumber.equals(origMaskedNumber)) {
0614: cardNumber = origCardNumber;
0615: }
0616: }
0617: context.put("cardNumber", cardNumber);
0618:
0619: newPm = new GenericValue(paymentMethod);
0620: toBeStored.add(newPm);
0621: newGc = new GenericValue(giftCard);
0622: toBeStored.add(newGc);
0623:
0624: Long newPmId = delegator.getNextSeqId("PaymentMethod");
0625:
0626: if (newPmId == null) {
0627: return ServiceUtil
0628: .returnError("ERROR: Could not update GiftCard info (id generation failure)");
0629: }
0630:
0631: newPm.set("partyId", partyId);
0632: newPm.set("fromDate", context.get("fromDate"), false);
0633: newPm.set("thruDate", context.get("thruDate"));
0634:
0635: newGc.set("cardNumber", context.get("cardNumber"));
0636: newGc.set("pinNumber", context.get("pinNumber"));
0637: newGc.set("expireDate", context.get("expireDate"));
0638:
0639: if (!newGc.equals(giftCard) || !newPm.equals(paymentMethod)) {
0640: newPm.set("paymentMethodId", newPmId.toString());
0641: newGc.set("paymentMethodId", newPmId.toString());
0642:
0643: newPm.set("fromDate",
0644: (context.get("fromDate") != null ? context
0645: .get("fromDate") : now));
0646: isModified = true;
0647: }
0648:
0649: if (isModified) {
0650: // set thru date on old paymentMethod
0651: paymentMethod.set("thruDate", now);
0652: toBeStored.add(paymentMethod);
0653:
0654: try {
0655: delegator.storeAll(toBeStored);
0656: } catch (GenericEntityException e) {
0657: Debug.logWarning(e.getMessage(), module);
0658: return ServiceUtil
0659: .returnError("ERROR: Could not update EFT Account (write failure): "
0660: + e.getMessage());
0661: }
0662: } else {
0663: result.put("newPaymentMethodId", paymentMethodId);
0664: result.put(ModelService.RESPONSE_MESSAGE,
0665: ModelService.RESPOND_SUCCESS);
0666: result.put(ModelService.SUCCESS_MESSAGE,
0667: "No changes made, not updating EFT Account");
0668:
0669: return result;
0670: }
0671:
0672: result.put("newPaymentMethodId", newGc
0673: .getString("paymentMethodId"));
0674: result.put(ModelService.RESPONSE_MESSAGE,
0675: ModelService.RESPOND_SUCCESS);
0676: return result;
0677: }
0678:
0679: /**
0680: * Creates EftAccount and PaymentMethod entities according to the parameters passed in the context
0681: * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_CREATE permission
0682: * @param ctx The DispatchContext that this service is operating in
0683: * @param context Map containing the input parameters
0684: * @return Map with the result of the service, the output parameters
0685: */
0686: public static Map createEftAccount(DispatchContext ctx, Map context) {
0687: Map result = new HashMap();
0688: GenericDelegator delegator = ctx.getDelegator();
0689: Security security = ctx.getSecurity();
0690: GenericValue userLogin = (GenericValue) context
0691: .get("userLogin");
0692:
0693: Timestamp now = UtilDateTime.nowTimestamp();
0694:
0695: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0696: security, context, result, "PAY_INFO", "_CREATE");
0697:
0698: if (result.size() > 0)
0699: return result;
0700:
0701: List toBeStored = new LinkedList();
0702: GenericValue newPm = delegator.makeValue("PaymentMethod", null);
0703:
0704: toBeStored.add(newPm);
0705: GenericValue newEa = delegator.makeValue("EftAccount", null);
0706:
0707: toBeStored.add(newEa);
0708:
0709: Long newPmId = delegator.getNextSeqId("PaymentMethod");
0710:
0711: if (newPmId == null) {
0712: return ServiceUtil
0713: .returnError("ERROR: Could not create credit card (id generation failure)");
0714: }
0715: newPm.set("partyId", partyId);
0716: newPm.set("fromDate",
0717: (context.get("fromDate") != null ? context
0718: .get("fromDate") : now));
0719: newPm.set("thruDate", context.get("thruDate"));
0720: newEa.set("bankName", context.get("bankName"));
0721: newEa.set("routingNumber", context.get("routingNumber"));
0722: newEa.set("accountType", context.get("accountType"));
0723: newEa.set("accountNumber", context.get("accountNumber"));
0724: newEa.set("nameOnAccount", context.get("nameOnAccount"));
0725: newEa.set("companyNameOnAccount", context
0726: .get("companyNameOnAccount"));
0727: newEa.set("contactMechId", context.get("contactMechId"));
0728:
0729: newPm.set("paymentMethodId", newPmId.toString());
0730: newPm.set("paymentMethodTypeId", "EFT_ACCOUNT");
0731: newEa.set("paymentMethodId", newPmId.toString());
0732:
0733: GenericValue newPartyContactMechPurpose = null;
0734: String contactMechId = (String) context.get("contactMechId");
0735:
0736: if (contactMechId != null && contactMechId.length() > 0) {
0737: // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
0738: String contactMechPurposeTypeId = "BILLING_LOCATION";
0739:
0740: GenericValue tempVal = null;
0741:
0742: try {
0743: List allPCMPs = EntityUtil
0744: .filterByDate(
0745: delegator
0746: .findByAnd(
0747: "PartyContactMechPurpose",
0748: UtilMisc
0749: .toMap(
0750: "partyId",
0751: partyId,
0752: "contactMechId",
0753: contactMechId,
0754: "contactMechPurposeTypeId",
0755: contactMechPurposeTypeId),
0756: null), true);
0757:
0758: tempVal = EntityUtil.getFirst(allPCMPs);
0759: } catch (GenericEntityException e) {
0760: Debug.logWarning(e.getMessage(), module);
0761: tempVal = null;
0762: }
0763:
0764: if (tempVal == null) {
0765: // no value found, create a new one
0766: newPartyContactMechPurpose = delegator.makeValue(
0767: "PartyContactMechPurpose", UtilMisc.toMap(
0768: "partyId", partyId, "contactMechId",
0769: contactMechId,
0770: "contactMechPurposeTypeId",
0771: contactMechPurposeTypeId, "fromDate",
0772: now));
0773: }
0774: }
0775:
0776: if (newPartyContactMechPurpose != null)
0777: toBeStored.add(newPartyContactMechPurpose);
0778:
0779: try {
0780: delegator.storeAll(toBeStored);
0781: } catch (GenericEntityException e) {
0782: Debug.logWarning(e.getMessage(), module);
0783: return ServiceUtil
0784: .returnError("ERROR: Could not create credit card (write failure): "
0785: + e.getMessage());
0786: }
0787:
0788: result.put("paymentMethodId", newEa
0789: .getString("paymentMethodId"));
0790: result.put(ModelService.RESPONSE_MESSAGE,
0791: ModelService.RESPOND_SUCCESS);
0792: return result;
0793: }
0794:
0795: /**
0796: * Updates EftAccount and PaymentMethod entities according to the parameters passed in the context
0797: * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
0798: * @param ctx The DispatchContext that this service is operating in
0799: * @param context Map containing the input parameters
0800: * @return Map with the result of the service, the output parameters
0801: */
0802: public static Map updateEftAccount(DispatchContext ctx, Map context) {
0803: Map result = new HashMap();
0804: GenericDelegator delegator = ctx.getDelegator();
0805: Security security = ctx.getSecurity();
0806: GenericValue userLogin = (GenericValue) context
0807: .get("userLogin");
0808:
0809: Timestamp now = UtilDateTime.nowTimestamp();
0810:
0811: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0812: security, context, result, "PAY_INFO", "_UPDATE");
0813:
0814: if (result.size() > 0)
0815: return result;
0816:
0817: List toBeStored = new LinkedList();
0818: boolean isModified = false;
0819:
0820: GenericValue paymentMethod = null;
0821: GenericValue newPm = null;
0822: GenericValue eftAccount = null;
0823: GenericValue newEa = null;
0824: String paymentMethodId = (String) context
0825: .get("paymentMethodId");
0826:
0827: try {
0828: eftAccount = delegator.findByPrimaryKey("EftAccount",
0829: UtilMisc.toMap("paymentMethodId", paymentMethodId));
0830: paymentMethod = delegator.findByPrimaryKey("PaymentMethod",
0831: UtilMisc.toMap("paymentMethodId", paymentMethodId));
0832: } catch (GenericEntityException e) {
0833: Debug.logWarning(e.getMessage(), module);
0834: return ServiceUtil
0835: .returnError("ERROR: Could not get EFT Account to update (read error): "
0836: + e.getMessage());
0837: }
0838:
0839: if (eftAccount == null || paymentMethod == null) {
0840: return ServiceUtil
0841: .returnError("ERROR: Could not find EFT Account to update with id "
0842: + paymentMethodId);
0843: }
0844:
0845: newPm = new GenericValue(paymentMethod);
0846: toBeStored.add(newPm);
0847: newEa = new GenericValue(eftAccount);
0848: toBeStored.add(newEa);
0849:
0850: Long newPmId = delegator.getNextSeqId("PaymentMethod");
0851:
0852: if (newPmId == null) {
0853: return ServiceUtil
0854: .returnError("ERROR: Could not update EFT Account info (id generation failure)");
0855: }
0856:
0857: newPm.set("partyId", partyId);
0858: newPm.set("fromDate", context.get("fromDate"), false);
0859: newPm.set("thruDate", context.get("thruDate"));
0860: newEa.set("bankName", context.get("bankName"));
0861: newEa.set("routingNumber", context.get("routingNumber"));
0862: newEa.set("accountType", context.get("accountType"));
0863: newEa.set("accountNumber", context.get("accountNumber"));
0864: newEa.set("nameOnAccount", context.get("nameOnAccount"));
0865: newEa.set("companyNameOnAccount", context
0866: .get("companyNameOnAccount"));
0867: newEa.set("contactMechId", context.get("contactMechId"));
0868:
0869: if (!newEa.equals(eftAccount) || !newPm.equals(paymentMethod)) {
0870: newPm.set("paymentMethodId", newPmId.toString());
0871: newEa.set("paymentMethodId", newPmId.toString());
0872:
0873: newPm.set("fromDate",
0874: (context.get("fromDate") != null ? context
0875: .get("fromDate") : now));
0876: isModified = true;
0877: }
0878:
0879: GenericValue newPartyContactMechPurpose = null;
0880: String contactMechId = (String) context.get("contactMechId");
0881:
0882: if (contactMechId != null && contactMechId.length() > 0) {
0883: // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
0884: String contactMechPurposeTypeId = "BILLING_LOCATION";
0885:
0886: GenericValue tempVal = null;
0887:
0888: try {
0889: List allPCMPs = EntityUtil
0890: .filterByDate(
0891: delegator
0892: .findByAnd(
0893: "PartyContactMechPurpose",
0894: UtilMisc
0895: .toMap(
0896: "partyId",
0897: partyId,
0898: "contactMechId",
0899: contactMechId,
0900: "contactMechPurposeTypeId",
0901: contactMechPurposeTypeId),
0902: null), true);
0903:
0904: tempVal = EntityUtil.getFirst(allPCMPs);
0905: } catch (GenericEntityException e) {
0906: Debug.logWarning(e.getMessage(), module);
0907: tempVal = null;
0908: }
0909:
0910: if (tempVal == null) {
0911: // no value found, create a new one
0912: newPartyContactMechPurpose = delegator.makeValue(
0913: "PartyContactMechPurpose", UtilMisc.toMap(
0914: "partyId", partyId, "contactMechId",
0915: contactMechId,
0916: "contactMechPurposeTypeId",
0917: contactMechPurposeTypeId, "fromDate",
0918: now));
0919: }
0920: }
0921:
0922: if (isModified) {
0923: // Debug.logInfo("yes, is modified", module);
0924: if (newPartyContactMechPurpose != null)
0925: toBeStored.add(newPartyContactMechPurpose);
0926:
0927: // set thru date on old paymentMethod
0928: paymentMethod.set("thruDate", now);
0929: toBeStored.add(paymentMethod);
0930:
0931: try {
0932: delegator.storeAll(toBeStored);
0933: } catch (GenericEntityException e) {
0934: Debug.logWarning(e.getMessage(), module);
0935: return ServiceUtil
0936: .returnError("ERROR: Could not update EFT Account (write failure): "
0937: + e.getMessage());
0938: }
0939: } else {
0940: result.put("newPaymentMethodId", paymentMethodId);
0941: result.put(ModelService.RESPONSE_MESSAGE,
0942: ModelService.RESPOND_SUCCESS);
0943: result.put(ModelService.SUCCESS_MESSAGE,
0944: "No changes made, not updating EFT Account");
0945:
0946: return result;
0947: }
0948:
0949: result.put("newPaymentMethodId", newEa
0950: .getString("paymentMethodId"));
0951:
0952: result.put(ModelService.RESPONSE_MESSAGE,
0953: ModelService.RESPOND_SUCCESS);
0954: return result;
0955: }
0956:
0957: /**
0958: * Creates a Payment entity according to the parameters passed in the context
0959: * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
0960: * @param ctx The DispatchContext that this service is operating in
0961: * @param context Map containing the input parameters
0962: * @return Map with the result of the service, the output parameters
0963: */
0964: public static Map createPayment(DispatchContext ctx, Map context) {
0965: Map result = new HashMap();
0966: GenericDelegator delegator = ctx.getDelegator();
0967: Security security = ctx.getSecurity();
0968: GenericValue userLogin = (GenericValue) context
0969: .get("userLogin");
0970:
0971: Timestamp now = UtilDateTime.nowTimestamp();
0972:
0973: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0974: security, context, result, "PAY_INFO", "_CREATE");
0975:
0976: if (result.size() > 0) {
0977: if (partyId != context.get("partyIdFrom")
0978: && partyId != context.get("partyIdTo")) {
0979: return ServiceUtil
0980: .returnError("ERROR: To Create a Payment you must either be the to or from party or have the PAY_INFO_CREATE or PAY_INFO_ADMIN permissions.");
0981: }
0982: }
0983:
0984: Long newPmId = delegator.getNextSeqId("Payment");
0985:
0986: if (newPmId == null) {
0987: return ServiceUtil
0988: .returnError("ERROR: Could not Create Payment (id generation failure)");
0989: }
0990:
0991: GenericValue payment = delegator.makeValue("Payment", null);
0992:
0993: payment.set("paymentId", newPmId.toString());
0994: payment.set("paymentTypeId", context.get("paymentTypeId"));
0995: payment.set("paymentMethodTypeId", context
0996: .get("paymentMethodTypeId"));
0997: payment.set("paymentMethodId", context.get("paymentMethodId"));
0998: payment.set("paymentPreferenceId", context
0999: .get("paymentPreferenceId"));
1000: payment.set("partyIdFrom", context.get("partyIdFrom"));
1001: payment.set("partyIdTo", context.get("partyIdTo"));
1002: payment.set("statusId", context.get("statusId"));
1003: payment.set("effectiveDate",
1004: context.get("effectiveDate") != null ? context
1005: .get("effectiveDate") : now);
1006: payment.set("paymentRefNum", context.get("paymentRefNum"));
1007: payment.set("amount", context.get("amount"));
1008: payment.set("comments", context.get("comments"));
1009:
1010: try {
1011: payment.create();
1012: } catch (GenericEntityException e) {
1013: Debug.logWarning(e.getMessage(), module);
1014: return ServiceUtil
1015: .returnError("ERROR: Could not Create Payment (write failure): "
1016: + e.getMessage());
1017: }
1018:
1019: result.put("paymentId", payment.getString("paymentId"));
1020: result.put(ModelService.RESPONSE_MESSAGE,
1021: ModelService.RESPOND_SUCCESS);
1022: return result;
1023: }
1024: }
|