官网文档和demo代码下载地址
https://mp.weixin.qq.com/htmledition/res/bussiness-faq/wx_app_pay.zip
-
基础常量代码
package com.xiaoshuai.util;/** * 配置类 * @author 小帅帅 * @date 2016-7-5下午01:40:53 */public class ConfigUtil { /** * 服务号相关信息 */ public final static String APPID = "";//服务号的应用号 public final static String APP_SECRECT = "";//服务号的应用密码 public final static String TOKEN = "weixinCourse";//服务号的配置token public final static String MCH_ID = "";//商户号 public final static String API_KEY = "";//API密钥 public final static String SIGN_TYPE = "MD5";//签名加密方式 public final static String CERT_PATH = "";//微信支付证书存放路径地址 //微信支付统一接口的回调action public final static String NOTIFY_URL = ""; //微信支付成功支付后跳转的地址 public final static String SUCCESS_URL = "http://www.xiaoshuaishuai.com"; //oauth2授权时回调action public final static String REDIRECT_URI = ""; /** * 微信基础接口地址 */ //获取token接口(GET) public final static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; //oauth2授权接口(GET) public final static String OAUTH2_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; //刷新access_token接口(GET) public final static String REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN"; /** * 微信支付接口地址 */ //微信支付统一接口(POST) public final static String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //微信退款接口(POST) public final static String REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //订单查询接口(POST) public final static String CHECK_ORDER_URL = "https://api.mch.weixin.qq.com/pay/orderquery"; //关闭订单接口(POST) public final static String CLOSE_ORDER_URL = "https://api.mch.weixin.qq.com/pay/closeorder"; //退款查询接口(POST) public final static String CHECK_REFUND_URL = "https://api.mch.weixin.qq.com/pay/refundquery"; //对账单接口(POST) public final static String DOWNLOAD_BILL_URL = "https://api.mch.weixin.qq.com/pay/downloadbill"; //短链接转换接口(POST) public final static String SHORT_URL = "https://api.mch.weixin.qq.com/tools/shorturl"; //接口调用上报接口(POST) public final static String REPORT_URL = "https://api.mch.weixin.qq.com/payitil/report";}
-
支付工具类代码
package com.xiaoshuai.util;import java.util.Date;import java.util.Iterator;import java.util.Map;import java.util.Random;import java.util.Set;import java.util.SortedMap;import java.util.TreeMap;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.alibaba.fastjson.*;/** * 支付类 * @author 小帅帅 * @date 2016-7-5下午01:41:54 */public class PayCommonUtil { @SuppressWarnings("unused") private static Logger log = LoggerFactory.getLogger(PayCommonUtil.class); public static String CreateNoncestr(int length) { String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String res = ""; for (int i = 0; i < length; i++) { Random rd = new Random(); res += chars.indexOf(rd.nextInt(chars.length() - 1)); } return res; } public static String CreateNoncestr() { String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String res = ""; for (int i = 0; i < 16; i++) { Random rd = new Random(); res += chars.charAt(rd.nextInt(chars.length() - 1)); } return res; } /** * @author 小帅帅 * @date 2016-7-5下午01:41:54 * @Description:sign签名 * @param characterEncoding 编码格式 * @param parameters 请求参数 * @return */ @SuppressWarnings("unchecked") public static String createSign(String characterEncoding,SortedMap
-
请求业务代码
package com.xiaoshuai.controller;import java.io.IOException;import java.io.PrintWriter;import java.math.BigDecimal;import java.util.Map;import java.util.Random;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.jdom.JDOMException;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.xiaoshuai.util.CommonUtil;import com.xiaoshuai.util.PayCommonUtil;/** * 支付请求Controller * @author 小帅帅 * @date 2016-7-5下午02:08:47 */@Controller@RequestMapping(value="pay")public class WeiXinPayController { /** * * index(支付首页) * @return */ @RequestMapping(value="index") public String index(){ return "pay"; } /** * * payOrder(调起微信支付) * @param request * @param totalFee //支付金额 * @param body //支付描述 * @return * @throws Exception */ @RequestMapping(value="payOrder") @ResponseBody public String payOrder(HttpServletRequest request,double totalFee,String body) throws Exception{ String resultString=null; BigDecimal fee = new BigDecimal(totalFee); // 微信支付参数以分为单位。 fee = fee.multiply(new BigDecimal(100)); String payFee=fee.longValue()+""; //1 生成预支付id String ipAddress=request.getRemoteAddr(); String orderNumber=System.currentTimeMillis()+new Random().nextInt(100)+""; String openid="o2VKNju8JqCeGVoEWJ1S8Ue_up8E"; //调用网页授权接口获取 try { Map
map=PayCommonUtil.getPrepayId(payFee, ipAddress, orderNumber, body, openid); String prepay_id=map.get("prepay_id"); //2 给H5页面传递参数 调起微信支付 resultString=PayCommonUtil.createPackageValue(prepay_id); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return resultString; } /** * * updateOrderState(异步回调通知) * * @param request */ @RequestMapping(value="noticePay") public void updateOrderState(HttpServletRequest request,HttpServletResponse response) { try { PrintWriter out= response.getWriter(); // xml请求解析 Map requestMap = CommonUtil.parseXml(request); String orderNumber = (String) requestMap.get("out_trade_no"); if (orderNumber != null) { // 调用微信查询订单接口, Map orderMap = PayCommonUtil.queryWeiXinOrder(orderNumber); if (orderMap.get("return_code") != null&& orderMap.get("return_code").equalsIgnoreCase("SUCCESS")) { if (orderMap.get("result_code") != null&& orderMap.get("result_code").equalsIgnoreCase("SUCCESS")) { if (orderMap.get("trade_state") != null&& orderMap.get("trade_state").equalsIgnoreCase("SUCCESS")) { // 支付成功。。。。开始更新你的订单状态吧 根据自己的业务去修改 String resultXml=" - 项目结构。只是简单的基础代码
5.pay.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>微信支付 支付金额: 支付描述: