此項目已開源歡迎Start、PR、發起Issues一塊兒討論交流共同進步
https://github.com/Javen205/IJPay
http://git.oschina.net/javen205/IJPayjava
咱們接着上一篇文章支付寶支付-刷卡支付(條碼支付)詳解 繼續來聊聊支付包掃碼支付git
掃碼支付,指用戶打開支付寶錢包中的「掃一掃」功能,掃描商戶針對每一個訂單實時生成的訂單二維碼,並在手機端確認支付。github
一、商戶系統調用支付寶預下單接口alipay.trade.precreate,得到該訂單二維碼圖片地址。json
二、發起輪詢得到支付結果:等待5秒後調用交易查詢接口alipay.trade.query經過支付時傳入的商戶訂單號(out_trade_no)查詢支付結果(返回參數TRADE_STATUS),若是仍然返回等待用戶付款(WAIT_BUYER_PAY),則再次等待5秒後繼續查詢,直到返回確切的支付結果(成功TRADE_SUCCESS 或 已撤銷關閉TRADE_CLOSED),或是超出輪詢時間。在最後一次查詢仍然返回等待用戶付款的狀況下,必須當即調用交易撤銷接口alipay.trade.cancel將這筆交易撤銷,避免用戶繼續支付。小程序
三、除了主動輪詢,也能夠經過接受異步通知得到支付結果,詳見掃碼異步通知,注意必定要對異步通知作驗籤,確保通知是支付寶發出的。微信小程序
交易撤銷接口alipay.trade.cancelmarkdown
掃碼異步通知app
其中 交易查詢接口alipay.trade.query以及交易撤銷接口alipay.trade.cancel在上篇文章中已有介紹
初始化SDK 上篇文章已提到過
static { alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPulicKey, signType); }
封裝SDK的調用
/** * 掃碼支付 * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.i0UVZn&treeId=193&articleId=105170&docType=1#s4 * @param notifyUrl * @return * @throws AlipayApiException */ public static String tradePrecreatePay(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{ AlipayTradePrecreateResponse response = tradePrecreatePayToResponse(model,notifyUrl); return response.getBody(); } public static AlipayTradePrecreateResponse tradePrecreatePayToResponse(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{ AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); request.setBizModel(model); request.setNotifyUrl(notifyUrl); return alipayClient.execute(request); }
/** * 掃碼支付 */ public void tradePrecreatePay() { String subject = "Javen 支付寶掃碼支付測試"; String totalAmount = "86"; String storeId = "123"; String notifyUrl = AliPayApi.NOTIFY_DOMAIN + "/alipay/precreate_notify_url"; AlipayTradePrecreateModel model = new AlipayTradePrecreateModel(); model.setSubject(subject); model.setTotalAmount(totalAmount); model.setStoreId(storeId); model.setTimeoutExpress("5m"); model.setOutTradeNo(StringUtils.getOutTradeNo()); try { String resultStr = AliPayApi.tradePrecreatePay(model, notifyUrl); JSONObject jsonObject = JSONObject.parseObject(resultStr); String qr_code = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code"); renderText(qr_code); } catch (Exception e) { e.printStackTrace(); } }
返回的結果
{
"alipay_trade_precreate_response": { "code": "10000", "msg": "Success", "out_trade_no": "050823252214942", "qr_code": "https://qr.alipay.com/bax04344igml342yrywa004f" }, "sign": "DIeOExAIIF4RhPsHLp4tZtQP15SU4k/CUsJ//Z782HOpTe/BYMK/YjiPv9I2JF6GHSIU+qotprO/unkwf1qN41d9g4qErx7oLpRLxGZgSOMo4js8wBcauN4osXmPRfmDvlCo6cO9s4rj3976223h/8kzBlRp3lpgOnD+uxyQrqqEsb6ddQosbEiqYW3XADu3X9GRTuDaJFsk4tkc1Uly6Mx17SXJs7c49CA0ObLJZ5RsV8Wd55qDWc93FqOKClNikOnFc0QwvAPuLXyW4DFpfdqk4LlegGT33jdfzTuPFoXXnab/P6/dRRGd4ZW82lVAx4pjGhia4Ad+Wb+sNf5QVw==" }
其中qr_code就是二維碼的圖片地址https://qr.alipay.com/bax04344igml342yrywa004f
詳細的介紹能夠參考官方文檔 掃碼異步通知 這裏直接提供代碼
/** * 掃碼支付通知 */ public void precreate_notify_url(){ try { Map<String, String> map = AliPayApi.toMap(getRequest()); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey()+" = "+entry.getValue()); } boolean flag = AlipaySignature.rsaCheckV1(map, AliPayApi.ALIPAY_PUBLIC_KEY, AliPayApi.CHARSET, AliPayApi.SIGN_TYPE); if (flag) { // TODO System.out.println("precreate_notify_url success"); renderText("success"); return; } else { // TODO System.out.println("precreate_notify_url failure"); renderText("failure"); } } catch (AlipayApiException e) { e.printStackTrace(); renderText("failure"); } }
將異步通知的參數轉化爲Map
/** * 將異步通知的參數轉化爲Map * @param request * @return */ public static Map<String, String> toMap(HttpServletRequest request) { System.out.println(">>>>" + request.getQueryString()); Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } // 亂碼解決,這段代碼在出現亂碼時使用。 // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } return params; }