更新時(shí)間:2022-04-28 11:20:29 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1621次
動(dòng)力節(jié)點(diǎn)小編告訴大家,微信掃碼支付。簡單來說就是你將微信支付所需的信息生成到二維碼圖片中。微信掃一掃,發(fā)起支付。我們需要做的是兩件事:
一是:根據(jù)微信掃碼支付規(guī)則生成二維碼信息。
二是:微信沒有提供生成二維碼圖片的接口。我們需要自己將二維碼信息生成到二維碼圖片中。
微信掃碼支付,有兩種方式,文檔中有介紹。第二種模式,微信界面會將二維碼信息返回給我們。第一種模式需要我們自己生成二維碼信息。會有點(diǎn)麻煩。尤其是參數(shù)的情況,以及簽名的問題,很容易出錯(cuò)。一般來說,第二種模式比第一種模式更簡單。我使用的只是第二種模式,它更通用。京東和攜程也采用第二種模式。
微信掃碼支付第二種方式,需要調(diào)用微信統(tǒng)一下單接口生成預(yù)交易訂單。(參數(shù)發(fā)送和接收都是 XML 格式。)
正確調(diào)用后,會返回包含交易ID和二維碼鏈接的URL。
HashMap<String, String> paramMap = Maps.newHashMap();
paramMap.put( "trade_type", "NATIVE" );//交易類型
paramMap.put( "spbill_create_ip" ,localIp());//本地IP
paramMap.put( "product_id" , payOrderIdsStr);//商家根據(jù)業(yè)務(wù)傳遞的參數(shù)為必填項(xiàng)
paramMap.put( "body" , orderSubject);//描述
paramMap.put( "out_trade_no" , payOrderIdsStr);//商戶后臺交易訂單號
paramMap.put( "total_fee", "" + totalCount);//金額必須為整數(shù),單位為分鐘
paramMap.put( "notify_url", "http://" + getAccessDomain() + "/wx_pay_notify" );//支付成功后回調(diào)地址
paramMap.put( "appid" , siteConfig.getWxPayAppId());//appid
paramMap.put( "mch_id" , siteConfig.getWxPayMchId());//商戶號
paramMap.put( "nonce_str", CommonUtilPub.createNoncestr(32 ));//隨機(jī)數(shù)
paramMap.put( "sign" ,CommonUtilPub.getSign(paramMap,siteConfig.getWxPayPartnerKey()));//根據(jù)微信簽名規(guī)則,生成簽名
String xmlData = CommonUtilPub.mapToXml(paramMap);//將參數(shù)轉(zhuǎn)換成XML數(shù)據(jù)格式
/**
* 獲取本地IP
*
* 獲取系統(tǒng)所有的網(wǎng)絡(luò)接口,然后遍歷每個(gè)網(wǎng)絡(luò)下的InterfaceAddress組。
* 獲取滿足<code>InetAddress instanceof Inet4Address</code>條件的IpV4地址
* @return
*/
@SuppressWarnings("rawtypes" )
private String localIp(){
String ip = null ;
枚舉 allNetInterfaces;
嘗試{
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
列表<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
for (InterfaceAddress add : InterfaceAddress) {
InetAddress Ip = add.getAddress();
if (Ip != null && Ip instanceof Inet4Address) {
ip =ip.getHostAddress();
}
}
}
}捕獲(SocketException e){
TODO 自動(dòng)生成的catch block
logger.warn("獲取本機(jī)IP失敗:異常信息:" + e.getMessage());
}
返回ip;
}
成功返回的 XML 數(shù)據(jù)為:
< xml >< return_code > <![CDATA[ SUCCESS ]]> </ return_code >
< return_msg > <![CDATA[ OK ]]> </ return_msg >
< appid > <![CDATA[ wx49342bda0ef105dd ]]> </ appid >
< mch_id > <![CDATA[ 10019460 ]]> </ mch_id >
< nonce_str > <![CDATA[ UneMQd4qWQd0hJ4L ]]></ nonce_str >
< sign > <![CDATA[ C621A9C586C1F0397D4C6B8003E0CBCE ]]> </ sign >
< result_code > <![CDATA[ SUCCESS ]]> </ result_code >
< prepay_id > <![CDATA[ wx2015070818251790742fea5e0865 ]3] > </ prepay_id >
< trade_type > <![CDATA[ NATIVE ]]> </ trade_type >
<code_url ><![CDATA[ weixin://wxpay/bizpayurl?pr=AOFEsxf ]]> </ code_url >
</ xml >
解析 XML 以獲取 code_url:
String resXml = HtmlUtil.postData("https://api.mch.weixin.qq.com/pay/unifiedorder" , xmlData);
文檔 dd = null ;
字符串 code_url= null ;
嘗試{
dd = DocumentHelper.parseText(resXml);
} catch (DocumentException e) {
return "" ;
}
if (dd != null ) {
元素根 = dd.getRootElement();
if (root == null ) {
返回"" ;
}
元素 codeUrl = root.element("code_url" );
if (piEle == null ) {
返回"" ;
}
code_url = codeUrl.getText();//解析xml得到code_url 19}
使用了 google ZXing 庫。提供一個(gè)jar地址,直接導(dǎo)入到你的項(xiàng)目中。
頁面代碼:
< img src ="qr_code.img?code_url= <#if code_url??>${code_url}</#if>" style ="width:300px;height:300px;" />
Java代碼:
/**
* 生成的二維碼圖片直接作為流輸出到頁面,不存儲
* @param content
* @param response
*/ @SuppressWarnings({ "unchecked", "rawtypes" })
public static void encodeQrcode(String content,HttpServletResponse response){
if (StringUtils.isBlank(content))
return ;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
地圖提示 = new HashMap();
提示.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 新的
位矩陣 bitMatrix = null ;
嘗試{
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300 ,hints);
BufferedImage 圖像 = toBufferedImage(bitMatrix);
輸出二維碼圖片流
try {
ImageIO.write(image, "png" , response.getOutputStream());
}捕捉(IOException e){
TODO 自動(dòng)生成的 catch 塊
e.printStackTrace();
}
}捕捉(WriterException e1){
TODO 自動(dòng)生成的 catch 塊
e1.printStackTrace();
}
}
然后,您可以通過微信掃描生成的圖片的代碼來發(fā)起支付。
支付成功后,微信會調(diào)用你之前設(shè)置的回調(diào)函數(shù)地址。并且會為你帶來之前發(fā)送給微信的商戶自定義參數(shù),幫助商戶完成剩下的業(yè)務(wù)流程。如果大家想了解更多相關(guān)知識,不妨來關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面的課程內(nèi)容細(xì)致全面,通俗易懂,適合小白學(xué)習(xí),希望對大家能夠有所幫助。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743