首先是微信APP的支付流程,看着很多流程,其实主要的大致分为以下几个步骤
1.客户端选择商品下单(即要购买的这个物品多少钱,我就向微信支付服务端发送金额的参数为多少)
2.微信支付服务端收到你的消息后,再去调用统一下单的API(这个要看支付的文档了,一般这个API的地址是不变的)
3.微信支付服务端获取到预订单信息时再发送给微信支付APP端,APP将通过SDK调起支付。
下面贴下相关参考代码
try {
String url = "http://www.xxx.com/?user_id=" + account + "&totalFee=" + total_fee*100;
byte[] buf = Util.httpGet(url);
if (buf != null && buf.length > 0) {
String content = new String(buf);
Log.e("get server pay params:", content);
JSONObject json = new JSONObject(content);
if (null != json && !json.has("retcode")) {
PayReq req = new PayReq();
req.appId = json.getString("appid");
req.partnerId = json.getString("partnerid");
req.prepayId = json.getString("prepayid");
req.nonceStr = json.getString("noncestr");
req.timeStamp = json.getString("timestamp");
req.packageValue = json.getString("package");
req.sign = json.getString("sign");
req.extData = "app data"; // optional
Toast.makeText(MainActivity.this, "调起支付中...", Toast.LENGTH_SHORT).show();
// 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信
api.sendReq(req);
} else {
Log.d("PAY_GET", "返回错误" + json.getString("retmsg"));
Toast.makeText(MainActivity.this, "返回错误" + json.getString("retmsg"), Toast.LENGTH_SHORT).show();
}
} else {
Log.d("PAY_GET", "服务器请求错误");
Toast.makeText(MainActivity.this, "服务器请求错误", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("PAY_GET", "异常:" + e.getMessage());
Toast.makeText(MainActivity.this, "异常:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
接下来是微信支付服务端
下面贴出主要代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.err.println(request.getParameter("user_id"));
System.err.println(request.getParameter("totalFee"));
String user_id = request.getParameter("user_id");
String totalFee = request.getParameter("totalFee");
String attach = "{\"user_id\":\"" + user_id + "\"}";
wxPayService = new WXserviceImpl();
System.err.println(wxPayService);
try {
//请求预支付订单
Map<String, String> result = wxPayService.dounifiedOrder(attach, totalFee);
JSONObject map = new JSONObject();
WxMD5Util md5Util = new WxMD5Util();
map.put("appid", result.get("appid"));
map.put("partnerid", result.get("mch_id"));
map.put("prepayid", result.get("prepay_id"));
map.put("package", "Sign=WXPay");
map.put("noncestr", result.get("nonce_str"));
map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));//单位为秒
String sign = md5Util.getSign(map);
map.put("sign",sign);
map.put("extdata", attach);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
response.getOutputStream().write(map.toString().getBytes("utf-8"));// 指定编码为utf-8
} catch (Exception e) {
// TODO: handle exception
System.err.println("异常信息"+e);
}
}
/**
* 调用官方SDK 获取预支付订单等参数
* @param attach 额外参数
* @param total_fee 总价
* @return
* @throws Exception
*/
@Override
public Map<String, String> dounifiedOrder(String attach, String total_fee) throws Exception {
WxMD5Util md5Util = new WxMD5Util();
Map<String, String> returnMap = new HashMap<>();
WXConfigUtil config = new WXConfigUtil();
System.err.println("使用官方API请求预付订单");
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<>();
//生成商户订单号,不可重复
String out_trade_no = "wxpay" + System.currentTimeMillis();
data.put("appid", config.getAppID());
data.put("mch_id", config.getMchID());
data.put("nonce_str", WXPayUtil.generateNonceStr());
String body = "订单支付";
data.put("body", body);
data.put("out_trade_no", out_trade_no);
data.put("total_fee", total_fee);
//自己的服务器IP地址
data.put("spbill_create_ip", SPBILL_CREATE_IP);
//异步通知地址(请注意必须是外网)
data.put("notify_url", NOTIFY_URL);
//交易类型
data.put("trade_type", TRADE_TYPE_APP);
//附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
data.put("attach", attach);
String sign1 = md5Util.getSign(data);
data.put("sign", sign1);
System.err.println("使用官方API请求预付订单");
try {
//使用官方API请求预付订单
Map<String, String> response = wxpay.unifiedOrder(data);
System.out.println(response);
String returnCode = response.get("return_code"); //获取返回码
//若返回码为SUCCESS,则会返回一个result_code,再对该result_code进行判断
if (returnCode.equals("SUCCESS")) {//主要返回以下5个参数
String resultCode = response.get("result_code");
returnMap.put("appid", response.get("appid"));
returnMap.put("mch_id", response.get("mch_id"));
returnMap.put("nonce_str", response.get("nonce_str"));
returnMap.put("sign", response.get("sign"));
if ("SUCCESS".equals(resultCode)) {//resultCode 为SUCCESS,才会返回prepay_id和trade_type
//获取预支付交易回话标志
returnMap.put("trade_type", response.get("trade_type"));
returnMap.put("prepay_id", response.get("prepay_id"));
return returnMap;
} else {
//此时返回没有预付订单的数据
return returnMap;
}
} else {
return returnMap;
}
} catch (Exception e) {
System.out.println(e);
//系统等其他错误的时候
}
return returnMap;
}
需要注意的是微信支付服务端需要APPID,商户号,商户秘钥以及从微信商户平台下载的安全证书(确保支付安全),对于支付成功的回调,查询订单和退订单,,这里就不再一一贴出了,有问题的可以留言或私信交流,一篇拙文,还望各位多多担待。