欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java网上图书商城(9)支付模块

程序员文章站 2024-03-11 19:39:07
本文实例为大家分享了java网上商城支付模块的具体代码,供大家参考,具体内容如下 1.支付准备 public string paymentpre(htt...

本文实例为大家分享了java网上商城支付模块的具体代码,供大家参考,具体内容如下

java网上图书商城(9)支付模块

1.支付准备

public string paymentpre(httpservletrequest req, httpservletresponse resp)
  throws servletexception, ioexception {
 req.setattribute("order", orderservice.load(req.getparameter("oid")));
 return "f:/jsps/order/pay.jsp";
}

 2.支付方法

public string payment(httpservletrequest req, httpservletresponse resp)
  throws servletexception, ioexception {
 properties props = new properties();
 props.load(this.getclass().getclassloader().getresourceasstream("payment.properties"));
 /*
  * 1. 准备13个参数
  */
 string p0_cmd = "buy";//业务类型,固定值buy
 string p1_merid = props.getproperty("p1_merid");//商号编码,在易宝的唯一标识
 string p2_order = req.getparameter("oid");//订单编码
 string p3_amt = "0.01";//支付金额
 string p4_cur = "cny";//交易币种,固定值cny
 string p5_pid = "";//商品名称
 string p6_pcat = "";//商品种类
 string p7_pdesc = "";//商品描述
 string p8_url = props.getproperty("p8_url");//在支付成功后,易宝会访问这个地址。
 string p9_saf = "";//送货地址
 string pa_mp = "";//扩展信息
 string pd_frpid = req.getparameter("yh");//支付通道
 string pr_needresponse = "1";//应答机制,固定值1
  
 /*
  * 2. 计算hmac
  * 需要13个参数
  * 需要keyvalue
  * 需要加密算法
  */
 string keyvalue = props.getproperty("keyvalue");
 string hmac = paymentutil.buildhmac(p0_cmd, p1_merid, p2_order, p3_amt,
   p4_cur, p5_pid, p6_pcat, p7_pdesc, p8_url, p9_saf, pa_mp,
   pd_frpid, pr_needresponse, keyvalue);
  
 /*
  * 3. 重定向到易宝的支付网关
  */
 stringbuilder sb = new stringbuilder("https://www.yeepay.com/app-merchant-proxy/node");
 sb.append("?").append("p0_cmd=").append(p0_cmd);
 sb.append("&").append("p1_merid=").append(p1_merid);
 sb.append("&").append("p2_order=").append(p2_order);
 sb.append("&").append("p3_amt=").append(p3_amt);
 sb.append("&").append("p4_cur=").append(p4_cur);
 sb.append("&").append("p5_pid=").append(p5_pid);
 sb.append("&").append("p6_pcat=").append(p6_pcat);
 sb.append("&").append("p7_pdesc=").append(p7_pdesc);
 sb.append("&").append("p8_url=").append(p8_url);
 sb.append("&").append("p9_saf=").append(p9_saf);
 sb.append("&").append("pa_mp=").append(pa_mp);
 sb.append("&").append("pd_frpid=").append(pd_frpid);
 sb.append("&").append("pr_needresponse=").append(pr_needresponse);
 sb.append("&").append("hmac=").append(hmac);
  
 resp.sendredirect(sb.tostring());
 return null;
}
 

3.回馈方法

当支付成功时,易宝会访问这里
用两种方法访问:
1. 引导用户的浏览器重定向(如果用户关闭了浏览器,就不能访问这里了)
2. 易宝的服务器会使用点对点通讯的方法访问这个方法。(必须回馈success,不然易宝服务器会一直调用这个方法)

public string back(httpservletrequest req, httpservletresponse resp)
  throws servletexception, ioexception {
 /*
  * 1. 获取12个参数
  */
 string p1_merid = req.getparameter("p1_merid");
 string r0_cmd = req.getparameter("r0_cmd");
 string r1_code = req.getparameter("r1_code");
 string r2_trxid = req.getparameter("r2_trxid");
 string r3_amt = req.getparameter("r3_amt");
 string r4_cur = req.getparameter("r4_cur");
 string r5_pid = req.getparameter("r5_pid");
 string r6_order = req.getparameter("r6_order");
 string r7_uid = req.getparameter("r7_uid");
 string r8_mp = req.getparameter("r8_mp");
 string r9_btype = req.getparameter("r9_btype");
 string hmac = req.getparameter("hmac");
 /*
  * 2. 获取keyvalue
  */
 properties props = new properties();
 props.load(this.getclass().getclassloader().getresourceasstream("payment.properties"));
 string keyvalue = props.getproperty("keyvalue");
 /*
  * 3. 调用paymentutil的校验方法来校验调用者的身份
  * >如果校验失败:保存错误信息,转发到msg.jsp
  * >如果校验通过:
  *  * 判断访问的方法是重定向还是点对点,如果要是重定向
  *  修改订单状态,保存成功信息,转发到msg.jsp
  *  * 如果是点对点:修改订单状态,返回success
  */
 boolean bool = paymentutil.verifycallback(hmac, p1_merid, r0_cmd, r1_code, r2_trxid,
   r3_amt, r4_cur, r5_pid, r6_order, r7_uid, r8_mp, r9_btype,
   keyvalue);
 if(!bool) {
  req.setattribute("code", "error");
  req.setattribute("msg", "无效的签名,支付失败!(你不是好人)");
  return "f:/jsps/msg.jsp";
 }
 if(r1_code.equals("1")) {
  orderservice.updatestatus(r6_order, 2);
  if(r9_btype.equals("1")) {
   req.setattribute("code", "success");
   req.setattribute("msg", "恭喜,支付成功!");
   return "f:/jsps/msg.jsp";    
  } else if(r9_btype.equals("2")) {
   resp.getwriter().print("success");
  }
 }
 return null;
}

4.pay.jsp

<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <title>pay.jsp</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">
 -->
 <link rel="stylesheet" type="text/css" href="<c:url value='/jsps/css/order/pay.css'/>">
 <script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script>
 
<script type="text/javascript">
$(function() {
 $("img").click(function() {
  $("#" + $(this).attr("name")).attr("checked", true);
 });
});
</script>
 </head>
 
 <body>
<div class="divcontent">
 <span class="spanprice">支付金额:</span><span class="price_t">¥${order.total }</span>
 <span class="spanoid">编号:${order.oid }</span>
</div>
<form action="<c:url value='/orderservlet'/>" method="post" id="form1" target="_top">
<input type="hidden" name="method" value="payment"/>
<input type="hidden" name="oid" value="${order.oid }"/>
<div class="divbank">
 <div class="divtext">选择网上银行</div>
 <div style="margin-left: 20px;">
  <div style="margin-bottom: 20px;">
  <input id="icbc-net-b2c" type="radio" name="yh" value="icbc-net-b2c" checked="checked"/>
  <img name="icbc-net-b2c" align="middle" src="<c:url value='/bank_img/icbc.bmp'/>"/>
   
  <input id="cmbchina-net-b2c" type="radio" name="yh" value="cmbchina-net-b2c"/>
  <img name="cmbchina-net-b2c" align="middle" src="<c:url value='/bank_img/cmb.bmp'/>"/>
 
  <input id="abc-net-b2c" type="radio" name="yh" value="abc-net-b2c"/>
  <img name="abc-net-b2c" align="middle" src="<c:url value='/bank_img/abc.bmp'/>"/>
   
  <input id="ccb-net-b2c" type="radio" name="yh" value="ccb-net-b2c"/>
  <img name="ccb-net-b2c" align="middle" src="<c:url value='/bank_img/ccb.bmp'/>"/>
  </div> 
  <div style="margin-bottom: 20px;">
  <input id="bccb-net-b2c" type="radio" name="yh" value="bccb-net-b2c"/>
  <img name="bccb-net-b2c" align="middle" src="<c:url value='/bank_img/bj.bmp'/>"/>
 
  <input id="boco-net-b2c" type="radio" name="yh" value="boco-net-b2c"/>
  <img name="boco-net-b2c" align="middle" src="<c:url value='/bank_img/bcc.bmp'/>"/>
 
  <input id="cib-net-b2c" type="radio" name="yh" value="cib-net-b2c"/>
  <img name="cib-net-b2c" align="middle" src="<c:url value='/bank_img/cib.bmp'/>"/>
 
  <input id="njcb-net-b2c" type="radio" name="yh" value="njcb-net-b2c"/>
  <img name="njcb-net-b2c" align="middle" src="<c:url value='/bank_img/nanjing.bmp'/>"/>
  </div>
  <div style="margin-bottom: 20px;">
  <input id="cmbc-net-b2c" type="radio" name="yh" value="cmbc-net-b2c"/>
  <img name="cmbc-net-b2c" align="middle" src="<c:url value='/bank_img/cmbc.bmp'/>"/>
 
  <input id="ceb-net-b2c" type="radio" name="yh" value="ceb-net-b2c"/>
  <img name="ceb-net-b2c" align="middle" src="<c:url value='/bank_img/guangda.bmp'/>"/>
 
  <input id="boc-net-b2c" type="radio" name="yh" value="boc-net-b2c"/>
  <img name="boc-net-b2c" align="middle" src="<c:url value='/bank_img/bc.bmp'/>"/>
 
  <input id="pinganbank-net" type="radio" name="yh" value="pinganbank-net"/>
  <img name="pinganbank-net" align="middle" src="<c:url value='/bank_img/pingan.bmp'/>"/>
  </div>
  <div style="margin-bottom: 20px;">
  <input id="cbhb-net-b2c" type="radio" name="yh" value="cbhb-net-b2c"/>
  <img name="cbhb-net-b2c" align="middle" src="<c:url value='/bank_img/bh.bmp'/>"/>
 
  <input id="hkbea-net-b2c" type="radio" name="yh" value="hkbea-net-b2c"/>
  <img name="hkbea-net-b2c" align="middle" src="<c:url value='/bank_img/dy.bmp'/>"/>
 
  <input id="nbcb-net-b2c" type="radio" name="yh" value="nbcb-net-b2c"/>
  <img name="nbcb-net-b2c" align="middle" src="<c:url value='/bank_img/ningbo.bmp'/>"/>
 
  <input id="ecitic-net-b2c" type="radio" name="yh" value="ecitic-net-b2c"/>
  <img name="ecitic-net-b2c" align="middle" src="<c:url value='/bank_img/zx.bmp'/>"/>
  </div>
  <div style="margin-bottom: 20px;">
  <input id="sdb-net-b2c" type="radio" name="yh" value="sdb-net-b2c"/>
  <img name="sdb-net-b2c" align="middle" src="<c:url value='/bank_img/sfz.bmp'/>"/>
 
  <input id="gdb-net-b2c" type="radio" name="yh" value="gdb-net-b2c"/>
  <img name="gdb-net-b2c" align="middle" src="<c:url value='/bank_img/gf.bmp'/>"/>
 
  <input id="shb-net-b2c" type="radio" name="yh" value="shb-net-b2c"/>
  <img name="shb-net-b2c" align="middle" src="<c:url value='/bank_img/sh.bmp'/>"/>
 
  <input id="spdb-net-b2c" type="radio" name="yh" value="spdb-net-b2c"/>
  <img name="spdb-net-b2c" align="middle" src="<c:url value='/bank_img/shpd.bmp'/>"/>
  </div>
  <div style="margin-bottom: 20px;">
  <input id="post-net-b2c" type="radio" name="yh" value="post-net-b2c"/>
  <img name="post-net-b2c" align="middle" src="<c:url value='/bank_img/post.bmp'/>"/>
 
  <input id="bjrcb-net-b2c" type="radio" name="yh" value="bjrcb-net-b2c"/>
  <img name="bjrcb-net-b2c" align="middle" src="<c:url value='/bank_img/beijingnongshang.bmp'/>"/>
 
  <input id="hxb-net-b2c" type="radio" name="yh" value="hxb-net-b2c"/>
  <img name="hxb-net-b2c" align="middle" src="<c:url value='/bank_img/hx.bmp'/>"/>
 
  <input id="cz-net-b2c" type="radio" name="yh" value="cz-net-b2c"/>
  <img name="cz-net-b2c" align="middle" src="<c:url value='/bank_img/zheshang.bmp'/>"/>
  </div>
 </div>
 <div style="margin: 40px;">
  <a href="javascript:void $('#form1').submit();" class="linknext">下一步</a>
 </div>
</div>
</form>
 </body>
</html>

payment.properties

p1_merid=10001126856
keyvalue=69cl522av6q613ii4w6u8k6xuw8vm1n6bfgyv769220iuye9u37n4y7ri4pl
p8_url=http\://localhost\:8080/goods/orderservlet?method\=back

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。