本文主要是介绍简单的支付小案例(易宝),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
jsp页面的搭建
<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title></title></head><body><div style="margin-bottom: 20px; margin-left: 100px; margin-right: 100px;"><input id="ICBC-NET-B2C" type="radio" name="yh" value="ICBC-NET-B2C" /> <img name="ICBC-NET-B2C" align="middle" src="bank_img/icbc.bmp" /> <input checked="checked" id="CCB-NET-B2C" type="radio" name="yh" value="CCB-NET-B2C"/> <img name="CCB-NET-B2C" align="middle" src="bank_img/ccb.bmp" style="margin-right: 4px" /><input id="ABC-NET-B2C" type="radio" name="yh" value="ABC-NET-B2C" /> <img name="ABC-NET-B2C" align="middle" src="bank_img/abc.bmp"/> <input id="CMBCHINA-NET-B2C" type="radio" name="yh" value="CMBCHINA-NET-B2C" /> <img name="CMBCHINA-NET-B2C" align="middle" src="bank_img/cmb.bmp" /></div><div style="margin-bottom: 20px; margin-left: 100px; margin-right: 100px;"><input id="BCCB-NET-B2C" type="radio" name="yh" value="BCCB-NET-B2C" /> <img name="BCCB-NET-B2C" align="middle" src="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="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="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="bank_img/nanjing.bmp" /></div><div style="margin-bottom: 20px; margin-left: 100px; margin-right: 100px;"><input id="CMBC-NET-B2C" type="radio" name="yh" value="CMBC-NET-B2C" /> <img name="CMBC-NET-B2C" align="middle" src="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="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="bank_img/bc.bmp" /> <input id="PINGANBANK-NET" type="radio"name="yh" value="PINGANBANK-NET" /><img name="PINGANBANK-NET" align="middle" src="bank_img/pingan.bmp" /></div><div style="margin-bottom: 20px; margin-left: 100px; margin-right: 100px;"><input id="CBHB-NET-B2C" type="radio" name="yh" value="CBHB-NET-B2C" /> <img name="CBHB-NET-B2C" align="middle" src="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="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="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="bank_img/zx.bmp" /></div><div style="margin-bottom: 20px; margin-left: 100px; margin-right: 100px;"><input id="SDB-NET-B2C" type="radio" name="yh" value="SDB-NET-B2C" /><img name="SDB-NET-B2C" align="middle" src="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="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="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="bank_img/shpd.bmp" /></div><div style="margin-bottom: 20px; margin-left: 100px; margin-right: 100px;"><input id="POST-NET-B2C" type="radio" name="yh" value="POST-NET-B2C" /> <img name="POST-NET-B2C" align="middle" src="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="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="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="bank_img/zheshang.bmp" /></div></body></html>
//图片省略
springMVC下的调用(使用的测试账户,有最大金额限制)
/**这里只从页面传了支付金额,选择的银行俩个参数,银行参数是固定了,可以去网上查,我在jsp里写了每一个单选框的name值*/@RequestMapping(value = "/fukuan.action")public String fukuan(String yh,String me) {String p0_Cmd = "Buy";// 业务类型,buyString p1_MerId = "10001126856";// 商户编号String p2_Order = "";// 商户订单号String p3_Amt = me;// 支付金额String p4_Cur = "CNY";// 交易币种,CNY为人名币String p5_Pid = "";// 商品名称String p6_Pcat = "";// 商品种类String p7_Pdesc = "";// 商品描述String p8_Url = "http://localhost:8080/ambowbook/fukuanhou.action";//回掉地址String p9_SAF = "";// 送货地址String pa_MP = "";// 商户扩展信息String pd_FrpId = yh;// 银行编码String pr_NeedResponse = "1";// 应答机制0 ,1String keyValue = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl";//测试账户,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);StringBuffer sb = new StringBuffer("https://www.yeepay.com/app-merchant-proxy/node?");sb.append("p0_Cmd=").append(p0_Cmd).append("&");sb.append("p1_MerId=").append(p1_MerId).append("&");sb.append("p2_Order=").append(p2_Order).append("&");sb.append("p3_Amt=").append(p3_Amt).append("&");sb.append("p4_Cur=").append(p4_Cur).append("&");sb.append("p5_Pid=").append(p5_Pid).append("&");sb.append("p6_Pcat=").append(p6_Pcat).append("&");sb.append("p7_Pdesc=").append(p7_Pdesc).append("&");sb.append("p8_Url=").append(p8_Url).append("&");sb.append("p9_SAF=").append(p9_SAF).append("&");sb.append("pa_MP=").append(pa_MP).append("&");sb.append("pd_FrpId=").append(pd_FrpId).append("&");sb.append("pr_NeedResponse=").append(pr_NeedResponse).append("&");sb.append("hmac=").append(hmac);return "redirect:" + sb.toString();}/**这里只从回掉参数里拿了支付状态,正确情况下还应拿订单号等*/@RequestMapping(value = "/fukuanhou.action")public String fukuanhou(String r9_BType, String r3_Amt, HttpSession session) {if ("1".equals(r9_BType)) {//状态为1代表支付成功return "success";}return "error";}
所需工具类的源码
package com.ambow.common.util.duanxin;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;public class PaymentUtil {private static String encodingCharset = "UTF-8";/*** 生成hmac方法* * @param p0_Cmd 业务类型* @param p1_MerId 商户编号* @param p2_Order 商户订单�?* @param p3_Amt 支付金额* @param p4_Cur 交易币种* @param p5_Pid 商品名称* @param p6_Pcat 商品种类* @param p7_Pdesc 商品描述* @param p8_Url 商户接收支付成功数据的地?* @param p9_SAF 送货地址* @param pa_MP 商户扩展信息* @param pd_FrpId 银行编码* @param pr_NeedResponse 应答机制* @param keyValue 商户密钥* @return*/public static String buildHmac(String p0_Cmd, String p1_MerId, String p2_Order, String p3_Amt, String p4_Cur,String p5_Pid, String p6_Pcat, String p7_Pdesc, String p8_Url, String p9_SAF, String pa_MP, String pd_FrpId,String pr_NeedResponse, String keyValue) {StringBuilder sValue = new StringBuilder();
// 业务类型sValue.append(p0_Cmd);
// 商户编号sValue.append(p1_MerId);
// 商户订单?sValue.append(p2_Order);
// 支付金额sValue.append(p3_Amt);
// 交易币种sValue.append(p4_Cur);
// 商品名称sValue.append(p5_Pid);
// 商品种类sValue.append(p6_Pcat);
// 商品描述sValue.append(p7_Pdesc);
// 商户接收支付成功数据的地?sValue.append(p8_Url);
// 送货地址sValue.append(p9_SAF);
// 商户扩展信息sValue.append(pa_MP);
// 银行编码sValue.append(pd_FrpId);
// 应答机制sValue.append(pr_NeedResponse);return PaymentUtil.hmacSign(sValue.toString(), keyValue);}/*** 返回校验hmac方法* * @param hmac 支付网关发来的加密验证码* @param p1_MerId 商户编号* @param r0_Cmd 业务类型* @param r1_Code 支付结果* @param r2_TrxId 易宝支付交易流水?* @param r3_Amt 支付金额* @param r4_Cur 交易币种* @param r5_Pid 商品名称* @param r6_Order 商户订单?* @param r7_Uid 易宝支付会员ID* @param r8_MP 商户扩展信息* @param r9_BType 交易结果返回类型* @param keyValue 密钥* @return*/public static boolean verifyCallback(String hmac, String p1_MerId, String r0_Cmd, String r1_Code, String r2_TrxId,String r3_Amt, String r4_Cur, String r5_Pid, String r6_Order, String r7_Uid, String r8_MP, String r9_BType,String keyValue) {StringBuilder sValue = new StringBuilder();
// 商户编号sValue.append(p1_MerId);
// 业务类型sValue.append(r0_Cmd);
// 支付结果sValue.append(r1_Code);
// 易宝支付交易流水�?sValue.append(r2_TrxId);
// 支付金额sValue.append(r3_Amt);
// 交易币种sValue.append(r4_Cur);
// 商品名称sValue.append(r5_Pid);
// 商户订单�?sValue.append(r6_Order);
// 易宝支付会员IDsValue.append(r7_Uid);
// 商户扩展信息sValue.append(r8_MP);
// 交易结果返回类型sValue.append(r9_BType);String sNewString = PaymentUtil.hmacSign(sValue.toString(), keyValue);return sNewString.equals(hmac);}/*** @param aValue* @param aKey* @return*/public static String hmacSign(String aValue, String aKey) {byte k_ipad[] = new byte[64];byte k_opad[] = new byte[64];byte keyb[];byte value[];try {keyb = aKey.getBytes(encodingCharset);value = aValue.getBytes(encodingCharset);} catch (UnsupportedEncodingException e) {keyb = aKey.getBytes();value = aValue.getBytes();}Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);Arrays.fill(k_opad, keyb.length, 64, (byte) 92);for (int i = 0; i < keyb.length; i++) {k_ipad[i] = (byte) (keyb[i] ^ 0x36);k_opad[i] = (byte) (keyb[i] ^ 0x5c);}MessageDigest md = null;try {md = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException e) {return null;}md.update(k_ipad);md.update(value);byte dg[] = md.digest();md.reset();md.update(k_opad);md.update(dg, 0, 16);dg = md.digest();return toHex(dg);}public static String toHex(byte input[]) {if (input == null)return null;StringBuffer output = new StringBuffer(input.length * 2);for (int i = 0; i < input.length; i++) {int current = input[i] & 0xff;if (current < 16)output.append("0");output.append(Integer.toString(current, 16));}return output.toString();}/*** * @param args* @param key* @return*/public static String getHmac(String[] args, String key) {if (args == null || args.length == 0) {return (null);}StringBuffer str = new StringBuffer();for (int i = 0; i < args.length; i++) {str.append(args[i]);}return (hmacSign(str.toString(), key));}/*** @param aValue* @return*/public static String digest(String aValue) {aValue = aValue.trim();byte value[];try {value = aValue.getBytes(encodingCharset);} catch (UnsupportedEncodingException e) {value = aValue.getBytes();}MessageDigest md = null;try {md = MessageDigest.getInstance("SHA");} catch (NoSuchAlgorithmException e) {e.printStackTrace();return null;}return toHex(md.digest(value));}}
本案例属自己编写,如有侵权联系作者删除
这篇关于简单的支付小案例(易宝)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!