前端代码(uni-app)
let that = this;
uni.login({
success:function(res){
if(res.code){
that.$u.post('',{
code: res.code
}).then(res => {
console.log(res)
uni.requestPayment({
provider: "wxpay",
timeStamp: res.timeStamp,
nonceStr: res.nonceStr,
package: res.package,
signType: res.signType,
paySign: res.paySign,
success: (response) => {
uni.showToast({
title: '支付成功',
duration: 1500
});
},
fail: (err) => {
uni.showToast({
title: '支付失败',
icon: 'none'
});
}
});
})
}
}
})
后端
//通过code、appid、secret获取
String temp = HttpUtils.sendGet(url, openidget);
Map<String, Object> unionidMap = (Map<String, Object>) JSON.parse(temp);
String openid = (String) unionidMap.get("openid");//拿openid
MyWxConfig config = new MyWxConfig(appid); //微信支付配置类
WXPay pay = new WXPay(config);
Map<String, String> param = new HashMap<>();
param.put("appid", appid);
param.put("mch_id", config.getMchID());
param.put("nonce_str", RandomStringUtils.randomAlphanumeric(32));
param.put("body", content);
param.put("out_trade_no", oid);
param.put("total_fee",fee);
param.put("openid", openid);
param.put("attach", "测试中");
param.put("sign", wxSign(param, config.getKey()));
param.put("spbill_create_ip", getClientIP(request)); //终端IP
param.put("notify_url", "http://lnfc.vip/api/wxNotify");
// param.put("notify_url", "http://u38455l599.zicp.vip/api/wxNotify");
param.put("trade_type", "JSAPI"); //交易类型
System.out.println("支付完毕准备回调");
Map<String, String> res = pay.unifiedOrder(param);
System.out.println(res);
Map<String, String> result = new HashMap<>();
result.put("appId", appid);
result.put("timeStamp", (new Date().getTime() / 1000) + "");
result.put("nonceStr", RandomStringUtils.randomAlphanumeric(32));
System.out.println("prepay_id="+res.get("prepay_id"));
result.put("package", "prepay_id=" + res.get("prepay_id"));
result.put("signType", "MD5");
result.put("paySign", wxSign(result, config.getKey()));
其他具体业务具体分析