Java微信支付之公众号支付、扫码支付实例

微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前开发过的微信支付。

一 、H5公众号支付

要点:正确获取openId以及统一下单接口,正确处理支付结果通知,正确配置支付授权目录

H5的支付方式是使用较为广泛的方式,这种支付方式主要用于微信内自定义菜单的网页,依赖手机上安装的微信客户端,高版本的微信才支持微信支付,下面按我的流程注意说明

1  编写用于支付的页面,由于是测试用就写的简单了点

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>"> 

 <title>微信支付样例</title> 

 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 --> 

 </head> 

 <body>
 <form action="oauthServlet" method="POST">
    订单号:<input type="text" name="orderNo" />
  <input type="submit" value="H5支付"/>
 </form>
 </br></br>
  <form action="scanCodePayServlet?flag=createCode" method="POST">
    订单号:<input type="text" name="orderNo" />
  <input type="submit" value="扫码支付"/>
 </form>
 </body>
</html>

2  编写一个servlet用于通过Oauth获取code

package com.debug.weixin.servlet; 

import java.io.IOException;
import java.io.PrintWriter; 

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import com.debug.weixin.util.CommonUtil;
import com.debug.weixin.util.ServerConfig; 

public class OauthServlet extends HttpServlet { 

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException { 

  this.doPost(request, response);
 } 

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException { 

   String orderNo=request.getParameter("orderNo");
   //调用微信Oauth2.0获取openid
   String redirectURL=ServerConfig.SERVERDOMAIN+"/BasicWeixin/payServletForH5?orderNo="+orderNo;
   String redirectURI="";
   try {
    redirectURI=CommonUtil.initOpenId(redirectURL);
   } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   }
   //System.out.println(redirectURI);
   //RequestDispatcher dis= request.getRequestDispatcher(redirectURI);
   //dis.forward(request, response);
   response.sendRedirect(redirectURI);
 } 

}

3 获取到code后,通过REDIRECTURI获取openId,调用统一下单接口

package com.debug.weixin.servlet; 

import java.io.IOException;
import java.io.PrintWriter;
import java.util.SortedMap;
import java.util.TreeMap; 

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import com.debug.weixin.pojo.WeixinOauth2Token;
import com.debug.weixin.pojo.WeixinQRCode;
import com.debug.weixin.util.AdvancedUtil;
import com.debug.weixin.util.CommonUtil;
import com.debug.weixin.util.ConfigUtil;
import com.debug.weixin.util.PayCommonUtil; 

public class PayServletForH5 extends HttpServlet { 

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException { 

  this.doPost(request, response);
 } 

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   String orderNo=request.getParameter("orderNo");
   String code=request.getParameter("code"); 

   //获取AccessToken 

   WeixinOauth2Token token=AdvancedUtil.getOauth2AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code); 

   String openId=token.getOpenId(); 

   //调用微信统一支付接口
   SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
  parameters.put("appid", ConfigUtil.APPID); 

  parameters.put("mch_id", ConfigUtil.MCH_ID);
  parameters.put("device_info", "1000");
  parameters.put("body", "我的测试订单");
  parameters.put("nonce_str", PayCommonUtil.CreateNoncestr()); 

  parameters.put("out_trade_no", orderNo);
  //parameters.put("total_fee", String.valueOf(total));
  parameters.put("total_fee", "1");
  parameters.put("spbill_create_ip", request.getRemoteAddr());
  parameters.put("notify_url", ConfigUtil.NOTIFY_URL);
  parameters.put("trade_type", "JSAPI");
  parameters.put("openid", openId); 

  String sign = PayCommonUtil.createSign("UTF-8", parameters);
  parameters.put("sign", sign); 

  String requestXML = PayCommonUtil.getRequestXml(parameters); 

  String result = CommonUtil.httpsRequestForStr(ConfigUtil.UNIFIED_ORDER_URL,"POST", requestXML);
  System.out.println("----------------------------------");
  System.out.println(result);
  System.out.println("----------------------------------"); 

  request.setAttribute("orderNo", orderNo);
  request.setAttribute("totalPrice", "0.01");
  String payJSON="";
  try {
   payJSON=CommonUtil.getH5PayStr(result,request); 

  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //System.out.println(payJSON);
  request.setAttribute("unifiedOrder",payJSON); 

  RequestDispatcher dis= request.getRequestDispatcher("h5Pay.jsp");
  dis.forward(request, response);
 } 

}

调用微信统一下单接口,需要注意签名算法,只有签名计算正确才能顺利支付

public static String getH5PayStr(String result,HttpServletRequest request) throws Exception{ 

   Map<String, String> map = XMLUtil.doXMLParse(result); 

    SortedMap<Object,Object> params = new TreeMap<Object,Object>();
   params.put("appId", ConfigUtil.APPID);
   params.put("timeStamp", Long.toString(new Date().getTime()));
   params.put("nonceStr", PayCommonUtil.CreateNoncestr());
   params.put("package", "prepay_id="+map.get("prepay_id"));
   params.put("signType", ConfigUtil.SIGN_TYPE);
   String paySign = PayCommonUtil.createSign("UTF-8", params); 

   params.put("paySign", paySign);  //paySign的生成规则和Sign的生成规则一致 

   String json = JSONObject.fromObject(params).toString(); 

   return json;
 }

4 编写最终的支付界面调起微信H5支付

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>"> 

 <title>微信H5支付</title> 

 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  <script type="text/javascript"> 

 function jsApiCall(){
  WeixinJSBridge.invoke(
   'getBrandWCPayRequest',<%=(String)request.getAttribute("unifiedOrder")%>, function(res){
    WeixinJSBridge.log(res.err_msg);
    //alert(res.err_code+res.err_desc+res.err_msg);
    if(res.err_msg == "get_brand_wcpay_request:ok" ) {
     alert("恭喜你,支付成功!");
    }else{
     alert(res.err_code+res.err_desc+res.err_msg);
    }
   }
  );
 } 

 function callpay(){
  if (typeof WeixinJSBridge == "undefined"){
   if( document.addEventListener ){
    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
   }else if (document.attachEvent){
    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
   }
  }else{
   jsApiCall();
  }
 }
 </script>
 </head> 

 <body>
  <input type="button" value="支付" onclick="callpay()"/>
 </body>
</html>

5 处理微信支付结果通知

package com.debug.weixin.servlet; 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import org.jdom.JDOMException; 

import com.debug.weixin.util.PayCommonUtil;
import com.debug.weixin.util.XMLUtil; 

public class PayHandlerServlet extends HttpServlet { 

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   this.doPost(request, response);
 } 

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException { 

  InputStream inStream = request.getInputStream();
  ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = inStream.read(buffer)) != -1) {
   outSteam.write(buffer, 0, len);
  } 

  outSteam.close();
  inStream.close();
  String result = new String(outSteam.toByteArray(),"utf-8");//获取微信调用我们notify_url的返回信息
  Map<Object, Object> map=null;
  try {
   map = XMLUtil.doXMLParse(result);
  } catch (JDOMException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  for(Object keyValue : map.keySet()){
   System.out.println(keyValue+"="+map.get(keyValue));
  }
  if (map.get("result_code").toString().equalsIgnoreCase("SUCCESS")) { 

   //对订单进行业务操作
   System.out.println("-------------OK");
   response.getWriter().write(PayCommonUtil.setXML("SUCCESS", "")); //告诉微信服务器,我收到信息了,不要在调用回调action了 

  }
 } 

}

对于上面的代码,有很多都是参考http://blog.csdn.net/u011160656/article/details/41759195,因此这部分的代码就不贴出来了,需要的话看这个博客就知道了。

二  微信扫码支付(模式一)

要点:必须调用长链接转短链接接口、正确配置扫码支付回调URL

1 根据订单号生成微信支付二维码

下面是几个生成二维码的方法:

package com.debug.weixin.util;
import com.google.zxing.common.BitMatrix; 

 import javax.imageio.ImageIO;
 import java.io.File;
 import java.io.OutputStream;
 import java.io.IOException;
 import java.awt.image.BufferedImage; 

 public final class MatrixToImageWriter { 

 private static final int BLACK = 0xFF000000;
 private static final int WHITE = 0xFFFFFFFF; 

 private MatrixToImageWriter() {} 

 public static BufferedImage toBufferedImage(BitMatrix matrix) {
  int width = matrix.getWidth();
  int height = matrix.getHeight();
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
   image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
  }
  }
  return image;
 } 

 public static void writeToFile(BitMatrix matrix, String format, File file)
  throws IOException {
  BufferedImage image = toBufferedImage(matrix);
  if (!ImageIO.write(image, format, file)) {
  throw new IOException("Could not write an image of format " + format + " to " + file);
  }
 } 

 public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
  throws IOException {
  BufferedImage image = toBufferedImage(matrix);
  if (!ImageIO.write(image, format, stream)) {
  throw new IOException("Could not write an image of format " + format);
  }
 } 

 }

这个算是工具类,还有一个就是把二维码显示在界面上的方法,CreateQRCode主要用到代码块:

 public static void createCodeStream(String text,HttpServletResponse response) throws Exception{ 

  // response.setContentType("image/jpeg");
  ServletOutputStream sos = response.getOutputStream(); 

  int width = 500;
  int height = 500;
  //二维码的图片格式
  String format = "jpg";
  MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
  Map hints = new HashMap();
  //内容所使用编码
  hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints); 

  //生成二维码 

  MatrixToImageWriter.writeToStream(bitMatrix, format,sos); 

  sos.close(); 

 }

2 长链接转短链接生成二维码,编写扫码支付回调方法并调用统一下单接口

 package com.debug.weixin.servlet; 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import org.jdom.JDOMException; 

import com.debug.weixin.util.CommonUtil;
import com.debug.weixin.util.ConfigUtil;
import com.debug.weixin.util.CreateQRCode;
import com.debug.weixin.util.PayCommonUtil;
import com.debug.weixin.util.XMLUtil;
import com.mongodb.DBObject; 

public class ScanCodePayServlet extends HttpServlet { 

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doPost(request, response); 

 } 

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException { 

  String flag=request.getParameter("flag");
  if("createCode".equals(flag)){
   createPayCode(request,response);
  }else{
   try {
    wxScanCodeHandler(request,response);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } 

 } 

 public void createPayCode(HttpServletRequest request,HttpServletResponse response){ 

  String orderNo=request.getParameter("orderNo"); 

  SortedMap<Object,Object> paras = new TreeMap<Object,Object>();
  paras.put("appid", ConfigUtil.APPID);
  paras.put("mch_id", ConfigUtil.MCH_ID);
  paras.put("time_stamp", Long.toString(new Date().getTime()));
  paras.put("nonce_str", PayCommonUtil.CreateNoncestr());
  paras.put("product_id", orderNo);//商品号要唯一
  String sign = PayCommonUtil.createSign("UTF-8", paras);
  paras.put("sign", sign); 

  String url = "weixin://wxpay/bizpayurl?sign=SIGN&appid=APPID&mch_id=MCHID&product_id=PRODUCTID&time_stamp=TIMESTAMP&nonce_str=NOCESTR";
  String nativeUrl = url.replace("SIGN", sign).replace("APPID", ConfigUtil.APPID).replace("MCHID", ConfigUtil.MCH_ID).replace("PRODUCTID", (String)paras.get("product_id")).replace("TIMESTAMP", (String)paras.get("time_stamp")).replace("NOCESTR", (String)paras.get("nonce_str")); 

   SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
   parameters.put("appid", ConfigUtil.APPID);
   parameters.put("mch_id", ConfigUtil.MCH_ID);
   parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());
   parameters.put("long_url", CommonUtil.urlEncodeUTF8(nativeUrl));
   String sign2 = PayCommonUtil.createSign("UTF-8", parameters);
   parameters.put("sign", sign2);
   String requestXML = PayCommonUtil.getRequestXml(parameters);
   String result =CommonUtil.httpsRequestForStr(ConfigUtil.SHORT_URL, "POST", requestXML); 

   Map<String, String> map=null;
  try {
   map = XMLUtil.doXMLParse(result);
  } catch (JDOMException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   String returnCode = map.get("return_code");
   String resultCode = map.get("result_code"); 

   if(returnCode.equalsIgnoreCase("SUCCESS")&&resultCode.equalsIgnoreCase("SUCCESS")){ 

    String shortUrl = map.get("short_url");
    //TODO 拿到shortUrl,写代码生成二维码
    System.out.println("shortUrl="+shortUrl);
    try {
    CreateQRCode.createCodeStream(shortUrl,response);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
  }
 } 

 public void wxScanCodeHandler(HttpServletRequest request,HttpServletResponse response) throws Exception {
  InputStream inStream = request.getInputStream();
  ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = inStream.read(buffer)) != -1) {
   outSteam.write(buffer, 0, len);
  } 

  outSteam.close();
  inStream.close();
  String result = new String(outSteam.toByteArray(),"utf-8");//获取微信调用我们notify_url的返回信息
  Map<Object, Object> map=null;
  try {
   map = XMLUtil.doXMLParse(result);
  } catch (JDOMException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  for(Object keyValue : map.keySet()){
   System.out.println(keyValue+"="+map.get(keyValue));
  }
  String orderNo=map.get("product_id").toString(); 

  //接收到请求参数后调用统一下单接口
  SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
  parameters.put("appid", ConfigUtil.APPID); 

  parameters.put("mch_id", ConfigUtil.MCH_ID);
  parameters.put("device_info", "1000");
  parameters.put("body", "测试扫码支付订单");
  parameters.put("nonce_str", PayCommonUtil.CreateNoncestr()); 

  parameters.put("out_trade_no", map.get("product_id"));
  //parameters.put("total_fee", String.valueOf(totalPrice));
  parameters.put("total_fee", "1");
  parameters.put("spbill_create_ip", request.getRemoteAddr());
  parameters.put("notify_url", ConfigUtil.NOTIFY_URL);
  parameters.put("trade_type", "NATIVE");
  parameters.put("openid", map.get("openid")); 

  String sign = PayCommonUtil.createSign("UTF-8", parameters); 

  parameters.put("sign", sign); 

  String requestXML = PayCommonUtil.getRequestXml(parameters); 

  String result2 = CommonUtil.httpsRequestForStr(ConfigUtil.UNIFIED_ORDER_URL,"POST", requestXML); 

  System.out.println("-----------------------------统一下单结果---------------------------");
  System.out.println(result2);
  Map<String, String> mm=null;
  try {
   mm=getH5PayMap(result2,request);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //String prepayId=getPrepayId(result2,request);
  //String returnNoneStr=getReturnNoneStr(result2,request);
  String prepayId=mm.get("prepay_id");
  String returnNoneStr=mm.get("nonce_str");;
  SortedMap<Object, Object> lastSign = new TreeMap<Object, Object>();
  lastSign.put("return_code", "SUCCESS");
  lastSign.put("appid", ConfigUtil.APPID);
  lastSign.put("mch_id", ConfigUtil.MCH_ID);
  lastSign.put("nonce_str", returnNoneStr);
  lastSign.put("prepay_id", prepayId);
  lastSign.put("result_code", "SUCCESS");
  lastSign.put("key", ConfigUtil.API_KEY); 

  String lastSignpara = PayCommonUtil.createSign("UTF-8", lastSign); 

  StringBuffer buf=new StringBuffer();
  buf.append("<xml>");
  buf.append("<return_code>SUCCESS</return_code>");
  buf.append("<appid>"+ConfigUtil.APPID+"</appid>");
  buf.append("<mch_id>"+ConfigUtil.MCH_ID+"</mch_id>");
  buf.append("<nonce_str>"+returnNoneStr+"</nonce_str>");
  buf.append("<prepay_id>"+prepayId+"</prepay_id>");
  buf.append("<result_code>SUCCESS</result_code>");
  buf.append("<sign>"+lastSignpara+"</sign>");
  buf.append("</xml>"); 

  response.getWriter().print(buf.toString());
 } 

 public Map<String, String> getH5PayMap(String result,HttpServletRequest request) throws Exception{ 

   Map<String, String> map = XMLUtil.doXMLParse(result);
   return map;
 } 

}

最终看下公众号支付和扫码支付的微信配置:

希望通过这篇文章,大家能明白就算通过Java来做微信公众号、微信支付而不借助github提供的那些坑人的代码也可以开发出另自己和客户满意的微信应用。虽然微信给出的demo都是PHP的,但这些都是浮云,开发语言是其次,理解接口调用需具备的底层只是才是程序员的必修课。

(0)

相关推荐

  • java实现微信H5支付方法详解

    前面做了app微信支付的回调处理,现在需要做微信公众号的支付,花了一天多时间,终于折腾出来了!鉴于坑爹的微信官方没有提供Java版的demo,所以全靠自己按照同样坑爹的文档敲敲敲,所以记录下来,以供自己及后来人参考,不足之处,还请指正. 首先,我们贴出调用支付接口的H5页面,当然,在这个页面之前,还需要做很多其他的操作,我们一步一步的来. 坑爹的官方文档给了两个不同的支付接口,在微信公众平台开发中文档的"微信JS-SDK说明文档"中,给出的支付方式是下面被屏蔽的那一部分,而在商户平台的

  • java开发微信公众号支付

    最近做了微信公众号支付的开发,由于是第一次做也摸索了几天的时间,也只是达到了实现功能的水平,并没有太多考虑到性能问题,所以这篇文章比较适合初学者. 微信公众号支付的总体其实很简单,大致就分为三步.第一步需要获取用户授权:第二步调用统一下单接口获取预支付id:第三步H5调起微信支付的内置的js.下面介绍具体每一步的开发流程. 一    首先要明确微信公众号支付属于网页版支付,所以相较于app的直接调取微信支付要多一步微信授权.也就是需要获取用户的openid.微信公众号使用的交易类型是JSAPI,

  • Java微信支付-微信红包

    微信红包的使用已经很广泛,本篇文章介绍了微信发红包的实例,需要有认证的公众号,且开通了微信支付,商户平台且开通了现金红包的权限即可. https://pay.weixin.qq.com商户登陆地址.选择查看营销中心的现金红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1 现金红包的官网文档说明 先看几个图 简单的测试.前提需要你去商户平台先充值.不支持预支付.本文只是总结微信现金红包接口的调用与

  • Java编程调用微信支付功能的方法详解

    本文实例讲述了Java编程调用微信支付功能的方法.分享给大家供大家参考,具体如下: 微信开发文档地址:https://mp.weixin.qq.com/wiki/home/ 从调用处开始 我的流程: 1.点击"支付"按钮,去后台 --> 2.后台生成支付所需数据返回页面 --> 3.页面点击"确认支付"调用微信支付js.完成支付功能. 支付按钮 <div class="button" id="pay" onc

  • Java SpringMVC实现PC端网页微信扫码支付(完整版)

    一:前期微信支付扫盲知识 前提条件是已经有申请了微信支付功能的公众号,然后我们需要得到公众号APPID和微信商户号,这个分别在微信公众号和微信支付商家平台上面可以发现.其实在你申请成功支付功能之后,微信会通过邮件把Mail转给你的,有了这些信息之后,我们就可以去微信支付服务支持页面:https://pay.weixin.qq.com/service_provider/index.shtml 打开这个页面,点击右上方的链接[开发文档]会进入到API文档说明页面,看起来如下 选择红色圆圈的扫码支付就

  • 微信APP支付Java代码

    本文实例为大家分享了java微信APP支付代码,供大家参考,具体内容如下 import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Random; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.htt

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

    微信小程序 支付后台java实现实例 前言: 前些天使用 LeanCloud 云引擎写了个小程序的支付相关 以前只做过 APP 支付 这次在小程序支付爬了两天的坑 把代码也分享出来 支付流程: 1.小程序前端获取微信 openId 以及订单号 传给后台 2,后台根据 openId 和订单号进行签名 post 微信统一下单接口 3.后台获取微信返回的xml字符串 解析 二次签名以后返回给前端 4.前端调起支付微信支付 API 先看支付函数: //获取支付信息 @EngineFunction("ge

  • 详解JAVA后端实现统一扫码支付:微信篇

    最近做完了一个项目,正好没事做,产品经理就给我安排了一个任务. 做一个像收钱吧这样可以统一扫码收钱的功能. 一开始并不知道是怎么实现的,咨询了好几个朋友,才知道大概的业务流程:先是开一个网页用来判断支付平台,是微信还是支付宝,判断过后就好办了,直接照搬微信支付和支付宝的官方文档.不过微信的文档感觉有点坑,得多花点心思. 现在讲讲怎么实现微信支付网页支付,也就是公众号支付: 1.判断支付平台,在判断是微信平台时,必须使用window.location打开网页,使用其他方法在IOS版微信无法打开网页

  • java服务端微信APP支付接口详解

    一.微信APP支付接入商户服务中心 [申请流程指引] (https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317780&token=84f23b4e9746c5963128711f225476cfd49ccf8c&lang=zh_CN) 二.开始开发 1.配置相关的配置信息 1.1.配置appid(Androi

  • Java通过JsApi方式实现微信支付

    要使用JsApi进行微信支付,首先要从微信获得一个prepay_id,然后通过调用微信的jsapi完成支付,JS API的返回结果get_brand_wcpay_request:ok仅在用户成功完成支付时返回.由于前端交互复杂,get_brand_wcpay_request:cancel或者get_brand_wcpay_request:fail可以统一处理为用户遇到错误或者主动放弃,不必细化区分. 示例代码如下: function onBridgeReady(){ WeixinJSBridge

  • java实现微信支付(服务端)

    废话不多说,直接看代码. RequestHandler requestHandler = new RequestHandler(super.getRequest(),super.getResponse()); //获取token //两小时内有效,两小时后重新获取 Token = requestHandler.GetToken(); //更新token 到应用中 requestHandler.getTokenReal(); System.out.println("微信支付获取token=====

随机推荐