微信V3支付-jsapi
package com.labour.utils;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.ArrayList;
import java.util.Base64;
/**
* @ClassName WxAPIV3SignUtils
* @Description: 微信API-V3签名工具
* @Author 是涛吖
* @Date 2021/8/32
* @Version V1.0
*/
public class WxAPIV3SignUtils {
//这个秘钥我删除掉了一些 属于个人隐私 要注意别泄露了
public static String privateKey = "-----BEGIN PRIVATE KEY-----\n" +
"xxxxxxxxxxxxxxxxxxxxxx\n" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" +
"pZRCozmRKLEQmWH7qgVZbX5DVp/xxxxx+J/ZL2yZtUyvg0/4\n" +
"xxxxxxxxxxxxxxxxxxxxxx+XwI0XFZLPrcakNA2OjOE0ryRI\n" +
"2+Mt+htmIHOgRf01/HxcWBhC+xxxxxx/vMsWsEle\n" +
"OqX9ax3ajrHqvCysN9mhiac+RWrxE6KHrAooLeHK/W4RkOLR8D4pm7+Aq/kym0vX\n" +
"JRusjiNpAgMBAAECggEBAJcj1pT+Cmr2We2dHfePSzunxU2ukIvlXmN+G4Cxm0g9\n" +
"zROrgFwC+eRSZ0ZTjkQWFHRNeGLrzCzB0M7+crj04uJ+O4yzWge+RQJ77krpRiXy\n" +
"vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZ\n" +
"9S5nV7nBpMn0uqJhK85lGt9+KJh5TMUGhPH8acW/IwUTLHkMcA2Wu/czHWXkuKRS\n" +
"MNl9Vxvou66KKrPHVn0drp0+GAXRw3cLhdKgxXAsDGb1LLhIGrJXO3OLWh48s1y7\n" +
"qd2yLKqp0nQTWdthkc1jEm1XxA==\n" +
"-----END PRIVATE KEY-----\n";
public static String appid = "wxfcxxxxxxxxxxxxxx2";//商户id
public static String buildSignMessage(ArrayList
if (signMessage == null || signMessage.size() <= 0) {
return null;
}
StringBuilder sbf = new StringBuilder();
for (String str : signMessage) {
sbf.append(str).append("\n");
}
return sbf.toString();
}
/**
* 生成签名
*
* @param message 请求体
* @return 生成base64位签名信息
* @throws Exception
*/
public static String sign(byte[] message, PrivateKey yourPrivateKey) {
try {
// 签名方式(固定SHA256withRSA)
Signature sign = Signature.getInstance("SHA256withRSA");
// 使用私钥进行初始化签名(私钥需要从私钥文件【证书】中读取)
sign.initSign(yourPrivateKey);
// 签名更新
sign.update(message);
// 对签名结果进行Base64编码
return Base64.getEncoder().encodeToString(sign.sign());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
到这一步就可以获取到签名了 可以main自己测试 然后复制到前端直接调用
public static void main(String[] args) {
System.out.println(">>>模拟微信支付二次签名<<>>");
long timestamp = System.currentTimeMillis()/1000;
String nonce_str = WXPayUtil.generateNonceStr();
String packageStr = "prepay_id="+prepay_id;
ArrayList
list.add(WxAPIV3SignUtils.appid);
list.add(timestamp+"");
list.add(nonce_str);
list.add(packageStr);
//加载签名
String packageSign = WxAPIV3SignUtils.sign(WxAPIV3SignUtils.buildSignMessage(list).getBytes(), merchantPrivateKey);
System.out.println("packageSign :"+packageSign);
System.out.println("timestamp :"+timestamp);
System.out.println("nonce_str :"+nonce_str);
}
微信支付前端调用代码
onBridgeReady(data) {
let packageStr = data.package//订单编号
let nonceStr = data.nonceStr
let timeStamp = data.timeStamp
let paySign = data.paySign
// console.log(nonceStr)
WeixinJSBridge.invoke('getBrandWCPayRequest', {
"appId": "wxxxxxxxxxxxx2", //公众号ID,由商户传入
"timeStamp": timeStamp, //时间戳,自1970年以来的秒数
"nonceStr": nonceStr, //
"package": packageStr,
"signType": "RSA", //微信签名方式:
"paySign": paySign//微信签名
},
function(res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
// 使用以上方式判断前端返回,微信团队郑重提示:
//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
},
————————————————
版权声明:本文为CSDN博主「斗志请给我来一打」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_25007459/article/details/119896078