易寶支付
阿新 • • 發佈:2017-12-27
util 地址 頁面 void res log rod arraylist w3c
package com.itheima.store.web.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; import com.itheima.store.domain.Order; import com.itheima.store.domain.OrderItem; import com.itheima.store.domain.PageBean; import com.itheima.store.domain.Product; import com.itheima.store.domain.User; import com.itheima.store.service.OrderService; import com.itheima.store.service.impl.OrderServiceImpl;import com.itheima.store.utils.PaymentUtil; import com.itheima.store.utils.UUIDUtils; /** * Servlet */ public class OrderServlet extends BaseServlet { private static final long serialVersionUID = 1L; // 支付訂單 public String payByOid(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { try { String oid = request.getParameter("oid"); String address = request.getParameter("address"); String name = request.getParameter("name"); String tel = request.getParameter("tel"); String pd_FrpId = request.getParameter("pd_FrpId"); OrderService service = new OrderServiceImpl(); service.updateOrder(oid, address, name, tel); // 支付的邏輯 // 執行支付操作 String p0_Cmd = "Buy"; String p1_MerId = "10001126856";// 商戶id String p2_Order = oid;// 訂單編號 String p3_Amt = "0.01";// 支付金額 String p4_Cur = "CNY"; String p5_Pid = ""; String p6_Pcat = ""; String p7_Pdesc = ""; // 支付成功後,當用戶點擊返回商家時跳轉的頁面 String p8_Url = "http://localhost:8080/Store/order?action=callback"; String p9_SAF = ""; String pa_MP = ""; String pr_NeedResponse = "1"; 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, "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl"); request.setAttribute("p0_Cmd", p0_Cmd); request.setAttribute("p1_MerId", p1_MerId); request.setAttribute("p2_Order", p2_Order); request.setAttribute("p3_Amt", p3_Amt); request.setAttribute("p4_Cur", p4_Cur); request.setAttribute("p5_Pid", p5_Pid); request.setAttribute("p6_Pcat", p6_Pcat); request.setAttribute("p7_Pdesc", p7_Pdesc); request.setAttribute("p8_Url", p8_Url); request.setAttribute("p9_SAF", p9_SAF); request.setAttribute("pa_MP", pa_MP); request.setAttribute("pd_FrpId", pd_FrpId); request.setAttribute("pr_NeedResponse", pr_NeedResponse); request.setAttribute("hmac", hmac); return "/jsp/confirm.jsp"; } catch (Exception e) { e.printStackTrace(); } return null; } // 支付成功以後的回調方法 public String callback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 更改狀態。 String p1_MerId = request.getParameter("p1_MerId"); String r0_Cmd = request.getParameter("r0_Cmd"); String r1_Code = request.getParameter("r1_Code");// 支付結果1 String r2_TrxId = request.getParameter("r2_TrxId"); String r3_Amt = request.getParameter("r3_Amt"); String r4_Cur = "RMB"; String r5_Pid = request.getParameter("r5_Pid"); String r6_Order = request.getParameter("r6_Order");// 用戶生成的定單號 String r7_Uid = request.getParameter("r7_Uid"); String r8_MP = request.getParameter("r8_MP"); String r9_BType = request.getParameter("r9_BType");// 交易結果返回類型 1 String rb_BankId = request.getParameter("rb_BankId"); String hmac = request.getParameter("hmac"); // 驗證數據的安全 boolean isVerifyCallback = 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, "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl"); // 支付成功 if (isVerifyCallback && "1".equals(r1_Code)) { try { // 修改訂單狀態 OrderService service = new OrderServiceImpl(); service.updateOrderState(r6_Order, 1); request.setAttribute("error", "親,您的訂單" + r6_Order + "已經付款,付款的金額是:" + r3_Amt); return "/jsp/error.jsp"; } catch (SQLException e) { e.printStackTrace(); } } return null; } }
PaymentUtil
package cn.itcast.utils; 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); // 易寶支付會員ID sValue.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)); } // public static void main(String[] args) { // System.out.println(hmacSign("AnnulCard1000043252120080620160450.0http://localhost/SZXpro/callback.asp榪?4564868265473632445648682654736324511","8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t")); // } }
confirm.jsp
<%@ 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 --> <form action="https://www.yeepay.com/app-merchant-proxy/node" method="post"> <h3>訂單號:${p2_Order},付款金額 :${p3_Amt }</h3> <input type="hidden" name="pd_FrpId" value="${pd_FrpId }" /> <input type="hidden" name="p0_Cmd" value="${p0_Cmd }" /> <input type="hidden" name="p1_MerId" value="${p1_MerId }" /> <input type="hidden" name="p2_Order" value="${p2_Order }" /> <input type="hidden" name="p3_Amt" value="${p3_Amt }" /> <input type="hidden" name="p4_Cur" value="${p4_Cur }" /> <input type="hidden" name="p5_Pid" value="${p5_Pid }" /> <input type="hidden" name="p6_Pcat" value="${p6_Pcat }" /> <input type="hidden" name="p7_Pdesc" value="${p7_Pdesc }" /> <input type="hidden" name="p8_Url" value="${p8_Url }" /> <input type="hidden" name="p9_SAF" value="${p9_SAF }" /> <input type="hidden" name="pa_MP" value="${pa_MP }" /> <input type="hidden" name="pr_NeedResponse" value="${pr_NeedResponse }" /> <input type="hidden" name="hmac" value="${hmac }" /> <input type="submit" value="確認支付" /> </form> </body> </html>
易寶支付