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

📄 ccpaymentservices.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: CCPaymentServices.java,v 1.1 2003/08/18 19:37:43 jonesde 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.clearcommerce;

import java.io.StringWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;

import com.clearcommerce.ccxclientapi.CcApiDocument;
import com.clearcommerce.ccxclientapi.CcApiException;
import com.clearcommerce.ccxclientapi.CcApiMoney;
import com.clearcommerce.ccxclientapi.CcApiRecord;

/**
 * ClearCommerce Integration Services
 *
 * @author     <a href="mailto:joe.eckard@redrocketcorp.com">Joe Eckard</a>
 * @version    $Revision: 1.1 $
 * @since      2.2
 */
public class CCPaymentServices {

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

    public static final String TYPE_AUTH = "PreAuth";
    public static final String TYPE_CAPTURE = "PostAuth";
    
    public static Map ccAuthProcessor(DispatchContext dctx, Map context) {
        return ccProcessor(dctx, context, TYPE_AUTH);
    }
    
    public static Map ccCaptureProcessor(DispatchContext dctx, Map context) {
        return ccProcessor(dctx, context, TYPE_CAPTURE);
    }
    
    private static Map ccProcessor(DispatchContext dctx, Map context, String txType) {        
        Map result = new HashMap();
        
        // quick parameter check
        if (!txType.matches(TYPE_AUTH + "|" + TYPE_CAPTURE)) {
            return ServiceUtil.returnError("Invalid transaction type specified.");
        }
        
        try {           
            CcApiDocument requestDoc = new CcApiDocument();
            
            if (txType.equals(TYPE_AUTH)) {
                buildAuthRequest(requestDoc, context);
            } else if (txType.equals(TYPE_CAPTURE)) {
                buildCaptureRequest(requestDoc, context);
            }
            
            if (Debug.verboseOn()) {
                Writer requestWriter = new StringWriter();

                requestDoc.writeTo(requestWriter);
                Debug.logVerbose("---- ClearCommerce Request ----", module);
                Debug.logVerbose("\n\n" + requestWriter, module);
                Debug.logVerbose("---- End Request ----", module);
            }

            CcApiDocument responseDoc = requestDoc.process();
            
            if (Debug.verboseOn()) {
                Writer responseWriter = new StringWriter();

                responseDoc.writeTo(responseWriter);
                Debug.logVerbose("---- ClearCommerce Response ----", module);
                Debug.logVerbose("\n\n" + responseWriter, module);
                Debug.logVerbose("---- End Response ----", module);
            }
            
            CcApiRecord recEngineDoc = responseDoc.getFirstRecord("EngineDoc");

            if (recEngineDoc == null) {
                return ServiceUtil.returnError("ERROR: ClearCommerce response did not contain an EngineDoc record.");
            }

            // check for messages
            CcApiRecord recMessageList = recEngineDoc.getFirstRecord("MessageList");

            if (recMessageList != null) {    
                CcApiRecord recMessage = recMessageList.getFirstRecord("Message");
                List ccApiMessageList = new ArrayList();

                while (recMessage != null) {
                    StringBuffer ccApiMessage = new StringBuffer();

                    ccApiMessage.append("Audience=" + recMessage.getFieldString("Audience") + ", ");
                    ccApiMessage.append("ContextId=" + recMessage.getFieldString("ContextId") + ", ");
                    ccApiMessage.append("Component=" + recMessage.getFieldString("Component") + ", ");
                    ccApiMessage.append("Sev=" + recMessage.getFieldS32("Sev") + ", ");
                    ccApiMessage.append("Text=" + recMessage.getFieldString("Text"));
                    ccApiMessageList.add(ccApiMessage.toString());
                    recMessage = recMessageList.getNextRecord("Message");
                }

                if (!ccApiMessageList.isEmpty()) {
                    Debug.logWarning("ClearCommerce response message(s): " + ccApiMessageList, module);
                    Integer maxSeverity = recMessageList.getFieldS32("MaxSev");

                    if (maxSeverity.intValue() > 4) {
                        return ServiceUtil.returnError(ccApiMessageList);
                    }
                }
            }
            
            CcApiRecord recOrderFormDoc = recEngineDoc.getFirstRecord("OrderFormDoc");

            if (recOrderFormDoc == null) {
                return ServiceUtil.returnError("ERROR: ClearCommerce response did not contain an OrderFormDoc record.");
            }
            CcApiRecord recTransaction = recOrderFormDoc.getFirstRecord("Transaction");

            if (recTransaction == null) {
                return ServiceUtil.returnError("ERROR: ClearCommerce response did not contain a Transaction record.");
            }
            CcApiRecord recProcResponse = recTransaction.getFirstRecord("CardProcResp");

            if (recProcResponse == null) {
                return ServiceUtil.returnError("ERROR: ClearCommerce response did not contain a CardProcResp record.");
            }
            
            if (txType.equals(TYPE_AUTH)) {
                processAuthResponse(responseDoc, result);
            } else if (txType.equals(TYPE_CAPTURE)) {
                processCaptureResponse(responseDoc, result);
            }
            
        } catch (CcApiException ce) {
            ce.printStackTrace();
            return ServiceUtil.returnError("ERROR: ClearCommerce Problem (" + ce.getMessage() + ").");
        } catch (GenericEntityException gee) {
            gee.printStackTrace();
            return ServiceUtil.returnError("ERROR: Could not get order information (" + gee.getMessage() + ").");
        }
        
        return result;
    }
        
    private static void buildAuthRequest(CcApiDocument doc, Map context) throws CcApiException, GenericEntityException {                
        String configString = (String) context.get("paymentConfig");

        if (configString == null) {
            configString = "payment.properties";
        }
        
        initDoc(doc, context);
            
        CcApiRecord recEngineDoc = doc.getFirstRecord("EngineDoc");
            
        // EngineDocList.EngineDoc.Instructions
        CcApiRecord recInstructions = recEngineDoc.addRecord("Instructions");

        recInstructions.setFieldString("Pipeline", "PaymentNoFraud");

        // EngineDocList.EngineDoc.OrderFormDoc
        CcApiRecord recOrderFormDoc = recEngineDoc.getFirstRecord("OrderFormDoc");
        recOrderFormDoc.setFieldString("Comments", (String) context.get("orderId"));

        // EngineDocList.EngineDoc.OrderFormDoc.Consumer
        CcApiRecord recConsumer = recOrderFormDoc.addRecord("Consumer");
        GenericValue contactEmail = (GenericValue) context.get("contactEmail");

        recConsumer.setFieldString("Email", contactEmail.getString("infoString"));

        // EngineDocList.EngineDoc.OrderFormDoc.Consumer.PaymentMech
        CcApiRecord recPaymentMech = recConsumer.addRecord("PaymentMech");

        recPaymentMech.setFieldString("Type", "CreditCard");

        // EngineDocList.EngineDoc.OrderFormDoc.Consumer.PaymentMech.CreditCard
        CcApiRecord recCreditCard = recPaymentMech.addRecord("CreditCard");
        GenericValue creditCard = (GenericValue) context.get("creditCard");

        recCreditCard.setFieldString("Number", creditCard.getString("cardNumber"));
        String expDate = creditCard.getString("expireDate");

        recCreditCard.setFieldExpirationDate("Expires", expDate.substring(0, 3) + expDate.substring(5));     
        
        boolean enableCVM = UtilProperties.propertyValueEqualsIgnoreCase(configString, "payment.clearcommerce.enableCVM", "Y");
        String cvmCode = (String) context.get("cardSecurityCode");

        if (enableCVM && (cvmCode != null)) {
            if (cvmCode.length() < 4) {
                StringBuffer sb = new StringBuffer(cvmCode);

⌨️ 快捷键说明

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