这一版本的编写是在我上一次博客的基础上写的,有不懂得童鞋可以先看下我的原先在线支付的博客,熟悉下:http://blog.csdn.net/yulei_qq/article/details/45197543
第一步,建立工程,工程目录如下:
我在原先案例代码的基础上增加了个Servlet类 GateWayPayment 和一个JSP文件. home.jsp rescources文件夹下放的是测试用的一些文件,如测试证书 . 下面我贴下GateWayPayment .java 和 home.jsp 的源码.
GateWayPayment.java
- package com.unionpay.acp;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.unionpay.acp.sdk.SDKConfig;
- public class GateWayPayment extends HttpServlet {
- @Override
- public void init() throws ServletException {
- System.out.println("初始化Servlet.....");
- SDKConfig.getConfig().loadPropertiesFromSrc();
- super.init();
- }
- /**
- *
- */
- private static final long serialVersionUID = 7414643114833354891L;
- /**
- * The doGet method of the servlet. <br>
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- /**
- * 组装请求报文
- */
- Map<String, String> data = new HashMap<String, String>();
- // 版本号
- data.put("version", "5.0.0");
- // 字符集编码 默认"UTF-8"
- data.put("encoding", "UTF-8");
- // 签名方法 01 RSA
- data.put("signMethod", "01");
- // 交易类型 01-消费
- data.put("txnType", "01");
- // 交易子类型 01:自助消费 02:订购 03:分期付款
- data.put("txnSubType", "01");
- // 业务类型
- data.put("bizType", "000201");
- // 渠道类型,07-PC,08-手机
- data.put("channelType", "08");
- // 前台通知地址 ,控件接入方式无作用
- data.put("frontUrl", "http://localhost:8080/PayOnLine/acp_front_url.do");
- // 后台通知地址
- data.put("backUrl", "http://localhost:8080/PayOnLine/acp_back_url.do");
- // 接入类型,商户接入填0 0- 商户 , 1: 收单, 2:平台商户
- data.put("accessType", "0");
- // 商户号码,请改成自己的商户号
- data.put("merId", "777290058112538");
- // 商户订单号,8-40位数字字母
- data.put("orderId", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())); //自动生成订单号 ,生产系统一般是一个自动增长的序列.
- // 订单发送时间,取系统时间
- data.put("txnTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
- // 交易金额,单位分
- data.put("txnAmt", "749900");
- // 交易币种
- data.put("currencyCode", "156");
- // 请求方保留域,透传字段,查询、通知、对账文件中均会原样出现
- // data.put("reqReserved", "透传信息");
- // 订单描述,可不上送,上送时控件中会显示该信息
- // data.put("orderDesc", "订单描述");
- Map<String, String> submitFromData = DemoBase.signData(data);
- // 交易请求url 从配置文件读取
- String requestFrontUrl = SDKConfig.getConfig().getFrontRequestUrl();
- /**
- *动态创建表单
- */
- String html = DemoBase.createHtml(requestFrontUrl, submitFromData);
- // System.out.println(html);
- out.print(html);
- out.flush();
- out.close();
- }
- /**
- * The doPost method of the servlet. <br>
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
home.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'Home.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- 点击如下图片,购买商品!
- <div>
- <a href="http://localhost:8080/acptest1/servlet/GateWayPayment"><img alt="相机" src="images/photo.png"></a>
- </div>
- </body>
- </html>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
- <display-name>acptest1</display-name>
- <servlet>
- <servlet-name>BackRcvResponse</servlet-name>
- <servlet-class>com.unionpay.acp.BackRcvResponse</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>BackRcvResponse</servlet-name>
- <url-pattern>/acp_back_url.do</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>FrontRcvResponse</servlet-name>
- <servlet-class>com.unionpay.acp.FrontRcvResponse</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>FrontRcvResponse</servlet-name>
- <url-pattern>/acp_front_url.do</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>GateWayPayment</servlet-name>
- <servlet-class>com.unionpay.acp.GateWayPayment</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>GateWayPayment</servlet-name>
- <url-pattern>/servlet/GateWayPayment</url-pattern>
- </servlet-mapping>
- </web-app>
第二步:配置acp_sdk.properties文件
- ###### SDK 配置文件 配置文件中日志和证书的存放路径根据实际情况配置,交易地址和证书根据测试环境、生产环境配套配置
- ##########################交易发送地址配#############################
- ######(以下配置为认证环境:入网测试环境用,生产环境配置见文档说明)#######
- ##前台交易请求地址
- acpsdk.frontTransUrl=https://101.231.204.80:5000/gateway/api/frontTransReq.do
- #### 后台交易请求地址
- acpsdk.backTransUrl=https://101.231.204.80:5000/gateway/api/backTransReq.do
- ##后台交易请求地址(若为有卡交易配置该地址)
- acpsdk.cardTransUrl=https://101.231.204.80:5000/gateway/api/cardTransReq.do
- ##单笔查询请求地址
- acpsdk.singleQueryUrl=https://101.231.204.80:5000/gateway/api/queryTrans.do
- ##批量交易请求地址
- acpsdk.batchTransUrl=https://101.231.204.80:5000/gateway/api/batchTransReq.do
- ##文件传输类交易地址
- acpsdk.fileTransUrl=https://101.231.204.80:9080/
- ## app 交易请求地址
- acpsdk.appTransUrl=https://101.231.204.80:5000/gateway/api/appTransReq.do
- #########################签名证书配置 ################################
- ######(以下配置为测试环境:入网测试环境用,生产环境配置见下述文档说明))#######
- ##签名证书路径
- acpsdk.signCert.path=D:\certs\PM_700000000000001_acp.pfx
- ##签名证书密码
- acpsdk.signCert.pwd=000000
- ##签名证书类型
- acpsdk.signCert.type=PKCS12
- ##########################加密证书配置################################
- ##密码加密证书路径
- acpsdk.validateCert.dir=D:\certs\
第四步:根据配置文件制定的证书路径配置证书,证书在工程的resources文件夹下.
第5步:集成网关支付测试,网址:https://open.unionpay.com/ajweb/account/myProduct/integrate?prodId=1 .
当状态变成以下状态时
就可以启动Tomcat服务器,访问网页:
点击如上图片就进入:
。表示环境已经搭建好了。
注:各位测试的时候,以下代码需要更换,请填写自己的商户号.
- // 商户号码,请改成自己的商户号
- t("merId", "777290058112538");