📄 giftcertificateservices.java
字号:
/* * $Id: GiftCertificateServices.java 7318 2006-04-17 22:15:43Z sichen $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.accounting.payment;import java.math.BigDecimal;import java.sql.Timestamp;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Random;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.order.finaccount.FinAccountHelper;import org.ofbiz.order.order.OrderReadHelper;import org.ofbiz.product.store.ProductStoreWorker;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;/** * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:sichen@opensourcestrategies.com">Si Chen</a> * @version $Rev: 7318 $ * @since 3.3 */public class GiftCertificateServices { public static final String module = GiftCertificateServices.class.getName(); // These are default settings, in case ProductStoreFinActSetting does not have them public static final int CARD_NUMBER_LENGTH = 14; public static final int PIN_NUMBER_LENGTH = 6; public static BigDecimal ZERO = new BigDecimal("0.00"); // Base Gift Certificate Services public static Map createGiftCertificate(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String productStoreId = (String) context.get("productStoreId"); Double initialAmount = (Double) context.get("initialAmount"); String partyId = (String) context.get("partyId"); if (UtilValidate.isEmpty(partyId)) { partyId = "_NA_"; } String currencyUom = (String) context.get("currency"); if (UtilValidate.isEmpty(currencyUom)) { currencyUom = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); } String cardNumber = null; String pinNumber = null; String refNum = null; String finAccountId = null; try { final String accountName = "Gift Certificate Account"; final String deposit = "DEPOSIT"; GenericValue giftCertSettings = delegator.findByPrimaryKeyCache("ProductStoreFinActSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId)); Map acctResult = null; if ("Y".equals(giftCertSettings.getString("requirePinCode"))) { // TODO: move this code to createFinAccountForStore as well int cardNumberLength = CARD_NUMBER_LENGTH; int pinNumberLength = PIN_NUMBER_LENGTH; if (giftCertSettings.getLong("accountCodeLength") != null) { cardNumberLength = giftCertSettings.getLong("accountCodeLength").intValue(); } if (giftCertSettings.getLong("pinCodeLength") != null) { pinNumberLength = giftCertSettings.getLong("pinCodeLength").intValue(); } cardNumber = generateNumber(delegator, cardNumberLength, true); pinNumber = generateNumber(delegator, pinNumberLength, false); // in this case, the card number is the finAccountId finAccountId = cardNumber; // create the FinAccount Map acctCtx = UtilMisc.toMap("finAccountId", finAccountId); acctCtx.put("finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId); acctCtx.put("finAccountName", accountName); acctCtx.put("finAccountCode", pinNumber); acctCtx.put("userLogin", userLogin); acctResult = dispatcher.runSync("createFinAccount", acctCtx); } else { acctResult = dispatcher.runSync("createFinAccountForStore", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId, "userLogin", userLogin)); if (acctResult.get("finAccountId") != null) { cardNumber = (String) acctResult.get("finAccountId"); } if (acctResult.get("finAccountCode") != null) { cardNumber = (String) acctResult.get("finAccountCode"); } } if (ServiceUtil.isError(acctResult)) { String error = ServiceUtil.getErrorMessage(acctResult); return ServiceUtil.returnError(error); } // create the initial (deposit) transaction refNum = GiftCertificateServices.createTransaction(delegator, dispatcher, userLogin, initialAmount, productStoreId, partyId, currencyUom, deposit, finAccountId); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to create gift certificate number."); } catch (GenericServiceException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to create gift certificate."); } catch (GeneralException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } Map result = ServiceUtil.returnSuccess(); result.put("cardNumber", cardNumber); result.put("pinNumber", pinNumber); result.put("initialAmount", initialAmount); result.put("processResult", Boolean.TRUE); result.put("responseCode", "1"); result.put("referenceNum", refNum); Debug.log("Create GC Result - " + result, module); return result; } public static Map addFundsToGiftCertificate(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); final String deposit = "DEPOSIT"; GenericValue userLogin = (GenericValue) context.get("userLogin"); String productStoreId = (String) context.get("productStoreId"); String cardNumber = (String) context.get("cardNumber"); String pinNumber = (String) context.get("pinNumber"); Double amount = (Double) context.get("amount"); String partyId = (String) context.get("partyId"); if (UtilValidate.isEmpty(partyId)) { partyId = "_NA_"; } String currencyUom = (String) context.get("currency"); if (UtilValidate.isEmpty(currencyUom)) { currencyUom = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); } String finAccountId = null; // validate the pin if the store requires it and figure out the finAccountId from card number try { GenericValue giftCertSettings = delegator.findByPrimaryKeyCache("ProductStoreFinActSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId)); if ("Y".equals(giftCertSettings.getString("requirePinCode"))) { if (!validatePin(delegator, cardNumber, pinNumber)) { return ServiceUtil.returnError("PIN number is not valid!"); } finAccountId = cardNumber; } else { GenericValue finAccount = FinAccountHelper.getFinAccountFromCode(cardNumber, delegator); if (finAccount != null) { finAccountId = finAccount.getString("finAccountId"); } } } catch (GenericEntityException ex) { return ServiceUtil.returnError("Cannot get store fin account settings " + ex.getMessage()); } if (finAccountId == null) { return ServiceUtil.returnError("Cannot get fin account for adding to balance"); } // get the previous balance BigDecimal previousBalance = ZERO; try { previousBalance = FinAccountHelper.getAvailableBalance(cardNumber, currencyUom, delegator); } catch (GeneralException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } // create the transaction BigDecimal balance = ZERO; String refNum = null; try { refNum = GiftCertificateServices.createTransaction(delegator, dispatcher, userLogin, amount, productStoreId, partyId, currencyUom, deposit, finAccountId); balance = FinAccountHelper.getAvailableBalance(cardNumber, currencyUom, delegator); } catch (GeneralException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } Map result = ServiceUtil.returnSuccess(); result.put("previousBalance", new Double(previousBalance.doubleValue())); result.put("balance", new Double(balance.doubleValue())); result.put("amount", amount); result.put("processResult", Boolean.TRUE); result.put("responseCode", "1"); result.put("referenceNum", refNum); Debug.log("Add Funds GC Result - " + result, module); return result; } public static Map redeemGiftCertificate(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); final String withdrawl = "WITHDRAWAL"; GenericValue userLogin = (GenericValue) context.get("userLogin"); String productStoreId = (String) context.get("productStoreId"); String cardNumber = (String) context.get("cardNumber"); String pinNumber = (String) context.get("pinNumber"); Double amount = (Double) context.get("amount"); String partyId = (String) context.get("partyId"); if (UtilValidate.isEmpty(partyId)) { partyId = "_NA_"; } String currencyUom = (String) context.get("currency"); if (UtilValidate.isEmpty(currencyUom)) { currencyUom = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); } // validate the amount if (amount.doubleValue() < 0.00) { return ServiceUtil.returnError("Amount should be a positive number."); } // validate the pin if the store requires it try { GenericValue giftCertSettings = delegator.findByPrimaryKeyCache("ProductStoreFinActSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId)); if ("Y".equals(giftCertSettings.getString("requirePinCode")) && !validatePin(delegator, cardNumber, pinNumber)) { return ServiceUtil.returnError("PIN number is not valid!"); } } catch (GenericEntityException ex) { return ServiceUtil.returnError("Cannot get store fin account settings " + ex.getMessage()); } Debug.logInfo("Attempting to redeem GC for " + amount, module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -