Skip to content

Commit eaae169

Browse files
Copilotbinarywang
andcommitted
文档:添加多公众号关联使用示例代码
Co-authored-by: binarywang <[email protected]>
1 parent 86ef11f commit eaae169

File tree

1 file changed

+246
-0
lines changed
  • spring-boot-starters/wx-java-pay-multi-spring-boot-starter/src/test/java/com/binarywang/spring/starter/wxjava/pay/example

1 file changed

+246
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
package com.binarywang.spring.starter.wxjava.pay.example;
2+
3+
import com.binarywang.spring.starter.wxjava.pay.service.WxPayMultiServices;
4+
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
5+
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
6+
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
7+
import com.github.binarywang.wxpay.service.WxPayService;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
/**
13+
* 微信支付多公众号关联使用示例.
14+
* <p>
15+
* 本示例展示了如何使用 wx-java-pay-multi-spring-boot-starter 来管理多个公众号的支付配置。
16+
* </p>
17+
*
18+
* @author Binary Wang
19+
*/
20+
@Slf4j
21+
@Service
22+
public class WxPayMultiExample {
23+
24+
@Autowired
25+
private WxPayMultiServices wxPayMultiServices;
26+
27+
/**
28+
* 示例1:根据appId创建支付订单.
29+
* <p>
30+
* 适用场景:系统需要支持多个公众号,根据用户所在的公众号动态选择支付配置
31+
* </p>
32+
*
33+
* @param appId 公众号appId
34+
* @param openId 用户的openId
35+
* @param totalFee 支付金额(分)
36+
* @param body 商品描述
37+
* @return JSAPI支付参数
38+
*/
39+
public WxPayUnifiedOrderV3Result.JsapiResult createJsapiOrder(String appId, String openId,
40+
Integer totalFee, String body) {
41+
try {
42+
// 根据appId获取对应的WxPayService
43+
WxPayService wxPayService = wxPayMultiServices.getWxPayService(appId);
44+
45+
if (wxPayService == null) {
46+
log.error("未找到appId对应的微信支付配置: {}", appId);
47+
throw new IllegalArgumentException("未找到appId对应的微信支付配置");
48+
}
49+
50+
// 构建支付请求
51+
WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
52+
request.setOutTradeNo(generateOutTradeNo());
53+
request.setDescription(body);
54+
request.setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(totalFee));
55+
request.setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(openId));
56+
request.setNotifyUrl(wxPayService.getConfig().getNotifyUrl());
57+
58+
// 调用微信支付API创建订单
59+
WxPayUnifiedOrderV3Result.JsapiResult result =
60+
wxPayService.createOrderV3(TradeTypeEnum.JSAPI, request);
61+
62+
log.info("创建JSAPI支付订单成功,appId: {}, outTradeNo: {}", appId, request.getOutTradeNo());
63+
return result;
64+
65+
} catch (Exception e) {
66+
log.error("创建JSAPI支付订单失败,appId: {}", appId, e);
67+
throw new RuntimeException("创建支付订单失败", e);
68+
}
69+
}
70+
71+
/**
72+
* 示例2:服务商模式 - 为不同子商户创建订单.
73+
* <p>
74+
* 适用场景:服务商为多个子商户提供支付服务
75+
* </p>
76+
*
77+
* @param configKey 配置标识(在配置文件中定义)
78+
* @param subOpenId 子商户用户的openId
79+
* @param totalFee 支付金额(分)
80+
* @param body 商品描述
81+
* @return JSAPI支付参数
82+
*/
83+
public WxPayUnifiedOrderV3Result.JsapiResult createPartnerOrder(String configKey, String subOpenId,
84+
Integer totalFee, String body) {
85+
try {
86+
// 根据配置标识获取WxPayService
87+
WxPayService wxPayService = wxPayMultiServices.getWxPayService(configKey);
88+
89+
if (wxPayService == null) {
90+
log.error("未找到配置: {}", configKey);
91+
throw new IllegalArgumentException("未找到配置");
92+
}
93+
94+
// 获取子商户信息
95+
String subAppId = wxPayService.getConfig().getSubAppId();
96+
String subMchId = wxPayService.getConfig().getSubMchId();
97+
log.info("使用服务商模式,子商户appId: {}, 子商户号: {}", subAppId, subMchId);
98+
99+
// 构建支付请求
100+
WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
101+
request.setOutTradeNo(generateOutTradeNo());
102+
request.setDescription(body);
103+
request.setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(totalFee));
104+
request.setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(subOpenId));
105+
request.setNotifyUrl(wxPayService.getConfig().getNotifyUrl());
106+
107+
// 调用微信支付API创建订单
108+
WxPayUnifiedOrderV3Result.JsapiResult result =
109+
wxPayService.createOrderV3(TradeTypeEnum.JSAPI, request);
110+
111+
log.info("创建服务商支付订单成功,配置: {}, outTradeNo: {}", configKey, request.getOutTradeNo());
112+
return result;
113+
114+
} catch (Exception e) {
115+
log.error("创建服务商支付订单失败,配置: {}", configKey, e);
116+
throw new RuntimeException("创建支付订单失败", e);
117+
}
118+
}
119+
120+
/**
121+
* 示例3:查询订单状态.
122+
* <p>
123+
* 适用场景:查询不同公众号的订单支付状态
124+
* </p>
125+
*
126+
* @param appId 公众号appId
127+
* @param outTradeNo 商户订单号
128+
* @return 订单状态
129+
*/
130+
public String queryOrderStatus(String appId, String outTradeNo) {
131+
try {
132+
WxPayService wxPayService = wxPayMultiServices.getWxPayService(appId);
133+
134+
if (wxPayService == null) {
135+
log.error("未找到appId对应的微信支付配置: {}", appId);
136+
throw new IllegalArgumentException("未找到appId对应的微信支付配置");
137+
}
138+
139+
// 查询订单
140+
var result = wxPayService.queryOrderV3(null, outTradeNo);
141+
String tradeState = result.getTradeState();
142+
143+
log.info("查询订单状态成功,appId: {}, outTradeNo: {}, 状态: {}", appId, outTradeNo, tradeState);
144+
return tradeState;
145+
146+
} catch (Exception e) {
147+
log.error("查询订单状态失败,appId: {}, outTradeNo: {}", appId, outTradeNo, e);
148+
throw new RuntimeException("查询订单失败", e);
149+
}
150+
}
151+
152+
/**
153+
* 示例4:申请退款.
154+
* <p>
155+
* 适用场景:为不同公众号的订单申请退款
156+
* </p>
157+
*
158+
* @param appId 公众号appId
159+
* @param outTradeNo 商户订单号
160+
* @param refundFee 退款金额(分)
161+
* @param totalFee 订单总金额(分)
162+
* @param reason 退款原因
163+
* @return 退款单号
164+
*/
165+
public String refund(String appId, String outTradeNo, Integer refundFee,
166+
Integer totalFee, String reason) {
167+
try {
168+
WxPayService wxPayService = wxPayMultiServices.getWxPayService(appId);
169+
170+
if (wxPayService == null) {
171+
log.error("未找到appId对应的微信支付配置: {}", appId);
172+
throw new IllegalArgumentException("未找到appId对应的微信支付配置");
173+
}
174+
175+
// 构建退款请求
176+
com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request request =
177+
new com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request();
178+
request.setOutTradeNo(outTradeNo);
179+
request.setOutRefundNo(generateRefundNo());
180+
request.setReason(reason);
181+
request.setNotifyUrl(wxPayService.getConfig().getRefundNotifyUrl());
182+
183+
com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request.Amount amount =
184+
new com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request.Amount();
185+
amount.setRefund(refundFee);
186+
amount.setTotal(totalFee);
187+
amount.setCurrency("CNY");
188+
request.setAmount(amount);
189+
190+
// 调用微信支付API申请退款
191+
var result = wxPayService.refundV3(request);
192+
193+
log.info("申请退款成功,appId: {}, outTradeNo: {}, outRefundNo: {}",
194+
appId, outTradeNo, request.getOutRefundNo());
195+
return request.getOutRefundNo();
196+
197+
} catch (Exception e) {
198+
log.error("申请退款失败,appId: {}, outTradeNo: {}", appId, outTradeNo, e);
199+
throw new RuntimeException("申请退款失败", e);
200+
}
201+
}
202+
203+
/**
204+
* 示例5:动态管理配置.
205+
* <p>
206+
* 适用场景:需要在运行时更新配置(如证书更新后需要重新加载)
207+
* </p>
208+
*
209+
* @param configKey 配置标识
210+
*/
211+
public void reloadConfig(String configKey) {
212+
try {
213+
// 移除缓存的WxPayService实例
214+
wxPayMultiServices.removeWxPayService(configKey);
215+
log.info("移除配置成功,下次获取时将重新创建: {}", configKey);
216+
217+
// 下次调用 getWxPayService 时会重新创建实例
218+
WxPayService wxPayService = wxPayMultiServices.getWxPayService(configKey);
219+
if (wxPayService != null) {
220+
log.info("重新加载配置成功: {}", configKey);
221+
}
222+
223+
} catch (Exception e) {
224+
log.error("重新加载配置失败: {}", configKey, e);
225+
throw new RuntimeException("重新加载配置失败", e);
226+
}
227+
}
228+
229+
/**
230+
* 生成商户订单号.
231+
*
232+
* @return 商户订单号
233+
*/
234+
private String generateOutTradeNo() {
235+
return "ORDER_" + System.currentTimeMillis();
236+
}
237+
238+
/**
239+
* 生成商户退款单号.
240+
*
241+
* @return 商户退款单号
242+
*/
243+
private String generateRefundNo() {
244+
return "REFUND_" + System.currentTimeMillis();
245+
}
246+
}

0 commit comments

Comments
 (0)