1.需要提前配置商户证书路径、微信支付异步通知地址,详见微信授权

package com.zkrt.smartsite.project.rural.config;import com.lly835.bestpay.config.WxPayH5Config;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** function** @author jhh* @date 2020/11/10 10:55*/
@Configuration
public class WeChatPayConfig {@Autowiredprivate WeChatAccountConfig weChatAccountConfig;@Beanpublic BestPayServiceImpl bestPayService(){BestPayServiceImpl bestPayService = new BestPayServiceImpl();bestPayService.setWxPayH5Config(wxPayH5Config());return bestPayService;}@Beanpublic WxPayH5Config wxPayH5Config(){WxPayH5Config wxPayH5Config = new WxPayH5Config();wxPayH5Config.setAppId(weChatAccountConfig.getMpAppId());wxPayH5Config.setAppSecret(weChatAccountConfig.getMpAppSecret());wxPayH5Config.setMchId(weChatAccountConfig.getMchId());wxPayH5Config.setMchKey(weChatAccountConfig.getMchKey());wxPayH5Config.setKeyPath(weChatAccountConfig.getKeyPath());wxPayH5Config.setNotifyUrl(weChatAccountConfig.getNotifyUrl());return wxPayH5Config;}
}

2.controller实现

@Api(tags = "农村饮用水缴费-微信支付信息")
@Controller
@RequestMapping("/pay")
public class WeChatPayController extends BaseController {//只需要接受orderid和回调地址@Autowiredprivate IRuralUserWaterPaymentService ruralUserWaterPaymentService;@Autowiredprivate WeChatPayServiceImpl weChatPayService;@ApiOperation("订单创建")@GetMapping("/create")public ModelAndView create(@RequestParam("orderId") String orderId,@RequestParam("openid") String openid,@RequestParam("returnUrl") String returnUrl,Map<String, Object> map) {//1. 根据传过来的orderid查询订单RuralUserWaterPayment payment = new RuralUserWaterPayment();payment.setOrderId(orderId);RuralUserWaterPayment orderDTO = ruralUserWaterPaymentService.selectRuralUserWaterPaymentList(payment).get(0);if(orderDTO.equals(0)){throw new SecurityException("ORDER_NOT_EXIST");}//2. 发起支付PayResponse payResponse = weChatPayService.create(orderDTO);map.put("payResponse", payResponse);map.put("openid",openid);map.put("returnUrl", returnUrl);//返回到WeixinJSBridge内置对象在其他浏览器中无效return new ModelAndView("pay/create", map);}/*** 微信异步通知* @param notifyData*/@ApiOperation("订单回调")@PostMapping("/notify")public ModelAndView notify(@RequestBody String notifyData) {weChatPayService.notify(notifyData);//返回给微信处理结果return new ModelAndView("pay/success");}
}

4.service层

public interface WeChatPayService {PayResponse create(RuralUserWaterPayment ruralUserWaterPayment);PayResponse notify(String notifyData);
}

5.具体impl代码

package com.zkrt.smartsite.project.rural.service.impl;import com.alibaba.fastjson.JSON;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.utils.JsonUtil;
import com.zkrt.smartsite.common.utils.MathUtil;
import com.zkrt.smartsite.project.rural.domain.RuralUserWaterPayment;
import com.zkrt.smartsite.project.rural.service.IRuralUserWaterPaymentService;
import com.zkrt.smartsite.project.rural.service.WeChatPayService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** function** @author jhh* @date 2020/11/10 20:32**/
@Slf4j
@Service
public class WeChatPayServiceImpl implements WeChatPayService {@Autowiredprivate BestPayService bestPayService;@Autowiredprivate IRuralUserWaterPaymentService ruralUserWaterPaymentService;private Logger logger = LoggerFactory.getLogger(WeChatPayServiceImpl.class);@Override// 第一步:发起支付(是在订单已经被创建好的前提下)public PayResponse create(RuralUserWaterPayment ruralUserWaterPayment){PayRequest payRequest = new PayRequest();payRequest.setOpenid(ruralUserWaterPayment.getUserOpenid());payRequest.setOrderAmount(ruralUserWaterPayment.getCost().doubleValue());payRequest.setOrderName("微信平台饮用水缴费订单");payRequest.setOrderId(ruralUserWaterPayment.getOrderId());payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);logger.info("【微信支付】发起支付,request={}", JsonUtil.toJson(payRequest));PayResponse payResponse = bestPayService.pay(payRequest);//根据传参得到预付支付的参数logger.info("【微信支付】发起支付生成预付信息,response={}", JsonUtil.toJson(payResponse));return payResponse;}//第二部:异步判断支付状态//1. 验证签名//2. 支付的状态//3. 支付金额//4. 支付人(下单人 == 支付人)//前两步sdk已做@Overridepublic PayResponse notify(String notifyData){PayResponse payResponse = bestPayService.asyncNotify(notifyData);logger.warn("【微信支付】异步通知,payResponse={}", JSON.toJSON(payResponse));//查询订单是否存在RuralUserWaterPayment payment = new RuralUserWaterPayment();payment.setOrderId(payResponse.getOrderId());RuralUserWaterPayment order = ruralUserWaterPaymentService.selectRuralUserWaterPaymentList(payment).get(0);if(order.equals(null)){logger.error("【微信支付】异步通知,订单不存在,orderId={}", payResponse.getOrderId());throw new SecurityException("ORDER_NOT_EXIST");}if(!MathUtil.equals(order.getCost().doubleValue(),(payResponse.getOrderAmount()))){logger.error("【微信支付】异步通知,订单金额不一致,orderId={},微信通知金额={},系统金额={}",payResponse.getOrderId(),payResponse.getOrderAmount(),order.getCost());throw new SecurityException("WXPAY_NOTIFY_MONEY_VERIFY_ERROR");}paid(order);log.info("XXXXXX记得修改订单状态啊啊啊啊啊啊啊啊");return payResponse;}public RuralUserWaterPayment paid(RuralUserWaterPayment payment){if(!payment.getFlag().equals("0")){logger.error("【订单完成,支付状态不正确】payment={}",payment.getFlag());
//            logger.info("【订单完成,支付状态不正确】payment={}",payment);throw new SecurityException("ORDER_STATUS_ERROR");}payment.setFlag("1");payment.setCostWay("微信支付");RuralUserWaterPayment payment1 = new RuralUserWaterPayment();BeanUtils.copyProperties(payment,payment1);Integer finishNum = ruralUserWaterPaymentService.updateRuralUserWaterPayment(payment1);RuralUserWaterPayment finishPayment = ruralUserWaterPaymentService.selectRuralUserWaterPaymentList(payment1).get(0);if(finishNum.equals(0)){logger.info("【微信订单支付状态更新失败】");throw new SecurityException("ORDER_UPDATE_FAIL");}return finishPayment;}
}

3.需要设置模板

(1)create.ftl 注:需要修改location.href的路径

<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" ) {location.href = "http://zhihecity.cn/wechat/index.html#/waterPay?&returnUrl=${returnUrl}&openid=${openid}";}     // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回    ok,但并不保证它绝对可靠。else {location.href = "http://zhihecity.cn/wechat/index.html#/waterPay?returnUrl=${returnUrl}&openid=${openid}";}<#--location.href = "${returnUrl}";-->});}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>

(2)success.ftl

<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg>
</xml>

农村饮用水项目微信支付完整代码相关推荐

  1. 小程序云开发实现微信支付完整代码

    效果图: 资质 需要是已经开通了微信支付,且已绑定了商户号的小程序. 开通 在云控制台 -> 设置 -> 全局设置中开通. 二, 创建支付的云函数 1,创建云函数pay 三,引入三方依赖t ...

  2. 杉德支付php代码实现_php实现微信支付的代码

    这篇文章主要介绍了关于php实现微信支付的代码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 1: 获取code;window.location.href="https:// ...

  3. asp php微信支付,Asp微信支付接口代码 微信中生成订单后可以直接调出微信钱包直接付款_随便下源码网...

    Asp微信支付接口代码 微信中生成订单后,可以直接调出微信钱包直接付款 软件介绍: 众所周到,目前微信支付已经十分普及,无论是商场.超市.网站上,微信支付的发展十分迅速,而ASP版微信支付在微信公众平 ...

  4. springboot整合支付宝微信支付案例+代码

    springboot支付宝微信支付 每天多学一点点~ 之前写好了一篇,不下心被覆盖的,我的锅....下次写博客记得导出来!!!! 话不多说,这就开始吧- 文章目录 springboot支付宝微信支付 ...

  5. 基于Python flask 框架的微信支付 全代码

    前台代码忽略 需要了解flask 框架 尤其是 模板传参 和重定向 传参 ###############################################<<各种需要用到的函 ...

  6. 集成微信支付的代码。兼容小程序,扫码,app,公众号。h5 支付 ,以及 服务商提现...

      /*** 因为微信总是很多个商户号很多和appid.很多个密钥文件,所以全部改成手动传值的方式,就可以支持多商户调用** @param appId 商户的appid* @param mchId 商 ...

  7. 小程序微信支付完整demo源码,包含退款

    最近刚完成一个商场小程序, 使用到了微信支付功能,其中遇到了很多的抗,所以,我把支付这块摘出来,以免大家少走弯路. demo小程序端很简单,就是一个页面: js代码: //支付pay: functio ...

  8. 【愚公系列】2022年10月 微信小程序-电商项目-微信支付后端功能实现(node版)

    文章目录 前言 一.微信支付后端功能实现(node版) 1.相关文档 2.项目配置 前言 微信支付是腾讯集团旗下的第三方支付平台,致力于为用户和企业提供安全.便捷.专业的在线支付服务.以"微 ...

  9. Java对接微信支付(完整全流程)

    Java对接微信支付及支付回调通知的全流程 一.所用框架.对接微信支付我们技术组用的是payment框架,因为该框架已整合springboot因此很方便快捷 <dependency>< ...

最新文章

  1. Lombok 的爱恨情仇
  2. 用linux下常用命令wget进行整站下载(递归下载至本地)
  3. JDBC-day01
  4. 在Ubuntu下增加root用户
  5. 【Spring Boot 分享】开源项目【8个】
  6. ssh登陆connection refused的解决办法
  7. appcan 微信支付
  8. linux-Centos7安装nginx
  9. 利用权限禁止QQ的自动升级(QQUpdateCenter)
  10. 关于博客园开放API的授权问题解决
  11. 远程批量升级IE11
  12. Makefile代码解释
  13. ae合成设置快捷键_(精品)AE从小白到大神之路(一)-AE入门
  14. 智能家居--domoticz配置和风天气 HTTP/HTTPS poller 的使用以及domoticz_updateDevice的介绍
  15. S-Paper电子纸在生产车间中的应用
  16. win7计算机开始里没有设置,win7电脑开始菜单的设置方法
  17. 获取微信用户openid
  18. 根据GPS经纬度判断当前所属的市区
  19. LearnGL - 06.1 - Matrix - 矩阵02 - 向量空间、向量空间的维度、为何矩阵乘法要有 [M x N] * [N * P] 的 N 要相等的限制
  20. 人脸识别 Face Recognition 入门

热门文章

  1. P1740 Ink on paper
  2. 一键构建云上高可用蛋白质结构预测平台
  3. H5原生js简单拼图游戏
  4. scala(三):流程控制-分支控制、嵌套循环、for、while、do…while循环、循环中断、多支循环
  5. 解决LaTeX中的\pdfendlink ended up in different nesting level than \pdfstartlink.问题
  6. PAT A1143 Lowest Common Ancestor ——沉舟侧畔千帆过,病树前头万木春
  7. 道路驾驶技能计算机评判项目,2017最新科目二和科目三考试评判标准变动情况...
  8. 一个老程序员的教诲(2)
  9. 【EXCEL分列小技巧:按特殊符号分列】
  10. Nginx、HLS、M3U8、TS 搭建手记