欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序 支付后台java实现实例

程序员文章站 2022-10-31 13:38:07
微信小程序 支付后台java实现实例 前言: 前些天使用 leancloud 云引擎写了个小程序的支付相关 以前只做过 app 支付 这次在小程序支付爬了两天的坑 把代...

微信小程序 支付后台java实现实例

前言:

前些天使用 leancloud 云引擎写了个小程序的支付相关 以前只做过 app 支付 这次在小程序支付爬了两天的坑 把代码也分享出来

支付流程:

1.小程序前端获取微信 openid 以及订单号 传给后台
2,后台根据 openid 和订单号进行签名 post 微信统一下单接口
3.后台获取微信返回的xml字符串 解析 二次签名以后返回给前端
4.前端调起支付微信支付 api

先看支付函数:

//获取支付信息
  @enginefunction("getpayinformation")
  public static map<string, object> getpayinformation(
      @enginefunctionparam("orderid") string orderid
  ) throws avexception, unsupportedencodingexception, documentexception {
    map<string, object> reqmap = new treemap<string, object>(
        new comparator<string>() {
          public int compare(string obj1, string obj2) {
            // 升序排序
            return obj1.compareto(obj2);
          }
        });
    if (avuser.getcurrentuser() != null) {
      string authdatajson = jsonarray.tojsonstring(avuser.getcurrentuser().get("authdata"));
      jsonobject jsonobject = json.parseobject(authdatajson);
      jsonobject.get("lc_weapp");
      jsonobject j2 = json.parseobject(jsonobject.get("lc_weapp").tostring());
      string openid = (string) j2.get("openid");

      avquery<order> query = avobject.getquery(order.class);
      order order = query.get(orderid);
      reqmap.put("appid", system.getenv("appid"));
      reqmap.put("mch_id", system.getenv("mch_id"));
      reqmap.put("nonce_str", wxpayutil.getnonce_str());
      reqmap.put("body", new string(order.getdisheslist().tostring().getbytes("utf-8")));
      reqmap.put("openid", openid);
      reqmap.put("out_trade_no", order.getobjectid());
      reqmap.put("total_fee", 1); //订单总金额,单位为分
      reqmap.put("spbill_create_ip", "192.168.0.1"); //用户端ip
      reqmap.put("notify_url", system.getenv("notify_url")); //通知地址
      reqmap.put("trade_type", system.getenv("trade_type")); //trade_type=jsapi时(即公众号支付),此参数必传,此参数为微信用户在商户对应appid下的唯一标识
      string reqstr = wxpayutil.map2xml(reqmap);
      string resultxml = httprequest.sendpost(reqstr);
      system.out.println("微信请求返回:" + resultxml);
      //解析微信返回串 如果状态成功 则返回给前端
      if (wxpayutil.getreturncode(resultxml) != null && wxpayutil.getreturncode(resultxml).equals("success")) {
        //成功
        map<string, object> resultmap = new treemap<>(
            new comparator<string>() {
              public int compare(string obj1, string obj2) {
                // 升序排序
                return obj1.compareto(obj2);
              }
            });
        resultmap.put("appid", system.getenv("appid"));
        resultmap.put("noncestr", wxpayutil.getnoncestr(resultxml));//解析随机字符串
        resultmap.put("package", "prepay_id=" + wxpayutil.getprepayid(resultxml));
        resultmap.put("signtype", "md5");
        resultmap.put("timestamp", string.valueof((system.currenttimemillis() / 1000)));//时间戳
        string paysign = wxpayutil.getsign(resultmap);
        resultmap.put("paysign", paysign);
        return resultmap;
      } else {
        throw new avexception(999, "微信请求支付失败");
      }
    } else {
      throw new avexception(98, "当前未登录用户");
    }
  }

其中appid和mch_id可以用系统常量

ps:这里注意一个坑

二次签名的时候使用 appid noncestr package signtype timestamp 这五个key生成签名(这里无视微信官方文档 以及注意 appid 的大小写)

前端调起api支付时 按照官方文档就可以

网络请求类:

httprequest

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.url;
import java.net.urlconnection;
import java.util.list;
import java.util.map;

public class httprequest {
  /**
   * 向指定url发送get方法的请求
   *
   * @param url  发送请求的url
   * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
   * @return url 所代表远程资源的响应结果
   */
  public static string sendget(string url, string param) {
    string result = "";
    bufferedreader in = null;
    try {
      string urlnamestring = url + "?" + param;
      url realurl = new url(urlnamestring);
      // 打开和url之间的连接
      urlconnection connection = realurl.openconnection();
      // 设置通用的请求属性
      connection.setrequestproperty("accept", "*/*");
      connection.setrequestproperty("connection", "keep-alive");
      connection.setrequestproperty("user-agent",
          "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
      // 建立实际的连接
      connection.connect();
      // 获取所有响应头字段
      map<string, list<string>> map = connection.getheaderfields();
      // 遍历所有的响应头字段
      for (string key : map.keyset()) {
        system.out.println(key + "--->" + map.get(key));
      }
      // 定义 bufferedreader输入流来读取url的响应
      in = new bufferedreader(new inputstreamreader(
          connection.getinputstream()));
      string line;
      while ((line = in.readline()) != null) {
        result += line;
      }
    } catch (exception e) {
      system.out.println("发送get请求出现异常!" + e);
      e.printstacktrace();
    }
    // 使用finally块来关闭输入流
    finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (exception e2) {
        e2.printstacktrace();
      }
    }
    return result;
  }

  /**
   * 向指定 url 发送post方法的请求
   *
   * @param url  发送请求的 url
   * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
   * @return 所代表远程资源的响应结果
   */
  public static string sendpost(string param) {
    printwriter out = null;
    bufferedreader in = null;
    string result = "";
    try {
      url realurl = new url("https://api.mch.weixin.qq.com/pay/unifiedorder");
      // 打开和url之间的连接
      urlconnection conn = realurl.openconnection();
      // 设置通用的请求属性
      conn.setrequestproperty("accept", "*/*");
      conn.setrequestproperty("connection", "keep-alive");
      conn.setrequestproperty("user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
//      conn.setrequestproperty("pragma:", "no-cache");
//      conn.setrequestproperty("cache-control", "no-cache");
//      conn.setrequestproperty("content-type", "text/xml;charset=utf-8");
      // 发送post请求必须设置如下两行
      conn.setdooutput(true);
      conn.setdoinput(true);
      // 获取urlconnection对象对应的输出流
      out = new printwriter(conn.getoutputstream());
      // 发送请求参数
      out.print(param);
      // flush输出流的缓冲
      out.flush();
      // 定义bufferedreader输入流来读取url的响应
      in = new bufferedreader(
          new inputstreamreader(conn.getinputstream()));
      string line;
      while ((line = in.readline()) != null) {
        result += line;
      }
    } catch (exception e) {
      system.out.println("发送 post 请求出现异常!" + e);
      e.printstacktrace();
    }
    //使用finally块来关闭输出流、输入流
    finally {
      try {
        if (out != null) {
          out.close();
        }
        if (in != null) {
          in.close();
        }
      } catch (ioexception ex) {
        ex.printstacktrace();
      }
    }
    return result;
  }
}

xml解析工具类

wxpayutil

import org.w3c.dom.nodelist;

import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import java.io.bytearrayinputstream;
import java.io.inputstream;
import java.io.unsupportedencodingexception;
import java.util.map;
import java.util.random;


public class wxpayutil {

  //生成随机字符串
  public static string getnonce_str() {
    string base = "abcdefghijklmnopqrstuvwxyz0123456789";
    random random = new random();
    stringbuilder sb = new stringbuilder();
    for (int i = 0; i < 15; i++) {
      int number = random.nextint(base.length());
      sb.append(base.charat(number));
    }
    return sb.tostring();
  }

  //map转xml 加上签名信息
  public static string map2xml(map<string, object> map) throws unsupportedencodingexception {
    stringbuffer sb = new stringbuffer();
    stringbuilder sb2 = new stringbuilder();
    sb2.append("<xml>");
    for (string key : map.keyset()) {
      sb.append(key);
      sb.append('=');
      sb.append(map.get(key));
      sb.append('&');
      // sb2是用来做请求的xml参数
      sb2.append("<" + key + ">");
//      sb2.append("<![cdata[" + map.get(key) + "]]>");
      sb2.append(map.get(key));
      sb2.append("</" + key + ">");
    }
    sb.append(system.getenv("signkey"));
    string sign = md5.getmessagedigest(sb.tostring().getbytes()).touppercase();
    sb2.append("<sign>");
    sb2.append(sign);
    sb2.append("</sign>");
    sb2.append("</xml>");
    return sb2.tostring();
  }

  //解析微信返回return_code success或file
  //根据微信返回resultxml再次生成签名
  public static string getsign(map<string, object> map) {
    stringbuffer sb = new stringbuffer();
    for (string key : map.keyset()) {
      sb.append(key);
      sb.append('=');
      sb.append(map.get(key));
      sb.append('&');
    }
    sb.append(system.getenv("signkey"));
    system.out.println("第二次签名内容:" + sb);
    system.out.println("第二次签名sing:" + md5.getmessagedigest(sb.tostring().getbytes()).touppercase());
    return md5.getmessagedigest(sb.tostring().getbytes()).touppercase();
  }

  //解析微信返回return_code success或file
  public static string getreturncode(string resultxml) {
    string noncestr;
    documentbuilderfactory dbf = documentbuilderfactory.newinstance();
    documentbuilder builder;
    try {
      builder = dbf.newdocumentbuilder();
      inputstream inputstream = new bytearrayinputstream(resultxml.getbytes());
      org.w3c.dom.document doc = builder.parse(inputstream); //
      // 下面开始读取
      org.w3c.dom.element root = doc.getdocumentelement(); // 获取根元素
      nodelist nl = root.getelementsbytagname("return_code");
      org.w3c.dom.element el = (org.w3c.dom.element) nl.item(0);
      org.w3c.dom.node nd = el.getfirstchild();
      noncestr = nd.getnodevalue();
      return noncestr;
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }

  //解析微信返回return_msg
  public static string getreturn_msg(string resultxml) {
    string noncestr;
    documentbuilderfactory dbf = documentbuilderfactory.newinstance();
    documentbuilder builder;
    try {
      builder = dbf.newdocumentbuilder();
      inputstream inputstream = new bytearrayinputstream(resultxml.getbytes());
      org.w3c.dom.document doc = builder.parse(inputstream); //
      // 下面开始读取
      org.w3c.dom.element root = doc.getdocumentelement(); // 获取根元素
      nodelist nl = root.getelementsbytagname("return_msg");
      org.w3c.dom.element el = (org.w3c.dom.element) nl.item(0);
      org.w3c.dom.node nd = el.getfirstchild();
      noncestr = nd.getnodevalue();
      return noncestr;
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }

  //解析微信返回appid
  public static string getappid(string resultxml) {
    string noncestr;
    documentbuilderfactory dbf = documentbuilderfactory.newinstance();
    documentbuilder builder;
    try {
      builder = dbf.newdocumentbuilder();
      inputstream inputstream = new bytearrayinputstream(resultxml.getbytes());
      org.w3c.dom.document doc = builder.parse(inputstream); //
      // 下面开始读取
      org.w3c.dom.element root = doc.getdocumentelement(); // 获取根元素
      nodelist nl = root.getelementsbytagname("appid");
      org.w3c.dom.element el = (org.w3c.dom.element) nl.item(0);
      org.w3c.dom.node nd = el.getfirstchild();
      noncestr = nd.getnodevalue();
      return noncestr;
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }

  //解析微信返回mch_id
  public static string getmchid(string resultxml) {
    string noncestr;
    documentbuilderfactory dbf = documentbuilderfactory.newinstance();
    documentbuilder builder;
    try {
      builder = dbf.newdocumentbuilder();
      inputstream inputstream = new bytearrayinputstream(resultxml.getbytes());
      org.w3c.dom.document doc = builder.parse(inputstream); //
      // 下面开始读取
      org.w3c.dom.element root = doc.getdocumentelement(); // 获取根元素
      nodelist nl = root.getelementsbytagname("mch_id");
      org.w3c.dom.element el = (org.w3c.dom.element) nl.item(0);
      org.w3c.dom.node nd = el.getfirstchild();
      noncestr = nd.getnodevalue();
      return noncestr;
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }

  //解析微信返回nonce_str
  public static string getnoncestr(string resultxml) {
    string noncestr;
    documentbuilderfactory dbf = documentbuilderfactory.newinstance();
    documentbuilder builder;
    try {
      builder = dbf.newdocumentbuilder();
      inputstream inputstream = new bytearrayinputstream(resultxml.getbytes());
      org.w3c.dom.document doc = builder.parse(inputstream); //
      // 下面开始读取
      org.w3c.dom.element root = doc.getdocumentelement(); // 获取根元素
      nodelist nl = root.getelementsbytagname("nonce_str");
      org.w3c.dom.element el = (org.w3c.dom.element) nl.item(0);
      org.w3c.dom.node nd = el.getfirstchild();
      noncestr = nd.getnodevalue();
      return noncestr;
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }

  //解析微信返回prepay_id
  public static string getprepayid(string resultxml) {
    string noncestr;
    documentbuilderfactory dbf = documentbuilderfactory.newinstance();
    documentbuilder builder;
    try {
      builder = dbf.newdocumentbuilder();
      inputstream inputstream = new bytearrayinputstream(resultxml.getbytes());
      org.w3c.dom.document doc = builder.parse(inputstream); //
      // 下面开始读取
      org.w3c.dom.element root = doc.getdocumentelement(); // 获取根元素
      nodelist nl = root.getelementsbytagname("prepay_id");
      org.w3c.dom.element el = (org.w3c.dom.element) nl.item(0);
      org.w3c.dom.node nd = el.getfirstchild();
      noncestr = nd.getnodevalue();
      return noncestr;
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }


}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!