先創建Springboot項目
已上傳至github庫 https://github.com/gaoruiqiang2017/weixinpay.git
pom文件添加依賴
<!--微信支付SDK-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
utils工具類(可使用自己項目的)
public class HttpUtil {
public static String doPost(String url, String requestXml) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
//創建httpClient連接對象
httpClient = HttpClients.createDefault();
//創建post請求連接對象
HttpPost httpPost = new HttpPost(url);
//創建連接請求對象,並設置連接參數
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(15000) //連接服務區主機超時時間
.setConnectionRequestTimeout(60000) //連接請求超時時間
.setSocketTimeout(60000).build(); //設置讀取響應數據超時時間
//爲httppost請求設置參數
httpPost.setConfig(requestConfig);
//將上傳參數放到entity屬性中
httpPost.setEntity(new StringEntity(requestXml, "UTF-8"));
//添加頭信息
httpPost.addHeader("Content-type", "text/xml");
String result = "";
try {
//發送請求
httpResponse = httpClient.execute(httpPost);
//從相應對象中獲取返回內容
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
Controller接口
@RestController
@RequestMapping("/weixinpay")
public class Weixinpay {
@Value("${appid}")
private String appid; //公衆賬號id
@Value("${mchid}")
private String mchId; //商戶號
@Value("${weixinKey}")
private String weixinKey; //密匙
@Value("${unifiedorderUrl}")
private String unifiedorderUrl; //統一下單接口
/**
* @param httpServletRequest
* @param httpServletResponse
* @param orderNo 訂單號 (前面要創建自己的訂單)
* @param money 金額
* @param body 商品內容
*/
@RequestMapping("/pay")
public void pay(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse, String orderNo, String money, String body)
throws Exception {
try {
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put("appid", appid); //公衆賬號ID
dataMap.put("mch_id", mchId); //商戶號
dataMap.put("nonce_str", WXPayUtil.generateNonceStr()); //隨機字符串,長度要求在32位以內。
dataMap.put("body", body); //商品描述
dataMap.put("out_trade_no", orderNo); //商品訂單號
dataMap.put("total_fee", money); //商品金
dataMap.put("spbill_create_ip", InetAddress.getLocalHost().getHostAddress()); //客戶端ip
dataMap.put("notify_url", "www.baidu.com"); //通知地址(假設是百度)
dataMap.put("trade_type", "NATIVE"); //交易類型
dataMap.put("product_id", "1"); //trade_type=NATIVE時,此參數必傳。商品ID,商戶自行定義。
//生成簽名
String signature = WXPayUtil.generateSignature(dataMap, weixinKey);
dataMap.put("sign", signature);//簽名
//將類型爲map的參數轉換爲xml
String requestXml = WXPayUtil.mapToXml(dataMap);
//發送參數,調用微信統一下單接口,返回xml
String responseXml = HttpUtil.doPost(unifiedorderUrl, requestXml);
Map<String, String> map = WXPayUtil.xmlToMap(responseXml);
if (map.get("return_code").toString().equals("SUCCESS") && map.get("result_code")
.toString().equals("SUCCESS")) {
String urlCode = (String) map.get("code_url"); //微信二維碼短鏈接
// 生成微信二維碼(或者直接傳給前端處理)
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// 內容所使用編碼
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(urlCode, BarcodeFormat
.QR_CODE, 300, 300, hints);
// 生成二維碼
MatrixToImageWriter.writeToFile(bitMatrix, "gif", new File("C:/downloads/二維碼文件" +
".gif"));
} else {
}
} catch (Exception e) {
}
}
//自定義回調接口
@RequestMapping("/notifyUrl")
public String notifyUrl(String unifiedorderUrl, String requestXml) {
System.out.print("h");
//掃碼支付完成之後回調接口,修改訂單狀態
return "回調成功";
}
}