微信支付公的眾號支付和掃碼支付
來源:程序員人生 發布時間:2016-07-07 09:00:55 閱讀次數:3223次
公眾號支付是手機真個微信公眾號H5頁面支付,這類支付方式必須是在微信內置閱讀器發起。
掃碼支付分為模式1和模式2,模式1主要為線下服務,該模式是先掃碼,再生成定單,商戶先為自己的商品生成2維碼連接,然后用戶掃碼以后決定是不是購買,2維碼無過期時間,比如自動售賣機大多采取這類模式;模式2主要為線上電商服務,用戶選擇商品后生成定單,根據定單生成2維碼,然后支付,該2維碼為臨時2維碼。
開發流程
1、授權目錄
官方文檔說必須是精確目錄,實際上是2級或3級目錄就能夠了,太精確的可能還會出現不辨認的情況。如果是掃碼支付模式1還需要設置掃碼支付回調URL

2.統1下單
注意傳入參數不要為null,盡可能不要是空字符串,如果在沒有拋出Exception的情況下支付失敗,10有89是參數致使的簽名有問題,微信支付的簽名規定的特別嚴格,必須依照微信給的規則來,建議第1次先用demo提供的簽名方法,以后可以再修改。這里的appid就是支付所在的公眾號的appid,openId是用戶對應當前公眾號的openId。
1、掃碼支付模式2
掃碼支付比較簡單,可以直接通過url發起,傳入統1下單參數,生成掃碼支付的url。掃碼支付的trade_type為NATIVE。
public void getCodeUrl(@PathVariable(value="tradeNo")String tradeNo, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//根據定單號獲得定單詳情
OrderProduceBean order = reservationCarService.getTradebyNo(tradeNo);
// 附加數據 原樣返回
String attach = "attach";
// 總金額以分為單位,不帶小數點
String totalFee = TenpayUtil.getMoney(order.getTotalFee().toString());
// 定單生成的機器 IP
String spbill_create_ip = IpUtil.getIpAddr(request);
// 這里notify_url是 支付完成后微信發給該鏈接信息,可以判斷會員是不是支付成功,改變定單狀態等。
String notify_url = TenPayConfig.notifyUrl;
String trade_type = "NATIVE";
// 商戶號
String mch_id = TenPayConfig.partner;
// 隨機字符串
String nonce_str = TenpayUtil.getNonceStr();
// 商品描寫根據情況修改
String body = order.getBody();
// 商戶定單號
String out_trade_no = order.getOutTradeNo();
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", TenPayConfig.appid);
packageParams.put("mch_id", mch_id);
packageParams.put("nonce_str", nonce_str);
packageParams.put("body", body);
packageParams.put("attach", attach);
packageParams.put("out_trade_no", out_trade_no);
packageParams.put("total_fee", totalFee);
packageParams.put("spbill_create_ip", spbill_create_ip);
packageParams.put("notify_url", notify_url);
packageParams.put("trade_type", trade_type);
RequestHandler reqHandler = new RequestHandler(null, null);
reqHandler.init(TenPayConfig.appid, TenPayConfig.appsecret,
TenPayConfig.partnerkey);
String sign = reqHandler.createSign(packageParams);
String xml = "<xml>" + "<appid>" + TenPayConfig.appid + "</appid>"
+ "<mch_id>" + mch_id + "</mch_id>" + "<nonce_str>" + nonce_str
+ "</nonce_str>" + "<sign>" + sign + "</sign>"
+ "<body><![CDATA[" + body + "]]></body>" + "<out_trade_no>"
+ out_trade_no + "</out_trade_no>" + "<attach>" + attach
+ "</attach>" + "<total_fee>" + totalFee + "</total_fee>"
+ "<spbill_create_ip>" + spbill_create_ip
+ "</spbill_create_ip>" + "<notify_url>" + notify_url
+ "</notify_url>" + "<trade_type>" + trade_type
+ "</trade_type>" + "</xml>";
System.out.println(xml);
String code_url = "";
String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
code_url = new TenPayCore().getCodeUrl(createOrderURL, xml);//調用統1下單接口
if (code_url == null || code_url.equalsIgnoreCase("")) {
logger.debug("****************************trade has closed or no this trade in tencentPay");
response.sendError(404);
return;
}
GenerateQrCodeUtil.encodeQrcode(code_url, response);
}
2.公眾號支付
公眾號支付首先通過H5調起支付api,微信生成定單,然后開發者獲得預支付id,最后由用戶確認支付。
①H5調起JSAPI。
調起有兩種方式,1種是WeixinJSBridge.invoke(),另外一種是最新的微信JSAPI,兩種方式都可,官方文檔用的第1種,使用第1種方式首先要引入http://res.wx.qq.com/open/js/jweixin⑴.0.0.js。
function toPay(){
$.ajax({
url : URL,
type : "GET",
dataType : 'json',
success : function(data) {
WeixinJSBridge.invoke('getBrandWCPayRequest', {
"appId" : data.appId, //公眾號名稱,由商戶傳入
"timeStamp" : data.timeStamp, //時間戳,自1970年以來的秒數
"nonceStr" : data.nonceStr, //隨機串
"package" : data.package,
"signType" : data.signType, //微信簽名方式:
"paySign" : data.paySign
//微信簽名
}, function(res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
} // 使用以上方式判斷前端返回:res.err_msg將在用戶支付成功后返回 ok,但其實不保證它絕對可靠。
else{
//res.err_msg;
}
});
}
});
}
②獲得預支付id
這個api和掃碼支付的api差不多,就是trade為JSAPI不1樣。
public void TencentPayController(@RequestParam("orderno")String orderno,HttpServletRequest request,
HttpServletResponse response) throws IOException {
//根據定單號查詢定單
OrderProduceBean order=reservationCarService.getTradebyNo(orderno);
String appid = TenPayConfig.appid;
String openId =order.getOpenId();
// 定單號
String orderId = order.getOutTradeNo();
// 附加數據 原樣返回
String attach = "attach";
// 總金額以分為單位,不帶小數點
String totalFee = TenpayUtil.getMoney(order.getTotalFee().toString());
// 定單生成的機器 IP
String spbill_create_ip = IpUtil.getIpAddr(request);;
// 這里notify_url是 支付完成后微信發給該鏈接信息,可以判斷會員是不是支付成功,改變定單狀態等。
String notify_url = TenPayConfig.notifyUrl;
String trade_type = "JSAPI";
// ---必須參數
// 商戶號
String mch_id = TenPayConfig.partner;
// 隨機字符串
String nonce_str = TenpayUtil.getNonceStr();
// 商品描寫根據情況修改
String body = order.getBody();
// 商戶定單號
String out_trade_no = orderId;
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", appid);
packageParams.put("mch_id", mch_id);
packageParams.put("nonce_str", nonce_str);
packageParams.put("body", body);
packageParams.put("attach", attach);
packageParams.put("out_trade_no", out_trade_no);
packageParams.put("total_fee", totalFee);
packageParams.put("spbill_create_ip", spbill_create_ip);
packageParams.put("notify_url", notify_url);
packageParams.put("trade_type", trade_type);
packageParams.put("openid", openId);
RequestHandler reqHandler = new RequestHandler(null, null);
reqHandler.init(appid, TenPayConfig.appsecret,TenPayConfig.partnerkey);
String sign = reqHandler.createSign(packageParams);
String xml = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>"
+ mch_id + "</mch_id>" + "<nonce_str>" + nonce_str
+ "</nonce_str>" + "<sign>" + sign + "</sign>"
+ "<body><![CDATA[" + body + "]]></body>"
+ "<out_trade_no>" + out_trade_no
+ "</out_trade_no>" + "<attach>" + attach + "</attach>"
+ "<total_fee>" + totalFee + "</total_fee>"
+ "<spbill_create_ip>" + spbill_create_ip
+ "</spbill_create_ip>" + "<notify_url>" + notify_url
+ "</notify_url>" + "<trade_type>" + trade_type
+ "</trade_type>" + "<openid>" + openId + "</openid>"
+ "</xml>";
String prepay_id = "";
String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
prepay_id = new TenPayCore().getPayNo(createOrderURL, xml);
logger.debug("********************************************");
logger.debug("prepay_id :" + prepay_id);
//獲得prepay_id后,拼接最后要求支付所需要的package
SortedMap<String, String> finalpackage = new TreeMap<String, String>();
String timestamp = Sha1Util.getTimeStamp();
String packages = "prepay_id="+prepay_id;
finalpackage.put("appId", appid);
finalpackage.put("timeStamp", timestamp);
finalpackage.put("nonceStr", nonce_str);
finalpackage.put("package", packages);
finalpackage.put("signType", "MD5");
//要簽名
String finalsign = reqHandler.createSign(finalpackage);
finalpackage.put("paySign", finalsign);
String finaPackage = "\"appId\":\"" + appid + "\",\"timeStamp\":\"" + timestamp
+ "\",\"nonceStr\":\"" + nonce_str + "\",\"package\":\""
+ packages + "\",\"signType\" : \"MD5" + "\",\"paySign\":\""
+ finalsign + "\"";
logger.debug("********************************************");
logger.debug("V3 jsApi package:" + finaPackage);
if(prepay_id.length()>0){
this.callBack(response, request,this.gson.toJson(finalpackage));
return;
}else{
this.callBack(response, request,"{\"error\":\"prepay_id is null\"}");
return;
}
}
3.支付回調
該回調方法的路徑就是獲得預支付id中的參數notify_url,用來處理支付完成后的業務邏輯,注意1定要檢查return_code和result_code是不是都為success,另外1定要給微信返回success信息.
public void notifyPay(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 示例報文
String inputLine;
String notityXml = "";
String resXml = "";
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityXml += inputLine;
}
request.getReader().close();
} catch (Exception e) {
e.printStackTrace();
}
Map<String, String> m = TenpayUtil.parseXmlToList(notityXml);
OrderTenPayBean orderTen = new OrderTenPayBean();
orderTen.setAppid(m.get("appid").toString());
orderTen.setBankType(m.get("bank_type").toString());
orderTen.setCashFee(m.get("cash_fee").toString());
orderTen.setFeeType(m.get("fee_type").toString());
orderTen.setIsSubscribe(m.get("is_subscribe").toString());
orderTen.setMchId(m.get("mch_id").toString());
orderTen.setNonceStr(m.get("nonce_str").toString());
orderTen.setOpenid(m.get("openid").toString());
orderTen.setOutTradeNo(m.get("out_trade_no").toString());
orderTen.setResultCode(m.get("result_code").toString());
orderTen.setReturnCode(m.get("return_code").toString());
orderTen.setSign(m.get("sign").toString());
orderTen.setTimeEnd(m.get("time_end").toString());
orderTen.setTotalFee(m.get("total_fee").toString());
orderTen.setTradeType(m.get("trade_type").toString());
orderTen.setTransactionId(m.get("transaction_id").toString());
if ("SUCCESS".equals(orderTen.getResultCode()) && "SUCCESS".equals(orderTen.getReturnCode())<span style="font-family: Arial, Helvetica, sans-serif;">) { //必須判斷</span>
//TODO 信息入庫,修改定單狀態
// 支付成功,必須給微信返回此信息,否則微信會1直調用此回調方法
resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
+ "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
} else {
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+ "<return_msg><![CDATA[報文為空]]></return_msg>" + "</xml> ";
}
this.callBack(response, request, resXml);
}
4.退款
退款需要把商戶證書apiclient_cert.p12放到指定目錄下,由于有證書的情況下退款不需要密碼,1定要注意業務要求的權限問題
public void refusePay(HttpServletResponse response,HttpServletRequest request,@PathVariable(value="outTradeNo")String outTradeNo) throws Exception {
OrderProduceBean tradebyNo = reservationCarService.getTradebyNo(outTradeNo);
SortedMap<String, String> parameters = new TreeMap<String, String>();
parameters.put("appid", TenPayConfig.appid);
parameters.put("mch_id", TenPayConfig.partner);
parameters.put("nonce_str", TenpayUtil.getNonceStr());
parameters.put("out_trade_no", tradebyNo.getOutTradeNo());
parameters.put("out_refund_no", tradebyNo.getOutTradeNo()); // 我們自己設定的退款申請號,束縛為UK
parameters.put("total_fee", TenpayUtil.getMoney(tradebyNo.getTotalFee().toString())); // 單位為分
parameters.put("refund_fee", TenpayUtil.getMoney(tradebyNo.getTotalFee().toString())); // 單位為分
parameters.put("op_user_id", TenPayConfig.partner);
RequestHandler reqHandler = new RequestHandler(null, null);
reqHandler.init(TenPayConfig.appid, TenPayConfig.appsecret,
TenPayConfig.partnerkey);
String sign = reqHandler.createSign(parameters);
parameters.put("sign", sign);
String reuqestXml = TenpayUtil.getRequestXml(parameters);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(Resources.getString("cert_url")));
try {
keyStore.load(instream, TenPayConfig.partner.toCharArray());
} finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, TenPayConfig.partner.toCharArray())
.build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
System.out.println("executing request" + httpPost.getRequestLine());
StringEntity reqEntity = new StringEntity(reuqestXml);
// 設置類型
reqEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(reqEntity);
CloseableHttpResponse reqs = httpclient.execute(httpPost);
try {
HttpEntity entity = reqs.getEntity();
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
String returnXml = "";
String text;
while ((text = bufferedReader.readLine()) != null) {
returnXml += text;
}
//微信退款返回的參數轉換為Map
Map<String, String> resultMaP = TenpayUtil.parseXmlToList(returnXml);
this.callBack(response, request, resultMaP.toString());
//TODO 信息入庫
}
EntityUtils.consume(entity);
} finally {
reqs.close();
}
} finally {
httpclient.close();
}
}
PS:代碼僅供參考!
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈