栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微信公众号支付(二)实现统一下单接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信公众号支付(二)实现统一下单接口

上一篇已经获取到了用户的OpenId

这篇主要是调用微信公众支付的统一下单API

API地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

看文档,主要流程就是把20个左右的参数封装为XML格式发送到微信给的接口地址,然后就可以获取到返回的内容了,如果成功里面就有支付所需要的预支付ID

请求参数就不解释了。

其中,随机字符串:我用的是UUID去中划线

 public static String create_nonce_str() {
 return UUID.randomUUID().toString().replace("-","");
 }

商户订单号:每个订单号只能使用一次,所以用的是系统的订单号加的时间戳。

总金额:不能为

通知地址:微信支付成功或失败回调给系统的地址

签名:

import java.io.Serializable;
 public class PayInfo implements Serializable{
 private static final long serialVersionUID = L;
 private String appid;
 private String mch_id;
 private String device_info;
 private String nonce_str;
 private String sign;
 private String body;
 private String attach;
 private String out_trade_no;
 private int total_fee;
 private String spbill_create_ip;
 private String notify_url;
 private String trade_type;
 private String openid;
 //下面是get,set方法 
 }


 
 public PayInfo createPayInfo(BizOrder bizOrder,String ip,String openId) {
  PayInfo payInfo = new PayInfo();
  payInfo.setAppid(Constants.appid);
  payInfo.setDevice_info("WEB");
  payInfo.setMch_id(Constants.mch_id);
  payInfo.setNonce_str(CommonUtil.create_nonce_str().replace("-", ""));
  payInfo.setBody("这里是某某白米饭的body");
  payInfo.setAttach(bizOrder.getId());
  payInfo.setOut_trade_no(bizOrder.getOrderCode().concat("A").concat(DateFormatUtils.format(new Date(), "MMddHHmmss")));
  payInfo.setTotal_fee((int)bizOrder.getFeeAmount());
  payInfo.setSpbill_create_ip(ip);
  payInfo.setNotify_url(Constants.notify_url);
  payInfo.setTrade_type("JSAPI");
  payInfo.setOpenid(openId);
  return payInfo;
 }

获取签名:


 public String getSign(PayInfo payInfo) throws Exception {
  String signTemp = "appid="+payInfo.getAppid()
   +"&attach="+payInfo.getAttach()
   +"&body="+payInfo.getBody()
   +"&device_info="+payInfo.getDevice_info()
   +"&mch_id="+payInfo.getMch_id()
   +"&nonce_str="+payInfo.getNonce_str()
   +"¬ify_url="+payInfo.getNotify_url()
   +"&openid="+payInfo.getOpenid()
   +"&out_trade_no="+payInfo.getOut_trade_no()
   +"&spbill_create_ip="+payInfo.getSpbill_create_ip()
   +"&total_fee="+payInfo.getTotal_fee()
   +"&trade_type="+payInfo.getTrade_type()
   +"&key="+Constants.key; //这个key注意
 MessageDigest md = MessageDigest.getInstance("MD");
 md.reset();
 md.update(signTemp.getBytes("UTF-"));
 String sign = CommonUtil.byteToStr(md.digest()).toUpperCase();
 return sign;
 }

注意:上面的Constants.key取值在商户号API安全的API密钥中。

一些工具方法:获取ip地址,将字节数组转换为十六进制字符串,将字节转换为十六进制字符串

 
 public static String byteToStr(byte[] byteArray) {
  String strDigest = "";
  for (int i = ; i < byteArray.length; i++) {
  strDigest += byteToHexStr(byteArray[i]);
  }
  return strDigest;
 }
 
 public static String byteToHexStr(byte bytes) {
  char[] Digit = { '', '', '', '', '', '', '', '', '', '', 'A', 'B', 'C', 'D', 'E', 'F' };
  char[] tempArr = new char[];
  tempArr[] = Digit[(bytes >>> ) & XF];
  tempArr[] = Digit[bytes & XF];
  String s = new String(tempArr);
  return s;
 }
 
 public static String getIpAddr(HttpServletRequest request) { 
  InetAddress addr = null; 
  try { 
  addr = InetAddress.getLocalHost(); 
  } catch (UnknownHostException e) { 
  return request.getRemoteAddr(); 
  } 
  byte[] ipAddr = addr.getAddress(); 
  String ipAddrStr = ""; 
  for (int i = ; i < ipAddr.length; i++) { 
  if (i > ) { 
   ipAddrStr += "."; 
  } 
  ipAddrStr += ipAddr[i] & xFF; 
  } 
  return ipAddrStr; 
 } 

这样就获取了签名,把签名与PayInfo中的其他数据转成XML格式,当做参数传递给统一下单地址。

 PayInfo pi = pu.createPayInfo(bo,"...","");
 String sign = pu.getSign(pi);
 pi.setSign(sign);

对象转XML

 
 private static XStream xstream = new XStream(new XppDriver() {
  public HierarchicalStreamWriter createWriter(Writer out) {
  return new PrettyPrintWriter(out) {
   //增加CDATA标记
   boolean cdata = true;
   @SuppressWarnings("rawtypes")
   public void startNode(String name, Class clazz) {
   super.startNode(name, clazz);
   }
   protected void writeText(QuickWriter writer, String text) {
   if (cdata) {
    writer.write("");
   } else {
    writer.write(text);
   }
   }
  };
  }
 });
 public static String payInfoToXML(PayInfo pi) {
  xstream.alias("xml", pi.getClass());
  return xstream.toXML(pi);
 }

xml转Map

 @SuppressWarnings("unchecked")
 public static Map parseXml(String xml) throws Exception {
  Map map = new HashMap();
  document document = documentHelper.parseText(xml);
  Element root = document.getRootElement();
  List elementList = root.elements();
  for (Element e : elementList)
  map.put(e.getName(), e.getText());
  return map;
 }

下面就是调用统一下单的URL了

log.info(MessageUtil.payInfoToXML(pi).replace("__", "_"));
   Map map = CommonUtil.httpsRequestToXML("https://api.mch.weixin.qq.com/pay/unifiedorder", "POST", MessageUtil.payInfoToXML(pi).replace("__", "_").replace("", ""));
 log.info(map);

 public static Map httpsRequestToXML(String requestUrl, String requestMethod, String outputStr) {
  Map result = new HashMap<>();
  try {
  StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr);
  result = MessageUtil.parseXml(buffer.toString());
  } catch (ConnectException ce) {
  log.error("连接超时:"+ce.getMessage());
  } catch (Exception e) {
  log.error("https请求异常:"+ece.getMessage());
  }
  return result;
 }

httpsRequest()这个方法在第一篇中

上面获取到的Map如果成功的话,里面就会有

String return_code = map.get("return_code");
 if(StringUtils.isNotBlank(return_code) && return_code.equals("SUCCESS")){
   String return_msg = map.get("return_msg");
 if(StringUtils.isNotBlank(return_msg) && !return_msg.equals("OK")) {
   return "统一下单错误!";
 }
 }else{
   return "统一下单错误!";
 }
 String prepay_Id = map.get("prepay_id");

这个prepay_id就是预支付的ID。后面支付需要它。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151271.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号