前端js代码:
function onBridgeReady(appId,timeStamp,nonceStr,package,signType,paySign){
let params = {
"appId":appId, //公众号名称,由商户传入
"timeStamp":timeStamp, //时间戳,自1970年以来的秒数
"nonceStr":nonceStr, //随机串
"package":package,
"signType":signType, //微信签名方式:
"paySign":paySign //微信签名
};
WeixinJSBridge.invoke(
'getBrandWCPayRequest', params,
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ){
WeixinJSBridge.call('closeWindow');
}
});
}
function callpay(){
if (typeof WeixinJSBridge === undefined){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}
}
var isClick = true;
$("#formSubmitBtn").click(function () {
weui.form.validate('#form', function (error) {
if (!error) {
// ajax提交审核/防止多次点击
if (isClick) {
isClick = false;
$("#formSubmitBtn").removeClass("weui-btn_primary").css("color", "gray");
//事件提交
foo();
//定时器
setTimeout(function () {
isClick = true;
$("#formSubmitBtn").addClass("weui-btn_primary").css("color", "#fff");
}, 5000);//五秒内不能重复点击
}
}
}, regexp);
});
let foo = function () {
var formData = new FormData($("#form")[0]);
setTimeout(function () {
callpay();
$.ajax({
url: "/accPay/weixinpay",
type: "post",
data: formData,
// dataType: "json",
//发送不想转换的的信息
processData: false,
contentType: false,
success: function (data) {
var obj = data.orderInfo;
if (data.code == 0) {
let appId = obj.appId;
let timeStamp = obj.timeStamp;
let nonceStr = obj.nonceStr;
let package = obj.package;
let signType = obj.signType;
let paySign = obj.sign;
onBridgeReady(appId, timeStamp, nonceStr, package, signType, paySign);
}else{
$.toast(data.msg,"cancel");
}
}
});
}, 1500);
}
pom.xml文件中添加依赖:
com.mxixm
fastboot-weixin
0.6.2
com.egzosn
pay-java-common
2.12.4
com.egzosn
pay-java-wx
2.12.4
通过WxWebUser获取openid配置:微信公众平台——登录并进入公众号——功能设置——网页授权域名,设置域名。
application.yml
wx:
appid: 公众号APPID
appsecret: 公众号秘钥
token: 公众号token
callback-url: 域名
path: wx
# 微信公众号支付配置
weixinpay:
mchId: # 商户Id
storePassword: b # 秘钥支付密码
appid: #公众号APPID
secretKey: # 公众号秘钥
notifyUrl: #微信支付回调,例:域名/accPay/weixinpayBack
公众号配置类:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 微信公众号的配置信息
*/
@Data
@Component
@ConfigurationProperties(prefix = "weixinpay")
public class WeixinAccPayConfig {
private String mchId;
private String appid;
private String secretKey;
private String notifyUrl;
private String keystore;
private String storePassword;
}
后台业务逻辑代码:
@RestController
@RequestMapping("/accPay")
@Slf4j
public class WeiXinAccPayController {
@Autowired
WeixinAccPayConfig weixinpayConfig;//微信公众号
private PayService service = null;
@PostConstruct
public void init() {
WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
wxPayConfigStorage.setMchId(weixinpayConfig.getMchId()); // 合作者id(商户号
wxPayConfigStorage.setAppid(weixinpayConfig.getAppid()); // 应用id
wxPayConfigStorage.setSecretKey(weixinpayConfig.getSecretKey()); // 密钥
wxPayConfigStorage.setNotifyUrl(weixinpayConfig.getNotifyUrl()); // 异步回调地址 http://域名:端口号/项目名/回调接口名称
wxPayConfigStorage.setSignType(SignUtils.MD5.name());
wxPayConfigStorage.setInputCharset("utf-8");
service = new WxPayService(wxPayConfigStorage);
}
/**
* 返回订单信息
*
* @param request
* @return
*/
@RequestMapping(value = "/weixinpay", method = RequestMethod.POST)
@NoRepeatSubmit
public Map weixinpay(HttpServletRequest request, 在这里可以加前台需要向后台传递的参数) {
WxWebUser wxWebUser = WxWebUtils.getWxWebUserFromSession();
if (wxWebUser == null) {
return R.error("请使用微信操作");
}
// 在这一步,可以传入一个订单Id,自行去搜索订单信息,并填写以下内容
PayOrder payOrder = new PayOrder();// 这个就是支付成功后,在微信支付里面返回的信息(支付订单信息)
// 一下内容需要分情况而定,自行填写。
payOrder.setSubject("body");
payOrder.setPrice(new BigDecimal(0.01));// 价格
payOrder.setOutTradeNo(outTradeNo);
payOrder.setDeviceInfo("WEB");
payOrder.setSpbillCreateIp(IPUtils.getIpAddr());//自行搜索IPUtils
payOrder.setOpenid(wxWebUser.getOpenId());
payOrder.setTransactionType(WxTransactionType.JSAPI);// 支付方式
Map orderInfo = service.orderInfo(payOrder);// 返回创建的订单信息
log.info("获取预支付订单信息回参" + orderInfo.toString());
// 可自行选择 ,是否将支付的流水插入到数据库中。返回的信息由:signType appId timeStamp nonceStr package sign
return R.ok().put("orderInfo", orderInfo);
}
/**
* 支付回调
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = "weixinpayBack")
public String payBack(HttpServletRequest request) throws IOException {
// 获取支付方返回的对应参数
Map params = service.getParameter2Map(request.getParameterMap(), request.getInputStream());
if (null == params) {
log.debug("通知失败");
return service.getPayOutMessage("failed", "通知失败").toMessage();
}
// 校验
if (service.verify(params)) {
// 这里处理业务逻辑 支付成功后的代码逻辑块
// ......业务逻辑处理块,注意:回调可能会多次调用........
//使用WxMessageTemplate.sendTemplateMessage(templateMessage)发送模板消息
log.info("request.getParameterMap:" + params.toString());
return service.getPayOutMessage("success", "支付成功").toMessage();
}
log.debug("通知支付失败");
return service.getPayOutMessage("fail", "支付失败").toMessage();
}