Back to Repositories

Testing WeChat Native Payment Integration in RuoYi-Vue-Pro

This test suite implements integration testing for WeChat (WeiXin) Native Pay functionality in the RuoYi Vue Pro framework. It validates payment and refund operations using WxPay V3 API integration, demonstrating real-world payment processing scenarios.

Test Coverage Overview

The test suite covers critical payment processing functionalities for WeChat Native Pay integration:

  • Payment creation using WxPay V3 API
  • Refund processing with complete transaction lifecycle
  • Configuration management for WxPay services
  • Request/response handling for payment operations

Implementation Analysis

The testing approach utilizes JUnit for integration testing with WxPay’s official Java SDK. It implements direct API calls to WeChat’s payment gateway, validating both payment creation and refund workflows with actual API credentials and configurations.

The implementation follows a structured pattern with separate test methods for payment and refund operations, incorporating proper exception handling and response validation.

Technical Details

Key technical components include:

  • WxPayServiceImpl for payment service implementation
  • WxPayConfig for API credentials and certificates
  • JSON request/response handling
  • Duration-based payment expiration
  • V3 API specific formatting and structures

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper separation of configuration and test logic
  • Comprehensive request object construction
  • Clear output logging for debugging
  • Realistic test data usage
  • Proper exception handling and test isolation

yunaiv/ruoyi-vue-pro

yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/WxNativePayClientIntegrationTest.java

            
package cn.iocoder.yudao.framework.pay.core.client.impl.weixin;

import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.time.Duration;

import static cn.iocoder.yudao.framework.pay.core.client.impl.weixin.AbstractWxPayClient.formatDateV3;

/**
 * {@link WxNativePayClient} 的集成测试,用于快速调试微信扫码支付
 *
 * @author 芋道源码
 */
@Disabled
public class WxNativePayClientIntegrationTest {

    @Test
    public void testPayV3() throws WxPayException {
        // 创建 config 配置
        WxPayConfig config = buildWxPayConfigV3();
        // 创建 WxPayService 客户端
        WxPayService client = new WxPayServiceImpl();
        client.setConfig(config);

        // 执行发起支付
        WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request()
                .setOutTradeNo(String.valueOf(System.currentTimeMillis()))
                .setDescription("测试支付-body")
                .setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(1)) // 单位分
                .setTimeExpire(formatDateV3(LocalDateTimeUtils.addTime(Duration.ofMinutes(2))))
                .setSceneInfo(new WxPayUnifiedOrderV3Request.SceneInfo().setPayerClientIp("127.0.0.1"))
                .setNotifyUrl("http://127.0.0.1:48080");
        System.out.println("========= request ==========");
        System.out.println(JsonUtils.toJsonPrettyString(request));
        String response = client.createOrderV3(TradeTypeEnum.NATIVE, request);
        System.out.println("========= response ==========");
        System.out.println(JsonUtils.toJsonPrettyString(response));
    }

    @Test
    public void testRefundV3() throws WxPayException {
        // 创建 config 配置
        WxPayConfig config = buildWxPayConfigV3();
        // 创建 WxPayService 客户端
        WxPayService client = new WxPayServiceImpl();
        client.setConfig(config);

        // 执行发起退款
        WxPayRefundV3Request request = new WxPayRefundV3Request()
                .setOutTradeNo("1689545729695")
                .setOutRefundNo(String.valueOf(System.currentTimeMillis()))
                .setAmount(new WxPayRefundV3Request.Amount().setTotal(1).setRefund(1).setCurrency("CNY"))
                .setReason("就是想退了");
        System.out.println("========= request ==========");
        System.out.println(JsonUtils.toJsonPrettyString(request));
        WxPayRefundV3Result response = client.refundV3(request);
        System.out.println("========= response ==========");
        System.out.println(JsonUtils.toJsonPrettyString(response));
    }

    private WxPayConfig buildWxPayConfigV3() {
        WxPayConfig config = new WxPayConfig();
        config.setAppId("wx62056c0d5e8db250");
        config.setMchId("1545083881");
        config.setApiV3Key("459arNsYHl1mgkiO6H9ZH5KkhFXSxaA4");
//        config.setCertSerialNo(serialNo);
        config.setPrivateCertPath("/Users/yunai/Downloads/wx_pay/apiclient_cert.pem");
        config.setPrivateKeyPath("/Users/yunai/Downloads/wx_pay/apiclient_key.pem");
        return config;
    }

}