首先创建项目alipay_struts
在WEB-INF的lib目录下导入jar包,struts用到的用关jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
comm-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.110.GA.jar
ognl-3.05.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
web.xml中配置struts的核心过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>alipay_struts</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> |
随机生成订单的工具:PayUtil
package util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; /** * 利用时间生成支付的订单号,再加4位随机生成的数字 * @author xy * */ public class PayUtil { //获取当前时间 public static String getCurrTime(){ Date now = new Date(); SimpleDateFormat out = new SimpleDateFormat("yyyyMMddHHmmss"); String s = out.format(now); return s; } //生成随机字符串 public static String getRandomString(int length){ Random random = new Random(); StringBuffer sb = new StringBuffer(); //第一次随机 for (int i = 0; i <length; i++) { int number = random.nextInt(3); long result = 0; switch (number){ //大写字母 case 0: //大写字母A是从65开始的 result = Math.round(Math.random()*25+65); sb.append(String.valueOf((char)result)); break; //小写字母 case 1: result = Math.round(Math.random()*25 + 97); sb.append(String.valueOf((char)result)); break; //生成随机数字 case 2 : sb.append(String.valueOf(new Random().nextInt(10))); break; } } return sb.toString(); } } |
生成签名:
在工具类里面创建Sign.java类:
package util; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; /** * 微信的sign签名 * @author xy * */ public class Sign { /** * 这个是微信的有关签名 * @return */ public static String sign(){ //微信需要的签名字符串 String appid= "wxd930ea5d5a258f4f"; String mch_id= "10000100"; String device_info= "1000"; String body= "test"; String nonce_str= "ibuaiVcKdpRxkhJA"; String key= "192006250b4c09247ec02edce69f6a2d"; //将参数按字母的先后顺序排序 SortedMap<Object, Object> parameters = new TreeMap<Object,Object>(); parameters.put("appid",appid); parameters.put("mch_id",mch_id); parameters.put("device_info",device_info); parameters.put("body",body); parameters.put("nonce_str",nonce_str); String sign = creatSign(parameters, key); System.out.println(sign); return sign; } /** * 将封装好的参数进行排序,只有排序了才能和支付宝方对应 * 并且拼接成"key=value"格式 * @param parameters * @param Key * @return */ public static String creatSign(SortedMap<Object,Object> parameters ,String Key){ //遍历排好序的Map集合,并拼接成"key=value"格式 StringBuffer sb = new StringBuffer(); Set es = parameters.entrySet(); Iterator<?> it = es.iterator(); while(it.hasNext()){ @SuppressWarnings("rawtype") Map.Entry entry =(Map.Entry) it.next(); String k =(String) entry.getKey(); Object v = entry.getValue(); if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)){ sb.append(k + "=" + v + "&"); } } String param = sb.toString(); //把最后的&去掉 param = param.substring(0, param.length()-1)+Key; System.out.println(param); String sign = MD5Util.MD5Encode(param.toString(), "UTF-8"); return sign; } public static void main(String[] args) { sign(); } } |
创建工具类MD5Util:
用于给签名加密的
package util; import java.security.MessageDigest; public class MD5Util { //将MD5输出的二进制转换为小写的16进制 private static String byteArrayToHexString(byte b[]){ StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n= b; if(n<0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1]+hexDigits[d2]; } private static final String hexDigits[]={"0","1","2","3","4","5" ,"6","7","8","9","a","b","c","d","e","f"}; public static String MD5Encode(String origin,String charsetname){ String resultString = null; try { resultString = new String(origin); //new一个MD5实例 MessageDigest md = MessageDigest.getInstance("MD5"); if(charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString.getBytes())); else resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } catch (Exception e) { // TODO: handle exception } return resultString; } } |
package action; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import org.apache.struts2.interceptor.RequestAware; import com.opensymphony.xwork2.ActionSupport; import util.PayUtil; import util.Sign; /** * 支付宝即时支付 * 访问url:http://127.0.0.1:8080/alipay_struts/pay_alipay * @author xy * */ public class CoreAction extends ActionSupport implements RequestAware{ //因为传导前台是键值对形式所以要一个map集合 private Map<String ,Object> request; public Map<String , Object>getRequest(){ return request; } @Override public void setRequest(Map<String, Object> arg0) { this.request=arg0; } public String alipay(){ //这些事由支付宝方提供给公司的 String partner = "2088911395487764"; String seller_id = partner; String key = "vuq3ivaa1n732a5vuz0oio7iyza9hvvk"; //通知地址 异步的 String notify_url = "http://qiukui801750.vicp.hk/alipay/notify_url.jsp"; //页面跳转地址 同步的 String return_url = "http://qiukui801750.vicp.hk/alipay/return_url.jsp"; //签名类型 String sign_type = "MD5"; //编码类型 String _input_charset = "utf-8"; //支付方式,现在支付宝的默认方式只有1 String payment_type = "1"; //写死的,一个接口,不能改变 String service = "create_direct_pay_by_user"; //商品标题 String subject = "测试123"; // 支付金额,支付宝支持的数字在0.01~1亿之间 double total_fee = 0.01; //订单号 String out_trade_no =PayUtil.getCurrTime()+PayUtil.getRandomString(5); //排序 SortedMap<Object,Object> sm = new TreeMap<Object,Object>(); sm.put("partner",partner ); sm.put("seller_id",seller_id ); //sm.put("key", key); sm.put("notify_url",notify_url ); sm.put("return_url", return_url); //sm.put("sign_type",sign_type ); sm.put("_input_charset",_input_charset ); sm.put("payment_type",payment_type ); sm.put("service",service ); sm.put("subject", subject); sm.put("total_fee",total_fee ); sm.put("out_trade_no",out_trade_no); String sign = Sign.creatSign(sm, key); System.out.println(sign); //向前台传值 request.put("partner",partner ); request.put("seller_id",seller_id ); request.put("notify_url",notify_url ); request.put("return_url",return_url); request.put("_input_charset",_input_charset); request.put("payment_type",payment_type); request.put("service",service); request.put("subject",subject); request.put("total_fee",total_fee); request.put("out_trade_no",out_trade_no); request.put("sign",sign); request.put("sign_type",sign_type); return "pay"; } } |
在src下面创建struts.xml,这个也是struts的核心配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 常量配置 --> <!-- 解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"></constant> <!-- 主题配置 --> <constant name="struts.ui.theme" value="simple"></constant> <!-- 自定义扩展名 --> <constant name="struts.action.extension" value="action,do,"></constant> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor-stack name="sshStack"> <interceptor-ref name="paramsPrepareParamsStack"> <param name="prepare.alvaysInvokePrepare">false</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="sshStack"></default-interceptor-ref> <action name="pay_*" class="action.CoreAction" method="{1}"> <!--下面这个路径可以自己建,我用的是eclipse如果jsp文件直接放在WebContent下就可以直接写jsp名--> </action> </package> </struts> |
创建jsp:
在WebContent-->WEB-INF下新建一个包,叫做jsp,在jsp下新建一个jsp文件pay.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>支付测试</title> </head> <body> <form action="https://mapi.alipay.com/gateway.do" method="get"> <input type="text" name="partner" value="${partner}"> <input type="text" name="seller_id" value="${seller_id}"> <input type="text" name="notify_url" value="${notify_url}"> <input type="text" name="return_url" value="${return_url}"> <input type="text" name="_input_charset" value="${_input_charset}"> <input type="text" name="payment_type" value="${payment_type}"> <input type="text" name="service" value="${service}"> <input type="text" name="subject" value="${subject}"> <input type="text" name="total_fee" value="${total_fee}"> <input type="text" name="out_trade_no" value="${out_trade_no}"> <input type="text" name="sign" value="${sign}"> <input type="text" name="sign_type" value="${sign_type}"> <input type="submit" value="即时支付"> </form> </body> </html> |
只是用来测试的所以页面只是很简单的一个jsp页面
访问:http://127.0.0.1:8080/alipay_struts/pay_alipay