微信APP支付-Java后台实现

Posted 女装大佬大老李

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信APP支付-Java后台实现相关的知识,希望对你有一定的参考价值。

转自:http://blog.csdn.net/walle167/article/details/50957503

1 注册微信开放者平台

开发者平台地址:https://open.weixin.qq.com/

2 成为开发者

3 上传APP

4 开通微信支付功能

5 设置商户号信息

APP 支付能力开通后,微信会给你一个商户号,用户和密码等信息。需要验证商户信息,还需要设置一个加密的密钥字段,这里就不一一细说了。

6 开发接口

微信APP支付接口,是很好调试的,(不像微信公众平台,需要80端口),可以直接在本地就可以进行调试。 具体业务就不细说,直接看代码就懂了。

1 基础信息配置
package com.qx.client.common.pay.weichart.config;

import java.util.Properties;

import com.tom.util.properties.PropertiesUtil;
import com.tom.util.system.RSystemConfig;


public class WeiChartConfig 

   /**
    * 预支付请求地址
    */
   public static final String  PrepayUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

   /**
    * 查询订单地址
    */
   public static final String  OrderUrl = "https://api.mch.weixin.qq.com/pay/orderquery";

   /**
    * 关闭订单地址
    */
   public static final String  CloseOrderUrl = "https://api.mch.weixin.qq.com/pay/closeorder";

   /**
    * 申请退款地址
    */
   public static final String  RefundUrl = "https://api.mch.weixin.qq.com/secapi/pay/refund";

   /**
    * 查询退款地址
    */
   public static final String  RefundQueryUrl = "https://api.mch.weixin.qq.com/pay/refundquery";

   /**
    * 下载账单地址
    */
   public static final String  DownloadBillUrl = "https://api.mch.weixin.qq.com/pay/downloadbill";

   /**
    * 商户APPID
    */
   public static final String  AppId = "wxabcdefghjjsdfsd";

   /**
    * 商户账户 获取支付能力后,从邮件中得到
    */
   public static final String  MchId = "13000000000001";

   /**
    * 商户秘钥  32位,在微信商户平台中设置
    */
   public static final String  AppSercret = "qx12345645778679789";

   /**
    * 服务器异步通知页面路径
    */
   public static String notify_url = getProperties().getProperty("notify_url");

   /**
    * 页面跳转同步通知页面路径
    */
   public static String return_url = getProperties().getProperty("return_url");

   /**
    * 退款通知地址
    */
   public static String refund_notify_url = getProperties().getProperty("refund_notify_url");

   /**
    * 退款需要证书文件,证书文件的地址
    */
   public static String refund_file_path = getProperties().getProperty("refund_file_path");

   /**
    * 商品名称
    */
   public static String subject =  getProperties().getProperty("subject");

   /**
    * 商品描述
    */
   public static String body = getProperties().getProperty("body");

   private static  Properties properties;

   public static synchronized Properties getProperties()
      if(properties == null)
         String path = System.getenv(RSystemConfig.KEY_WEB_HOME_CONF) + "/weichart.properties";
         properties = PropertiesUtil.getInstance().getProperties(path);
      
      return properties;
   



 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
2 http /https请求的工具类

其中有需要证书的,也有不需要证书的。 
证书是在需要退款接口的时候需要使用,直接把证书放在服务器上,然后传路径

package com.qx.client.common.pay.weichart.util.httpClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyStore;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil

   public static String post(String url,
                             Map<String, String> headMap,
                             Map<String, String> params)
      try
         HttpClient httpclient = new HttpClient();
         httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
         PostMethod httpPost = new PostMethod(url);
         if(null != headMap)
            for(String key : headMap.keySet())
               httpPost.setRequestHeader(key, headMap.get(key));
            
         

         if(null != params)
            for(String pkey : params.keySet())
               httpPost.addParameter(pkey, params.get(pkey));
            
         
         httpclient.executeMethod(httpPost);

         BufferedReader reader = new BufferedReader(new InputStreamReader(httpPost.getResponseBodyAsStream()));
         StringBuffer stringBuffer = new StringBuffer();
         String str = "";
         while((str = reader.readLine()) != null)
            stringBuffer.append(str);
         
         reader.close();
         return stringBuffer.toString();
      catch(Exception e)
         e.printStackTrace();
      
      return null;
   

   public static String postHttplient(String url,
                                      String xmlInfo)
      try
         HttpClient httpclient = new HttpClient();
         httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
         PostMethod httpPost = new PostMethod(url);
         httpPost.setRequestEntity(new StringRequestEntity(xmlInfo));
         httpclient.executeMethod(httpPost);

         BufferedReader reader = new BufferedReader(new InputStreamReader(httpPost.getResponseBodyAsStream()));
         StringBuffer stringBuffer = new StringBuffer();
         String str = "";
         while((str = reader.readLine()) != null)
            stringBuffer.append(str);
         
         reader.close();
         return stringBuffer.toString();
      catch(Exception e)
         e.printStackTrace();
      
      return null;
   

   /**
    * 需要加密执行的
    * @param url
    * @param xmlInfo
    * @return
    * @throws Exception 
    */
   public static String postHttplientNeedSSL(String url,
                                             String xmlInfo,
                                             String cretPath,
                                             String mrchId)
         throws Exception
      //选择初始化密钥文件格式
      KeyStore keyStore = KeyStore.getInstance("PKCS12");
      //得到密钥文件流
      FileInputStream instream = new FileInputStream(new File(cretPath));
      try
         //用商户的ID 来解读文件
         keyStore.load(instream, mrchId.toCharArray());
      finally
         instream.close();
      
      //用商户的ID 来加载
      SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mrchId.toCharArray()).build();
      // Allow TLSv1 protocol only
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]  "TLSv1" , null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
      //用最新的httpclient 加载密钥
      CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
      StringBuffer ret = new StringBuffer();
      try
         HttpPost httpPost = new HttpPost(url);
         httpPost.setEntity(new StringEntity(xmlInfo));
         CloseableHttpResponse response = httpclient.execute(httpPost);
         try
            HttpEntity entity = response.getEntity();
            if(entity != null)
               BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
               String text;
               while((text = bufferedReader.readLine()) != null)
                  ret.append(text);
               
            
            EntityUtils.consume(entity);
         finally
            response.close();
         
      finally
         httpclient.close();
      
      return ret.toString();
   

   public static String postHtpps(String urlStr,
                                  String xmlInfo)
      try
         URL url = new URL(urlStr);
         URLConnection con = url.openConnection();
         con.setDoOutput(true);
         con.setRequestProperty("Pragma:", "no-cache");
         con.setRequestProperty("Cache-Control", "no-cache");
         con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
         OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "utf-8");
         out.write(xmlInfo);
         out.flush();
         out.close();
         BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
         StringBuffer lines = new StringBuffer();
         String line = "";
         for(line = br.readLine(); line != null; line = br.readLine())
            lines.append(line);
         
         return lines.toString();
      catch(MalformedURLException e)
         e.printStackTrace();
      catch(IOException e)
         e.printStackTrace();
      
      return null;