springboot使用com.github.binarywang 包实现微信网页上的支付和退款

本文主要是介绍springboot使用com.github.binarywang 包实现微信网页上的支付和退款,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前提

微信小程序中实现微信支付是从小程序中调去微信支付的界面直接进行支付,那么在pc端需要实现微信的支付呢,是需要出现一个二维码让用户使用扫码支付的。
注意:
需要实现pc端的微信支付,需要在微信商户平台开通native支付,并且下载并配置商户证书
在这里插入图片描述
在这里插入图片描述
设置好这些之后,直接看看在springboot 或者springclound 中如何实现。

maven依赖

 		<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>4.1.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

步骤

在yml中定义微信的商户号,密码等等

wxpay:appId: appIdmchId: mchIdmchKey: mchKeykeyPath: /home/wxpay_cert/apiclient_cert.p12privateKeyPath: /home/wxpay_cert/apiclient_key.pemprivateCertPath: /home/wxpay_cert/apiclient_cert.pemnotifyUrl: notifyUrlrefundNotifyUrl: refundNotifyUrlserialNo: serialNo

notifuyUrl 是 支付回调地址
refundNotifyUrl是 退款回调地址
这两个地址都必须是外网可访问的地址

新建WechatPayConfig类来读取yml文件的中信息

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;@Data
@ConfigurationProperties(prefix = "wxpay")
public class WechatPayConfig {private String appId;private String mchId;private String mchKey;private String keyPath;private String privateKeyPath;private String privateCertPath;private String notifyUrl;private String redUrl;private String refundNotifyUrl;private String serialNo;
}

获取ip地址帮助类

IpUtils

public class IpUtils
{public static String getIpAddr(){return getIpAddr(ServletUtils.getRequest());}}

ServletUtils (客户端工具类)

   public static HttpServletRequest getRequest(){try{return getRequestAttributes().getRequest();}catch (Exception e){return null;}}public static ServletRequestAttributes getRequestAttributes(){try{RequestAttributes attributes = RequestContextHolder.getRequestAttributes();return (ServletRequestAttributes) attributes;}catch (Exception e){return null;}}

微信支付Service类

@Service
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WechatPayConfig.class)
@AllArgsConstructor
public class WechatPayService {static Logger logger = LoggerFactory.getLogger(WechatPayService.class);private WechatPayConfig wechatPayConfig;public WxPayService wxPayService() {WxPayConfig payConfig = new WxPayConfig();payConfig.setAppId(wechatPayConfig.getAppId());payConfig.setMchId(wechatPayConfig.getMchId());payConfig.setMchKey(wechatPayConfig.getMchKey());payConfig.setApiV3Key(wechatPayConfig.getMchKey());payConfig.setKeyPath(wechatPayConfig.getKeyPath());payConfig.setUseSandboxEnv(false);logger.info("wechatPayConfig.getPrivateKeyPath():{}",wechatPayConfig.getPrivateKeyPath());WxPayService wxPayService = new WxPayServiceImpl();wxPayService.setConfig(payConfig); //微信配置信息return wxPayService;}//生成支付二维码//BookingInfo 是你订单表public String generatePayQrCode(BookingInfo booking, String ip) {try {WxPayService wxPayService = wxPayService();WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();orderRequest.setOutTradeNo(booking.getDisplayNo()); //订单号orderRequest.setTotalFee(BigDecimal.valueOf(booking.getDisplayPrice()).multiply(new BigDecimal(100)).intValue()); //金额,转换成分 ,至少支付1分钱orderRequest.setSpbillCreateIp(ip);orderRequest.setNotifyUrl(wechatPayConfig.getRedUrl());orderRequest.setBody("");orderRequest.setAttach("");orderRequest.setTradeType("NATIVE");//交易类型orderRequest.setProductId(booking.getId().toString());WxPayNativeOrderResult wxPayNativeOrderResult = (WxPayNativeOrderResult) wxPayService.createOrder(orderRequest);return wxPayNativeOrderResult.getCodeUrl();} catch (Exception e) {// 处理异常e.printStackTrace();return e.getMessage();}}//支付回调函数public String payNotify(String xmlData) {try {WxPayService wxPayService = wxPayService();WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);String orderId = notifyResult.getOutTradeNo();//拿到订单号获取订单logger.info("wechatPayConfig.rePayNotify():{}", xmlData);//处理的业务//因为会重复调用,如果你这里是钱包这种,那么一定要判断是否支付过了return WxPayNotifyResponse.success("成功_" + orderInfo.getId());} catch (WxPayException e) {e.printStackTrace();return WxPayNotifyResponse.fail(e.getMessage());}}//退款订单请求//RefundInfo 退款的订单,这里单笔订单,如果是多笔,你可以构建list 要循环执行//RefundInfo 中需要包括 支付订单的订单号、支付金额、退款的订单号、退款金额public String redRefundPay(RefundInfo refund){try {if (refund == null) {return "订单获取失败";}WxPayService wxPayService = wxPayService();WxPayRefundRequest orderRequest = new WxPayRefundRequest();orderRequest.setOutTradeNo(refund.getDisplayNo());orderRequest.setNotifyUrl(wechatPayConfig.getRefundNotifyUrl());orderRequest.setTotalFee(BigDecimal.valueOf(refund.getDisplaySum()).multiply(new BigDecimal(100)).intValue());orderRequest.setRefundFee(BigDecimal.valueOf(refund.getDisplayPrice()).multiply(new BigDecimal(100)).intValue());orderRequest.setOutRefundNo(memberRedRefund.getDisplayNo());WxPayRefundResult wxPayRefundResult = wxPayService.refund(orderRequest);if ("SUCCESS".equals(wxPayRefundResult.getReturnCode())&& "SUCCESS".equals(wxPayRefundResult.getResultCode())) {/*** 系统内部业务逻辑*/logger.info("wechatPayConfig.redRefundNotify():{}", refund.toString());return "退款中";}return "退款失败";} catch (Exception e) {// 处理异常e.printStackTrace();return e.getMessage();}}//退款回调函数public String redRefundNotify(String xmlData) {try {WxPayService wxPayService = wxPayService();WxPayRefundNotifyResult notifyResult = wxPayService.parseRefundNotifyResult(xmlData);String orderId = notifyResult.getReqInfo().getOutRefundNo();//拿到订单号获取订单logger.info("wechatPayConfig.redRefundNotify():{}", xmlData);//编写自己的业务处理逻辑return WxPayNotifyResponse.success("成功_" + orderInfo.getId());} catch (WxPayException e) {e.printStackTrace();return WxPayNotifyResponse.fail(e.getMessage());}}}

这篇关于springboot使用com.github.binarywang 包实现微信网页上的支付和退款的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/855059

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买