最近在做共享洗车小程序中有些店铺是采用按用户充值比例分账的模式和店主合作,故此记录下分账模式的使用。
建议先在官网看一遍大概流程
微信支付-分账-官网链接
准备工作
- 登录微信支付商户,设置好V3密匙
- 分账需要证书文件apiclient_cert.pem,apiclient_key.pem
1. maven引入
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-pay-spring-boot-starter</artifactId>
<version>4.4.0</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
如果项目中有其他jar包引入guava,导致guava版本不一致的情况下会报错,所以需要排除(根据自己项目看是否需要排除)
2. 直接贴测试用例
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WxpayTest {
@Autowired
private WxPayService wxPayService;
@Autowired
//获取微信支付配置信息
private WechatService wechatService;
@Test
public void test1() throws WxPayException {
Long storeId = 1L;
//查询出门店对应的微信支付配置信息
WechatEntity wechatEntity = wechatService.getOne(new LambdaQueryWrapper<WechatEntity>().eq(WechatEntity::getStoreId, storeId));
//WxPayUtils是自己写的工具类,作用就是把查询出来的配置信息转换为WxPayConfig
WxPayConfig wxPayConfig = WxPayUtils.convertToPayConfig(wechatEntity);
wxPayConfig.setPrivateKeyPath("E:\\apiclient_key.pem");
wxPayConfig.setPrivateCertPath("E:\\apiclient_cert.pem");
wxPayService.addConfig(wxPayConfig.getMchId(), wxPayConfig);
WxPayService myWxPayService = wxPayService.switchoverTo(wechatEntity.getPayMchid());
//一定使用V3,V3比V2使用起来简洁太多了
ProfitSharingV3Service profitSharingV3Service = myWxPayService.getProfitSharingV3Service();
String nonceStr = RandomUtil.randomString(32);
String appId = "您的appid";
//添加分账方
/* ProfitSharingReceiver profitSharingReceiver = new ProfitSharingReceiver();
profitSharingReceiver.setAccount("appid对应的openId");
profitSharingReceiver.setAmount(1l);
profitSharingReceiver.setAppid(appId);
profitSharingReceiver.setType("PERSONAL_OPENID");
profitSharingReceiver.setRelationType("PARTNER");
profitSharingV3Service.addProfitSharingReceiver(profitSharingReceiver);*/
//分账
ProfitSharingRequest profitSharingRequest = new ProfitSharingRequest();
profitSharingRequest.setAppid(appId);
profitSharingRequest.setTransactionId("微信支付订单号");
profitSharingRequest.setOutOrderNo("业务系统唯一编号");
//分账完成后,剩余金额自动解冻并返回给商户账号,默认false
profitSharingRequest.setUnfreezeUnsplit(true);
//待分账金额1元
Long money = 1L;
List<ProfitSharingReceiver> profitSharingReceivers = new ArrayList<>();
ProfitSharingReceiver profitSharingReceiver = new ProfitSharingReceiver();
profitSharingReceiver.setAccount("appid对应的openId,分账用户1");
//分账百分之5
profitSharingReceiver.setAmount(money * 100 / 5);
profitSharingReceiver.setAppid(appId);
profitSharingReceiver.setType("PERSONAL_OPENID");
profitSharingReceiver.setRelationType("PARTNER");
profitSharingReceiver.setDescription("test01");
profitSharingReceivers.add(profitSharingReceiver);
ProfitSharingReceiver receiver = new ProfitSharingReceiver();
receiver.setAccount("appid对应的openId,分账用户2");
//百分之10
receiver.setAmount(money * 100 / 10);
receiver.setAppid(appId);
receiver.setType("PERSONAL_OPENID");
receiver.setRelationType("PARTNER");
receiver.setDescription("test02");
profitSharingReceivers.add(receiver);
profitSharingRequest.setReceivers(profitSharingReceivers);
profitSharingV3Service.profitSharing(profitSharingRequest);
}
}
总结
- 一定要先添加分账方,才能分账,否则会报openid和appid没有捆版关系
- 添加分账方可以通过api,也可以通过登录微信支付商户后台,有添加分账方功能
- 我在使用wxjava的时候一直都是用的v2,在使用分账时开始也是打算用v2,但测试了很久v2代码组织起来非常复杂,后来测试v3发现几行就可以搞定,所以说一个大的版本升级,对用户来说使用是非常便捷的,建议有条件就使用V3
如果本文对您有帮助,动动小手点个赞鼓励下吧~