准备工作
依赖引入
<dependency> <groupid>com.github.binarywang</groupid> <artifactid>weinxin-java-pay</artifactid> <version>对应的版本号</version> </dependency>
证书申请和下载
官方文档: 直达
撸起袖子使劲干
配置类
参数配置类
/** * @author: eden4j * @description: 微信支付商户信息配置类 */ @data @component @refreshscope @configurationproperties(prefix = "wechat.pay") public class wechatpayproperties { /** * 微信小程序或者微信公众号appid */ private string appid; /** * 商户号 */ private string mchid; /** * 商户密钥 */ private string mchkey; /** * 商户证书序列号 */ private string serialno; /** * apiv3key */ private string apiv3key; /** * 证书 */ private string keypath; /** * 商户私钥文件 */ private string privatekeypath; /** * apiclient_cert.pem证书文件 */ private string privatecertpath; /** * 交易类型 * jsapi--公众号支付 * native--原生扫码支付 * app--app支付 */ private string tradetype; /** * 支付结果异步通知回调地址 */ private string notifyurl; }
微信支付配置类
/** * @author: eden4j * @description: 微信支付配置类 */ @component @conditionalonclass(wxpayservice.class) @requiredargsconstructor public class wechatpayconfig { private final wechatpayproperties properties; @bean @conditionalonmissingbean public wxpayconfig payconfig() { wxpayconfig payconfig = new wxpayconfig(); payconfig.setappid(stringutils.trimtonull(this.properties.getappid())); payconfig.setmchid(stringutils.trimtonull(this.properties.getmchid())); payconfig.setmchkey(stringutils.trimtonull(this.properties.getmchkey())); payconfig.setcertserialno(stringutils.trimtonull(this.properties.getserialno())); payconfig.setapiv3key(stringutils.trimtonull(this.properties.getapiv3key())); payconfig.setkeypath(stringutils.trimtonull(this.properties.getkeypath())); payconfig.setprivatekeypath(stringutils.trimtonull(this.properties.getprivatekeypath())); payconfig.setprivatecertpath(stringutils.trimtonull(this.properties.getprivatecertpath())); payconfig.settradetype(stringutils.trimtonull(this.properties.gettradetype())); return payconfig; } @bean public wxpayservice wxpayservice(wxpayconfig payconfig) { wxpayservice wxpayservice = new wxpayserviceimpl(); wxpayservice.setconfig(payconfig); return wxpayservice; } }
自定义的微信预支付返回信息类
@data public class wxpayinfovo implements serializable { private string appid; private string timestamp; private string noncestr; private string packagevalue; private string signtype; private string paysign; }
工具类
/** * @author: eden4j * @description: 微信支付相关工具类 */ public class wxpayutil { public static string sign(string signstr, privatekey privatekey) { sign sign = secureutil.sign(signalgorithm.sha256withrsa, privatekey.getencoded(), null); return base64.encode(sign.sign(signstr.getbytes())); } public static string buildmessage(string appid, string timestamp, string noncestr, string body) { return appid + "\n" + timestamp + "\n" + noncestr + "\n" + body + "\n"; } public static autoupdatecertificatesverifier getverifier(wxpayconfig wxpayconfig) throws ioexception { privatekey privatekey = pemutils.loadprivatekey(new classpathresource("apiclient_key.pem").getinputstream()); autoupdatecertificatesverifier verifier = new autoupdatecertificatesverifier( new wxpaycredentials(wxpayconfig.getmchid(), new privatekeysigner(wxpayconfig.getcertserialno(), privatekey)) , wxpayconfig.getapiv3key().getbytes("utf-8") ); return verifier; } /** * 验证签名 * * @param certificate * @param message * @param signature * @return */ public static boolean verify(autoupdatecertificatesverifier certificate, byte[] message, string signature) { try { signature sign = signature.getinstance("sha256withrsa"); sign.initverify(certificate.getvalidcertificate()); sign.update(message); return sign.verify(java.util.base64.getdecoder().decode(signature)); } catch (nosuchalgorithmexception e) { throw new runtimeexception("当前java环境不支持sha256withrsa", e); } catch (signatureexception e) { throw new runtimeexception("签名验证过程发生了错误", e); } catch (invalidkeyexception e) { throw new runtimeexception("无效的证书", e); } } }
接口层和服务处理层代码
重点注意:微信支付回调结果通知不要使用jsonobject去做接收
接口层
@restcontroller @requestmapping("/请求路径") @requiredargsconstructor public class appcustomeruserrechargeordercontroller { private final iappcustomeruserrechargeorderservice rechargeorderservice; @postmapping("/preorder") public result<wxpayinfovo> preorder(@requestparam("goodid") string goodid) { return result.success(rechargeorderservice.preorder(goodid)); } // 支付回调 @postmapping("/back") public map<string,string> back(@requestbody map body,httpservletrequest request) { return rechargeorderservice.formalorder(body, request); } }
处理层
@override public wxpayinfovo prerecharge(string goodid) { try { //看看是否有其他的业务逻辑 // 调用微信的统一下单接口获取预支付单返回前端 createorderv3(tradetypeenum tradetype, wxpayunifiedorderv3request request) wxpayunifiedorderv3request payrequest = new wxpayunifiedorderv3request(); payrequest.setouttradeno(rechargeno);//系统充值订单号 payrequest.setdescription(goodspackage.getpackagename()); //订单失效时间 simpledateformat dateformat = new simpledateformat("yyyy-mm-dd't'hh:mm:ssxxx"); payrequest.settimeexpire(dateformat.format(dateutil.offset(new date(), datefield.hour_of_day, 1))); wxpayunifiedorderv3request.payer payer = new wxpayunifiedorderv3request.payer(); payer.setopenid(useraccount.getopenid()); payrequest.setpayer(payer); wxpayunifiedorderv3request.amount amount = new wxpayunifiedorderv3request.amount(); amount.settotal(rechargeorder.gettotalamount().multiply(bigdecimal.valueof(100)).intvalue()); amount.setcurrency("cny"); payrequest.setamount(amount); payrequest.setnotifyurl(wechatpayproperties.getnotifyurl()); wxpayunifiedorderv3result.jsapiresult jsapiresult = wxpayservice.createorderv3(tradetypeenum.jsapi, payrequest); string encode = wxpayutil.sign(wxpayutil.buildmessage(wechatpayproperties.getappid(), jsapiresult.gettimestamp(), jsapiresult.getnoncestr(), jsapiresult.getpackagevalue()) , pemutils.loadprivatekey(new classpathresource("apiclient_key.pem").getinputstream())); wxpayinfovo payinfovo = pojoutil.exchangepojo(jsapiresult, wxpayinfovo.class); payinfovo.setpaysign(encode); return payinfovo; } catch (wxpayexception e) { e.printstacktrace(); throw new bizexception("微信支付失败", e); } catch (ioexception e) { e.printstacktrace(); throw new bizexception("微信支付失败", e); } } @override public map<string, string> createformalorder(map body, httpservletrequest request) { try { map<string, string> resultmap = new hashmap<>(); resultmap.put("code", "fail"); objectmapper objectmapper = new objectmapper(); try { //官方文档中有说明切记不要改变原始报文主体,如果使用jsonobject接收的话,不能使用json转换出来的字符串,会出现验签错误的情况,请注意 string data = objectmapper.writevalueasstring(body); signatureheader header = new signatureheader(); header.settimestamp(request.getheader("wechatpay-timestamp")); header.setnonce(request.getheader("wechatpay-nonce")); header.setsignature(request.getheader("wechatpay-signature")); header.setserial(request.getheader("wechatpay-serial")); wxpaynotifyv3result notifyv3result =wxpayservice.parseordernotifyv3result(data, header); wxpaynotifyv3result.decryptnotifyresult decryptnotifyresult = notifyv3result.getresult();//解密后的数据 if ("success".equalsignorecase(decryptnotifyresult.gettradestate())) { resultmap.put("code", "success"); } } catch (exception e) { e.printstacktrace(); } return resultmap; } catch (exception e) { e.printstacktrace(); throw new bizexception("微信支付成功回调失败", e); } }
到此这篇关于springboot整合weixin-java-pay实现微信小程序支付的示例代码的文章就介绍到这了,更多相关springboot weixin-java-pay微信小程序支付内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论