giftcertificateservices.java
来自「Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电」· Java 代码 · 共 1,365 行 · 第 1/5 页
JAVA
1,365 行
// check the actual balance (excluding authorized amounts) and create the transaction if it is sufficient double previousBalance = 0.00; try { previousBalance = FinAccountHelper.getBalance(cardNumber, currencyUom, delegator).doubleValue(); } catch (GeneralException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } double balance = 0.00; String refNum = null; Boolean procResult; if (previousBalance >= amount.doubleValue()) { try { refNum = GiftCertificateServices.createTransaction(delegator, dispatcher, userLogin, amount, productStoreId, partyId, currencyUom, withdrawl, cardNumber); balance = FinAccountHelper.getAvailableBalance(cardNumber, currencyUom, delegator).doubleValue(); procResult = Boolean.TRUE; } catch (GeneralException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } } else { procResult = Boolean.FALSE; balance = previousBalance; refNum = "N/A"; } Map result = ServiceUtil.returnSuccess(); result.put("previousBalance", new Double(previousBalance)); result.put("balance", new Double(balance)); result.put("amount", amount); result.put("processResult", procResult); result.put("responseCode", "2"); result.put("referenceNum", refNum); Debug.log("Redeem GC Result - " + result, module); return result; } public static Map checkGiftCertificateBalance(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String cardNumber = (String) context.get("cardNumber"); String pinNumber = (String) context.get("pinNumber"); // validate the pin if (!validatePin(delegator, cardNumber, pinNumber)) { return ServiceUtil.returnError("PIN number is not valid!"); } // TODO: get the real currency from context String currencyUom = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); // get the balance double balance = 0.00; try { balance = FinAccountHelper.getAvailableBalance(cardNumber, currencyUom, delegator).doubleValue(); } catch (GeneralException e) { return ServiceUtil.returnError(e.getMessage()); } Map result = ServiceUtil.returnSuccess(); result.put("balance", new Double(balance)); Debug.log("GC Balance Result - " + result, module); return result; } // Fullfilment Services public static Map giftCertificateProcessor(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Double amount = (Double) context.get("processAmount"); String currency = (String) context.get("currency"); // make sure we have a currency if (currency == null) { currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); } // get the authorizations GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); GenericValue authTransaction = (GenericValue) context.get("authTrans"); if (authTransaction == null){ authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); } if (authTransaction == null) { return ServiceUtil.returnError("No authorization transaction found for the OrderPaymentPreference; cannot capture"); } // get the gift certificate and its authorization from the authorization String finAccountAuthId = authTransaction.getString("referenceNum"); try { GenericValue finAccountAuth = delegator.findByPrimaryKey("FinAccountAuth", UtilMisc.toMap("finAccountAuthId", finAccountAuthId)); GenericValue giftCard = finAccountAuth.getRelatedOne("FinAccount"); // make sure authorization has not expired Timestamp authExpiration = finAccountAuth.getTimestamp("thruDate"); if ((authExpiration != null) && (authExpiration.before(UtilDateTime.nowTimestamp()))) { return ServiceUtil.returnError("Authorization transaction [" + authTransaction.getString("paymentGatewayResponseId") + "] has expired as of " + authExpiration); } // make sure the fin account itself has not expired if ((giftCard.getTimestamp("thruDate") != null) && (giftCard.getTimestamp("thruDate").before(UtilDateTime.nowTimestamp()))) { return ServiceUtil.returnError("Gift certificate has expired as of " + giftCard.getTimestamp("thruDate")); } // obtain the order information OrderReadHelper orh = new OrderReadHelper(delegator, orderPaymentPreference.getString("orderId")); Map redeemCtx = new HashMap(); redeemCtx.put("userLogin", userLogin); redeemCtx.put("productStoreId", orh.getProductStoreId()); redeemCtx.put("cardNumber", giftCard.get("finAccountId")); redeemCtx.put("pinNumber", giftCard.get("finAccountCode")); redeemCtx.put("currency", currency); if (orh.getBillToParty() != null) { redeemCtx.put("partyId", orh.getBillToParty().get("partyId")); } redeemCtx.put("amount", amount); // invoke the redeem service Map redeemResult = null; redeemResult = dispatcher.runSync("redeemGiftCertificate", redeemCtx); if (ServiceUtil.isError(redeemResult)) { return redeemResult; } // now release the authorization should this use the gift card release service? Map releaseResult = dispatcher.runSync("expireFinAccountAuth", UtilMisc.toMap("userLogin", userLogin, "finAccountAuthId", finAccountAuthId)); if (ServiceUtil.isError(releaseResult)) { return releaseResult; } Map result = ServiceUtil.returnSuccess(); if (redeemResult != null) { Boolean processResult = (Boolean) redeemResult.get("processResult"); result.put("processAmount", amount); result.put("captureResult", processResult); result.put("captureCode", "C"); result.put("captureRefNum", redeemResult.get("referenceNum")); } return result; } catch (GenericEntityException ex) { return ServiceUtil.returnError("Cannot process gift card: " + ex.getMessage()); } catch (GenericServiceException ex) { return ServiceUtil.returnError("Cannot process gift card: " + ex.getMessage()); }} public static Map giftCertificateAuthorize(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); GenericValue giftCard = (GenericValue) context.get("giftCard"); String currency = (String) context.get("currency"); String orderId = (String) context.get("orderId"); Double amount = (Double) context.get("processAmount"); // make sure we have a currency if (currency == null) { currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); } // obtain the order information OrderReadHelper orh = new OrderReadHelper(delegator, orderId); String productStoreId = orh.getProductStoreId(); try { // if the store requires pin codes, then validate pin code against card number, and the gift certificate's finAccountId is the gift card's card number // otherwise, the gift card's card number is an ecrypted string, which must be decoded to find the FinAccount GenericValue giftCertSettings = delegator.findByPrimaryKeyCache("ProductStoreFinActSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId)); GenericValue finAccount = null; String finAccountId = null; if ("Y".equals(giftCertSettings.getString("requirePinCode"))) { if (validatePin(delegator, giftCard.getString("cardNumber"), giftCard.getString("pinNumber"))) { finAccountId = giftCard.getString("cardNumber"); finAccount = delegator.findByPrimaryKey("FinAccount", UtilMisc.toMap("finAccountId", finAccountId)); } } else { finAccount = FinAccountHelper.getFinAccountFromCode(giftCard.getString("cardNumber"), delegator); if (finAccount == null) { return ServiceUtil.returnError("Gift certificate not found"); } finAccountId = finAccount.getString("finAccountId"); } if (finAccountId == null) { return ServiceUtil.returnError("Gift certificate pin number is invalid"); } // check for expiration date if ((finAccount.getTimestamp("thruDate") != null) && (finAccount.getTimestamp("thruDate").before(UtilDateTime.nowTimestamp()))) { return ServiceUtil.returnError("Gift certificate has expired as of " + finAccount.getTimestamp("thruDate")); } // check the amount to authorize against the available balance of fin account, which includes active authorizations as well as transactions BigDecimal availableBalance = FinAccountHelper.getAvailableBalance(finAccountId, currency, delegator); Boolean processResult = null; String refNum = null; Map result = ServiceUtil.returnSuccess(); // turn amount into a big decimal, making sure to round and scale it to the same as availableBalance BigDecimal amountBd = (new BigDecimal(amount.doubleValue())).setScale(FinAccountHelper.decimals, FinAccountHelper.rounding); // if availableBalance equal to or greater than amount, then auth if (availableBalance.compareTo(amountBd) > -1) { Timestamp thruDate = null; if (giftCertSettings.getLong("authValidDays") != null) { thruDate = UtilDateTime.getDayEnd(UtilDateTime.nowTimestamp(), giftCertSettings.getLong("authValidDays").intValue()); } Map tmpResult = dispatcher.runSync("createFinAccountAuth", UtilMisc.toMap("finAccountId", finAccountId, "amount", amount, "currencyUomId", currency, "thruDate", thruDate, "userLogin", userLogin)); if (ServiceUtil.isError(tmpResult)) { return tmpResult; } else { refNum = (String) tmpResult.get("finAccountAuthId"); processResult = Boolean.TRUE; } } else { Debug.logError("Attempted to authorize [" + amount + "] against a balance of only [" + availableBalance + "]", module); refNum = "N/A"; // a refNum is always required from authorization processResult = Boolean.FALSE; } result.put("processAmount", amount); result.put("authResult", processResult); result.put("processAmount", amount); result.put("authFlag", "2"); result.put("authCode", "A"); result.put("captureCode", "C"); result.put("authRefNum", refNum); return result; } catch (GenericEntityException ex) { Debug.logError(ex, "Cannot authorize gift certificate", module); return ServiceUtil.returnError("Cannot authorize gift certificate due to " + ex.getMessage()); } catch (GenericServiceException ex) { Debug.logError(ex, "Cannot authorize gift certificate", module); return ServiceUtil.returnError("Cannot authorize gift certificate due to " + ex.getMessage()); } } public static Map giftCertificateRefund(DispatchContext dctx, Map context) { GenericValue userLogin = (GenericValue) context.get("userLogin"); GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); String currency = (String) context.get("currency"); Double amount = (Double) context.get("refundAmount"); return giftCertificateRestore(dctx, userLogin, paymentPref, amount, currency, "refund"); } public static Map giftCertificateRelease(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); String err = "Unable to expire financial account authorization for Gift Certificate: "; try { // expire the related financial authorization transaction GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(paymentPref); if (authTransaction == null) { return ServiceUtil.returnError(err + " Could not find authorization transaction."); } Map input = UtilMisc.toMap("userLogin", userLogin, "finAccountAuthId", authTransaction.get("referenceNum")); Map serviceResults = dispatcher.runSync("expireFinAccountAuth", input); Map result = ServiceUtil.returnSuccess(); result.put("releaseRefNum", authTransaction.getString("referenceNum")); result.put("releaseAmount", authTransaction.getDouble("amount")); result.put("releaseResult", new Boolean(true)); // if there's an error, don't release if (ServiceUtil.isError(serviceResults)) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?