KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > payment > PaymentMethodServices


1 /*
2  * $Id: PaymentMethodServices.java 6726 2006-02-14 03:24:33Z jonesde $
3  *
4  * Copyright (c) 2001-2006 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.accounting.payment;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
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 /**
47  * Services for Payment maintenance
48  *
49  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
50  * @version $Rev: 6726 $
51  * @since 2.0
52  */

53 public class PaymentMethodServices {
54     
55     public final static String JavaDoc module = PaymentMethodServices.class.getName();
56
57     /**
58      * Deletes a PaymentMethod entity according to the parameters passed in the context
59      * <b>security check</b>: userLogin partyId must equal paymentMethod partyId, or must have PAY_INFO_DELETE permission
60      * @param ctx The DispatchContext that this service is operating in
61      * @param context Map containing the input parameters
62      * @return Map with the result of the service, the output parameters
63      */

64     public static Map JavaDoc deletePaymentMethod(DispatchContext ctx, Map JavaDoc context) {
65         Map JavaDoc result = new HashMap JavaDoc();
66         GenericDelegator delegator = ctx.getDelegator();
67         Security security = ctx.getSecurity();
68         GenericValue userLogin = (GenericValue) context.get("userLogin");
69
70         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
71
72         // never delete a PaymentMethod, just put a to date on the link to the party
73
String JavaDoc paymentMethodId = (String JavaDoc) 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         // <b>security check</b>: userLogin partyId must equal paymentMethod partyId, or must have PAY_INFO_DELETE permission
88
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 JavaDoc makeExpireDate(DispatchContext ctx, Map JavaDoc context) {
107         Map JavaDoc result = new HashMap JavaDoc();
108         String JavaDoc expMonth = (String JavaDoc) context.get("expMonth");
109         String JavaDoc expYear = (String JavaDoc) context.get("expYear");
110         
111         StringBuffer JavaDoc expDate = new StringBuffer JavaDoc();
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     /**
121      * Creates CreditCard and PaymentMethod entities according to the parameters passed in the context
122      * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_CREATE permission
123      * @param ctx The DispatchContext that this service is operating in
124      * @param context Map containing the input parameters
125      * @return Map with the result of the service, the output parameters
126      */

127     public static Map JavaDoc createCreditCard(DispatchContext ctx, Map JavaDoc context) {
128         Map JavaDoc result = new HashMap JavaDoc();
129         GenericDelegator delegator = ctx.getDelegator();
130         Security security = ctx.getSecurity();
131         GenericValue userLogin = (GenericValue) context.get("userLogin");
132
133         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
134
135         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE");
136
137         if (result.size() > 0) return result;
138
139         // do some more complicated/critical validation...
140
List JavaDoc messages = new LinkedList JavaDoc();
141
142         // first remove all spaces from the credit card number
143
context.put("cardNumber", StringUtil.removeSpaces((String JavaDoc) context.get("cardNumber")));
144         if (!UtilValidate.isCardMatch((String JavaDoc) context.get("cardType"), (String JavaDoc) context.get("cardNumber")))
145             messages.add(
146                 (String JavaDoc) context.get("cardNumber")
147                     + UtilValidate.isCreditCardPrefixMsg
148                     + (String JavaDoc) context.get("cardType")
149                     + UtilValidate.isCreditCardSuffixMsg
150                     + " (It appears to be a "
151                     + UtilValidate.getCardType((String JavaDoc) context.get("cardNumber"))
152                     + " credit card number)");
153         if (!UtilValidate.isDateAfterToday((String JavaDoc) context.get("expireDate")))
154             messages.add("The expiration date " + (String JavaDoc) context.get("expireDate") + " is before today.");
155         if (messages.size() > 0) {
156             return ServiceUtil.returnError(messages);
157         }
158
159         List JavaDoc toBeStored = new LinkedList JavaDoc();
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 JavaDoc newPmId = null;
168         try {
169             newPmId = delegator.getNextSeqId("PaymentMethod");
170         } catch (IllegalArgumentException JavaDoc 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 JavaDoc contactMechId = (String JavaDoc) context.get("contactMechId");
195
196         if (contactMechId != null && contactMechId.length() > 0 && !contactMechId.equals("_NEW_")) {
197             // set the contactMechId on the credit card
198
newCc.set("contactMechId", context.get("contactMechId"));
199             // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
200
String JavaDoc contactMechPurposeTypeId = "BILLING_LOCATION";
201
202             GenericValue tempVal = null;
203
204             try {
205                 List JavaDoc 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                 // no value found, create a new one
216
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     /**
236      * Updates CreditCard and PaymentMethod entities according to the parameters passed in the context
237      * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
238      * @param ctx The DispatchContext that this service is operating in
239      * @param context Map containing the input parameters
240      * @return Map with the result of the service, the output parameters
241      */

242     public static Map JavaDoc updateCreditCard(DispatchContext ctx, Map JavaDoc context) {
243         Map JavaDoc result = new HashMap JavaDoc();
244         GenericDelegator delegator = ctx.getDelegator();
245         Security security = ctx.getSecurity();
246         GenericValue userLogin = (GenericValue) context.get("userLogin");
247
248         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
249
250         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE");
251
252         if (result.size() > 0) return result;
253
254         List JavaDoc toBeStored = new LinkedList JavaDoc();
255         boolean isModified = false;
256
257         GenericValue paymentMethod = null;
258         GenericValue newPm = null;
259         GenericValue creditCard = null;
260         GenericValue newCc = null;
261         String JavaDoc paymentMethodId = (String JavaDoc) 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         // do some more complicated/critical validation...
280
List JavaDoc messages = new LinkedList JavaDoc();
281         
282         // first remove all spaces from the credit card number
283
String JavaDoc updatedCardNumber = StringUtil.removeSpaces((String JavaDoc) context.get("cardNumber"));
284         if (updatedCardNumber.startsWith("*")) {
285             // get the masked card number from the db
286
String JavaDoc origCardNumber = creditCard.getString("cardNumber");
287             Debug.log(origCardNumber);
288             String JavaDoc 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             // compare the two masked numbers
297
if (updatedCardNumber.equals(origMaskedNumber)) {
298                 updatedCardNumber = origCardNumber;
299             }
300         }
301         context.put("cardNumber", updatedCardNumber);
302         
303         if (!UtilValidate.isCardMatch((String JavaDoc) context.get("cardType"), (String JavaDoc) context.get("cardNumber")))
304             messages.add((String JavaDoc) context.get("cardNumber")
305                     + UtilValidate.isCreditCardPrefixMsg + (String JavaDoc) context.get("cardType") + UtilValidate.isCreditCardSuffixMsg
306                     + " (It appears to be a " + UtilValidate.getCardType((String JavaDoc) context.get("cardNumber")) + " credit card number)");
307         if (!UtilValidate.isDateAfterToday((String JavaDoc) context.get("expireDate")))
308             messages.add("The expiration date " + (String JavaDoc) 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 JavaDoc newPmId = null;
319         try {
320             newPmId = delegator.getNextSeqId("PaymentMethod");
321         } catch (IllegalArgumentException JavaDoc 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         // The following check is needed to avoid to reactivate an expired pm
330
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 JavaDoc contactMechId = (String JavaDoc) context.get("contactMechId");
346
347         if (contactMechId != null && contactMechId.length() > 0 && !contactMechId.equals("_NEW_")) {
348             // set the contactMechId on the credit card
349
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             // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
363
String JavaDoc contactMechPurposeTypeId = "BILLING_LOCATION";
364
365             GenericValue tempVal = null;
366
367             try {
368                 List JavaDoc 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                 // no value found, create a new one
379
newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose",
380                         UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
381             }
382         }
383
384         if (isModified) {
385             // Debug.logInfo("yes, is modified", module);
386
if (newPartyContactMechPurpose != null) toBeStored.add(newPartyContactMechPurpose);
387
388             // set thru date on old paymentMethod
389
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 JavaDoc createGiftCard(DispatchContext ctx, Map JavaDoc context) {
415         Map JavaDoc result = new HashMap JavaDoc();
416         GenericDelegator delegator = ctx.getDelegator();
417         Security security = ctx.getSecurity();
418         GenericValue userLogin = (GenericValue) context.get("userLogin");
419
420         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
421
422         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE");
423
424         if (result.size() > 0)
425             return result;
426
427         List JavaDoc toBeStored = new LinkedList JavaDoc();
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 JavaDoc newPmId = null;
434         try {
435             newPmId = delegator.getNextSeqId("PaymentMethod");
436         } catch (IllegalArgumentException JavaDoc 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 JavaDoc updateGiftCard(DispatchContext ctx, Map JavaDoc context) {
466         Map JavaDoc result = new HashMap JavaDoc();
467         GenericDelegator delegator = ctx.getDelegator();
468         Security security = ctx.getSecurity();
469         GenericValue userLogin = (GenericValue) context.get("userLogin");
470
471         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
472
473         String JavaDoc partyId =
474             ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE");
475
476         if (result.size() > 0)
477             return result;
478
479         List JavaDoc toBeStored = new LinkedList JavaDoc();
480         boolean isModified = false;
481
482         GenericValue paymentMethod = null;
483         GenericValue newPm = null;
484         GenericValue giftCard = null;
485         GenericValue newGc = null;
486         String JavaDoc paymentMethodId = (String JavaDoc) 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         // card number (masked)
505
String JavaDoc cardNumber = StringUtil.removeSpaces((String JavaDoc) context.get("cardNumber"));
506         if (cardNumber.startsWith("*")) {
507             // get the masked card number from the db
508
String JavaDoc origCardNumber = giftCard.getString("cardNumber");
509             //Debug.log(origCardNumber);
510
String JavaDoc 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             // compare the two masked numbers
522
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 JavaDoc newPmId = null;
534         try {
535             newPmId = delegator.getNextSeqId("PaymentMethod");
536         } catch (IllegalArgumentException JavaDoc 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             // set thru date on old paymentMethod
559
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     /**
583      * Creates EftAccount and PaymentMethod entities according to the parameters passed in the context
584      * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_CREATE permission
585      * @param ctx The DispatchContext that this service is operating in
586      * @param context Map containing the input parameters
587      * @return Map with the result of the service, the output parameters
588      */

589     public static Map JavaDoc createEftAccount(DispatchContext ctx, Map JavaDoc context) {
590         Map JavaDoc result = new HashMap JavaDoc();
591         GenericDelegator delegator = ctx.getDelegator();
592         Security security = ctx.getSecurity();
593         GenericValue userLogin = (GenericValue) context.get("userLogin");
594
595         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
596
597         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_CREATE");
598
599         if (result.size() > 0) return result;
600
601         List JavaDoc toBeStored = new LinkedList JavaDoc();
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 JavaDoc newPmId = null;
610         try {
611             newPmId = delegator.getNextSeqId("PaymentMethod");
612         } catch (IllegalArgumentException JavaDoc 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 JavaDoc contactMechId = (String JavaDoc) context.get("contactMechId");
634
635         if (contactMechId != null && contactMechId.length() > 0) {
636             // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
637
String JavaDoc contactMechPurposeTypeId = "BILLING_LOCATION";
638
639             GenericValue tempVal = null;
640             try {
641                 List JavaDoc 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                 // no value found, create a new one
652
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     /**
673      * Updates EftAccount and PaymentMethod entities according to the parameters passed in the context
674      * <b>security check</b>: userLogin partyId must equal partyId, or must have PAY_INFO_UPDATE permission
675      * @param ctx The DispatchContext that this service is operating in
676      * @param context Map containing the input parameters
677      * @return Map with the result of the service, the output parameters
678      */

679     public static Map JavaDoc updateEftAccount(DispatchContext ctx, Map JavaDoc context) {
680         Map JavaDoc result = new HashMap JavaDoc();
681         GenericDelegator delegator = ctx.getDelegator();
682         Security security = ctx.getSecurity();
683         GenericValue userLogin = (GenericValue) context.get("userLogin");
684
685         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
686
687         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PAY_INFO", "_UPDATE");
688
689         if (result.size() > 0) return result;
690
691         List JavaDoc toBeStored = new LinkedList JavaDoc();
692         boolean isModified = false;
693
694         GenericValue paymentMethod = null;
695         GenericValue newPm = null;
696         GenericValue eftAccount = null;
697         GenericValue newEa = null;
698         String JavaDoc paymentMethodId = (String JavaDoc) 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 JavaDoc newPmId = null;
723         try {
724             newPmId = delegator.getNextSeqId("PaymentMethod");
725         } catch (IllegalArgumentException JavaDoc 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 JavaDoc contactMechId = (String JavaDoc) context.get("contactMechId");
750
751         if (contactMechId != null && contactMechId.length() > 0) {
752             // add a PartyContactMechPurpose of BILLING_LOCATION if necessary
753
String JavaDoc contactMechPurposeTypeId = "BILLING_LOCATION";
754
755             GenericValue tempVal = null;
756
757             try {
758                 List JavaDoc 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                 // no value found, create a new one
768
newPartyContactMechPurpose = delegator.makeValue("PartyContactMechPurpose",
769                         UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId, "contactMechPurposeTypeId", contactMechPurposeTypeId, "fromDate", now));
770             }
771         }
772
773         if (isModified) {
774             // Debug.logInfo("yes, is modified", module);
775
if (newPartyContactMechPurpose != null)
776                 toBeStored.add(newPartyContactMechPurpose);
777
778             // set thru date on old paymentMethod
779
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