首先是微信APP的支付流程流酬,看著很多流程币厕,其實主要的大致分為以下幾個步驟
1.客戶端選擇商品下單(即要購買的這個物品多少錢,我就向微信支付服務端發(fā)送金額的參數(shù)為多少)
2.微信支付服務端收到你的消息后芽腾,再去調用統(tǒng)一下單的API(這個要看支付的文檔了旦装,一般這個API的地址是不變的)
3.微信支付服務端獲取到預訂單信息時再發(fā)送給微信支付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 獲取預支付訂單等參數(shù)
? ? * @param attach 額外參數(shù)
? ? * @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);
? ? ? ? //異步通知地址(請注意必須是外網(wǎng))
? ? ? ? data.put("notify_url", NOTIFY_URL);
? ? ? ? //交易類型
? ? ? ? data.put("trade_type", TRADE_TYPE_APP);
? ? ? ? //附加數(shù)據(jù),在查詢API和支付通知中原樣返回腺兴,該字段主要用于商戶攜帶訂單的自定義數(shù)據(jù)
? ? ? ? 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個參數(shù)
? ? ? ? ? ? ? ? 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 {
? ? ? ? ? ? ? ? ? ? //此時返回沒有預付訂單的數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? return returnMap;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? return returnMap;
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? System.out.println(e);
? ? ? ? ? ? //系統(tǒng)等其他錯誤的時候
? ? ? ? }
? ? ? ? return returnMap;
? ? }
需要注意的是微信支付服務端需要APPID页响,商戶號篓足,商戶秘鑰以及從微信商戶平臺下載的安全證書(確保支付安全),對于支付成功的回調闰蚕,查詢訂單和退訂單栈拖,,這里就不再一一貼出了没陡,有問題的可以留言或私信交流涩哟,一篇拙文,還望各位多多擔待盼玄。