Java对接银联支付

一、下载银联sdk;

二、将所需的jar包导入项目中;

三、将示例代码拷贝到项目中来;

四、修改SDKConfig.java中加载公钥和私钥的位置,用动态加载的方式:

path.substring(0,path.indexOf("WEB-INF")) + File.separator + "WEB-INF" + File.separator + "pub.key";

/**
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   xshu       2014-05-28       MPI基本参数工具类
 * =============================================================================
 */
package com.unionpay.acp.sdk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @ClassName SDKConfig
 * @Description acpsdk配置文件acp_sdk.properties配置信息类
 * @date 2016-7-22 下午4:04:55
 * 声明:以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己需要,按照技术文档编写。该代码仅供参考,不提供编码,性能,规范性等方面的保障
*/ public class SDKConfig { public static final String FILE_NAME = "acp_sdk.properties"; /** 前台请求URL. */ private String frontRequestUrl; /** 后台请求URL. */ private String backRequestUrl; /** 单笔查询 */ private String singleQueryUrl; /** 批量查询 */ private String batchQueryUrl; /** 批量交易 */ private String batchTransUrl; /** 文件传输 */ private String fileTransUrl; /** 签名证书路径. */ private String signCertPath; /** 签名证书密码. */ private String signCertPwd; /** 签名证书类型. */ private String signCertType; /** 加密公钥证书路径. */ private String encryptCertPath; /** 验证签名公钥证书目录. */ private String validateCertDir; /** 按照商户代码读取指定签名证书目录. */ private String signCertDir; /** 磁道加密证书路径. */ private String encryptTrackCertPath; /** 磁道加密公钥模数. */ private String encryptTrackKeyModulus; /** 磁道加密公钥指数. */ private String encryptTrackKeyExponent; /** 有卡交易. */ private String cardRequestUrl; /** app交易 */ private String appRequestUrl; /** 证书使用模式(单证书/多证书) */ private String singleMode; /** 安全密钥(SHA256和SM3计算时使用) */ private String secureKey; /** 中级证书路径 */ private String middleCertPath; /** 根证书路径 */ private String rootCertPath; /** 是否验证验签证书CN,除了false都验 */ private boolean ifValidateCNName = true; /** 是否验证https证书,默认都不验 */ private boolean ifValidateRemoteCert = false; /** signMethod,没配按01吧 */ private String signMethod = "01"; /** version,没配按5.0.0 */ private String version = "5.0.0"; /** frontUrl */ private String frontUrl; /** backUrl */ private String backUrl; /*缴费相关地址*/ private String jfFrontRequestUrl; private String jfBackRequestUrl; private String jfSingleQueryUrl; private String jfCardRequestUrl; private String jfAppRequestUrl; private String qrcBackTransUrl; private String qrcB2cIssBackTransUrl; private String qrcB2cMerBackTransUrl; /** 配置文件中的前台URL常量. */ public static final String SDK_FRONT_URL = "acpsdk.frontTransUrl"; /** 配置文件中的后台URL常量. */ public static final String SDK_BACK_URL = "acpsdk.backTransUrl"; /** 配置文件中的单笔交易查询URL常量. */ public static final String SDK_SIGNQ_URL = "acpsdk.singleQueryUrl"; /** 配置文件中的批量交易查询URL常量. */ public static final String SDK_BATQ_URL = "acpsdk.batchQueryUrl"; /** 配置文件中的批量交易URL常量. */ public static final String SDK_BATTRANS_URL = "acpsdk.batchTransUrl"; /** 配置文件中的文件类交易URL常量. */ public static final String SDK_FILETRANS_URL = "acpsdk.fileTransUrl"; /** 配置文件中的有卡交易URL常量. */ public static final String SDK_CARD_URL = "acpsdk.cardTransUrl"; /** 配置文件中的app交易URL常量. */ public static final String SDK_APP_URL = "acpsdk.appTransUrl"; /** 以下缴费产品使用,其余产品用不到,无视即可 */ // 前台请求地址 public static final String JF_SDK_FRONT_TRANS_URL= "acpsdk.jfFrontTransUrl"; // 后台请求地址 public static final String JF_SDK_BACK_TRANS_URL="acpsdk.jfBackTransUrl"; // 单笔查询请求地址 public static final String JF_SDK_SINGLE_QUERY_URL="acpsdk.jfSingleQueryUrl"; // 有卡交易地址 public static final String JF_SDK_CARD_TRANS_URL="acpsdk.jfCardTransUrl"; // App交易地址 public static final String JF_SDK_APP_TRANS_URL="acpsdk.jfAppTransUrl"; // 人到人 public static final String QRC_BACK_TRANS_URL="acpsdk.qrcBackTransUrl"; // 人到人 public static final String QRC_B2C_ISS_BACK_TRANS_URL="acpsdk.qrcB2cIssBackTransUrl"; // 人到人 public static final String QRC_B2C_MER_BACK_TRANS_URL="acpsdk.qrcB2cMerBackTransUrl"; /** 配置文件中签名证书路径常量. */ public static final String SDK_SIGNCERT_PATH = "acpsdk.signCert.path"; /** 配置文件中签名证书密码常量. */ public static final String SDK_SIGNCERT_PWD = "acpsdk.signCert.pwd"; /** 配置文件中签名证书类型常量. */ public static final String SDK_SIGNCERT_TYPE = "acpsdk.signCert.type"; /** 配置文件中密码加密证书路径常量. */ public static final String SDK_ENCRYPTCERT_PATH = "acpsdk.encryptCert.path"; /** 配置文件中磁道加密证书路径常量. */ public static final String SDK_ENCRYPTTRACKCERT_PATH = "acpsdk.encryptTrackCert.path"; /** 配置文件中磁道加密公钥模数常量. */ public static final String SDK_ENCRYPTTRACKKEY_MODULUS = "acpsdk.encryptTrackKey.modulus"; /** 配置文件中磁道加密公钥指数常量. */ public static final String SDK_ENCRYPTTRACKKEY_EXPONENT = "acpsdk.encryptTrackKey.exponent"; /** 配置文件中验证签名证书目录常量. */ public static final String SDK_VALIDATECERT_DIR = "acpsdk.validateCert.dir"; /** 配置文件中是否加密cvn2常量. */ public static final String SDK_CVN_ENC = "acpsdk.cvn2.enc"; /** 配置文件中是否加密cvn2有效期常量. */ public static final String SDK_DATE_ENC = "acpsdk.date.enc"; /** 配置文件中是否加密卡号常量. */ public static final String SDK_PAN_ENC = "acpsdk.pan.enc"; /** 配置文件中证书使用模式 */ public static final String SDK_SINGLEMODE = "acpsdk.singleMode"; /** 配置文件中安全密钥 */ public static final String SDK_SECURITYKEY = "acpsdk.secureKey"; /** 配置文件中根证书路径常量 */ public static final String SDK_ROOTCERT_PATH = "acpsdk.rootCert.path"; /** 配置文件中根证书路径常量 */ public static final String SDK_MIDDLECERT_PATH = "acpsdk.middleCert.path"; /** 配置是否需要验证验签证书CN,除了false之外的值都当true处理 */ public static final String SDK_IF_VALIDATE_CN_NAME = "acpsdk.ifValidateCNName"; /** 配置是否需要验证https证书,除了true之外的值都当false处理 */ public static final String SDK_IF_VALIDATE_REMOTE_CERT = "acpsdk.ifValidateRemoteCert"; /** signmethod */ public static final String SDK_SIGN_METHOD ="acpsdk.signMethod"; /** version */ public static final String SDK_VERSION = "acpsdk.version"; /** 后台通知地址 */ public static final String SDK_BACKURL = "acpsdk.backUrl"; /** 前台通知地址 */ public static final String SDK_FRONTURL = "acpsdk.frontUrl"; /** 操作对象. */ private static SDKConfig config = new SDKConfig(); /** 属性文件对象. */ private Properties properties; private SDKConfig() { super(); } /** * 获取config对象. * @return */ public static SDKConfig getConfig() { return config; } /** * 从properties文件加载 * * @param rootPath * 不包含文件名的目录. */ public void loadPropertiesFromPath(String rootPath) { if (StringUtils.isNotBlank(rootPath)) { LogUtil.writeLog("从路径读取配置文件: " + rootPath+File.separator+FILE_NAME); File file = new File(rootPath + File.separator + FILE_NAME); InputStream in = null; if (file.exists()) { try { in = new FileInputStream(file); properties = new Properties(); properties.load(in); loadProperties(properties); } catch (FileNotFoundException e) { LogUtil.writeErrorLog(e.getMessage(), e); } catch (IOException e) { LogUtil.writeErrorLog(e.getMessage(), e); } finally { if (null != in) { try { in.close(); } catch (IOException e) { LogUtil.writeErrorLog(e.getMessage(), e); } } } } else { // 由于此时可能还没有完成LOG的加载,因此采用标准输出来打印日志信息 LogUtil.writeErrorLog(rootPath + FILE_NAME + "不存在,加载参数失败"); } } else { loadPropertiesFromSrc(); } } /** * 从classpath路径下加载配置参数 */ public void loadPropertiesFromSrc() { InputStream in = null; try { LogUtil.writeLog("从classpath: " +SDKConfig.class.getClassLoader().getResource("").getPath()+" 获取属性文件"+FILE_NAME); in = SDKConfig.class.getClassLoader().getResourceAsStream(FILE_NAME); if (null != in) { properties = new Properties(); try { properties.load(in); } catch (IOException e) { throw e; } } else { LogUtil.writeErrorLog(FILE_NAME + "属性文件未能在classpath指定的目录下 "+SDKConfig.class.getClassLoader().getResource("").getPath()+" 找到!"); return; } loadProperties(properties); } catch (IOException e) { LogUtil.writeErrorLog(e.getMessage(), e); } finally { if (null != in) { try { in.close(); } catch (IOException e) { LogUtil.writeErrorLog(e.getMessage(), e); } } } } /** * 根据传入的 {@link #load(java.util.Properties)}对象设置配置参数 * * @param pro */ public void loadProperties(Properties pro) { LogUtil.writeLog("开始从属性文件中加载配置项"); String value = null; value = pro.getProperty(SDK_SIGNCERT_PATH); if (!SDKUtil.isEmpty(value)) { this.signCertPath = value.trim(); LogUtil.writeLog("配置项:私钥签名证书路径==>"+SDK_SIGNCERT_PATH +"==>"+ value+" 已加载"); } value = pro.getProperty(SDK_SIGNCERT_PWD); if (!SDKUtil.isEmpty(value)) { this.signCertPwd = value.trim(); LogUtil.writeLog("配置项:私钥签名证书密码==>"+SDK_SIGNCERT_PWD +" 已加载"); } value = pro.getProperty(SDK_SIGNCERT_TYPE); if (!SDKUtil.isEmpty(value)) { this.signCertType = value.trim(); LogUtil.writeLog("配置项:私钥签名证书类型==>"+SDK_SIGNCERT_TYPE +"==>"+ value+" 已加载"); } value = pro.getProperty(SDK_ENCRYPTCERT_PATH); if (!SDKUtil.isEmpty(value)) { this.encryptCertPath = value.trim(); LogUtil.writeLog("配置项:敏感信息加密证书==>"+SDK_ENCRYPTCERT_PATH +"==>"+ value+" 已加载"); } value = pro.getProperty(SDK_VALIDATECERT_DIR); if (!SDKUtil.isEmpty(value)) { this.validateCertDir = value.trim(); LogUtil.writeLog("配置项:验证签名证书路径(这里配置的是目录,不要指定到公钥文件)==>"+SDK_VALIDATECERT_DIR +"==>"+ value+" 已加载"); } value = pro.getProperty(SDK_FRONT_URL); if (!SDKUtil.isEmpty(value)) { this.frontRequestUrl = value.trim(); } value = pro.getProperty(SDK_BACK_URL); if (!SDKUtil.isEmpty(value)) { this.backRequestUrl = value.trim(); } value = pro.getProperty(SDK_BATQ_URL); if (!SDKUtil.isEmpty(value)) { this.batchQueryUrl = value.trim(); } value = pro.getProperty(SDK_BATTRANS_URL); if (!SDKUtil.isEmpty(value)) { this.batchTransUrl = value.trim(); } value = pro.getProperty(SDK_FILETRANS_URL); if (!SDKUtil.isEmpty(value)) { this.fileTransUrl = value.trim(); } value = pro.getProperty(SDK_SIGNQ_URL); if (!SDKUtil.isEmpty(value)) { this.singleQueryUrl = value.trim(); } value = pro.getProperty(SDK_CARD_URL); if (!SDKUtil.isEmpty(value)) { this.cardRequestUrl = value.trim(); } value = pro.getProperty(SDK_APP_URL); if (!SDKUtil.isEmpty(value)) { this.appRequestUrl = value.trim(); } value = pro.getProperty(SDK_ENCRYPTTRACKCERT_PATH); if (!SDKUtil.isEmpty(value)) { this.encryptTrackCertPath = value.trim(); } value = pro.getProperty(SDK_SECURITYKEY); if (!SDKUtil.isEmpty(value)) { this.secureKey = value.trim(); } value = pro.getProperty(SDK_ROOTCERT_PATH); if (!SDKUtil.isEmpty(value)) { this.rootCertPath = value.trim(); } value = pro.getProperty(SDK_MIDDLECERT_PATH); if (!SDKUtil.isEmpty(value)) { this.middleCertPath = value.trim(); } /**缴费部分**/ value = pro.getProperty(JF_SDK_FRONT_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.jfFrontRequestUrl = value.trim(); } value = pro.getProperty(JF_SDK_BACK_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.jfBackRequestUrl = value.trim(); } value = pro.getProperty(JF_SDK_SINGLE_QUERY_URL); if (!SDKUtil.isEmpty(value)) { this.jfSingleQueryUrl = value.trim(); } value = pro.getProperty(JF_SDK_CARD_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.jfCardRequestUrl = value.trim(); } value = pro.getProperty(JF_SDK_APP_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.jfAppRequestUrl = value.trim(); } value = pro.getProperty(QRC_BACK_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.qrcBackTransUrl = value.trim(); } value = pro.getProperty(QRC_B2C_ISS_BACK_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.qrcB2cIssBackTransUrl = value.trim(); } value = pro.getProperty(QRC_B2C_MER_BACK_TRANS_URL); if (!SDKUtil.isEmpty(value)) { this.qrcB2cMerBackTransUrl = value.trim(); } value = pro.getProperty(SDK_ENCRYPTTRACKKEY_EXPONENT); if (!SDKUtil.isEmpty(value)) { this.encryptTrackKeyExponent = value.trim(); } value = pro.getProperty(SDK_ENCRYPTTRACKKEY_MODULUS); if (!SDKUtil.isEmpty(value)) { this.encryptTrackKeyModulus = value.trim(); } value = pro.getProperty(SDK_IF_VALIDATE_CN_NAME); if (!SDKUtil.isEmpty(value)) { if( SDKConstants.FALSE_STRING.equals(value.trim())) this.ifValidateCNName = false; } value = pro.getProperty(SDK_IF_VALIDATE_REMOTE_CERT); if (!SDKUtil.isEmpty(value)) { if( SDKConstants.TRUE_STRING.equals(value.trim())) this.ifValidateRemoteCert = true; } value = pro.getProperty(SDK_SIGN_METHOD); if (!SDKUtil.isEmpty(value)) { this.signMethod = value.trim(); } value = pro.getProperty(SDK_SIGN_METHOD); if (!SDKUtil.isEmpty(value)) { this.signMethod = value.trim(); } value = pro.getProperty(SDK_VERSION); if (!SDKUtil.isEmpty(value)) { this.version = value.trim(); } value = pro.getProperty(SDK_FRONTURL); if (!SDKUtil.isEmpty(value)) { this.frontUrl = value.trim(); } value = pro.getProperty(SDK_BACKURL); if (!SDKUtil.isEmpty(value)) { this.backUrl = value.trim(); } } public String getFrontRequestUrl() { return frontRequestUrl; } public void setFrontRequestUrl(String frontRequestUrl) { this.frontRequestUrl = frontRequestUrl; } public String getBackRequestUrl() { return backRequestUrl; } public void setBackRequestUrl(String backRequestUrl) { this.backRequestUrl = backRequestUrl; } public String getSignCertPath() { return signCertPath; } public void setSignCertPath(String signCertPath) { this.signCertPath = signCertPath; } public String getSignCertPwd() { return signCertPwd; } public void setSignCertPwd(String signCertPwd) { this.signCertPwd = signCertPwd; } public String getSignCertType() { return signCertType; } public void setSignCertType(String signCertType) { this.signCertType = signCertType; } public String getEncryptCertPath() { return encryptCertPath; } public void setEncryptCertPath(String encryptCertPath) { this.encryptCertPath = encryptCertPath; } public String getValidateCertDir() { return validateCertDir; } public void setValidateCertDir(String validateCertDir) { this.validateCertDir = validateCertDir; } public String getSingleQueryUrl() { return singleQueryUrl; } public void setSingleQueryUrl(String singleQueryUrl) { this.singleQueryUrl = singleQueryUrl; } public String getBatchQueryUrl() { return batchQueryUrl; } public void setBatchQueryUrl(String batchQueryUrl) { this.batchQueryUrl = batchQueryUrl; } public String getBatchTransUrl() { return batchTransUrl; } public void setBatchTransUrl(String batchTransUrl) { this.batchTransUrl = batchTransUrl; } public String getFileTransUrl() { return fileTransUrl; } public void setFileTransUrl(String fileTransUrl) { this.fileTransUrl = fileTransUrl; } public String getSignCertDir() { return signCertDir; } public void setSignCertDir(String signCertDir) { this.signCertDir = signCertDir; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public String getCardRequestUrl() { return cardRequestUrl; } public void setCardRequestUrl(String cardRequestUrl) { this.cardRequestUrl = cardRequestUrl; } public String getAppRequestUrl() { return appRequestUrl; } public void setAppRequestUrl(String appRequestUrl) { this.appRequestUrl = appRequestUrl; } public String getEncryptTrackCertPath() { return encryptTrackCertPath; } public void setEncryptTrackCertPath(String encryptTrackCertPath) { this.encryptTrackCertPath = encryptTrackCertPath; } public String getJfFrontRequestUrl() { return jfFrontRequestUrl; } public void setJfFrontRequestUrl(String jfFrontRequestUrl) { this.jfFrontRequestUrl = jfFrontRequestUrl; } public String getJfBackRequestUrl() { return jfBackRequestUrl; } public void setJfBackRequestUrl(String jfBackRequestUrl) { this.jfBackRequestUrl = jfBackRequestUrl; } public String getJfSingleQueryUrl() { return jfSingleQueryUrl; } public void setJfSingleQueryUrl(String jfSingleQueryUrl) { this.jfSingleQueryUrl = jfSingleQueryUrl; } public String getJfCardRequestUrl() { return jfCardRequestUrl; } public void setJfCardRequestUrl(String jfCardRequestUrl) { this.jfCardRequestUrl = jfCardRequestUrl; } public String getJfAppRequestUrl() { return jfAppRequestUrl; } public void setJfAppRequestUrl(String jfAppRequestUrl) { this.jfAppRequestUrl = jfAppRequestUrl; } public String getSingleMode() { return singleMode; } public void setSingleMode(String singleMode) { this.singleMode = singleMode; } public String getEncryptTrackKeyExponent() { return encryptTrackKeyExponent; } public void setEncryptTrackKeyExponent(String encryptTrackKeyExponent) { this.encryptTrackKeyExponent = encryptTrackKeyExponent; } public String getEncryptTrackKeyModulus() { return encryptTrackKeyModulus; } public void setEncryptTrackKeyModulus(String encryptTrackKeyModulus) { this.encryptTrackKeyModulus = encryptTrackKeyModulus; } public String getSecureKey() { return secureKey; } public void setSecureKey(String securityKey) { this.secureKey = securityKey; } public String getMiddleCertPath() { return middleCertPath; } public void setMiddleCertPath(String middleCertPath) { this.middleCertPath = middleCertPath; } public boolean isIfValidateCNName() { return ifValidateCNName; } public void setIfValidateCNName(boolean ifValidateCNName) { this.ifValidateCNName = ifValidateCNName; } public boolean isIfValidateRemoteCert() { return ifValidateRemoteCert; } public void setIfValidateRemoteCert(boolean ifValidateRemoteCert) { this.ifValidateRemoteCert = ifValidateRemoteCert; } public String getSignMethod() { return signMethod; } public void setSignMethod(String signMethod) { this.signMethod = signMethod; } public String getQrcBackTransUrl() { return qrcBackTransUrl; } public void setQrcBackTransUrl(String qrcBackTransUrl) { this.qrcBackTransUrl = qrcBackTransUrl; } public String getQrcB2cIssBackTransUrl() { return qrcB2cIssBackTransUrl; } public void setQrcB2cIssBackTransUrl(String qrcB2cIssBackTransUrl) { this.qrcB2cIssBackTransUrl = qrcB2cIssBackTransUrl; } public String getQrcB2cMerBackTransUrl() { return qrcB2cMerBackTransUrl; } public void setQrcB2cMerBackTransUrl(String qrcB2cMerBackTransUrl) { this.qrcB2cMerBackTransUrl = qrcB2cMerBackTransUrl; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getFrontUrl() { return frontUrl; } public void setFrontUrl(String frontUrl) { this.frontUrl = frontUrl; } public String getBackUrl() { return backUrl; } public void setBackUrl(String backUrl) { this.backUrl = backUrl; } public String getRootCertPath() { return rootCertPath; } public void setRootCertPath(String rootCertPath) { this.rootCertPath = rootCertPath; } }

五、其他接口如果有操作权限,按照文档填充代码:

package com.bdt.etc.recharge;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.bdt.etc.recharge.sdk.SDKConstants;
import com.dev.util.secret.AesSecret;

public class UnionpayRequstPar
{
	
	public static final String host;
	//pc端host
	public static final String host2;
	
	//默认配置的是UTF-8
	public static String encoding_UTF8 = "UTF-8";
	
	public static String encoding_GBK = "GBK";
	//全渠道固定值
	public static String version = "5.0.0";

	//回调host+"/rechargeCallBack"
	public static final String backUrl;//受理方和发卡方自选填写的域[O]--后台通知地址
	
	//后台服务对应的写法参照 BackRcvResponse.java
	//public static String backUrl = "http://110.249.159.218:9090/Unionpay_AppServer/BackResponse";//受理方和发卡方自选填写的域[O]--后台通知地址
	static{
		InputStream in = UnionpayRequstPar. class .getResourceAsStream("/acp_sdk.properties");
		Properties prop = new Properties();    
		try {
			prop.load(in);
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		host = prop.getProperty("backUrl");
		host2 = prop.getProperty("pcBackUrl");
		backUrl = host;
		
	}
	// 商户发送交易时间 格式:YYYYMMDDhhmmss
	public static String getCurrentTime() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	// AN8..40 商户订单号,不能含"-"或"_"
	public static String getOrderId() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
   /**
	 * 组装请求,返回报文字符串用于显示
	 * @param data
	 * @return
	 */
    public static String genHtmlResult(Map data){

    	TreeMap tree = new TreeMap();
		Iterator> it = data.entrySet().iterator();
		while (it.hasNext()) {
			Entry en = it.next();
			tree.put(en.getKey(), en.getValue());
		}
		it = tree.entrySet().iterator();
		StringBuffer sf = new StringBuffer();
		while (it.hasNext()) {
			Entry en = it.next();
			String key = en.getKey(); 
			String value =  en.getValue();
			if("respCode".equals(key)){
				sf.append(""+key + SDKConstants.EQUAL + value+"
"); }else sf.append(key + SDKConstants.EQUAL + value+"
"); } return sf.toString(); } /** * 功能:解析全渠道商户对账文件中的ZM文件并以List方式返回 * 适用交易:对账文件下载后对文件的查看 * @param filePath ZM文件全路径 * @return 包含每一笔交易中 序列号 和 值 的map序列 */ @SuppressWarnings("rawtypes") public static List parseZMFile(String filePath){ int lengthArray[] = {3,11,11,6,10,19,12,4,2,21,2,32,2,6,10,13,13,4,15,2,2,6,2,4,32,1,21,15,1,15,32,13,13,8,32,13,13,12,2,1,131}; return parseFile(filePath,lengthArray); } /** * 功能:解析全渠道商户对账文件中的ZME文件并以List方式返回 * 适用交易:对账文件下载后对文件的查看 * @param filePath ZME文件全路径 * @return 包含每一笔交易中 序列号 和 值 的map序列 */ @SuppressWarnings("rawtypes") public static List parseZMEFile(String filePath){ int lengthArray[] = {3,11,11,6,10,19,12,4,2,21,2,32,2,6,10,13,13,4,15,2,2,6,2,4,32,1,21,15,1,15,32,13,13,8,32,13,13,12,2,1,131}; return parseFile(filePath,lengthArray); } /** * 功能:解析全渠道商户 ZM,ZME对账文件 * @param filePath * @param lengthArray 参照《全渠道平台接入接口规范 第3部分 文件接口》 全渠道商户对账文件 6.1 ZM文件和6.2 ZME 文件 格式的类型长度组成int型数组 * @return */ @SuppressWarnings("rawtypes") private static List parseFile(String filePath,int lengthArray[]){ List ZmDataList = new ArrayList(); try { String encoding="UTF-8"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ //解析的结果MAP,key为对账文件列序号,value为解析的值 Map ZmDataMap = new LinkedHashMap(); //左侧游标 int leftIndex = 0; //右侧游标 int rightIndex = 0; for(int i=0;i ZmDataMapTmp = ZmDataList.get(i); for(Iterator it = ZmDataMapTmp.keySet().iterator();it.hasNext();){ Integer key = it.next(); String value = ZmDataMapTmp.get(key); System.out.println("序号:"+ key + " 值: '"+ value +"'"); } }*/ return ZmDataList; } public static HashMap getIosRechargeAskPara(String orderId,String txnAmt,String txnTime,HashMap reqReserved) { HashMapmap=new HashMap(); map.put("version", version); //版本号,全渠道默认值 map.put("encoding", encoding_UTF8); map.put("signMethod", "01"); //签名方法 map.put("txnType", "01"); //交易类型 ,01:消费 map.put("txnSubType", "01"); //交易子类型, 01:自助消费 map.put("bizType", "000201"); map.put("channelType", "08"); /***商户接入参数***/ map.put("merId", "898130173991161"); //商户号码,请改成自己申请的正式商户号或者open上注册得来的777测试商户号 map.put("accessType", "0"); map.put("orderId", orderId); map.put("txnTime", txnTime); map.put("accType", "01"); map.put("txnAmt", txnAmt); map.put("currencyCode", "156"); map.put("reqReserved",AesSecret.encode(JSON.toJSONString(reqReserved))); map.put("backUrl", backUrl); return map; } public static HashMap getRechargeAskPara(String orderId,String txnAmt,HashMap reqReserved,String txnTime,String host) { HashMapmap=new HashMap(); map.put("orderId",orderId); map.put("txnAmt", txnAmt); //map.put("cardNum",cardNum); map.put("reqReserved",AesSecret.encode(JSON.toJSONString(reqReserved))); //请求方保留域,如需使用请启用即可;透传字段(可以实现商户自定义参数的追踪)本交易的后台通知,对本交易的交易状态查询交易、对账文件中均会原样返回,商户可以按需上传,长度为1-1024个字节。出现&={}[]符号时可能导致查询接口应答报文解析失败,建议尽量只传字母数字并使用|分割,或者可以最外层做一次base64编码(base64编码之后出现的等号不会导致解析失败可以不用管)。 /***银联全渠道系统,产品参数,除了encoding自行选择外其他不需修改***/ map.put("version", version); //版本号,全渠道默认值 map.put("encoding", encoding_UTF8); //字符集编码,可以使用UTF-8,GBK两种方式 map.put("signMethod", "01"); //签名方法 map.put("txnType", "01"); //交易类型 ,01:消费 map.put("txnSubType", "01"); //交易子类型, 01:自助消费 map.put("bizType", "000201"); //业务类型,B2C网关支付,手机wap支付 map.put("channelType", "08"); //渠道类型,这个字段区分B2C网关支付和手机wap支付;07:PC,平板 08:手机 /***商户接入参数***/ map.put("merId", "898130173991161"); //商户号码,请改成自己申请的正式商户号或者open上注册得来的777测试商户号 map.put("accessType", "0"); //接入类型,0:直连商户 map.put("txnTime",txnTime); //订单发送时间,取系统时间,格式为YYYYMMDDhhmmss,必须取当前时间,否则会报txnTime无效 map.put("currencyCode", "156"); //交易币种(境内商户一般是156 人民币) //交易金额,单位分,不要带小数点 //前台通知地址 (需设置为外网能访问 http https均可),支付成功后的页面 点击“返回商户”按钮的时候将异步通知报文post到该地址 //如果想要实现过几秒中自动跳转回商户页面权限,需联系银联业务申请开通自动返回商户权限 //异步通知参数详见open.unionpay.com帮助中心 下载 产品接口规范 网关支付产品接口规范 消费交易 商户通知 map.put("frontUrl", host); //后台通知地址(需设置为【外网】能访问 http https均可),支付成功后银联会自动将异步通知报文post到商户上送的该地址,失败的交易银联不会发送后台通知 //后台通知参数详见open.unionpay.com帮助中心 下载 产品接口规范 网关支付产品接口规范 消费交易 商户通知 //注意:1.需设置为外网能访问,否则收不到通知 2.http https均可 3.收单后台通知后需要10秒内返回http200或302状态码 // 4.如果银联通知服务器发送通知后10秒内未收到返回状态码或者应答码非http200,那么银联会间隔一段时间再次发送。总共发送5次,每次的间隔时间为0,1,2,4分钟。 // 5.后台通知地址如果上送了带有?的参数,例如:http://abc/web?a=b&c=d 在后台通知处理程序验证签名之前需要编写逻辑将这些字段去掉再验签,否则将会验签失败 map.put("backUrl", backUrl); // 订单超时时间。 // 超过此时间后,除网银交易外,其他交易银联系统会拒绝受理,提示超时。 跳转银行网银交易如果超时后交易成功,会自动退款,大约5个工作日金额返还到持卡人账户。 // 此时间建议取支付时的北京时间加15分钟。 // 超过超时时间调查询接口应答origRespCode不是A6或者00的就可以判断为失败。 map.put("payTimeout", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime() + 15 * 60 * 1000)); return map; } public static HashMap getPcRechargePara(String orderId,String txnAmt,HashMap reqReserved,String txnTime,String host) { HashMapmap=new HashMap(); map.put("orderId",orderId); map.put("txnAmt", txnAmt); //map.put("cardNum",cardNum); map.put("reqReserved",AesSecret.encode(JSON.toJSONString(reqReserved))); //请求方保留域,如需使用请启用即可;透传字段(可以实现商户自定义参数的追踪)本交易的后台通知,对本交易的交易状态查询交易、对账文件中均会原样返回,商户可以按需上传,长度为1-1024个字节。出现&={}[]符号时可能导致查询接口应答报文解析失败,建议尽量只传字母数字并使用|分割,或者可以最外层做一次base64编码(base64编码之后出现的等号不会导致解析失败可以不用管)。 /***银联全渠道系统,产品参数,除了encoding自行选择外其他不需修改***/ map.put("version", version); //版本号,全渠道默认值 map.put("encoding", encoding_UTF8); //字符集编码,可以使用UTF-8,GBK两种方式 map.put("signMethod", "01"); //签名方法 map.put("txnType", "01"); //交易类型 ,01:消费 map.put("txnSubType", "01"); //交易子类型, 01:自助消费 map.put("bizType", "000201"); //业务类型,B2C网关支付,手机wap支付 map.put("channelType", "08"); //渠道类型,这个字段区分B2C网关支付和手机wap支付;07:PC,平板 08:手机 /***商户接入参数***/ map.put("merId", "898130173991161"); //商户号码,请改成自己申请的正式商户号或者open上注册得来的777测试商户号 map.put("accessType", "0"); //接入类型,0:直连商户 map.put("txnTime",txnTime); //订单发送时间,取系统时间,格式为YYYYMMDDhhmmss,必须取当前时间,否则会报txnTime无效 map.put("currencyCode", "156"); //交易币种(境内商户一般是156 人民币) //交易金额,单位分,不要带小数点 //前台通知地址 (需设置为外网能访问 http https均可),支付成功后的页面 点击“返回商户”按钮的时候将异步通知报文post到该地址 //如果想要实现过几秒中自动跳转回商户页面权限,需联系银联业务申请开通自动返回商户权限 //异步通知参数详见open.unionpay.com帮助中心 下载 产品接口规范 网关支付产品接口规范 消费交易 商户通知 map.put("frontUrl", host); //后台通知地址(需设置为【外网】能访问 http https均可),支付成功后银联会自动将异步通知报文post到商户上送的该地址,失败的交易银联不会发送后台通知 //后台通知参数详见open.unionpay.com帮助中心 下载 产品接口规范 网关支付产品接口规范 消费交易 商户通知 //注意:1.需设置为外网能访问,否则收不到通知 2.http https均可 3.收单后台通知后需要10秒内返回http200或302状态码 // 4.如果银联通知服务器发送通知后10秒内未收到返回状态码或者应答码非http200,那么银联会间隔一段时间再次发送。总共发送5次,每次的间隔时间为0,1,2,4分钟。 // 5.后台通知地址如果上送了带有?的参数,例如:http://abc/web?a=b&c=d 在后台通知处理程序验证签名之前需要编写逻辑将这些字段去掉再验签,否则将会验签失败 map.put("backUrl", backUrl); // 订单超时时间。 // 超过此时间后,除网银交易外,其他交易银联系统会拒绝受理,提示超时。 跳转银行网银交易如果超时后交易成功,会自动退款,大约5个工作日金额返还到持卡人账户。 // 此时间建议取支付时的北京时间加15分钟。 // 超过超时时间调查询接口应答origRespCode不是A6或者00的就可以判断为失败。 map.put("payTimeout", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date().getTime() + 15 * 60 * 1000)); return map; } }

你可能感兴趣的:(第三方支付)