【支付】springboot实现微信支付

以下是使用 Spring Boot 对接微信支付,实现支付和退款功能的详细步骤和代码示例:

  1. 添加依赖
    在 pom.xml 中添加必要的依赖,包含 Spring Boot Web 和微信支付 SDK:
<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 微信支付 SDK -->
    <dependency>
        <groupId>com.github.wechatpay-apiv3</groupId>
        <artifactId>wechatpay-java</artifactId>
        <version>0.4.12</version>
    </dependency>
</dependencies>
  1. 配置微信支付信息
    在 application.yml 中配置微信支付所需的信息:
wechat:
  pay:
    appid: your_appid
    mchid: your_mch_id
    api-v3-key: your_api_v3_key
    private-key-path: classpath:apiclient_key.pem
    certificate-path: classpath:apiclient_cert.pem
    notify-url: your_notify_url
  1. 配置微信支付客户端
    创建配置类初始化微信支付客户端:
import com.github.wechatpay-apiv3.WechatPayBuilder;
import com.github.wechatpay-apiv3.WechatPayService;
import com.github.wechatpayapiv3.auth.PrivateKeySigner;
import com.github.wechatpayapiv3.auth.Signer;
import com.github.wechatpayapiv3.model.AesGcmEncryptor;
import com.github.wechatpayapiv3.model.CertificateProvider;
import com.github.wechatpayapiv3.model.InMemoryCertificateProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Base64;

@Configuration
public class WechatPayConfig {

    @Value("${wechat.pay.appid}")
    private String appid;

    @Value("${wechat.pay.mchid}")
    private String mchid;

    @Value("${wechat.pay.api-v3-key}")
    private String apiV3Key;

    @Value("${wechat.pay.private-key-path}")
    private String privateKeyPath;

    @Value("${wechat.pay.certificate-path}")
    private String certificatePath;

    @Bean
    public WechatPayService wechatPayService() throws IOException {
        PrivateKey privateKey = loadPrivateKey(privateKeyPath);
        X509Certificate certificate = loadCertificate(certificatePath);
        Signer signer = new PrivateKeySigner(mchid, privateKey);
        CertificateProvider certificateProvider = new InMemoryCertificateProvider(certificate);
        AesGcmEncryptor encryptor = new AesGcmEncryptor(apiV3Key.getBytes(StandardCharsets.UTF_8));

        return WechatPayBuilder.builder()
               .withSigner(signer)
               .withCertificateProvider(certificateProvider)
               .withEncryptor(encryptor)
               .build();
    }

    private PrivateKey loadPrivateKey(String path) throws IOException {
        // 实现加载私钥的逻辑
        return null;
    }

    private X509Certificate loadCertificate(String path) throws IOException {
        // 实现加载证书的逻辑
        return null;
    }
}
  1. 实现支付功能
    创建服务类处理支付业务逻辑:
import com.github.wechatpay-apiv3.WechatPayService;
import com.github.wechatpay-apiv3.model.PrepayRequest;
import com.github.wechatpay-apiv3.model.PrepayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;

@Service
public class WechatPayService {

    @Autowired
    private WechatPayService wechatPayService;

    @Value("${wechat.pay.appid}")
    private String appid;

    @Value("${wechat.pay.notify-url}")
    private String notifyUrl;

    public PrepayResponse prepay(String outTradeNo, int totalFee, String description) throws IOException {
        PrepayRequest request = new PrepayRequest();
        request.setAppid(appid);
        request.setMchid(mchid);
        request.setOutTradeNo(outTradeNo);
        request.setDescription(description);
        request.setNotifyUrl(notifyUrl);
        request.setAmount(totalFee);

        return wechatPayService.prepay(request);
    }
}
  1. 实现退款功能
    在服务类中添加退款方法:
import com.github.wechatpay-apiv3.WechatPayService;
import com.github.wechatpay-apiv3.model.RefundRequest;
import com.github.wechatpay-apiv3.model.RefundResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;

@Service
public class WechatPayService {

    @Autowired
    private WechatPayService wechatPayService;

    @Value("${wechat.pay.appid}")
    private String appid;

    @Value("${wechat.pay.notify-url}")
    private String notifyUrl;

    public RefundResponse refund(String outTradeNo, String outRefundNo, int refundFee, int totalFee) throws IOException {
        RefundRequest request = new RefundRequest();
        request.setAppid(appid);
        request.setMchid(mchid);
        request.setOutTradeNo(outTradeNo);
        request.setOutRefundNo(outRefundNo);
        request.setRefundFee(refundFee);
        request.setTotalFee(totalFee);
        request.setNotifyUrl(notifyUrl);

        return wechatPayService.refund(request);
    }
}
  1. 创建控制器
    创建控制器处理支付和退款请求:
import com.github.wechatpay-apiv3.model.PrepayResponse;
import com.github.wechatpay-apiv3.model.RefundResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RestController
@RequestMapping("/wechat/pay")
public class WechatPayController {

    @Autowired
    private WechatPayService wechatPayService;

    @PostMapping("/prepay")
    public PrepayResponse prepay(@RequestParam String outTradeNo, @RequestParam int totalFee, @RequestParam String description) throws IOException {
        return wechatPayService.prepay(outTradeNo, totalFee, description);
    }

    @PostMapping("/refund")
    public RefundResponse refund(@RequestParam String outTradeNo, @RequestParam String outRefundNo, @RequestParam int refundFee, @RequestParam int totalFee) throws IOException {
        return wechatPayService.refund(outTradeNo, outRefundNo, refundFee, totalFee);
    }
}

代码解释
依赖添加:引入 Spring Boot Web 和微信支付 SDK 以支持 Web 开发和微信支付功能。
配置信息:在 application.yml 中配置微信支付所需的关键信息,如应用 ID、商户号、API V3 密钥等。
微信支付客户端配置:创建 WechatPayConfig 类初始化微信支付客户端,包含私钥和证书的加载。
支付服务类:WechatPayService 类包含支付和退款的业务逻辑,调用微信支付客户端的方法完成相应操作。
控制器:WechatPayController 处理前端的支付和退款请求,调用服务类的方法完成支付和退款操作。
注意事项
要保证私钥和证书文件的正确性与安全性。
处理支付和退款回调时,要对签名进行验证,防止数据被篡改。
严格按照微信支付的开发文档进行开发,及时处理可能出现的异常情况。

你可能感兴趣的:(spring,boot,微信,后端)