SpringBoot集成微信支付(二维码支付)

一、流程

  1. 需要在官方申请接入微信支付 微信支付商户申请地址(申请需要营业执照,个人无法申请)
  2. 申请完成后需要等待 1-3 天审核,审核通过后需要在 微信商户平台 获取 appID(公众号ID),mchID (商户ID)、key (在微信支付中设置的私钥)、安全证书

二、在springboot整合微信支付

1. 导入所需的依赖

<!-- 生成二维码工具 -->
<dependency>
     <groupId>com.google.zxing</groupId>
     <artifactId>core</artifactId>
     <version>3.2.1</version>
 </dependency>
 <dependency>
     <groupId>com.google.zxing</groupId>
     <artifactId>javase</artifactId>
     <version>3.2.0</version>
 </dependency>
 <!-- 微信支付所需sdk -->
 <dependency>
    <groupId>com.github.wxpay</groupId>
    <artifactId>wxpay-sdk</artifactId>
    <version>0.0.3</version>
</dependency>

2. yml文件配置相关信息

# 服务器域名地址
server:
  service-domain: 域名地址信息
#微信app支付
pay:
  wxpay:
    app:
      appID: 公众号id
      mchID: 商户id
      key: 设置的私钥
      certPath: static/cert/wxpay/apiclient_cert.p12 # 从微信商户平台下载的安全证书存放的路径、我放在resources下面,切记一定要看看target目录下的class文件下有没有打包apiclient_cert.p12文件
      payNotifyUrl: ${
   server.service-domain}/wxPay/callback # 微信支付成功的异步通知接口

3. 读取配置文件信息

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.InputStream;

@Component
@ConfigurationProperties(prefix = "pay.wxpay.app")
public class MyWXPayConfig implements WXPayConfig {
   
    /**
     * appID
     */
    private String appID;

    /**
     * 商户号
     */
    private String mchID;

    /**
     * API 密钥
     */
    private String key;

    /**
     * API证书绝对路径 (本项目放在了 resources/cert/wxpay/apiclient_cert.p12")
     */
    private String certPath;

    /**
     * HTTP(S) 连接超时时间,单位毫秒
     */
    private int httpConnectTimeoutMs = 8000;

    /**
     * HTTP(S) 读数据超时时间,单位毫秒
     */
    private int httpReadTimeoutMs = 10000;

    /**
     * 微信支付异步通知地址
     */
    private String payNotifyUrl;

    /**
     * 微信退款异步通知地址
     */
    private String refundNotifyUrl;

    /**
     * 统一下单url
     */
    private static final String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    /**
     * 获取商户证书内容(这里证书需要到微信商户平台进行下载)
     *
     * @return 商户证书内容
     */
    @Override
    public InputStream getCertStream() {
   
        InputStream certStream  =getClass().getClassLoader().getResourceAsStream(certPath);
        return certStream;
    }
	// ... 省略 getter 和 setter 方法

4. 微信支付用到的 utils 类

解析 xml 的工具类

import org.w3c.dom.Document;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public final class WXPayXmlUtil {
   
    public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
   
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        documentBuilderFactory.setXIncludeAware(false);
        documentBuilderFactory.setExpandEntityReferences(false);

        return documentBuilderFactory.newDocumentBuilder();
    }

    public static Document newDocument() throws ParserConfigurationException {
   
        return newDocumentBuilder().newDocument();
    }
}
import com.github.wxpay.sdk.WXPayUtil;
import com.qjp.system.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
Spring Boot集成微信支付,你可以按照以下步骤进行操作: 1. 首先,你需要在微信商户平台注册账号并开通支付功能。获取到微信支付的商户号(mchId)、API密钥(apiKey)和应用ID(appId)等关键信息。 2. 在你的Spring Boot项目中添加相关依赖。你可以在项目的pom.xml文件中添加以下依赖信息: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>3.0.10</version> </dependency> ``` 3. 创建一个配置类,配置微信支付相关参数。在该配置类中,你需要使用上面获取到的商户号、API密钥等信息进行配置: ```java @Configuration public class WxPayConfig { @Value("${wxpay.appId}") private String appId; @Value("${wxpay.mchId}") private String mchId; @Value("${wxpay.apiKey}") private String apiKey; // 创建WxPayService Bean,并配置相关参数 @Bean public WxPayService wxPayService() { WxPayConfig wxPayConfig = new WxPayConfig(); wxPayConfig.setAppId(appId); wxPayConfig.setMchId(mchId); wxPayConfig.setMchKey(apiKey); wxPayConfig.setNotifyUrl("你的异步通知地址"); return new WxPayServiceImpl(wxPayConfig); } } ``` 4. 创建一个Controller处理支付请求。在该Controller中,你可以使用WxPayService来进行支付相关操作,例如生成支付订单、发起支付等。 ```java @RestController @RequestMapping("/pay") public class WxPayController { @Autowired private WxPayService wxPayService; @PostMapping("/createOrder") public String createOrder() { // 生成支付订单 WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); // 设置订单参数 // ... WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(request); // 处理支付结果,返回给前端 // ... return "success"; } } ``` 这只是一个简单的示例,你可以根据实际需求进行更详细的配置和处理。同时,你还需要根据自己的业务逻辑来处理支付结果的异步通知和订单查询等操作。 希望以上信息对你有所帮助!如果你还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值