溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用java如何實(shí)現(xiàn)一個(gè)微信APP支付接口

發(fā)布時(shí)間:2020-11-18 15:29:49 來源:億速云 閱讀:159 作者:Leah 欄目:編程語言

這篇文章給大家介紹利用java如何實(shí)現(xiàn)一個(gè)微信APP支付接口,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

開始開發(fā)

1、配置相關(guān)的配置信息

1.1、配置appid(Android)、mch_id(ios)、微信支付后的回調(diào)地址

sys.properties配置文件:

  appid=wx***************1
  mch_id=1********2
  notify_url=http://6*.***.***.**/returnmsg.do 

//回調(diào)通知的地址,一定是要可以直接訪問的地址

2、微信支付–下單

@ResponseBody
@RequestMapping(value = "/weixinpay.do", produces = "text/html;charset=UTF-8",method={RequestMethod.POST})
  public static String weixinpay(String body, //商品描述
      String detail, //商品詳情
      String attach, //附加數(shù)據(jù),在查詢API和支付通知中原樣返回,該字段主要用于商戶攜帶訂單的自定義數(shù)據(jù)
      String out_trade_no, //商戶系統(tǒng)內(nèi)部的訂單號(hào),32個(gè)字符內(nèi)、可包含字母, 其他說明見商戶訂單號(hào)
      String total_price, //訂單總金額,單位為分,詳見支付金額
      String spbill_create_ip //用戶端實(shí)際ip

      ) throws Exception { 

    WeixinConfigUtils config = new WeixinConfigUtils();
    //參數(shù)組
    String appid = config.appid;//微信開放平臺(tái)審核通過的應(yīng)用APPID
    System.out.println("appid是:"+appid);
    String mch_id = config.mch_id;
    System.out.println("mch_id是:"+mch_id);
    String nonce_str = RandCharsUtils.getRandomString(16);
    System.out.println("隨機(jī)字符串是:"+nonce_str);


    body = body;//"測試微信支付0.01_2";
    detail = detail;//"0.01_元測試開始";
    //attach = attach;//"備用參數(shù),先留著,后面會(huì)有用的";
    //String out_trade_no = OrderUtil.getOrderNo();//"2015112500001000811017342394";

    double totalfee =0;
    try{
      totalfee=Double.parseDouble(total_price);////單位是分,即是0.01元
    }catch (Exception e) {
      totalfee=0;
    }
    int total_fee=(int) (totalfee*100);
    spbill_create_ip = spbill_create_ip;//"127.0.0.1";

    String time_start = RandCharsUtils.timeStart();
    System.out.println(time_start);
    String time_expire = RandCharsUtils.timeExpire();
    System.out.println(time_expire);
    String notify_url = config.notify_url;
    System.out.println("notify_url是:"+notify_url);
    String trade_type = "APP";

    //參數(shù):開始生成簽名
    SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
    parameters.put("appid", appid);
    parameters.put("mch_id", mch_id);
    parameters.put("nonce_str", nonce_str);
    parameters.put("body", body);
    //parameters.put("nonce_str", nonce_str);
    parameters.put("detail", detail);
    parameters.put("attach", attach);
    parameters.put("out_trade_no", out_trade_no);
    parameters.put("total_fee", total_fee);
    parameters.put("time_start", time_start);
    parameters.put("time_expire", time_expire);
    parameters.put("notify_url", notify_url);
    parameters.put("trade_type", trade_type);
    parameters.put("spbill_create_ip", spbill_create_ip);

    String sign = WXSignUtils.createSign("UTF-8", parameters);
    System.out.println("簽名是:"+sign);


    Unifiedorder unifiedorder = new Unifiedorder();
    unifiedorder.setAppid(appid);
    unifiedorder.setMch_id(mch_id);
    unifiedorder.setNonce_str(nonce_str);
    unifiedorder.setSign(sign);
    unifiedorder.setBody(body);
    unifiedorder.setDetail(detail);
    unifiedorder.setAttach(attach);
    unifiedorder.setOut_trade_no(out_trade_no);
    unifiedorder.setTotal_fee(total_fee);
    unifiedorder.setSpbill_create_ip(spbill_create_ip);
    unifiedorder.setTime_start(time_start);
    unifiedorder.setTime_expire(time_expire);
    unifiedorder.setNotify_url(notify_url);
    unifiedorder.setTrade_type(trade_type);

    System.out.println(MD5Utils.md5("fenxiangzhuyi") + "========================");

    //構(gòu)造xml參數(shù)
    String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);

    String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    String method = "POST";

    String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();

    System.out.println(weixinPost);

    ParseXMLUtils.jdomParseXml(weixinPost);



    String json = JsonUtil.xml2jsonString(weixinPost);

    System.out.println("=========================================================");

    Bean b = JsonUtil.getSingleBean(json, Bean.class);
    if(null!=b){
      WeixinOrder weixin = b.getXml();
      //參數(shù):開始生成簽名
      SortedMap<Object,Object> par = new TreeMap<Object,Object>();
      par.put("appid", weixin.getAppid());
      par.put("partnerid", weixin.getMch_id());
      par.put("prepayid", weixin.getPrepay_id());
      par.put("package", "Sign=WXPay");
      par.put("noncestr", weixin.getNonce_str());

      //時(shí)間戳
       Date date = new Date();
       long time = date.getTime();
       //mysq 時(shí)間戳只有10位 要做處理
       String dateline = time + "";
       dateline = dateline.substring(0, 10);

      par.put("timestamp", dateline);

      String signnew = WXSignUtils.createSign("UTF-8", par);
      System.out.println("再次簽名是:"+signnew);


      SetPay setPay = new SetPay();

      setPay.setAppid(weixin.getAppid());
      setPay.setPartnerid(weixin.getMch_id());
      setPay.setPrepayid(weixin.getPrepay_id());
      setPay.setNoncestr(weixin.getNonce_str());

      setPay.setTimestamp(dateline);
      setPay.setSign(signnew);
      setPay.setPack("Sign=WXPay");

      JSONObject js = JSONObject.fromObject(setPay);
      StringBuilder msg = new StringBuilder();
      msg.append("{\"code\":\"1\",");
      msg.append("\"msg\":\"查詢成功!\",");
      msg.append("\"datas\":");
      msg.append(js.toString());
      msg.append("}");

      System.out.println(js);

      return msg.toString();
    }
    StringBuilder msg = new StringBuilder();
    msg.append("{\"code\":\"1\",");
    msg.append("\"msg\":\"查詢成功!\",");
    msg.append("\"datas\":");
    msg.append("支付失??!");
    msg.append("}");


    return msg.toString();

  }

2.1、微信支付簽名算法sign

package com.wx.weixincontroller.pay.weixin.Utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

import com.wx.weixin.utils.MD5Utils;

/**
 * 微信支付簽名
 * @author iYjrg_xiebin
 * @date 2016年10月25日下午4:47:07
 */
public class WXSignUtils {
  //http://mch.weixin.qq.com/wiki/doc/api/index.php&#63;chapter=4_3
  //商戶Key:改成公司申請(qǐng)的即可
  //32位密碼設(shè)置地址:http://www.sexauth.com/ jdex1hvufnm1sdcb0e81t36k0d0f15nc

  private static String Key = "***cb**e**ef**c*e*d***e*fd***cb*";

  /**
   * 微信支付簽名算法sign
   * @param characterEncoding
   * @param parameters
   * @return
   */
  @SuppressWarnings("rawtypes")
  public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){


    StringBuffer sb = new StringBuffer();
    Set es = parameters.entrySet();//所有參與傳參的參數(shù)按照accsii排序(升序)
    Iterator it = es.iterator();
    while(it.hasNext()) {
      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 + "&");
      }
    }
    sb.append("key=" + Key);
    System.out.println("字符串拼接后是:"+sb.toString());
    String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
    return sign;
  }

}

2.2、POST提交XML格式的參數(shù)

package com.wx.weixincontroller.pay.weixin.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import com. com.wx.weixin.wxcontroller.pay.weixin.entity.Unifiedorder;

/**
 * post提交xml格式的參數(shù)
 * @author iYjrg_xiebin
 * @date 2016年10月25日下午3:33:38
 */
public class HttpXmlUtils {

  /**
   * 開始post提交參數(shù)到接口
   * 并接受返回
   * @param url
   * @param xml
   * @param method
   * @param contentType
   * @return
   */
  public static String xmlHttpProxy(String url,String xml,String method,String contentType){
    InputStream is = null;
    OutputStreamWriter os = null;

    try {
      URL _url = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
      conn.setDoInput(true);  
      conn.setDoOutput(true);  
      conn.setRequestProperty("Content-type", "text/xml");
      conn.setRequestProperty("Pragma:", "no-cache"); 
      conn.setRequestProperty("Cache-Control", "no-cache"); 
      conn.setRequestMethod("POST");
      os = new OutputStreamWriter(conn.getOutputStream());
      os.write(new String(xml.getBytes(contentType)));
      os.flush();

      //返回值
      is = conn.getInputStream();
      return getContent(is, "utf-8");
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
      try {
        if(os!=null){os.close();}
        if(is!=null){is.close();}
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }

  /**
   * 解析返回的值
   * @param is
   * @param charset
   * @return
   */
  public static String getContent(InputStream is, String charset) {
    String pageString = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    StringBuffer sb = null;
    try {
      isr = new InputStreamReader(is, charset);
      br = new BufferedReader(isr);
      sb = new StringBuffer();
      String line = null;
      while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
      }
      pageString = sb.toString();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (is != null){
          is.close();
        }
        if(isr!=null){
          isr.close();
        }
        if(br!=null){
          br.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      sb = null;
    }
    return pageString;
  }

  /**
   * 構(gòu)造xml參數(shù)
   * @param xml
   * @return
   */
  public static String xmlInfo(Unifiedorder unifiedorder){
    //構(gòu)造xml參數(shù)的時(shí)候,至少又是個(gè)必傳參數(shù)
    /*
     * <xml>
        <appid>wx2421b1c4370ec43b</appid>
        <attach>支付測試</attach>
        <body>JSAPI支付測試</body>
        <mch_id>10000100</mch_id>
        <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
        <notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php</notify_url>
        <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
        <out_trade_no>1415659990</out_trade_no>
        <spbill_create_ip>14.23.150.211</spbill_create_ip>
        <total_fee>1</total_fee>
        <trade_type>JSAPI</trade_type>
        <sign>0CB01533B8C1EF103065174F50BCA001</sign>
      </xml>
     */

    if(unifiedorder!=null){
      StringBuffer bf = new StringBuffer();
      bf.append("<xml>");

      bf.append("<appid><![CDATA[");
      bf.append(unifiedorder.getAppid());
      bf.append("]]></appid>");

      bf.append("<mch_id><![CDATA[");
      bf.append(unifiedorder.getMch_id());
      bf.append("]]></mch_id>");

      bf.append("<nonce_str><![CDATA[");
      bf.append(unifiedorder.getNonce_str());
      bf.append("]]></nonce_str>");

      bf.append("<sign><![CDATA[");
      bf.append(unifiedorder.getSign());
      bf.append("]]></sign>");

      bf.append("<body><![CDATA[");
      bf.append(unifiedorder.getBody());
      bf.append("]]></body>");

      bf.append("<detail><![CDATA[");
      bf.append(unifiedorder.getDetail());
      bf.append("]]></detail>");

      bf.append("<attach><![CDATA[");
      bf.append(unifiedorder.getAttach());
      bf.append("]]></attach>");

      bf.append("<out_trade_no><![CDATA[");
      bf.append(unifiedorder.getOut_trade_no());
      bf.append("]]></out_trade_no>");

      bf.append("<total_fee><![CDATA[");
      bf.append(unifiedorder.getTotal_fee());
      bf.append("]]></total_fee>");

      bf.append("<spbill_create_ip><![CDATA[");
      bf.append(unifiedorder.getSpbill_create_ip());
      bf.append("]]></spbill_create_ip>");

      bf.append("<time_start><![CDATA[");
      bf.append(unifiedorder.getTime_start());
      bf.append("]]></time_start>");

      bf.append("<time_expire><![CDATA[");
      bf.append(unifiedorder.getTime_expire());
      bf.append("]]></time_expire>");

      bf.append("<notify_url><![CDATA[");
      bf.append(unifiedorder.getNotify_url());
      bf.append("]]></notify_url>");

      bf.append("<trade_type><![CDATA[");
      bf.append(unifiedorder.getTrade_type());
      bf.append("]]></trade_type>");


      bf.append("</xml>");
      return bf.toString();
    }

    return "";
  }




  /**
   * post請(qǐng)求并得到返回結(jié)果
   * @param requestUrl
   * @param requestMethod
   * @param output
   * @return
   */
  public static String httpsRequest(String requestUrl, String requestMethod, String output) {
    try{
      URL url = new URL(requestUrl);
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setUseCaches(false);
      connection.setRequestMethod(requestMethod);
      if (null != output) {
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(output.getBytes("UTF-8"));
        outputStream.close();
      }
      // 從輸入流讀取返回內(nèi)容
      InputStream inputStream = connection.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String str = null;
      StringBuffer buffer = new StringBuffer();
      while ((str = bufferedReader.readLine()) != null) {
        buffer.append(str);
      }
      bufferedReader.close();
      inputStreamReader.close();
      inputStream.close();
      inputStream = null;
      connection.disconnect();
      return buffer.toString();
    }catch(Exception ex){
      ex.printStackTrace();
    }

    return "";
  }

}

3、微信支付–回調(diào)通知業(yè)務(wù)處理

 //通知處理類
 @ResponseBody
 @RequestMapping(value = "/returnmsg.do", produces = "text/html;charset=UTF-8",method={RequestMethod.POST})
    public String returnmsg(HttpServletRequest request, HttpServletResponse response) throws Exception {

      // 解析結(jié)果存儲(chǔ)在HashMap
      Map<String, String> map = new HashMap<String, String>();
      InputStream inputStream = request.getInputStream();

      // 讀取輸入流
      SAXReader reader = new SAXReader();
      Document document = reader.read(inputStream);
      // 得到xml根元素
      Element root = document.getRootElement();
      // 得到根元素的所有子節(jié)點(diǎn)
      List<Element> elementList = root.elements();

      // 遍歷所有子節(jié)點(diǎn)
      for (Element e : elementList) {
        map.put(e.getName(), e.getText());
      }

      JSONObject json = JSONObject.fromObject(map);

      System.out.println("===消息通知的結(jié)果:" + json.toString() + "==========================");
      System.out.println("===return_code===" + map.get("return_code"));
      System.out.println("===return_msg===" + map.get("return_msg"));
      System.out.println("===out_trade_no===" + map.get("out_trade_no"));

      //驗(yàn)證簽名的過程

      //判斷是否支付成功
      if(map.get("return_code").equals("SUCCESS")) {

          /**
          *支付成功之后的業(yè)務(wù)處理
          */

          // 釋放資源
          inputStream.close();
          inputStream = null;


          //bis.close();
          return "SUCCESS";
        }

      }
      if (map.get("return_code").equals("FAIL")) {

        /**
         *支付失敗后的業(yè)務(wù)處理
         */

          // 釋放資源
          inputStream.close();

          inputStream = null;

          return "SUCCESS";
        }


      }

      // 釋放資源
      inputStream.close();
      inputStream = null;

      return "SUCCESS";

    }

關(guān)于利用java如何實(shí)現(xiàn)一個(gè)微信APP支付接口就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI