微信支付统一下单

  1. 估计大多数人和我一样吧,每次要使用腾讯的开发api,都有一种想上吊的感觉,尤其是微信支付和支付宝对比起来文档确实差距还是有点大的  

关于微信支付的申请我就不多说了,网上有很多,我们直接来说一下微信支付

微信支付是以下步骤(官方文档说明 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3)

步骤1:用户在商户APP中选择商品,提交订单,选择微信支付。

步骤2:商户后台收到用户支付单,调用微信支付统一下单接口。参见【统一下单API】。

步骤3:统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP。参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。注意:package的值格式为Sign=WXPay

      步骤3要注意千万不要按照他给的签名字段进行前面,我就被坑了一天

     appid
     partnerid
     prepayid
     package
     noncestr
     timestamp
     sign

   签名字段一定不要按照文档的驼峰写法,应该是全部小写,就像上面

步骤4:商户APP调起微信支付。api参见本章节【app端开发步骤说明

步骤5:商户后台接收支付通知。api参见【支付结果通知API

步骤6:商户后台查询支付结果。,api参见【查询订单API

接下来我们就开始讲解统一下单

请求URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder

参数

字段名变量名必填类型示例值描述
应用IDappidString(32)wxd678efh567hg6787微信开放平台审核通过的应用APPID
商户号mch_idString(32)1230000109微信支付分配的商户号
设备号device_infoString(32)013467007045764终端设备号(门店号或收银设备ID),默认请传"WEB"
随机字符串nonce_strString(32)5K8264ILTKCH16CQ2502SI8ZNMTM67VS随机字符串,不长于32位。推荐随机数生成算法
签名signString(32)C380BEC2BFD727A4B6845133519F3AD6签名,详见签名生成算法
商品描述bodyString(128)Ipad mini  16G  白色商品或支付单简要描述
商品详情detailString(8192)Ipad mini  16G  白色商品名称明细列表
附加数据attachString(127)深圳分店附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
商户订单号out_trade_noString(32)20150806125346商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
货币类型fee_typeString(16)CNY符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
总金额total_feeInt888订单总金额,单位为分,详见支付金额
终端IPspbill_create_ipString(16)123.12.12.123用户端实际ip
交易起始时间time_startString(14)20091225091010订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
交易结束时间time_expireString(14)20091227091010

订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。其他详见时间规则

注意:最短失效时间间隔必须大于5分钟
商品标记goods_tagString(32)WXG商品标记,代金券或立减优惠功能的参数,说明详见代金券或立减优惠
通知地址notify_urlString(256)http://www.weixin.qq.com/wxpay/pay.php接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
交易类型trade_typeString(16)APP支付类型
指定支付方式limit_payString(32)no_creditno_credit--指定不能使用信用卡支付
我们先上工具类吧
  1. public class PayCommonUtil {  
  2.     //微信参数配置  
  3.     public static String API_KEY="";  
  4.     public static String APPID="";  
  5.     public static String MCH_ID="";  
  6.     //随机字符串生成  
  7.     public static String getRandomString(int length) { //length表示生成字符串的长度      
  8.            String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";         
  9.            Random random = new Random();         
  10.            StringBuffer sb = new StringBuffer();         
  11.            for (int i = 0; i < length; i++) {         
  12.                int number = random.nextInt(base.length());         
  13.                sb.append(base.charAt(number));         
  14.            }         
  15.            return sb.toString();         
  16.         }    
  17.     //请求xml组装  
  18.       public static String getRequestXml(SortedMap<String,Object> parameters){  
  19.             StringBuffer sb = new StringBuffer();  
  20.             sb.append("<xml>");  
  21.             Set es = parameters.entrySet();  
  22.             Iterator it = es.iterator();  
  23.             while(it.hasNext()) {  
  24.                 Map.Entry entry = (Map.Entry)it.next();  
  25.                 String key = (String)entry.getKey();  
  26.                 String value = (String)entry.getValue();  
  27.                 if ("attach".equalsIgnoreCase(key)||"body".equalsIgnoreCase(key)||"sign".equalsIgnoreCase(key)) {  
  28.                     sb.append("<"+key+">"+"<![CDATA["+value+"]]></"+key+">");  
  29.                 }else {  
  30.                     sb.append("<"+key+">"+value+"</"+key+">");  
  31.                 }  
  32.             }  
  33.             sb.append("</xml>");  
  34.             return sb.toString();  
  35.         }  
  36.       //生成签名  
  37.       public static String createSign(String characterEncoding,SortedMap<String,Object> parameters){  
  38.             StringBuffer sb = new StringBuffer();  
  39.             Set es = parameters.entrySet();  
  40.             Iterator it = es.iterator();  
  41.             while(it.hasNext()) {  
  42.                 Map.Entry entry = (Map.Entry)it.next();  
  43.                 String k = (String)entry.getKey();  
  44.                 Object v = entry.getValue();  
  45.                 if(null != v && !"".equals(v)  
  46.                         && !"sign".equals(k) && !"key".equals(k)) {  
  47.                     sb.append(k + "=" + v + "&");  
  48.                 }  
  49.             }  
  50.             sb.append("key=" + API_KEY);  
  51.             String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();  
  52.             return sign;  
  53.         }  
  54.       //请求方法  
  55.       public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {  
  56.             try {  
  57.                  
  58.                 URL url = new URL(requestUrl);  
  59.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  60.                 
  61.                 conn.setDoOutput(true);  
  62.                 conn.setDoInput(true);  
  63.                 conn.setUseCaches(false);  
  64.                 // 设置请求方式(GET/POST)  
  65.                 conn.setRequestMethod(requestMethod);  
  66.                 conn.setRequestProperty("content-type""application/x-www-form-urlencoded");  
  67.                 // 当outputStr不为null时向输出流写数据  
  68.                 if (null != outputStr) {  
  69.                     OutputStream outputStream = conn.getOutputStream();  
  70.                     // 注意编码格式  
  71.                     outputStream.write(outputStr.getBytes("UTF-8"));  
  72.                     outputStream.close();  
  73.                 }  
  74.                 // 从输入流读取返回内容  
  75.                 InputStream inputStream = conn.getInputStream();  
  76.                 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
  77.                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
  78.                 String str = null;  
  79.                 StringBuffer buffer = new StringBuffer();  
  80.                 while ((str = bufferedReader.readLine()) != null) {  
  81.                     buffer.append(str);  
  82.                 }  
  83.                 // 释放资源  
  84.                 bufferedReader.close();  
  85.                 inputStreamReader.close();  
  86.                 inputStream.close();  
  87.                 inputStream = null;  
  88.                 conn.disconnect();  
  89.                 return buffer.toString();  
  90.             } catch (ConnectException ce) {  
  91.                 System.out.println("连接超时:{}"+ ce);  
  92.             } catch (Exception e) {  
  93.                 System.out.println("https请求异常:{}"+ e);  
  94.             }  
  95.             return null;  
  96.         }  
  97.       //退款的请求方法  
  98.       public static String httpsRequest2(String requestUrl, String requestMethod, String outputStr) throws Exception {  
  99.             KeyStore keyStore  = KeyStore.getInstance("PKCS12");  
  100.             StringBuilder res = new StringBuilder("");  
  101.             FileInputStream instream = new FileInputStream(new File("/home/apiclient_cert.p12"));  
  102.             try {  
  103.                 keyStore.load(instream, "".toCharArray());  
  104.             } finally {  
  105.                 instream.close();  
  106.             }  
  107.   
  108.             // Trust own CA and all self-signed certs  
  109.             SSLContext sslcontext = SSLContexts.custom()  
  110.                     .loadKeyMaterial(keyStore, "1313329201".toCharArray())  
  111.                     .build();  
  112.             // Allow TLSv1 protocol only  
  113.             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(  
  114.                     sslcontext,  
  115.                     new String[] { "TLSv1" },  
  116.                     null,  
  117.                     SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);  
  118.             CloseableHttpClient httpclient = HttpClients.custom()  
  119.                     .setSSLSocketFactory(sslsf)  
  120.                     .build();  
  121.             try {  
  122.   
  123.                 HttpPost httpost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");  
  124.                 httpost.addHeader("Connection""keep-alive");  
  125.                 httpost.addHeader("Accept""*/*");  
  126.                 httpost.addHeader("Content-Type""application/x-www-form-urlencoded; charset=UTF-8");  
  127.                 httpost.addHeader("Host""api.mch.weixin.qq.com");  
  128.                 httpost.addHeader("X-Requested-With""XMLHttpRequest");  
  129.                 httpost.addHeader("Cache-Control""max-age=0");  
  130.                 httpost.addHeader("User-Agent""Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");  
  131.                  StringEntity entity2 = new StringEntity(outputStr ,Consts.UTF_8);  
  132.                  httpost.setEntity(entity2);  
  133.                 System.out.println("executing request" + httpost.getRequestLine());  
  134.   
  135.                 CloseableHttpResponse response = httpclient.execute(httpost);  
  136.                  
  137.                 try {  
  138.                     HttpEntity entity = response.getEntity();  
  139.                       
  140.                     System.out.println("----------------------------------------");  
  141.                     System.out.println(response.getStatusLine());  
  142.                     if (entity != null) {  
  143.                         System.out.println("Response content length: " + entity.getContentLength());  
  144.                         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));  
  145.                         String text;res.append(text);  
  146.                         while ((text = bufferedReader.readLine()) != null) {  
  147.                             res.append(text);  
  148.                             System.out.println(text);  
  149.                         }  
  150.                          
  151.                     }  
  152.                     EntityUtils.consume(entity);  
  153.                 } finally {  
  154.                     response.close();  
  155.                 }  
  156.             } finally {  
  157.                 httpclient.close();  
  158.             }  
  159.             return  res.toString();  
  160.               
  161.         }  
  162.       //xml解析  
  163.       public static Map doXMLParse(String strxml) throws JDOMException, IOException {  
  164.             strxml = strxml.replaceFirst("encoding=\".*\"""encoding=\"UTF-8\"");  
  165.   
  166.             if(null == strxml || "".equals(strxml)) {  
  167.                 return null;  
  168.             }  
  169.               
  170.             Map m = new HashMap();  
  171.               
  172.             InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));  
  173.             SAXBuilder builder = new SAXBuilder();  
  174.             Document doc = builder.build(in);  
  175.             Element root = doc.getRootElement();  
  176.             List list = root.getChildren();  
  177.             Iterator it = list.iterator();  
  178.             while(it.hasNext()) {  
  179.                 Element e = (Element) it.next();  
  180.                 String k = e.getName();  
  181.                 String v = "";  
  182.                 List children = e.getChildren();  
  183.                 if(children.isEmpty()) {  
  184.                     v = e.getTextNormalize();  
  185.                 } else {  
  186.                     v = getChildrenText(children);  
  187.                 }  
  188.                   
  189.                 m.put(k, v);  
  190.             }  
  191.               
  192.             //关闭流  
  193.             in.close();  
  194.               
  195.             return m;  
  196.         }  
  197.         
  198.       public static String getChildrenText(List children) {  
  199.             StringBuffer sb = new StringBuffer();  
  200.             if(!children.isEmpty()) {  
  201.                 Iterator it = children.iterator();  
  202.                 while(it.hasNext()) {  
  203.                     Element e = (Element) it.next();  
  204.                     String name = e.getName();  
  205.                     String value = e.getTextNormalize();  
  206.                     List list = e.getChildren();  
  207.                     sb.append("<" + name + ">");  
  208.                     if(!list.isEmpty()) {  
  209.                         sb.append(getChildrenText(list));  
  210.                     }  
  211.                     sb.append(value);  
  212.                     sb.append("</" + name + ">");  
  213.                 }  
  214.             }  
  215.               
  216.             return sb.toString();  
  217.         }  
  218.        
  219. }  

统一下单

参数说明
sn订单号
totalAmount支付金额
description产品描述

  1. public static Map<String, String> weixinPrePay(String sn,BigDecimal totalAmount,  
  2.             String description, HttpServletRequest request) {  
  3.         Setting setting = SettingUtils.get();  
  4.         SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();  
  5.         parameterMap.put("appid", PayCommonUtil.APPID);  
  6.         parameterMap.put("mch_id", PayCommonUtil.MCH_ID);  
  7.         parameterMap.put("nonce_str", PayCommonUtil.getRandomString(32));  
  8.         parameterMap.put("body",  
  9.                 StringUtils.abbreviate(description.replaceAll(  
  10.                         "[^0-9a-zA-Z\\u4e00-\\u9fa5 ]"""), 600));  
  11.         parameterMap.put("out_trade_no", sn);  
  12.         parameterMap.put("fee_type""CNY");  
  13.         System.out.println("jiner");  
  14.         BigDecimal total = totalAmount.multiply(new BigDecimal(100));  
  15.         java.text.DecimalFormat df=new java.text.DecimalFormat("0");  
  16.         parameterMap.put("total_fee", df.format(total));  
  17.         System.out.println("jiner2");  
  18.         parameterMap.put("spbill_create_ip", request.getRemoteAddr());  
  19.         parameterMap.put("notify_url""http://xxx.com");  
  20.         parameterMap.put("trade_type""APP");  
  21.         System.out.println("");  
  22.         String sign = PayCommonUtil.createSign("UTF-8", parameterMap);  
  23.         System.out.println("jiner2");  
  24.         parameterMap.put("sign", sign);  
  25.         String requestXML = PayCommonUtil.getRequestXml(parameterMap);  
  26.         System.out.println(requestXML);  
  27.         String result = PayCommonUtil.httpsRequest(  
  28.                 "https://api.mch.weixin.qq.com/pay/unifiedorder""POST",  
  29.                 requestXML);  
  30.         System.out.println(result);  
  31.         Map<String, String> map = null;  
  32.         try {  
  33.             map = PayCommonUtil.doXMLParse(result);  
  34.         } catch (JDOMException e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         } catch (IOException e) {  
  38.             // TODO Auto-generated catch block  
  39.             e.printStackTrace();  
  40.         }  
  41.         return map;  
  42.     }  


获取prepay_id,二次签名发送结果给app,此方法结合业务自己优化一下,这里只是举个例子

  1. public static String createSignAgain(HttpServletRequest request)  
  2.             Map<String, String> map = weixinPrePay(payment.getSn(), payment.getAmount(),description,  
  3.                 request);  
  4.         JSONObject jsonObject = new JSONObject();  
  5.         SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();  
  6.         parameterMap.put("appid", PayCommonUtil.APPID);  
  7.         parameterMap.put("partnerid", PayCommonUtil.MCH_ID);  
  8.         parameterMap.put("prepayid", map.get("prepay_id"));  
  9.         parameterMap.put("package""Sign=WXPay");  
  10.         parameterMap.put("noncestr", PayCommonUtil.getRandomString(32));  
  11.         parameterMap.put("timestamp", System.currentTimeMillis());  
  12.         String sign = PayCommonUtil.createSign("UTF-8", parameterMap);  
  13.         parameterMap.put("sign", sign);  
  14.         jsonObject.put("parameterMap",parameterMap);  
  15.         return jsonObject.toString();  
  16.     }  


到此统一下下单完成,关于app支付我就不说了,下一篇说一下支付结果通用通知
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值