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: appId
  mchId: mchId
  mchKey: mchKey
  keyPath: /home/wxpay_cert/apiclient_cert.p12
  privateKeyPath: /home/wxpay_cert/apiclient_key.pem
  privateCertPath: /home/wxpay_cert/apiclient_cert.pem

  notifyUrl: notifyUrl
  refundNotifyUrl: refundNotifyUrl
  serialNo: 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());
        }


    }

	

}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Spring Boot项目中导入com.github.pagehelper.PageHelper,你需要按照以下步骤进行操作: 1. 在你的项目的pom.xml文件中,添加以下依赖项: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 2. 在你的Spring Boot应用程序的配置文件(通常是application.properties或application.yml)中,配置PageHelper的属性。例如,你可以添加以下属性: ```properties # 开启PageHelper支持 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql ``` 这些属性可以根据你的具体需求进行配置。 3. 在你的Mapper接口中使用PageHelper来实现分页查询。例如: ```java import com.github.pagehelper.PageHelper; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface YourMapper { // 分页查询方法示例 List<YourEntity> selectByPage(int pageNum, int pageSize); } ``` 4. 在你的Service或Controller层调用Mapper接口中的分页查询方法,并传入页码和每页大小参数。例如: ```java import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class YourService { @Autowired private YourMapper yourMapper; public PageInfo<YourEntity> getEntitiesByPage(int pageNum, int pageSize) { // 使用PageHelper进行分页查询 PageHelper.startPage(pageNum, pageSize); List<YourEntity> entities = yourMapper.selectByPage(pageNum, pageSize); return new PageInfo<>(entities); } } ``` 这样,你就成功地在Spring Boot项目中导入了com.github.pagehelper.PageHelper,并可以使用它进行分页查询了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

layman0528

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值