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

相关文章

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行