?? paymentgatewayservices.java
字號:
/* * $Id: PaymentGatewayServices.java 7233 2006-04-07 14:26:24Z 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.text.DecimalFormat;import java.text.ParseException;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Calendar;import java.util.Set;import java.math.BigDecimal;import java.sql.Timestamp;import org.ofbiz.accounting.invoice.InvoiceWorker;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.UtilNumber;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityJoinOperator;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.order.order.OrderChangeHelper;import org.ofbiz.order.order.OrderReadHelper;import org.ofbiz.party.contact.ContactHelper;import org.ofbiz.product.store.ProductStoreWorker;import org.ofbiz.security.Security;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * PaymentGatewayServices * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7233 $ * @since 2.0 */public class PaymentGatewayServices { public static final String module = PaymentGatewayServices.class.getName(); public static final String AUTH_SERVICE_TYPE = "PRDS_PAY_AUTH"; public static final String REAUTH_SERVICE_TYPE = "PRDS_PAY_REAUTH"; public static final String RELEASE_SERVICE_TYPE = "PRDS_PAY_RELEASE"; public static final String CAPTURE_SERVICE_TYPE = "PRDS_PAY_CAPTURE"; public static final String REFUND_SERVICE_TYPE = "PRDS_PAY_REFUND"; public static final String CREDIT_SERVICE_TYPE = "PRDS_PAY_CREDIT"; private static final int TX_TIME = 300; private static BigDecimal ZERO = new BigDecimal("0"); private static int decimals = -1; private static int rounding = -1; static { decimals = UtilNumber.getBigDecimalScale("order.decimals"); rounding = UtilNumber.getBigDecimalRoundingMode("order.rounding"); // set zero to the proper scale if (decimals != -1) ZERO.setScale(decimals); } /** * Helper method to parse total remaining balance in an order from order read helper. */ private static double getTotalRemaining(OrderReadHelper orh) throws ParseException, NumberFormatException { String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); DecimalFormat formatter = new DecimalFormat(currencyFormat); String grandTotalString = formatter.format(orh.getOrderGrandTotal()); Double grandTotal = new Double(formatter.parse(grandTotalString).doubleValue()); return grandTotal.doubleValue(); } /** * Authorizes a single order preference with an option to specify an amount. The result map has the Booleans * "errors" and "finished" which notify the user if there were any errors and if the authorizatoin was finished. * There is also a List "messages" for the authorization response messages and a Double, "processAmount" as the * amount processed. TODO: it might be nice to return the paymentGatewayResponseId */ public static Map authOrderPaymentPreference(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String orderPaymentPreferenceId = (String) context.get("orderPaymentPreferenceId"); Double overrideAmount = (Double) context.get("overrideAmount"); // validate overrideAmount if its available if (overrideAmount != null) { if (overrideAmount.doubleValue() < 0) return ServiceUtil.returnError("Amount entered (" + overrideAmount + ") is negative."); if (overrideAmount.doubleValue() == 0) return ServiceUtil.returnError("Amount entered (" + overrideAmount + ") is zero."); } GenericValue orderHeader = null; GenericValue orderPaymentPreference = null; try { orderPaymentPreference = delegator.findByPrimaryKey("OrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreferenceId)); orderHeader = orderPaymentPreference.getRelatedOne("OrderHeader"); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Problems getting required information: orderPaymentPreference [" + orderPaymentPreferenceId + "]"); } OrderReadHelper orh = new OrderReadHelper(orderHeader); // get the total remaining double totalRemaining = 0.0; try { totalRemaining = getTotalRemaining(orh); } catch (Exception e) { Debug.logError(e, "Problem getting parsed grand total amount", module); return ServiceUtil.returnError("ERROR: Cannot parse grand total from formatted string; see logs"); } // get the process attempts so far Long procAttempt = orderPaymentPreference.getLong("processAttempt"); if (procAttempt == null) { procAttempt = new Long(0); } // update the process attempt count orderPaymentPreference.set("processAttempt", new Long(procAttempt.longValue() + 1)); try { orderPaymentPreference.store(); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to update OrderPaymentPreference record!"); } // if we are already authorized, then this is a re-auth request boolean reAuth = false; if (orderPaymentPreference.get("statusId") != null && "PAYMENT_AUTHORIZED".equals(orderPaymentPreference.getString("statusId"))) { reAuth = true; } // use overrideAmount or maxAmount Double transAmount = null; if (overrideAmount != null) { transAmount = overrideAmount; } else { transAmount = orderPaymentPreference.getDouble("maxAmount"); } // prepare the return map (always return success, default finished=false, default errors=false Map results = UtilMisc.toMap(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS, "finished", new Boolean(false), "errors", new Boolean(false)); // if our transaction amount exists and is zero, there's nothing to process, so return if ((transAmount != null) && (transAmount.doubleValue() <= 0)) { return results; } // call the authPayment method Map processorResult = authPayment(dispatcher, userLogin, orh, orderPaymentPreference, totalRemaining, reAuth, overrideAmount); // handle the response if (processorResult != null) { // get the customer messages if (processorResult.get("customerRespMsgs") != null) { results.put("messages", processorResult.get("customerRespMsgs")); } // not null result means either an approval or decline; null would mean error Double thisAmount = (Double) processorResult.get("processAmount"); // process the auth results boolean processResult = false; try { processResult = processResult(dctx, processorResult, userLogin, orderPaymentPreference); if (processResult) { results.put("processAmount", thisAmount); results.put("finished", new Boolean(true)); } } catch (GeneralException e) { Debug.logError(e, "Trouble processing the result; processorResult: " + processorResult, module); results.put("errors", new Boolean(true)); } } else { // error with payment processor; will try later Debug.logInfo("Invalid OrderPaymentPreference; maxAmount is 0", module); orderPaymentPreference.set("statusId", "PAYMENT_CANCELLED"); try { orderPaymentPreference.store(); } catch (GenericEntityException e) { Debug.logError(e, "ERROR: Problem setting OrderPaymentPreference status to CANCELLED", module); } results.put("errors", new Boolean(true)); } return results; } /** * Processes payments through service calls to the defined processing service for the ProductStore/PaymentMethodType * @return APPROVED|FAILED|ERROR for complete processing of ALL payment methods. */ public static Map authOrderPayments(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String orderId = (String) context.get("orderId"); Map result = new HashMap(); // get the order header and payment preferences GenericValue orderHeader = null; List paymentPrefs = null; try { // get the OrderHeader orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); // get the payments to auth Map lookupMap = UtilMisc.toMap("orderId", orderId, "statusId", "PAYMENT_NOT_AUTH"); List orderList = UtilMisc.toList("maxAmount"); paymentPrefs = delegator.findByAnd("OrderPaymentPreference", lookupMap, orderList); } catch (GenericEntityException gee) { Debug.logError(gee, "Problems getting the order information", module); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); result.put(ModelService.ERROR_MESSAGE, "ERROR: Could not get order information (" + gee.getMessage() + ")."); return result; } // make sure we have a OrderHeader if (orderHeader == null) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -