📄 ccpaymentservices.java
字号:
/* * $Id: CCPaymentServices.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2004 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.thirdparty.clearcommerce;import java.util.Map;import java.util.List;import java.util.Iterator;import java.util.ArrayList;import java.text.DecimalFormat;import java.io.OutputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;import org.ofbiz.base.util.*;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.ServiceUtil;import org.ofbiz.accounting.payment.PaymentGatewayServices;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.SAXException;import org.apache.xml.serialize.XMLSerializer;import org.apache.xml.serialize.OutputFormat;/** * ClearCommerce Payment Services (CCE 5.4) * * @author <a href="mailto:eckardjf@pobox.com">J. Eckard</a> * @version $Rev: 5462 $ */public class CCPaymentServices { public final static String module = CCPaymentServices.class.getName(); public static Map ccAuth(DispatchContext dctx, Map context) { Document authRequestDoc = buildPrimaryTxRequest(context, "PreAuth", (Double) context.get("processAmount"), (String) context.get("orderId")); Document authResponseDoc = null; try { authResponseDoc = sendRequest(authRequestDoc, (String) context.get("paymentConfig")); } catch (ClearCommerceException cce) { return ServiceUtil.returnError(cce.getMessage()); } if (getMessageListMaxSev(authResponseDoc) > 4) { Map result = ServiceUtil.returnSuccess(); result.put("authResult", new Boolean(false)); result.put("processAmount", new Double(0.00)); result.put("authRefNum", getReferenceNum(authResponseDoc)); List messages = getMessageList(authResponseDoc); if (UtilValidate.isNotEmpty(messages)) { result.put("internalRespMsgs", messages); } return result; } return processAuthResponse(authResponseDoc); } public static Map ccCredit(DispatchContext dctx, Map context) { Document creditRequestDoc = buildPrimaryTxRequest(context, "Credit", (Double) context.get("creditAmount"), (String) context.get("referenceCode")); Document creditResponseDoc = null; try { creditResponseDoc = sendRequest(creditRequestDoc, (String) context.get("paymentConfig")); } catch (ClearCommerceException cce) { return ServiceUtil.returnError(cce.getMessage()); } if (getMessageListMaxSev(creditResponseDoc) > 4) { Map result = ServiceUtil.returnSuccess(); result.put("creditResult", new Boolean(false)); result.put("creditAmount", new Double(0.00)); result.put("creditRefNum", getReferenceNum(creditResponseDoc)); List messages = getMessageList(creditResponseDoc); if (UtilValidate.isNotEmpty(messages)) { result.put("internalRespMsgs", messages); } return result; } return processCreditResponse(creditResponseDoc); } public static Map ccCapture(DispatchContext dctx, Map context) { GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); if (authTransaction == null) { return ServiceUtil.returnError("No authorization transaction found; cannot capture"); } Document captureRequestDoc = buildSecondaryTxRequest(context, authTransaction.getString("referenceNum"), "PostAuth", (Double) context.get("captureAmount")); Document captureResponseDoc = null; try { captureResponseDoc = sendRequest(captureRequestDoc, (String) context.get("paymentConfig")); } catch (ClearCommerceException cce) { return ServiceUtil.returnError(cce.getMessage()); } if (getMessageListMaxSev(captureResponseDoc) > 4) { Map result = ServiceUtil.returnSuccess(); result.put("captureResult", new Boolean(false)); result.put("captureAmount", new Double(0.00)); result.put("captureRefNum", getReferenceNum(captureResponseDoc)); List messages = getMessageList(captureResponseDoc); if (UtilValidate.isNotEmpty(messages)) { result.put("internalRespMsgs", messages); } return result; } return processCaptureResponse(captureResponseDoc); } public static Map ccRelease(DispatchContext dctx, Map context) { GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); if (authTransaction == null) { return ServiceUtil.returnError("No authorization transaction found; cannot release"); } Document releaseRequestDoc = buildSecondaryTxRequest(context, authTransaction.getString("referenceNum"), "Void", null); Document releaseResponseDoc = null; try { releaseResponseDoc = sendRequest(releaseRequestDoc, (String) context.get("paymentConfig")); } catch (ClearCommerceException cce) { return ServiceUtil.returnError(cce.getMessage()); } if (getMessageListMaxSev(releaseResponseDoc) > 4) { Map result = ServiceUtil.returnSuccess(); result.put("releaseResult", new Boolean(false)); result.put("releaseAmount", new Double(0.00)); result.put("releaseRefNum", getReferenceNum(releaseResponseDoc)); List messages = getMessageList(releaseResponseDoc); if (UtilValidate.isNotEmpty(messages)) { result.put("internalRespMsgs", messages); } return result; } return processReleaseResponse(releaseResponseDoc); } public static Map ccReleaseNoop(DispatchContext dctx, Map context) { GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); if (authTransaction == null) { return ServiceUtil.returnError("No authorization transaction found; cannot release"); } Map result = ServiceUtil.returnSuccess(); result.put("releaseResult", Boolean.valueOf(true)); result.put("releaseCode", authTransaction.getString("gatewayCode")); result.put("releaseAmount", authTransaction.getDouble("amount")); result.put("releaseRefNum", authTransaction.getString("referenceNum")); result.put("releaseFlag", authTransaction.getString("gatewayFlag")); result.put("releaseMessage", "Approved."); return result; } public static Map ccRefund(DispatchContext dctx, Map context) { GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); if (authTransaction == null) { return ServiceUtil.returnError("No authorization transaction found; cannot refund"); } // Although refunds are applied to captured transactions, using the auth reference number is ok here // Related auth and capture transactions will always have the same reference number Document refundRequestDoc = buildSecondaryTxRequest(context, authTransaction.getString("referenceNum"), "Credit", (Double) context.get("refundAmount")); Document refundResponseDoc = null; try { refundResponseDoc = sendRequest(refundRequestDoc, (String) context.get("paymentConfig")); } catch (ClearCommerceException cce) { return ServiceUtil.returnError(cce.getMessage()); } if (getMessageListMaxSev(refundResponseDoc) > 4) { Map result = ServiceUtil.returnSuccess(); result.put("refundResult", new Boolean(false)); result.put("refundAmount", new Double(0.00)); result.put("refundRefNum", getReferenceNum(refundResponseDoc)); List messages = getMessageList(refundResponseDoc); if (UtilValidate.isNotEmpty(messages)) { result.put("internalRespMsgs", messages); } return result; } return processRefundResponse(refundResponseDoc); } public static Map ccReAuth(DispatchContext dctx, Map context) { GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference"); GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference); if (authTransaction == null) { return ServiceUtil.returnError("No authorization transaction found; cannot re-auth."); } Document reauthRequestDoc = buildSecondaryTxRequest(context, authTransaction.getString("referenceNum"), "RePreAuth", (Double) context.get("reauthAmount")); Document reauthResponseDoc = null; try { reauthResponseDoc = sendRequest(reauthRequestDoc, (String) context.get("paymentConfig")); } catch (ClearCommerceException cce) { return ServiceUtil.returnError(cce.getMessage()); } if (getMessageListMaxSev(reauthResponseDoc) > 4) { Map result = ServiceUtil.returnSuccess(); result.put("reauthResult", new Boolean(false)); result.put("reauthAmount", new Double(0.00)); result.put("reauthRefNum", getReferenceNum(reauthResponseDoc)); List messages = getMessageList(reauthResponseDoc); if (UtilValidate.isNotEmpty(messages)) { result.put("internalRespMsgs", messages); } return result; } return processReAuthResponse(reauthResponseDoc); } private static Map processAuthResponse(Document responseDocument) { Element engineDocElement = UtilXml.firstChildElement(responseDocument.getDocumentElement(), "EngineDoc"); Element orderFormElement = UtilXml.firstChildElement(engineDocElement, "OrderFormDoc"); Element transactionElement = UtilXml.firstChildElement(orderFormElement, "Transaction"); Element procResponseElement = UtilXml.firstChildElement(transactionElement, "CardProcResp"); Map result = ServiceUtil.returnSuccess(); String errorCode = UtilXml.childElementValue(procResponseElement, "CcErrCode"); if ("1".equals(errorCode)) { result.put("authResult", Boolean.valueOf(true)); result.put("authCode", UtilXml.childElementValue(transactionElement, "AuthCode"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -