---
title: "网上支付"
author: "fly-sky"
date: "2011-03-28T12:57:40Z"
tags: ["JSP", "Servlet", "浏览器", "算法"]
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2011-03-28T12:57:40Z"
id: 977270
url: "https://fly-sky.iteye.com/blog/977270"
---

两种接入方案 相接与银行对接（优点：交易安全，交易量大，每月结算全额企业，缺点：开发量比较大，维护难） 通过中间公司间接与银行对接（优点：开发工作量少，交易金额不大，缺点：资金安全问题） 目前在国内做的比较好的中间支持公司： 首信支付：http://www.beijing.com.cn/ 每年需要交纳一定的接口使用费，并且从交易金额中扣除1%的手续费，像当当网、红孩子、京东商城使用了首信支付。 易宝支付：http://www.yeepay.com 接入免费，只从交易金额中扣除1%的手续费。像盛大、e龙网、巴巴运动网使用了易宝支付。 易宝支付接入规范 名词MD5-hmac: hmac是一种秘密的密钥验证算法。hmac提供的数据完整性和源身份验证完全取决于密钥分配的范围，如果只有发起者和接收知道hmac密钥，那么这就是对两者间发送的数据提供了源身份验证和完整性保证。 本例给大家提供了该算法的加密类：DigesUtil 使用方法如下： \[quote]StringBuffer svalue = new StringBuffer(); svalue.append("zhanshan"); svalue.append("01219931"); String hmacMD5 = DigesUtil.hmacSign(svalue.toString(),"123456");\[/quote] 第一个参数为加密的原文，第二个参数为密钥。 支付流程： 通过http请求方式向易宝支付网关发起一个请求，简单地说就是向https://www.yeepay.com/app-merchant-proxy/node发起请求，请求可以是get或者post方式提交，页面应采用GBK/GB2312编码。下面使用表单以post方式向易宝支付网关发起一个支付请求。 \[code="jsp"]

\[/code] 易宝支付网关对企业发来的数据使用用户的密钥生成MD5-hmac码，然后跟企业发来MD5-hmac码（即上面表单由hmac字段提供的值）比较是否相同，如果相同即把请求转发到银行网关，当用户支付完成后，银行网关会引导用户的浏览器重定向到易宝支付网关，然后易宝支付网关再引导用户的浏览器重定向到企业提供的url(即p8\_Url提供的地址） 支付请求的数据加密生成md5-hmac \[code="java"] request.setCharacterEncoding("GBK"); 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 = PanymentUtil.buildHmac(messageType, p1\_MerId, orderid, amount, currency, productId, productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, pd\_FrpId, pr\_NeedResponse, keyValue);\[/code] 测试用的账号与密钥，接收返回路征(必须是公网的路征此地)： \[quote]p1\_MerId=10000326625 keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek merchantCallbackURL=http\\://localhost\\:8080/payment/servlet/yeepay/response\[/quote] 测试网关： http://tech.yeepay.com:8080/robot/debug.action 正式网关： https://www.yeepay.com/app-merchant-proxy/node 表单自动提交： 对支付结果返回的数据加密生成md5-hmac \[code="java"]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);\[/code] 如果测试用本地路游器，要修改转发规则： 设置端口，IP地址，协议，并启用它 校验结果： \[code="java"]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", "数据来源不合法"); }\[/code] 要点注意用户多次刷新问题，避免多次叠加，造成损失。 要对每一个交易要进行日志处理，这是必须有的信息。