Skip to content

Commit 1d8f143

Browse files
Copilotbinarywang
andcommitted
Add WeChat merchant transfer confirmation-free receipt authorization mode support
Co-authored-by: binarywang <[email protected]>
1 parent ae4126a commit 1d8f143

File tree

3 files changed

+149
-4
lines changed

3 files changed

+149
-4
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/transfer/TransferBillsRequest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ public class TransferBillsRequest implements Serializable {
8787
@SerializedName("transfer_scene_report_infos")
8888
private List<TransferSceneReportInfo> transferSceneReportInfos;
8989

90+
/**
91+
* 收款授权模式
92+
* <pre>
93+
* 字段名:收款授权模式
94+
* 变量名:receipt_authorization_mode
95+
* 是否必填:否
96+
* 类型:string
97+
* 描述:
98+
* 控制收款方式的授权模式,可选值:
99+
* - CONFIRM_RECEIPT_AUTHORIZATION:需确认收款授权模式(默认值)
100+
* - NO_CONFIRM_RECEIPT_AUTHORIZATION:免确认收款授权模式(需要用户事先授权)
101+
* 为空时,默认为需确认收款授权模式
102+
* 示例值:NO_CONFIRM_RECEIPT_AUTHORIZATION
103+
* </pre>
104+
*
105+
* @see com.github.binarywang.wxpay.constant.WxPayConstants.ReceiptAuthorizationMode
106+
*/
107+
@SerializedName("receipt_authorization_mode")
108+
private String receiptAuthorizationMode;
109+
90110

91111
@Data
92112
@Builder(builderMethodName = "newBuilder")

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,25 @@ public static class CASH_MARKETING {
436436
}
437437

438438
}
439+
440+
/**
441+
* 收款授权模式
442+
*
443+
* @see <a href="https://pay.weixin.qq.com/doc/v3/merchant/4014399293">官方文档</a>
444+
*/
445+
@UtilityClass
446+
public static class ReceiptAuthorizationMode {
447+
/**
448+
* 需确认收款授权模式(默认值)
449+
* 用户需要手动确认才能收款
450+
*/
451+
public static final String CONFIRM_RECEIPT_AUTHORIZATION = "CONFIRM_RECEIPT_AUTHORIZATION";
452+
453+
/**
454+
* 免确认收款授权模式
455+
* 用户授权后,收款不需要确认,转账直接到账
456+
*/
457+
public static final String NO_CONFIRM_RECEIPT_AUTHORIZATION = "NO_CONFIRM_RECEIPT_AUTHORIZATION";
458+
}
459+
439460
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/example/NewTransferApiExample.java

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
44
import com.github.binarywang.wxpay.bean.transfer.*;
55
import com.github.binarywang.wxpay.config.WxPayConfig;
6+
import com.github.binarywang.wxpay.constant.WxPayConstants;
67
import com.github.binarywang.wxpay.exception.WxPayException;
78
import com.github.binarywang.wxpay.service.TransferService;
89
import com.github.binarywang.wxpay.service.WxPayService;
@@ -215,6 +216,100 @@ public void batchTransferExample() {
215216
}
216217
}
217218

219+
/**
220+
* 使用免确认收款授权模式进行转账示例
221+
* 注意:使用此模式前,用户需要先进行授权
222+
*/
223+
public void transferWithNoConfirmAuthModeExample() {
224+
try {
225+
// 构建转账请求,使用免确认收款授权模式
226+
TransferBillsRequest request = TransferBillsRequest.newBuilder()
227+
.appid("wx1234567890123456")
228+
.outBillNo("NO_CONFIRM_" + System.currentTimeMillis()) // 商户转账单号
229+
.transferSceneId("1005") // 转账场景ID(佣金报酬)
230+
.openid("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o") // 收款用户的openid
231+
.transferAmount(200) // 转账金额,单位:分(此处为2元)
232+
.transferRemark("免确认收款转账") // 转账备注
233+
.receiptAuthorizationMode(WxPayConstants.ReceiptAuthorizationMode.NO_CONFIRM_RECEIPT_AUTHORIZATION)
234+
.userRecvPerception("Y") // 用户收款感知
235+
.build();
236+
237+
// 发起转账
238+
TransferBillsResult result = transferService.transferBills(request);
239+
240+
System.out.println("=== 免确认授权模式转账成功 ===");
241+
System.out.println("商户单号: " + result.getOutBillNo());
242+
System.out.println("微信转账单号: " + result.getTransferBillNo());
243+
System.out.println("状态: " + result.getState());
244+
System.out.println("说明: 使用免确认授权模式,转账直接到账,无需用户确认");
245+
246+
} catch (WxPayException e) {
247+
System.err.println("免确认授权转账失败: " + e.getMessage());
248+
System.err.println("错误代码: " + e.getErrCode());
249+
250+
// 可能的错误原因
251+
if ("USER_NOT_AUTHORIZED".equals(e.getErrCode())) {
252+
System.err.println("用户未授权免确认收款,请先引导用户进行授权");
253+
}
254+
}
255+
}
256+
257+
/**
258+
* 使用需确认收款授权模式进行转账示例(默认模式)
259+
*/
260+
public void transferWithConfirmAuthModeExample() {
261+
try {
262+
// 构建转账请求,显式设置为需确认收款授权模式
263+
TransferBillsRequest request = TransferBillsRequest.newBuilder()
264+
.appid("wx1234567890123456")
265+
.outBillNo("CONFIRM_" + System.currentTimeMillis()) // 商户转账单号
266+
.transferSceneId("1005") // 转账场景ID
267+
.openid("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o") // 收款用户的openid
268+
.transferAmount(150) // 转账金额,单位:分(此处为1.5元)
269+
.transferRemark("需确认收款转账") // 转账备注
270+
.receiptAuthorizationMode(WxPayConstants.ReceiptAuthorizationMode.CONFIRM_RECEIPT_AUTHORIZATION)
271+
.userRecvPerception("Y") // 用户收款感知
272+
.build();
273+
274+
// 发起转账
275+
TransferBillsResult result = transferService.transferBills(request);
276+
277+
System.out.println("=== 需确认授权模式转账成功 ===");
278+
System.out.println("商户单号: " + result.getOutBillNo());
279+
System.out.println("微信转账单号: " + result.getTransferBillNo());
280+
System.out.println("状态: " + result.getState());
281+
System.out.println("packageInfo: " + result.getPackageInfo());
282+
System.out.println("说明: 使用需确认授权模式,用户需要手动确认才能收款");
283+
284+
} catch (WxPayException e) {
285+
System.err.println("需确认授权转账失败: " + e.getMessage());
286+
}
287+
}
288+
289+
/**
290+
* 权限模式对比示例
291+
* 展示两种权限模式的区别和使用场景
292+
*/
293+
public void authModeComparisonExample() {
294+
System.out.println("\n=== 收款授权模式对比 ===");
295+
System.out.println("1. 需确认收款授权模式 (CONFIRM_RECEIPT_AUTHORIZATION):");
296+
System.out.println(" - 这是默认模式");
297+
System.out.println(" - 用户收到转账后需要手动点击确认才能到账");
298+
System.out.println(" - 适用于一般的转账场景");
299+
System.out.println(" - 转账状态可能包含 WAIT_USER_CONFIRM 等待确认状态");
300+
301+
System.out.println("\n2. 免确认收款授权模式 (NO_CONFIRM_RECEIPT_AUTHORIZATION):");
302+
System.out.println(" - 用户事先授权后,转账直接到账,无需确认");
303+
System.out.println(" - 提升用户体验,减少操作步骤");
304+
System.out.println(" - 适用于高频转账场景,如佣金发放等");
305+
System.out.println(" - 需要用户先进行授权,否则会返回授权错误");
306+
307+
System.out.println("\n使用建议:");
308+
System.out.println("- 高频业务场景推荐使用免确认模式,提升用户体验");
309+
System.out.println("- 首次使用需引导用户进行授权");
310+
System.out.println("- 处理授权相关异常,提供友好的错误提示");
311+
}
312+
218313
/**
219314
* 使用配置示例
220315
*/
@@ -230,20 +325,29 @@ public static void main(String[] args) {
230325
// 创建示例实例
231326
NewTransferApiExample example = new NewTransferApiExample(config);
232327

328+
// 权限模式对比说明
329+
example.authModeComparisonExample();
330+
233331
// 运行示例
234332
System.out.println("新版商户转账API使用示例");
235333
System.out.println("===============================");
236334

237-
// 1. 发起单笔转账
335+
// 1. 发起转账(使用免确认授权模式)
336+
// example.transferWithNoConfirmAuthModeExample();
337+
338+
// 2. 发起转账(使用需确认授权模式)
339+
// example.transferWithConfirmAuthModeExample();
340+
341+
// 3. 发起单笔转账(默认模式)
238342
example.transferExample();
239343

240-
// 2. 查询转账结果
344+
// 4. 查询转账结果
241345
// example.queryByOutBillNoExample();
242346

243-
// 3. 撤销转账
347+
// 5. 撤销转账
244348
// example.cancelTransferExample();
245349

246-
// 4. 批量转账(传统API)
350+
// 6. 批量转账(传统API)
247351
// example.batchTransferExample();
248352
}
249353
}

0 commit comments

Comments
 (0)