项目需要搞微信支付,百度了那么多但是太复杂了,还有腾讯的那些个文档真是RLGL
下面直接上步骤:
第一步:加入第三方jar包:
<dependency> <groupId>cn.springboot</groupId> <artifactId>best-pay-sdk</artifactId> <version>1.1.0</version></dependency> 第二步:在项目根路径下创建配置文件weixinpay.properties,当然你写死在代码里也可以。(这步就可以省略)
# 公众账号IDweixin.mpAppId=#商户号weixin.mchId=#商户密钥weixin.mchKey=商户证书路径weixin.keyPath=#微信支付异步通知地址weixin.notifyUrl= 第三步:创建微信支付配置文件类
package com.github.lly835.config;import com.lly835.bestpay.config.WxPayH5Config;import com.lly835.bestpay.service.impl.BestPayServiceImpl;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component; import java.util.ResourceBundle; /** * 微信支付配置文件 */@Componentpublic class PayConfig { @Bean public WxPayH5Config wxPayH5Config() { WxPayH5Config wxPayH5Config = new WxPayH5Config(); ResourceBundle resource = ResourceBundle.getBundle("weixinpay");//不要加.properties后缀,我加了报错
wxPayH5Config.setAppId(resource.getString("weixin.mpAppId")); //这里的参数你如果写死,第二步可以省略 wxPayH5Config.setMchId(resource.getString("weixin.mchId")); wxPayH5Config.setMchKey(resource.getString("weixin.mchKey")); wxPayH5Config.setKeyPath(resource.getString("weixin.keyPath")); wxPayH5Config.setNotifyUrl(resource.getString("weixin.notifyUrl")); return wxPayH5Config; } @Bean public BestPayServiceImpl bestPayService(WxPayH5Config wxPayH5Config) { BestPayServiceImpl bestPayService = new BestPayServiceImpl(); bestPayService.setWxPayH5Config(wxPayH5Config); return bestPayService; }} 第四步:微信支付调用
/** * 微信支付 */@Controller@Slf4jpublic class PayController { final static Logger log = LoggerFactory.getLogger(PayController.class); @Autowired private BestPayServiceImpl bestPayService; /** * 发起微信支付 * @param openid 用户唯一标识 * @param orderAmount 订单金额 * @param orderName 订单描述 * @param orderId 订单编号 * @param map * @return */ @GetMapping(value = "/pay") public ModelAndView pay(@RequestParam("openid") String openid,Double orderAmount,String orderName,String orderId, Map<String, Object> map) { PayRequest request = new PayRequest(); //支付请求参数 request.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5); request.setOrderId(orderId); request.setOrderAmount(orderAmount); request.setOrderName(orderName); request.setOpenid(openid); log.info("【发起支付】request={}", JsonUtil.toJson(request)); PayResponse payResponse = bestPayService.pay(request); log.info("【发起支付】response={}", JsonUtil.toJson(payResponse)); map.put("payResponse", payResponse); return new ModelAndView("weixin/create", map); } /** * 异步回调 * @param notifyData <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg> * @return * @throws Exception */ @PostMapping(value = "/notify") public ModelAndView notify(@RequestBody String notifyData) throws Exception { log.info("【异步回调】request={}", notifyData); PayResponse response = bestPayService.asyncNotify(notifyData); log.info("【异步回调】response={}", JsonUtil.toJson(response)); return new ModelAndView("weixin/success"); }}
前端代码:create.ftl代码
<script> function onBridgeReady(){ WeixinJSBridge.invoke( ‘getBrandWCPayRequest‘, { "appId":"${payResponse.appId}", //公众号名称,由商户传入 "timeStamp":"${payResponse.timeStamp}", //时间戳,自1970年以来的秒数 "nonceStr":"${payResponse.nonceStr}", //随机串 "package":"${payResponse.packAge}", "signType":"MD5", //微信签名方式: "paySign":"${payResponse.paySign}" //微信签名 }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert(‘支付成功‘); }else if(res.err_msg == "get_brand_wcpay_request:cancel") { alert(‘支付过程中用户取消‘); }else if(res.err_msg == "get_brand_wcpay_request:fail") { alert(‘支付失败‘); }else { alert(‘未知异常‘); } } ); } if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener(‘WeixinJSBridgeReady‘, onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent(‘WeixinJSBridgeReady‘, onBridgeReady); document.attachEvent(‘onWeixinJSBridgeReady‘, onBridgeReady); } }else{ onBridgeReady(); }</script> success.ftl代码
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg></xml>
整个项目结构图:
原文地址:https://www.cnblogs.com/zeussbook/p/10239885.html
时间: 2024-10-11 16:29:06