spring boot整合微信支付
一、官方概述
com.github.wxpay.sdk.WXPay类下提供了对应的方法:
-
注意: 证书文件不能放在web服务器虚拟目录,应放在有访问权限控制的目录中,防止被他人下载 建议将证书文件名改为复杂且不容易猜测的文件名 商户服务器要做好病毒和木马防护工作,不被非法侵入者窃取证书文件 请妥善保管商户支付密钥、公众帐号SECRET,避免密钥泄露 参数为Map<String, String>对象,返回类型也是Map<String, String> 方法内部会将参数会转换成含有appid、mch_id、nonce_str、sign\_type和sign的XML 可选HMAC-SHA256算法和MD5算法签名 通过HTTPS请求得到返回数据后会对其做必要的处理(例如验证签名,签名错误则抛出异常) 对于downloadBill,无论是否成功都返回Map,且都含有return_code和return_msg,若成功,其中return_code为SUCCESS,另外data对应对账单数据
二、准备工作
2.获取appid
3.获取mchId
4.配置JSAPI域名
5.获取api密钥(遗忘需要重置)
7.获取apiclient_cert.p12文件
三、着手开发
引入依赖
GetMpController 文件(获取 MP_verify_cqlNsdZY4xZktC2l.txt 文件内容)
WXinConfig
package com.zygh.webapi.config;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class WXinConfig {
@Bean
public WxMpService wxmpservice() {
WxMpServiceImpl wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
MyConfig config = new MyConfig();
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setSecret(config.getAppSecret());
wxMpConfigStorage.setAppId(config.getAppID());
return wxMpConfigStorage;
}
}
MyConfig文件
WxPayEntity文件
WxPayController文件
前端
获取code
获取openId
this.$axiox({
method: GET,
url: this.$AXIOSURL + "/WxPay/getCode",
params: {
code: this.getQueryVariable("code")
}
}).then((response) => {
// this.openId = response;
sessionStorage.setItem(openId, response);
this.$router.replace(/myCenter);
// console.log(response, 418);
}).catch((response) => {
console.log(response)
Toast.fail(网络异常);
})
*注:所有{}内的内容为需要替换的内容,所有前后端代码已脱敏处理,前端接口替换未使用{}进行标注,需自行查找并修改
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
springboot集成微信支付
