KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > event > PaymentEvents


1 /*
2  * $Id: PaymentEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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  */

25 package org.ofbiz.pos.event;
26
27 import org.ofbiz.base.util.Debug;
28 import org.ofbiz.base.util.GeneralException;
29 import org.ofbiz.base.util.UtilValidate;
30 import org.ofbiz.pos.PosTransaction;
31 import org.ofbiz.pos.component.Input;
32 import org.ofbiz.pos.component.Journal;
33 import org.ofbiz.pos.screen.PosScreen;
34 import org.ofbiz.base.util.UtilProperties;
35
36 import java.util.Locale JavaDoc;
37
38
39 /**
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 3.1
44  */

45 public class PaymentEvents {
46
47     public static final String JavaDoc module = PaymentEvents.class.getName();
48
49     public static synchronized void payCash(PosScreen pos) {
50         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
51         trans.clearPayment("CASH");
52
53         // all cash transactions are NO_PAYMENT; no need to check
54
try {
55             double amount = processAmount(trans, pos, null);
56             Debug.log("Processing [Cash] Amount : " + amount, module);
57
58             // add the payment
59
trans.addPayment("CASH", amount);
60         } catch (GeneralException e) {
61             // errors handled
62
}
63
64         pos.refresh();
65     }
66
67     public static synchronized void payCheck(PosScreen pos) {
68         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
69         Input input = pos.getInput();
70         String JavaDoc[] ckInfo = input.getFunction("CHECK");
71
72         // check for no/external payment processing
73
int paymentCheck = trans.checkPaymentMethodType("PERSONAL_CHECK");
74         if (paymentCheck == PosTransaction.NO_PAYMENT) {
75             processNoPayment(pos, "PERSONAL_CHECK");
76             return;
77         } else if (paymentCheck == PosTransaction.EXTERNAL_PAYMENT) {
78             if (ckInfo == null) {
79                 input.setFunction("CHECK");
80                 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault()));
81                 return;
82             } else {
83                 processExternalPayment(pos, "PERSONAL_CHECK", ckInfo[1]);
84                 return;
85             }
86         }
87
88         // now for internal payment processing
89
pos.showDialog("dialog/error/notyetsupported");
90     }
91
92     public static synchronized void payGiftCard(PosScreen pos) {
93         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
94         Input input = pos.getInput();
95         String JavaDoc[] gcInfo = input.getFunction("GIFTCARD");
96
97         // check for no/external payment processing
98
int paymentCheck = trans.checkPaymentMethodType("GIFT_CARD");
99         if (paymentCheck == PosTransaction.NO_PAYMENT) {
100             processNoPayment(pos, "GIFT_CARD");
101             return;
102         } else if (paymentCheck == PosTransaction.EXTERNAL_PAYMENT) {
103             if (gcInfo == null) {
104                 input.setFunction("GIFTCARD");
105                 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault()));
106                 return;
107             } else {
108                 processExternalPayment(pos, "GIFT_CARD", gcInfo[1]);
109                 return;
110             }
111         }
112
113         // now for internal payment processing
114
pos.showDialog("dialog/error/notyetsupported");
115     }
116
117     public static synchronized void payCredit(PosScreen pos) {
118         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
119         Input input = pos.getInput();
120         String JavaDoc[] msrInfo = input.getFunction("MSRINFO");
121         String JavaDoc[] crtInfo = input.getFunction("CREDIT");
122
123         // check for no/external payment processing
124
int paymentCheck = trans.checkPaymentMethodType("CREDIT_CARD");
125         if (paymentCheck == PosTransaction.NO_PAYMENT) {
126             processNoPayment(pos, "CREDIT_CARD");
127             return;
128         } else if (paymentCheck == PosTransaction.EXTERNAL_PAYMENT) {
129             if (crtInfo == null) {
130                 input.setFunction("CREDIT");
131                 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault()));
132                 return;
133             } else {
134                 processExternalPayment(pos, "CREDIT_CARD", crtInfo[1]);
135                 return;
136             }
137         }
138
139         // now for internal payment processing
140
if (crtInfo == null) {
141             input.setFunction("CREDIT");
142             pos.getOutput().print(UtilProperties.getMessage("pos","CREDNO",Locale.getDefault()));
143         } else {
144             Debug.log("Credit Func Info : " + crtInfo[1], module);
145             if (msrInfo == null) {
146                 if (UtilValidate.isCreditCard(input.value())) {
147                     input.setFunction("MSRINFO");
148                     pos.getOutput().print(UtilProperties.getMessage("pos","CREDEX",Locale.getDefault()));
149                 } else {
150                     Debug.log("Invalid card number - " + input.value(), module);
151                     input.clearFunction("MSRINFO");
152                     input.clearInput();
153                     pos.showDialog("dialog/error/invalidcardnumber");
154                 }
155             } else {
156                 String JavaDoc msrInfoStr = msrInfo[1];
157                 if (UtilValidate.isNotEmpty(input.value())) {
158                     if (UtilValidate.isNotEmpty(msrInfoStr)) {
159                         msrInfoStr = msrInfoStr + "|" + input.value();
160                     } else {
161                         msrInfoStr = input.value();
162                     }
163                 }
164                 input.setFunction("MSRINFO", msrInfoStr);
165                 String JavaDoc[] msrInfoArr = msrInfoStr.split("\\|");
166                 int allInfo = msrInfoArr.length;
167                 String JavaDoc firstName = null;
168                 String JavaDoc lastName = null;
169                 switch (allInfo) {
170                     case 4:
171                         lastName = msrInfoArr[3];
172                     case 3:
173                         firstName = msrInfoArr[2];
174                     case 2: // card number & exp date found
175
double amount = 0;
176                         try {
177                             amount = processAmount(trans, pos, crtInfo[1]);
178                             Debug.log("Processing Credit Card Amount : " + amount, module);
179                         } catch (GeneralException e) {
180                         }
181
182                         String JavaDoc cardNumber = msrInfoArr[0];
183                         String JavaDoc expDate = msrInfoArr[1];
184                         String JavaDoc pmId = trans.makeCreditCardVo(cardNumber, expDate, firstName, lastName);
185                         if (pmId != null) {
186                             trans.addPayment(pmId, amount);
187                         }
188                         pos.refresh();
189                         break;
190                     case 1: // card number only found
191
pos.getOutput().print(UtilProperties.getMessage("pos","CREDEX",Locale.getDefault()));
192                         break;
193                     default:
194                         Debug.log("Hit the default switch case [" + allInfo + "] refreshing.", module);
195                         input.clearFunction("MSRINFO");
196                         pos.getOutput().print(UtilProperties.getMessage("pos","CREDNO",Locale.getDefault()));
197                         break;
198                 }
199             }
200         }
201     }
202
203     private static void processNoPayment(PosScreen pos, String JavaDoc paymentMethodTypeId) {
204         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
205
206         try {
207             double amount = processAmount(trans, pos, null);
208             Debug.log("Processing [" + paymentMethodTypeId + "] Amount : " + amount, module);
209
210             // add the payment
211
trans.addPayment(paymentMethodTypeId, amount, "N/A", null);
212         } catch (GeneralException e) {
213             // errors handled
214
}
215
216         pos.refresh();
217     }
218
219     private static void processExternalPayment(PosScreen pos, String JavaDoc paymentMethodTypeId, String JavaDoc amountStr) {
220         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
221         Input input = pos.getInput();
222         String JavaDoc refNum = input.value();
223         if (refNum == null) {
224             pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault()));
225             return;
226         }
227         input.clearInput();
228
229         try {
230             double amount = processAmount(trans, pos, amountStr);
231             Debug.log("Processing [" + paymentMethodTypeId + "] Amount : " + amount, module);
232
233             // add the payment
234
trans.addPayment(paymentMethodTypeId, amount, refNum, null);
235         } catch (GeneralException e) {
236             // errors handled
237
}
238
239         pos.refresh();
240     }
241
242     public static void clearPayment(PosScreen pos) {
243         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
244         Journal journal = pos.getJournal();
245         String JavaDoc sku = journal.getSelectedSku();
246         String JavaDoc idx = journal.getSelectedIdx();
247         if (UtilValidate.isNotEmpty(idx) && UtilValidate.isEmpty(sku)) {
248             int index = -1;
249             try {
250                 index = Integer.parseInt(idx);
251             } catch (Exception JavaDoc e) {
252             }
253             if (index > -1) {
254                 trans.clearPayment(index);
255             }
256         }
257         pos.getInput().clearFunction("GIFTCARD");
258         pos.getInput().clearFunction("CREDIT");
259         pos.getInput().clearFunction("CHECK");
260         pos.refresh();
261     }
262
263     public static void clearAllPayments(PosScreen pos) {
264         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
265         trans.clearPayments();
266         pos.getInput().clearFunction("GIFTCARD");
267         pos.getInput().clearFunction("CREDIT");
268         pos.getInput().clearFunction("CHECK");
269         pos.refresh();
270     }
271
272     public static void setRefNum(PosScreen pos) {
273         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
274         Journal journal = pos.getJournal();
275         String JavaDoc sku = journal.getSelectedSku();
276         String JavaDoc idx = journal.getSelectedIdx();
277
278         if (UtilValidate.isNotEmpty(idx) && UtilValidate.isEmpty(sku)) {
279             String JavaDoc refNum = pos.getInput().value();
280             if (UtilValidate.isEmpty(refNum)) {
281                 pos.getOutput().print(UtilProperties.getMessage("pos","REFNUM",Locale.getDefault()));
282                 pos.getInput().setFunction("REFNUM");
283             } else {
284                 int index = -1;
285                 try {
286                     index = Integer.parseInt(idx);
287                 } catch (Exception JavaDoc e) {
288                 }
289                 if (index > -1) {
290                     trans.setPaymentRefNum(index, refNum, refNum);
291                     pos.refresh();
292                 }
293             }
294         } else {
295             pos.refresh();
296         }
297     }
298
299     public static synchronized void processSale(PosScreen pos) {
300         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
301         PosScreen.currentScreen.getOutput().print(UtilProperties.getMessage("pos","Processing",Locale.getDefault()));
302
303         if (trans.isEmpty()) {
304             PosScreen newPos = pos.showPage("pospanel");
305             newPos.showDialog("dialog/error/noitems");
306         } else if (trans.getTotalDue() > 0) {
307             pos.showDialog("dialog/error/notenoughfunds");
308         } else {
309             // manual locks (not secured; will be unlocked on clear)
310
pos.getInput().setLock(true);
311             pos.getButtons().setLock(true);
312             pos.refresh(false);
313
314             // process the sale
315
try {
316                 trans.processSale(pos.getOutput());
317                 pos.getInput().setFunction("PAID");
318             } catch (GeneralException e) {
319                 Debug.logError(e, e.getMessage(), module);
320                 pos.getInput().setLock(false);
321                 pos.getButtons().setLock(false);
322                 pos.showDialog("dialog/error/exception", e.getMessage());
323             }
324         }
325     }
326
327     private static double processAmount(PosTransaction trans, PosScreen pos, String JavaDoc amountStr) throws GeneralException {
328         Input input = pos.getInput();
329
330         if (input.isFunctionSet("TOTAL")) {
331             String JavaDoc amtStr = amountStr != null ? amountStr : input.value();
332             double amount;
333             if (UtilValidate.isNotEmpty(amtStr)) {
334                 try {
335                     amount = Double.parseDouble(amtStr);
336                 } catch (NumberFormatException JavaDoc e) {
337                     Debug.logError("Invalid number for amount : " + amtStr, module);
338                     pos.getOutput().print("Invalid Amount!");
339                     input.clearInput();
340                     throw new GeneralException();
341                 }
342                 amount = amount / 100; // convert to decimal
343
Debug.log("Set amount / 100 : " + amount, module);
344             } else {
345                 Debug.log("Amount is empty; assumption is full amount : " + trans.getTotalDue(), module);
346                 amount = trans.getTotalDue();
347                 if (amount <= 0) {
348                     throw new GeneralException();
349                 }
350             }
351             return amount;
352         } else {
353             Debug.log("TOTAL function NOT set", module);
354             throw new GeneralException();
355         }
356     }
357 }
Popular Tags