小編給大家分享一下java如何實(shí)現(xiàn)微信掃碼支付功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供相山網(wǎng)站建設(shè)、相山做網(wǎng)站、相山網(wǎng)站設(shè)計(jì)、相山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、相山企業(yè)網(wǎng)站模板建站服務(wù),十余年相山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
具體內(nèi)容如下
1、maven項(xiàng)目的pom.xml中添加如下jar包:
com.github.wxpay wxpay-sdk 0.0.3
2、編寫WeWxConfig類:
package com.xx.wxpay; import com.github.wxpay.sdk.WXPayConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.InputStream; /** * 描述:微信支付配置信息 * * @author ssl * @create 2018/04/24 19:25 */ @Component public class WeWxConfig implements WXPayConfig { @Value("${wechat.public.appid}") private String appId; @Value("${wechat.merchant}") private String mchId; @Value("${wechat.public.apikey}") private String apiKey; /** * 公眾賬號(hào)ID:微信支付分配的公眾賬號(hào)ID(企業(yè)號(hào)corpid即為此appId) * * @return */ @Override public String getAppID() { return appId; } /** * 商戶號(hào):微信支付分配的商戶號(hào) * * @return */ @Override public String getMchID() { return mchId; } /** * @return */ @Override public String getKey() { return apiKey; } @Override public InputStream getCertStream() { return null; } @Override public int getHttpConnectTimeoutMs() { return 0; } @Override public int getHttpReadTimeoutMs() { return 0; } }
3、編寫WeWxPayService:
package com.xx.wxpay; import com.alibaba.fastjson.JSONObject; import com.github.wxpay.sdk.WXPay; import com.google.common.collect.Maps; import com.xx.model.Order; import com.xx.model.Product; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; /** * 描述: * * @author ssl * @create 2018/04/24 20:15 */ @Service public class WeWxPayService { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${project.url}") private String projectUrl; @Autowired private WeWxConfig weWxConfig; /** * 統(tǒng)一下單 * * @param product * @param order * @return */ public MapunifiedOrder(Product product, Order order) { Map data = Maps.newHashMap(); WXPay wxpay = new WXPay(weWxConfig); data.put("body", "XX-" + product.getName()); data.put("detail", "詳細(xì)信息"); data.put("out_trade_no", order.getOrderNo()); data.put("device_info", "WEB"); data.put("fee_type", "CNY"); data.put("total_fee", order.getAmount() + ""); data.put("spbill_create_ip", "127.0.0.1"); data.put("notify_url", projectUrl + "/base/order/notifyUrl"); data.put("trade_type", "NATIVE"); // 此處指定為掃碼支付 data.put("product_id", product.getId() + ""); try { Map resp = wxpay.unifiedOrder(data); logger.debug(JSONObject.toJSONString(resp)); return resp; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 訂單查詢 * * @param orderNo:訂單號(hào) * @return */ public Map orderQuery(String orderNo) { Map reqData = Maps.newHashMap(); reqData.put("out_trade_no", orderNo); WXPay wxpay = new WXPay(weWxConfig); try { Map resp = wxpay.orderQuery(reqData); logger.debug(JSONObject.toJSONString(resp)); return resp; } catch (Exception e) { e.printStackTrace(); } return null; } public static String getUrl() { WXPay wxpay = new WXPay(new WeWxConfig()); Map data = new HashMap (); data.put("body", "上屏名稱"); data.put("detail", "商品詳情"); data.put("out_trade_no", "2ab9071b06b9f739b950ddb41db2690d"); data.put("device_info", ""); data.put("fee_type", "CNY"); data.put("total_fee", "1"); data.put("spbill_create_ip", "218.17.160.245"); data.put("notify_url", "http://www.example.com/wxpay/notify"); data.put("trade_type", "NATIVE"); // 此處指定為掃碼支付 data.put("product_id", "12"); try { Map resp = wxpay.unifiedOrder(data); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } return ""; } }
4、調(diào)用:
/** 向微信支付系統(tǒng)下單,并得到二維碼返回給用戶 */ MapresData = weWxPayService.unifiedOrder(product, order);
5、resData.get("code_url")為微信下單成功后返回的二維碼地址,頁面中用QRCode.js來顯示該二維碼,且該頁面用定時(shí)器定時(shí)查詢訂單支付狀態(tài)
看完了這篇文章,相信你對(duì)“java如何實(shí)現(xiàn)微信掃碼支付功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!