Java之微信 APIv3 商家转账到零钱SDK版

1、测试类 TransferTest :

import com.alibaba.fastjson.JSON;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TransferTest {
    @Test
    public void transfer() {
        String outBatchNo = generateOrderSn();
        String batchName = "商家转账到零钱";
        String batchRemark = "商家转账到零钱";
        Long totalAmount = 10L;
        Integer totalNum = 1;
        String openId = "123456";
        RSAConfig config =
                new RSAConfig.Builder()
                        .merchantId("1234567899")
                        .privateKeyFromPath("src/main/resources/apiclient_key.pem")
                        .merchantSerialNumber("E10ADC3949BA59ABBE56E057F20F883E12345678")
                        .wechatPayCertificatesFromPath("src/main/resources/wechatpay_E10ADC3949BA59ABBE56E057F20F883E98765432.pem")
                        .build();
        try {
            TransferBatchService transferBatchService = new TransferBatchService.Builder().config(config).build();
            String appId = "wx123456789";
            InitiateBatchTransferRequest initiateBatchTransferRequest =
                    new InitiateBatchTransferRequest();
            initiateBatchTransferRequest.setAppid(appId);
            initiateBatchTransferRequest.setOutBatchNo(outBatchNo);
            initiateBatchTransferRequest.setBatchName(batchName);
            initiateBatchTransferRequest.setBatchRemark(batchRemark);
            initiateBatchTransferRequest.setTotalAmount(totalAmount);
            initiateBatchTransferRequest.setTotalNum(totalNum);
            {
                List<TransferDetailInput> transferDetailListList = new ArrayList<>();
                {
                    TransferDetailInput transferDetailInput = new TransferDetailInput();
                    transferDetailInput.setOutDetailNo(outBatchNo);
                    transferDetailInput.setTransferAmount(totalAmount);
                    transferDetailInput.setTransferRemark(batchRemark);
                    transferDetailInput.setOpenid(openId);
                    transferDetailListList.add(transferDetailInput);
                }
                initiateBatchTransferRequest.setTransferDetailList(
                        transferDetailListList);
            }
            InitiateBatchTransferResponse response =
                    transferBatchService.initiateBatchTransfer(initiateBatchTransferRequest);
            log.info("转账返回信息:{}", JSON.toJSONString(response));
        } catch (HttpException e) { // 发送HTTP请求失败
            // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
            log.error(e.getMessage());
            throw new RuntimeException("微信转账失败");
        } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
            // 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
            log.error(e.getMessage());
            throw new RuntimeException("微信转账失败");
        } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
            // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
            log.error(e.getMessage());
            throw new RuntimeException("微信转账失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("微信转账失败");
        }
    }

    @Test
    public void transferQuery() {
        TransferQueryVO transferQueryVO = new TransferQueryVO();
        //商家转账批次单号
        String outBatchNo = "1122334455667788";
        //商家转账明细单号
        String outDetailNo = "1122334455667788";
        RSAConfig config =
                new RSAConfig.Builder()
                        .merchantId("1234567899")
                        .privateKeyFromPath("src/main/resources/apiclient_key.pem")
                        .merchantSerialNumber("E10ADC3949BA59ABBE56E057F20F883E12345678")
                        .wechatPayCertificatesFromPath("src/main/resources/wechatpay_E10ADC3949BA59ABBE56E057F20F883E98765432.pem")
                        .build();
        try {
            TransferBatchService transferBatchService = new TransferBatchService.Builder().config(config).build();
            GetTransferDetailByOutNoRequest request = new GetTransferDetailByOutNoRequest();
            request.setOutBatchNo(outBatchNo);
            request.setOutDetailNo(outDetailNo);
            TransferDetailEntity response = transferBatchService.getTransferDetailByOutNo(request);
            log.info("转账返回信息:{}", JSON.toJSONString(response));
            transferQueryVO.setResCode(WxTransferEnum.getCodeByName(response.getDetailStatus()));
            transferQueryVO.setResCodeDes(WxTransferEnum.getMessageByName(response.getDetailStatus()));
            if ("FAIL".equals(response.getDetailStatus())) {
                transferQueryVO.setResCodeDes(WxTransferFailEnum.getReasonByReason(response.getFailReason()));
            }
            log.info("转账返回信息II:{}", JSON.toJSONString(transferQueryVO));
        } catch (HttpException e) { // 发送HTTP请求失败
            // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
            log.error(e.getMessage());
            throw new RuntimeException("微信转账失败");
        } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
            // 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
            log.error(e.getMessage());
            throw new RuntimeException("微信转账失败");
        } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
            // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
            log.error(e.getMessage());
            throw new RuntimeException("微信转账失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("微信转账失败");
        }
    }

    private String generateOrderSn() {
        String time = cn.hutool.core.date.DateUtil.format(new Date(), "yyyyMMddHHmmssSSS").substring(2);
        int randomInt = ThreadLocalRandom.current().nextInt(1000, 10000);
        return time + randomInt;
    }
}

2、引入SDKjar包 pom.xml:

        <dependency>
            <groupId>com.github.wechatpay-apiv3</groupId>
            <artifactId>wechatpay-java</artifactId>
            <version>0.2.9</version>
        </dependency>

 3、文件过滤 pom.xml :

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>pem</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>
    </build>

4、文件拷贝 apiclient_key.pem :

将 apiclient_key.pem、拷贝至 resources 目录下。

5、生成微信 APIv3 平台证书并拷贝至 resources 目录下:
wechatpay_E10ADC3949BA59ABBE56E057F20F883E98765432.pem

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值