微信支付升級V3接口總結(jié):
主要步驟如下:
- 首先需要獲取所需要的證書讹语、私鑰甫题、秘鑰等信息
- 簡化加簽驗(yàn)簽符欠,使用微信提供的支付sdk工具
- 根據(jù)sdk demo中的實(shí)例蛹批,提供所需要的信息仰泻,使用微信提供的http方法進(jìn)行接口請求
一荆陆、獲取證書、私鑰集侯、秘鑰等信息
開發(fā)需要讓運(yùn)營聯(lián)系財(cái)務(wù)在微信后臺對V3秘鑰的獲取和證書的下載被啼,具體教程請參考:
證書:https://kf.qq.com/faq/161222NneAJf161222U7fARv.html
秘鑰:https://kf.qq.com/faq/180830E36vyQ180830AZFZvu.html
通過如上步驟之后會(huì)獲取到如下信息: 商戶號帜消、apiKey3 APIv3 密鑰、apiclient_cert.pem浓体、apiclient_key.pem泡挺、apiclient_cert.p12、證書使用說明.txt
二命浴、獲取微信支付sdk
sdk對應(yīng)的github地址:https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
maven依賴:
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.3</version>
</dependency>
創(chuàng)建加密后的HttpClient:
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withWechatPay(wechatpayCertificates);
// ... 接下來娄猫,你仍然可以通過builder設(shè)置各種參數(shù),來配置你的HttpClient
// 通過WechatPayHttpClientBuilder構(gòu)造的HttpClient生闲,會(huì)自動(dòng)的處理簽名和驗(yàn)簽
HttpClient httpClient = builder.build();
// 后面跟使用Apache HttpClient一樣
HttpResponse response = httpClient.execute(...);
參數(shù)說明:
merchantId
商戶號媳溺。merchantSerialNumber
商戶API證書的證書序列號。merchantPrivateKey
商戶API私鑰碍讯,如何加載商戶API私鑰請看常見問題褂删。wechatpayCertificates
微信支付平臺證書。你也可以使用后面章節(jié)提到的“自動(dòng)更新證書功能”冲茸,而不需要關(guān)心平臺證書的來龍去脈屯阀。
merchantSerialNumber為證書序列號需要通過命令獲取: openssl x509 -in apiclient_cert.pem -noout -serial 命令進(jìn)行獲取
merchantPrivateKey通過apiclient_key.pem文件獲取,注意需要對證書內(nèi)容進(jìn)行清洗轴术,具體代碼如下:
/**
* 獲取私鑰难衰。
*
* @param filename 私鑰文件路徑 (required)
* @return 私鑰對象
*/
public static PrivateKey getPrivateKey(String filename) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8");
try {
String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s+", "");
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("當(dāng)前Java環(huán)境不支持RSA", e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException("無效的密鑰格式");
}
}
wechatpayCertificates是最難理解,我也沒理解明白逗栽,大概意思是為了讓商戶自己來完成證書的自動(dòng)更新和自動(dòng)下載盖袭,但是首次使用的不是財(cái)務(wù)同學(xué)從微信商戶后臺下載下來的apiclient_key.pem或者apiclient_cert.p12,需要我們手動(dòng)的下載一次彼宠,微信提供了下載證書的jar包CertificateDownloader.jar鳄虱,
下載地址為:https://github.com/EliasZzz/CertificateDownloader/releases
使用說明參考:https://github.com/wechatpay-apiv3/CertificateDownloader
具體命令: java -jar CertificateDownloader.jar -k 76a5e**************9eccd3arh66 -m --mchid=122*****801 -f --privatekey=apiclient_key.pem -s --serialno=4B2882**********************CD03 -o --output=newCerDir
// 在第一次一般用不到這個(gè)參數(shù)選項(xiàng) -c apiclient_cert.pem
此時(shí)會(huì)在newCerDir下載到新的證書,至此凭峡,我們HttpClient所需要的所有參數(shù)都已經(jīng)獲取成功拙已。
三、參考微信sdk中提供的demo請求示例為:
PrivateKey privateKey = getPrivateKey("$filePath");
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant("12*****801", "4B********CD03", privateKey)
.withWechatpay(Arrays.asList(getCertificate(new FileInputStream(new File("${filePath}/newCerDir/wechatpay_22A***********0733515.pem")))));
URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/merchant/fund/balance/BASIC");
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.addHeader("Accept", "application/json");
httpGet.addHeader("Content-type","application/json; charset=utf-8");
HttpClient httpClient = builder.build();
HttpResponse response = httpClient.execute(httpGet);
response.getEntity()
參考的文檔比較多的摧冀,比較詳細(xì)的文檔為:https://developers.weixin.qq.com/community/develop/article/doc/000cca8440c6a0dca61a3efb053c13
再次鳴謝作者倍踪,和此文檔差異的地方是,本文使用的是微信提供的java SDK這種比較簡單的方式索昂,