⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 icspaymentservices.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: IcsPaymentServices.java,v 1.9 2004/02/05 21:39:39 ajzeneski Exp $
 *
 * Copyright (c) 2003 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.cybersource;

import java.util.*;
import java.text.DecimalFormat;

import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.base.util.*;
import org.ofbiz.accounting.payment.PaymentGatewayServices;

import com.cybersource.ws.client.axis.basic.Client;
import com.cybersource.ws.client.axis.basic.BasicClientException;
import com.cybersource.ws.client.axis.AxisFaultException;

/**
 * CyberSource WS Integration Services
 *
 * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
 * @version    $Revision: 1.9 $
 * @since      3.0
 */
public class IcsPaymentServices {

    public static final String module = IcsPaymentServices.class.getName();

    // load the JSSE properties
    static {
        SSLUtil.loadJsseProperties();
    }

    public static Map ccAuth(DispatchContext dctx, Map context) {
        // generate the request/properties
        Properties props = buildCsProperties(context);
        Map request = buildAuthRequest(context);
        request.put("merchantID", props.get("merchantID"));

        // transmit the request
        Map reply = null;
        try {
            reply = Client.runTransaction(request, props);
        } catch (AxisFaultException e) {
            Debug.logError(e, "ERROR: Exception from Axis to CyberSource", module);
            Debug.logError(e.getAxisFault(), "Axis Fault : " + e.getAxisFault().getFaultString(), module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        } catch (BasicClientException e) {
            Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        }

        // process the reply
        Map result = ServiceUtil.returnSuccess();
        processAuthResult(reply, result);
        return result;
    }

    public static Map ccReAuth(DispatchContext dctx, Map context) {
        return ServiceUtil.returnSuccess();
    }

    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 for the OrderPaymentPreference; cannot capture");
        }

        // generate the request/properties
        Properties props = buildCsProperties(context);
        Map request = buildCaptureRequest(context, authTransaction);
        request.put("merchantID", props.get("merchantID"));

        // transmit the request
        Map reply = null;
        try {
            reply = Client.runTransaction(request, props);
        } catch (AxisFaultException e) {
            Debug.logError(e, "ERROR: Exception from Axis to CyberSource", module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        } catch (BasicClientException e) {
            Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        }

        // process the reply
        Map result = ServiceUtil.returnSuccess();
        processCaptureResult(reply, result);
        return result;
    }

    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 for the OrderPaymentPreference; cannot release");
        }

        // generate the request/properties
        Properties props = buildCsProperties(context);
        Map request = buildReleaseRequest(context, authTransaction);
        request.put("merchantID", props.get("merchantID"));

        // transmit the request
        Map reply = null;
        try {
            reply = Client.runTransaction(request, props);
        } catch (AxisFaultException e) {
            Debug.logError(e, "ERROR: Exception from Axis to CyberSource", module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        } catch (BasicClientException e) {
            Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        }

        // process the reply
        Map result = ServiceUtil.returnSuccess();
        processReleaseResult(reply, result);
        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 for the OrderPaymentPreference; cannot refund");
        }

        // generate the request/properties
        Properties props = buildCsProperties(context);
        Map request = buildRefundRequest(context, authTransaction);
        request.put("merchantID", props.get("merchantID"));

        // transmit the request
        Map reply = null;
        try {
            reply = Client.runTransaction(request, props);
        } catch (AxisFaultException e) {
            Debug.logError(e, "ERROR: Exception from Axis to CyberSource", module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        } catch (BasicClientException e) {
            Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        }

        // process the reply
        Map result = ServiceUtil.returnSuccess();
        processRefundResult(reply, result);
        return result;
    }

    public static Map ccCredit(DispatchContext dctx, Map context) {
        // generate the request/properties
        Properties props = buildCsProperties(context);
        Map request = buildCreditRequest(context);
        request.put("merchantID", props.get("merchantID"));

        // transmit the request
        Map reply = null;
        try {
            reply = Client.runTransaction(request, props);
        } catch (AxisFaultException e) {
            Debug.logError(e, "ERROR: Exception from Axis to CyberSource", module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        } catch (BasicClientException e) {
            Debug.logError(e, "ERROR: CyberSource Client exception : " + e.getMessage(), module);
            return ServiceUtil.returnError("Unable to communicate with CyberSource");
        }

        // process the reply
        Map result = ServiceUtil.returnSuccess();
        processCreditResult(reply, result);
        return result;
    }

    private static Properties buildCsProperties(Map context) {
        String configString = (String) context.get("paymentConfig");
        if (configString == null) {
            configString = "payment.properties";
        }

        String merchantId = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantID");
        String serverUrl = UtilProperties.getPropertyValue(configString, "payment.cybersource.serverURL");

        String keysPath = UtilProperties.getPropertyValue(configString, "payment.cybersource.keysDir");
        String keysFile = UtilProperties.getPropertyValue(configString, "payment.cybersource.keysFile");
        String demo = UtilProperties.getPropertyValue(configString, "payment.cybersource.demo", "N");
        demo = "Y".equalsIgnoreCase(demo) ? "true" : "false";

        // create some properties for CS Client
        Properties props = new Properties();
        props.put("merchantID", merchantId);
        props.put("cybersourceURL", serverUrl);
        props.put("keysDir", keysPath);

        if (keysFile != null && keysFile.length() > 0) {
            props.put("keyFilename", keysFile);
        }
        props.put("demo", demo);
        //Debug.logInfo("Created CyberSource Properties : " + props, module);

        return props;
    }

    private static Map buildAuthRequest(Map context) {
        // make the request map
        String orderId = (String) context.get("orderId");
        Map request = new HashMap();
        request.put("ccAuthService_run", "true");              // run auth service
        request.put("merchantReferenceCode", orderId);         // set the order ref number
        appendFullBillingInfo(request, context);               // add in all address info
        appendItemLineInfo(request, context, "processAmount"); // add in the item info
        appendAvsRules(request, context);                      // add in the AVS flags and decline codes
        return request;
    }

    private static Map buildCaptureRequest(Map context, GenericValue authTransaction) {
        GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
        String configString = (String) context.get("paymentConfig");
        String currency = (String) context.get("currency");
        if (configString == null) {
            configString = "payment.properties";
        }
        String merchantDesc = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantDescr", null);
        String merchantCont = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantContact", null);

        Map request = new HashMap();
        request.put("ccCaptureService_run", "true");
        request.put("ccCaptureService_authRequestID", authTransaction.getString("referenceNum"));
        request.put("item_0_unitPrice", getAmountString(context, "captureAmount"));
        request.put("merchantReferenceCode", orderPaymentPreference.getString("orderId"));
        request.put("purchaseTotals_currency", currency);

        // TODO: add support for verbal authorizations
        //request.put("ccCaptureService_authType", null);   -- should be 'verbal'
        //request.put("ccCaptureService_verbalAuthCode", null); -- code from verbal auth

        if (merchantDesc != null) {
            request.put("invoiceHeader_merchantDescriptor", merchantDesc);        // merchant description
        }
        if (merchantCont != null) {
            request.put("invoiceHeader_merchantDescriptorContact", merchantCont); // merchant contact info
        }

        return request;
    }

    private static Map buildReleaseRequest(Map context, GenericValue authTransaction) {
        GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
        String currency = (String) context.get("currency");
        Map request = new HashMap();
        request.put("ccAuthReversalService_run", "true");
        request.put("ccAuthReversalService_authRequestID", authTransaction.getString("referenceNum"));
        request.put("item_0_unitPrice", getAmountString(context, "releaseAmount"));
        request.put("merchantReferenceCode", orderPaymentPreference.getString("orderId"));
        request.put("purchaseTotals_currency", currency);
        return request;
    }

    private static Map buildRefundRequest(Map context, GenericValue authTransaction) {
        GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
        String configString = (String) context.get("paymentConfig");
        String currency = (String) context.get("currency");
        if (configString == null) {
            configString = "payment.properties";
        }
        String merchantDesc = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantDescr", null);
        String merchantCont = UtilProperties.getPropertyValue(configString, "payment.cybersource.merchantContact", null);

        Map request = new HashMap();
        request.put("ccCreditService_run", "true");
        request.put("ccCreditService_captureRequestID", authTransaction.getString("referenceNum"));
        request.put("item_0_unitPrice", getAmountString(context, "refundAmount"));
        request.put("merchantReferenceCode", orderPaymentPreference.getString("orderId"));
        request.put("purchaseTotals_currency", currency);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -