易宝网上在线支付


在线支付的步骤:

1.index.jsp:是个表单界面,有你要支付的订单号和支付金额,并叫你选择银行开始支付行为。点击“确认支付”后跳转至PaymentRequest

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="PaymentRequest" method="post" name="paymentform">
订单号:<INPUT TYPE="text" NAME="orderid"> 应付金额:¥<INPUT TYPE="text" NAME="amount" size="6">元 <br>
请您选择在线支付银行 <br>

<input TYPE="radio" NAME="pd_FrpId" value="CMBCHINA-NET">招商银行 <br>
<input TYPE="radio" NAME="pd_FrpId" value="ICBC-NET">工商银行 <br>
<input TYPE="radio" NAME="pd_FrpId" value="ABC-NET">农业银行 <br>
<input TYPE="radio" NAME="pd_FrpId" value="CCB-NET">建设银行 <br>
<input type="submit" value=" 确认支付 " />
</form>
</body>
</html>


2.PaymentRequestl:从index.jsp表单界面获取数据,再将其设置为request的属性,最后跳转至connection.jsp页面。

package wang.payment.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import wang.payment.utils.ConfigInfo;
import wang.payment.utils.PaymentUtil;


/**
 * Servlet implementation class PaymentRequest
 */
public class PaymentRequest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaymentRequest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String orderid = request.getParameter("orderid");//订单号
		String amount = request.getParameter("amount");//支付金额
		String pd_FrpId = request.getParameter("pd_FrpId");//选择的支付银行
		String p1_MerId = ConfigInfo.getValue("p1_MerId");
		String keyValue = ConfigInfo.getValue("keyValue");
		String merchantCallbackURL = ConfigInfo.getValue("merchantCallbackURL");		
		String messageType = "Buy"; // 请求命令,在线支付固定为Buy
		String currency = "CNY"; // 货币单位
		String productDesc = ""; // 商品描述
		String productCat = ""; // 商品种类
		String productId = ""; // 商品ID
		String addressFlag = "0"; // 需要填写送货信息 0:不需要 1:需要		
		String sMctProperties = ""; // 商家扩展信息
		String pr_NeedResponse = "0"; // 应答机制
		String md5hmac = PaymentUtil.buildHmac(messageType, p1_MerId, orderid, amount, currency,
				productId, productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, 
				pd_FrpId, pr_NeedResponse, keyValue);
		
		request.setAttribute("messageType", messageType);
		request.setAttribute("merchantID", p1_MerId);
		request.setAttribute("orderId", orderid);
		request.setAttribute("amount", amount);
		request.setAttribute("currency", currency);
		request.setAttribute("productId", productId);
		request.setAttribute("productCat", productCat);
		request.setAttribute("productDesc", productDesc);
		request.setAttribute("merchantCallbackURL", merchantCallbackURL);
		request.setAttribute("addressFlag", addressFlag);
		request.setAttribute("sMctProperties", sMctProperties);
		request.setAttribute("frpId", pd_FrpId);
		request.setAttribute("pr_NeedResponse", pr_NeedResponse);
		request.setAttribute("hmac", md5hmac);
		
		request.getRequestDispatcher("connection.jsp").forward(request, response);
			
	}

}


3.connection.jsp:向银行发起支付请求。从request属性通过EL表达式获取数据,然后通过隐藏表单发给易宝网。

<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>发起支付请求</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body onload="javascript:document.forms[0].submit()">
  	<!-- http://tech.yeepay.com:8080/robot/debug.action -->
	<form name="yeepay" action="https://www.yeepay.com/app-merchant-proxy/node" method='post'>	
		<input type='hidden' name='p0_Cmd'   value="${messageType}"> <!-- 请求命令,在线支付固定为Buy -->
		<input type='hidden' name='p1_MerId' value="${merchantID}"> <!-- 商家ID -->
		<input type="hidden" name="p2_Order" value="${orderId}"> <!-- 商家的交易定单号 -->
		<input type='hidden' name='p3_Amt'   value="${amount}"> <!-- 订单金额 -->
		<input type='hidden' name='p4_Cur'   value="${currency}"> <!-- 货币单位 -->
		<input type='hidden' name='p5_Pid'   value="${productId}"> <!-- 商品ID -->
		<input type='hidden' name='p6_Pcat'  value="${productCat}"> <!-- 商品种类 -->
		<input type='hidden' name='p7_Pdesc' value="${productDesc}"> <!-- 商品描述 -->
		<input type='hidden' name='p8_Url'   value="${merchantCallbackURL}"> <!-- 交易结果通知地址 -->
		<input type='hidden' name='p9_SAF'   value="${addressFlag}"> <!-- 需要填写送货信息 0:不需要 1:需要 -->
		<input type='hidden' name='pa_MP'    value="${sMctProperties}"> <!-- 商家扩展信息 -->
		<input type='hidden' name='pd_FrpId' value="${frpId}"> <!-- 银行ID -->
		<!-- 应答机制 为“1”: 需要应答机制;为“0”: 不需要应答机制 -->
		<input type="hidden" name="pr_NeedResponse"  value="0">
		<input type='hidden' name='hmac' value="${hmac}"><!-- MD5-hmac验证码 -->
	</form>
  </body>
</html>

4.银行完成支付之后,会给用户返回一些订单处理信息。这时我们要获取这些信息,并判断支付是否成功,若成功,则显示成功页面。

package cn.itcast.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.utils.ConfigInfo;
import cn.itcast.utils.PanymentUtil;
/**
 * 响应银行支付结果请求
 * @author 传智播客
 *
 */
public class PaymentResutlResponse extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		String merchantID = ConfigInfo.getValue("p1_MerId"); // 商家ID
		String keyValue = ConfigInfo.getValue("keyValue"); // 商家密钥
		
		String sCmd = request.getParameter("r0_Cmd"); //业务类型
		String sResultCode = request.getParameter("r1_Code"); //扣款结果,该字段值为1时表示扣款成功.
		String sTrxId = request.getParameter("r2_TrxId"); //YeePay易宝交易订单号
		String amount = request.getParameter("r3_Amt");//扣款金额,交易结束后,YeePay易宝交易系统将实际扣款金额返回给商户
		String currency = request.getParameter("r4_Cur");//交易币种,人民币为CNY
		String productId = request.getParameter("r5_Pid");//商品ID
		String orderId = request.getParameter("r6_Order");//商户订单号
		String userId = request.getParameter("r7_Uid");//YeePay易宝会员ID
		String mp  = request.getParameter("r8_MP");//商户扩展信息,可以任意填写1K 的字符串,交易返回时将原样返回
		String bType = request.getParameter("r9_BType");//交易结果通知类型,1: 交易成功回调(浏览器重定向)2: 交易成功主动通知(服务器点对点通讯)
		String rb_BankId  = request.getParameter("rb_BankId");//支付银行
		String rp_PayDate = request.getParameter("rp_PayDate");//在银行支付时的时间
		String hmac = request.getParameter("hmac");//MD5交易签名
		
		boolean result = PanymentUtil.verifyCallback(hmac, merchantID, sCmd, sResultCode, sTrxId, amount,
				currency, productId, orderId, userId, mp, bType, keyValue);
		if(result){
			if("1".equals(sResultCode)){
				//你们这个地方应该把数据库中订单的支付状态设置成已经支付.
				String message = "订单号为:"+ orderId+ "的订单支付成功了";
				message += ",用户支付了"+ amount +"元";
				message +=",交易结果通知类型:";
				if("1".equals(bType)){
					 message += "浏览器重定向";
				}else if("2".equals(bType)){
					 message += "易宝支付网关后台程序通知";
				}
				message += ",易宝订单系统中的订单号为:"+ sTrxId;
				request.setAttribute("message", message);
			}else{
				request.setAttribute("message", "用户支付失败");
			}
		}else{
			request.setAttribute("message", "数据来源不合法");
		}
		request.getRequestDispatcher("/WEB-INF/page/paymentResult.jsp").forward(request, response);
	}

}


注:省略了对数据加密的类和读取配置文件的类(ConfigInfo)。

原文地址:https://www.cnblogs.com/wangerxiansheng/p/3836394.html