寫在前面:?
1. 后端使用微信官方sdk會(huì)簡單很多,可以看看在第五個(gè)大標(biāo)題上。
2. 相關(guān)文檔匯總:
? ? ? ? 1、WxJava - MiniApp Java SDK 4.6.0 API
? ? ? ? 2、wechatpay-java/README.md at main · wechatpay-apiv3/wechatpay-java · GitHub
? ? ? ? 3、網(wǎng)絡(luò) | 微信開放文檔
? ? ? ? 4、開發(fā)指引 - JSAPI支付 | 微信支付商戶文檔中心
3. 以下所有問題均基于uniapp開發(fā)微信小程序。
4. 所使用的工具版本為:
????????1、微信開發(fā)者工具:穩(wěn)定版?Stable Build?(1.06.2402040);
????????2、uniapp工具:HBuilder X 3.99;
????????3、后端程序語言:java;
5. 官方支付流程:
一、請求路徑不在以下 request 合法域名列表中
1. 報(bào)錯(cuò)詳細(xì)信息:
http://localhost:30013 不在以下 request 合法域名列表中,請參考文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html
2. 解決思路:
? ? ? ? 2.1 將域名配置在小程序中
? ? ? ? 根據(jù)報(bào)錯(cuò)信息查看微信相關(guān)文檔,網(wǎng)絡(luò) | 微信開放文檔
? ? ? ? 2.2 如果只是臨時(shí)修改或有其他需求可以修改微信開發(fā)工具中的配置
? ? ? ? ? ? ? ? 啟用設(shè)置中的“不校驗(yàn)合法域名、web-view業(yè)務(wù)域名、TLS版本以及HTTPS證書”選項(xiàng)
二、獲取用戶openid出現(xiàn)40029響應(yīng)碼
1. 請求url:
https://api.weixin.qq.com/sns/jscode2session
2. 微信響應(yīng)原文:
Response Body:?
????{"errcode":40029,"errmsg":"invalid code, rid: 660a3033-68f26499-0b8d2eb4"}
3. 解決思路:
? ? ? ? 3.1 查看代碼code是否被使用了兩次;
? ? ? ? 3.2 檢查小程序配置的appid是否與代碼中一致、appSecret是否正確
? ? ? ? ? ? ? ??檢查小程序配置的appid:看appid是否與微信開發(fā)工具中配置的一致。
三、uni.requestPayment()下單時(shí)產(chǎn)生參數(shù)校驗(yàn)錯(cuò)誤parameter error
1. 報(bào)錯(cuò)具體信息:
requestPayment:fail parameter error: parameter.timeStamp should be String instead of Undefined;parameter.nonceStr should be String instead of Undefined;parameter.package should be String instead of Undefined;parameter.signType should be String instead of Undefined;parameter.paySign should be String instead of Undefined;
2. 解決思路:
可以將所有參數(shù)都打印出來查看。下面出現(xiàn)錯(cuò)誤可能原因:
? ? ? ? 2.1 請求數(shù)據(jù)字段數(shù)據(jù)類型必須為String
? ? ? ? 后臺(tái)可能返回的不是String而是int類型,需要調(diào)整為String;
????????2.2?響應(yīng)數(shù)據(jù)取用層級不對。
? ? ? ? 比如后臺(tái)返回的數(shù)據(jù)是{data:{data:{}}},只取到數(shù)據(jù)所在層級前幾層;
????????2.3?請求數(shù)據(jù)所放層級不對。
? ? ? ? 正確的應(yīng)該是paySign等參數(shù)與success平行,結(jié)果參照了其他請求示例,將這些參數(shù)放在orderInfo下面了,正確的官方請求示例為:
?四、解密退款通知時(shí)產(chǎn)生Tag mismatch錯(cuò)誤
官方文檔相關(guān)問題鏈接:微信支付-QA文檔
1. 報(bào)錯(cuò)具體信息:
javax.crypto.AEADBadTagException: Tag mismatch!
2. 相關(guān)代碼:
//原始代碼
JSONObject resource = jsonObject.getJSONObject("resource");
AesUtil aesUtil = new AesUtil("v3密鑰".getBytes());
String s = aesUtil.decryptToString(
resource.getString("associated_data").getBytes(),
resource.getString("nonce").getBytes(),
resource.getString("ciphertext")
);
3. 解決思路:
在網(wǎng)上查詢后發(fā)現(xiàn)的可能原因:
? ? ? ? 3.1 v3秘鑰有問題。
? ? ? ? 檢查使用的v3秘鑰是否有問題
? ? ? ? 3.2 解密入?yún)⑿枰評tf-8轉(zhuǎn)換。
? ? ? ? 相關(guān)文章鏈接:分享微信支付回調(diào)解密提示:Tag mismatch | 微信開放社區(qū)
//更改后
JSONObject resource = jsonObject.getJSONObject("resource");
AesUtil aesUtil = new AesUtil("你的密鑰".getBytes(StandardCharsets.UTF_8));
String s = aesUtil.decryptToString(
resource.getString("associated_data").getBytes(StandardCharsets.UTF_8),
resource.getString("nonce").getBytes(StandardCharsets.UTF_8),
resource.getString("ciphertext")
);
? ? ? ? 3.3?解密時(shí)接口遺漏傳入附加數(shù)據(jù)(associated_data)。
? ? ? ? 我忘了我為了美觀規(guī)范寫成了小駝峰的形式,導(dǎo)致 接收數(shù)據(jù)類的字段名 和 微信傳過來的 不一致,所以associated_data沒有接收到?(最好使用微信官方sdk,各種實(shí)體類和方法就不用再次創(chuàng)建了):
//微信原始發(fā)送的json
{"id":"xxx","create_time":"2024-04-02T11:53:30+08:00","resource_type":"encrypt-{
"id": "xxx",
"create_time": "2024-04-02T11:53:30+08:00",
"resource_type": "encrypt-resource",
"event_type": "REFUND.SUCCESS",
"summary": "退款成功",
"resource": {
"original_type": "refund",
"algorithm": "AEAD_AES_256_GCM",
"ciphertext": "xxx",
"associated_data": "refund",
"nonce": "xxx"
}
}
//錯(cuò)誤接收的json
{
"id": "xxx",
"resource": {
"algorithm": "AEAD_AES_256_GCM",
"ciphertext": "xxx",
"nonce": "xxx"
},
"summary": "退款成功"
}
五、使用代碼示例:后端使用微信官方sdk示例
pom:
<!-- 微信sdk依賴 -->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.12</version>
</dependency>
代碼:?文章來源:http://www.zghlxwxcb.cn/news/detail-857640.html
/**
* refundApply 退款申請
*
* @param request
* @return Result<java.lang.Object>
*/
@PostMapping("refund-apply")
public Result<Object> refundApply(@RequestBody CreateRequest request) {
// 獲取.p12文件并獲取其中的信息
P12InfoVo vo = getP12Info();
// 初始化商戶配置
Config config =
new RSAAutoCertificateConfig.Builder()
.merchantId(readWechatProfile.getMchId())
.privateKey(vo.getPrivateKey())
.merchantSerialNumber(vo.getSerialNo())
.apiV3Key(readWechatProfile.getV3())
.build();
// 初始化服務(wù),使用微信sdk對應(yīng)功能的service
refundService = new RefundService.Builder().config(config).build();
// 調(diào)用對應(yīng)service中的接口
Refund response = refundService.create(request);
log.info("退款申請響應(yīng):{}", response);
return Result.success();
}
其中privateKey方法是可以替換成讀取私鑰的.pem文件地址,具體可查看類中api。另外讀取p12文件api:文章來源地址http://www.zghlxwxcb.cn/news/detail-857640.html
public P12InfoVo getP12Info2() {
String p12Path = "xx/xx/xxx.p12";// p12證書路徑
char[] mchid = "xxx" .toCharArray();//商戶號
try {
// 獲取文件流
FileInputStream fileInputStream = new FileInputStream(p12Path);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(fileInputStream, mchid);
String keyAlias = null;
//解析證書,必須有別名
Enumeration<String> aliases = keyStore.aliases();
if (aliases.hasMoreElements()) {
keyAlias = aliases.nextElement();
}
//解析私鑰
PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, mchid);
Certificate cert = keyStore.getCertificate(keyAlias);
BigInteger serialNumber = ((X509CertImpl) cert).getSerialNumber();
//證書一般都使用16進(jìn)制表示
String certSn = serialNumber.toString(16).toUpperCase();
//設(shè)置證書公鑰、私鑰、序列號
return P12InfoVo.builder()
.publicKey(cert.getPublicKey())
.privateKey(privateKey)
.serialNo(certSn)
.build();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
到了這里,關(guān)于微信小程序?qū)游⑿胖Ц端鰡栴}合集的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!