官方已經停止使用小微商戶進件API了,但是聽朋友說他們還在使用。果然是微信區別對待服務商,這裏記錄一下,有渠道開通的朋友可以瞭解一下。
微信小微商戶進件(一):獲取證書
先來看一下官方提供的文檔
地址:https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=19_11
接口調用請求說明
請求Url | https://api.mch.weixin.qq.com/risk/getcertficates |
---|---|
是否需要證書 | 否 |
請求方式 | POST、XML |
簽名方式 | HMAC-SHA256 |
1. 首先準備請求的參數
WxGetcertficatesModel.java
package com.pay.wechat.protocol;
import java.util.HashMap;
import java.util.Map;
import com.pay.wechat.util.Signature;
import com.util.Config;
import com.util.UUIDUtil;
/**
* 調用獲取平臺證書V2接口之前,請前往微信支付商戶平臺升級API證書,升級後纔可成功調用本接口。
*
* @author libaibai
* @version 1.0 2020年5月26日
*/
public class WxGetcertficatesModel {
private String mch_id = Config.MCHIDSP; // 服務商商戶號或渠道號
private String nonce_str = UUIDUtil.getUUID19(); //
private String sign; // 簽名
private String sign_type = "HMAC-SHA256"; //
public WxGetcertficatesModel(String key) {
this.sign = Signature.getSignSha(getMap(), key);
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getSign_type() {
return sign_type;
}
public void setSign_type(String sign_type) {
this.sign_type = sign_type;
}
private Map<String, Object> getMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("mch_id", Config.MCHIDSP);
map.put("nonce_str", nonce_str);
map.put("sign_type", "HMAC-SHA256");
return map;
}
}
2. 簽名驗證
這裏的簽名採用的是sign_type=HMAC-SHA256的加密方式
Signature.java
package com.pay.wechat.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.wsimpl.bo.wx.einvoice.HMacShaUtil;
/**
* 簽名util 類
*
* @author libaibai
* @version 1.0 2020年5月10日
*/
public class Signature {
/**
* 簽名算法( HMAC-SHA256)
*
* @param map
* @param key
* @return
*/
public static String getSignSha(Map<String, Object> map, String key) {
ArrayList<String> list = new ArrayList<String>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != "") {
list.add(entry.getKey() + "=" + entry.getValue() + "&");
}
}
int size = list.size();
String[] arrayToSort = list.toArray(new String[size]);
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append(arrayToSort[i]);
}
String result = sb.toString();
result += "key=" + key;
result = HMacShaUtil.sha256_HMAC(result, key).toUpperCase();
return result;
}
}
HMacShaUtil.java
package com.wsimpl.bo.wx.einvoice;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* HMacSHA256加密解密工具類
*
* @author libaibai
* @version 1.0 2020年5月10日
*/
public class HMacShaUtil {
private static final Logger LOG = LogManager.getLogger(HMacShaUtil.class);
/**
* 將加密後的字節數組轉換成字符串
*
* @param b 字節數組
* @return 字符串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b!=null && n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1)
hs.append('0');
hs.append(stmp);
}
return hs.toString();
//return hs.toString().toLowerCase();
}
/**
* sha256_HMAC加密
* @param message 消息
* @param secret 祕鑰
* @return 加密後字符串
*/
public static String sha256_HMAC(String message, String secret) {
String hash = "";
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
hash = byteArrayToHexString(bytes);
//hash = new String(bytes,"UTF-8");
} catch (Exception e) {
LOG.info("Error HmacSHA256加密:" + e.getMessage());
}
return hash;
}
}
3. 發送請求
package com.pay.wechat.bo.small;
import org.springframework.stereotype.Component;
import com.pay.wechat.protocol.WxGetcertficatesModel;
import com.pay.wechat.util.HttpsRequest;
import com.util.Config;
import net.sf.json.JSONObject;
/**
* 調用獲取平臺證書
*
* @author libaibai
* @version 1.0 2020年5月26日
*/
public class WxGetcertficatesBo {
public void exe() {
try {
WxGetcertficatesModel model = new WxGetcertficatesModel(Config.APIKEY);
System.out.println("model=" + JSONObject.fromObject(model));
HttpsRequest request = new HttpsRequest();
String url = "https://api.mch.weixin.qq.com/risk/getcertficates";
String str = request.sendPost(url, model);
System.out.println(str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
WxGetcertficatesBo bo = new WxGetcertficatesBo();
bo.exe();
}
}
直接運行上面main方法,得到返回結果
ok,獲取證書測試通過!!!