📄 paymentgatewayservices.java
字号:
/*
* $Id: PaymentGatewayServices.java,v 1.26 2004/01/22 17:47:24 ajzeneski Exp $
*
* Copyright (c) 2002 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 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.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.EntityUtil;
import org.ofbiz.order.order.OrderReadHelper;
import org.ofbiz.party.contact.ContactHelper;
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.ModelService;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.security.Security;
/**
* PaymentGatewayServices
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.26 $
* @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";
/**
* 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();
GenericValue userLogin = (GenericValue) context.get("userLogin");
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) {
return ServiceUtil.returnError("Could not find OrderHeader with orderId: " + orderId + "; not processing payments.");
}
// get the order amounts
String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
OrderReadHelper orh = new OrderReadHelper(orderHeader);
DecimalFormat formatter = new DecimalFormat(currencyFormat);
String grandTotalString = formatter.format(orh.getOrderGrandTotal());
Double grandTotal = null;
try {
grandTotal = new Double(formatter.parse(grandTotalString).doubleValue());
} catch (ParseException e) {
Debug.logError(e, "Problem getting parsed grand total amount", module);
return ServiceUtil.returnError("ERROR: Cannot parse grand total from formatted string; see logs");
}
double totalRemaining = grandTotal.doubleValue();
// loop through and auth each payment
List finished = new ArrayList();
List hadError = new ArrayList();
Iterator payments = paymentPrefs.iterator();
while (payments.hasNext()) {
GenericValue paymentPref = (GenericValue) payments.next();
boolean reAuth = false;
// if we are already authorized, then this is a re-auth request
if (paymentPref.get("statusId") != null && "PAYMENT_AUTHORIZED".equals(paymentPref.getString("statusId"))) {
reAuth = true;
}
// check the maxAmount for 0.00
Double maxAmount = paymentPref.getDouble("maxAmount");
if (maxAmount == null || maxAmount.doubleValue() > 0) {
// call the authPayment method
Map processorResult = authPayment(dispatcher, userLogin, orh, paymentPref, totalRemaining, reAuth);
// handle the response
if (processorResult != null) {
// not null result means either an approval or decline; null would mean error
GenericValue paymentSettings = (GenericValue) processorResult.get("paymentSettings");
Double thisAmount = (Double) processorResult.get("processAmount");
// process the auth results
boolean processResult = false;
try {
processResult = processResult(dctx, processorResult, userLogin, paymentPref, paymentSettings);
if (processResult) {
totalRemaining -= thisAmount.doubleValue();
finished.add(processorResult);
}
} catch (GeneralException e) {
Debug.logError(e, "Trouble processing the result; processorResult: " + processorResult, module);
hadError.add(paymentPref);
ServiceUtil.returnError("Trouble processing the auth results");
}
} else {
// error with payment processor; will try later
hadError.add(paymentPref);
continue;
}
} else {
Debug.logInfo("Invalid OrderPaymentPreference; maxAmount is 0", module);
paymentPref.set("statusId", "PAYMENT_CANCELLED");
try {
paymentPref.store();
} catch (GenericEntityException e) {
Debug.logError(e, "ERROR: Problem setting OrderPaymentPreference status to CANCELLED", module);
}
finished.add(null);
}
}
Debug.logInfo("Finished with auth(s) checking results", module);
if (hadError.size() > 0) {
Debug.logError("Error(s) (" + hadError.size() + ") during auth; returning ERROR", module);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
result.put("processResult", "ERROR");
return result;
} else if (finished.size() == paymentPrefs.size()) {
Debug.logInfo("All auth(s) passed total remaining : " + totalRemaining, module);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
result.put("processResult", "APPROVED");
return result;
} else {
Debug.logInfo("Only (" + finished.size() + ") passed auth; returning FAILED", module);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
result.put("processResult", "FAILED");
return result;
}
}
private static Map authPayment(LocalDispatcher dispatcher, GenericValue userLogin, OrderReadHelper orh, GenericValue paymentPref, double totalRemaining, boolean reauth) {
String paymentConfig = null;
String serviceName = null;
// get the payment settings i.e. serviceName and config properties file name
String serviceType = AUTH_SERVICE_TYPE;
if (reauth) {
serviceType = REAUTH_SERVICE_TYPE;
}
GenericValue paymentSettings = getPaymentSettings(orh.getOrderHeader(), paymentPref, serviceType, false);
if (paymentSettings != null) {
serviceName = paymentSettings.getString("paymentService");
paymentConfig = paymentSettings.getString("paymentPropertiesPath");
} else {
Debug.logError("Invalid payment settings entity, no payment settings found", module);
return null;
}
// make sure the service name is not null
if (serviceName == null) {
Debug.logError("Invalid payment processor: + " + paymentSettings, module);
return null;
}
// get the process context
Map processContext = null;
try {
processContext = makeAuthContext(orh, userLogin, paymentPref, paymentConfig, totalRemaining);
} catch (GeneralException e) {
Debug.logError(e, "Problems creating the context for the auth service", module);
return null;
}
// the amount of this transaction
Double thisAmount = (Double) processContext.get("processAmount");
// invoke the processor.
Map processorResult = null;
try {
processorResult = dispatcher.runSync(serviceName, processContext);
} catch (GenericServiceException gse) {
Debug.logError("Error occurred on: " + serviceName + " => " + processContext, module);
Debug.logError(gse, "Problems invoking payment processor! Will retry later." + "(" + orh.getOrderId() + ")", module);
return null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -