- 引入jetpack仓库
maven
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
</repositories>
gradle
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
- 引入SDK
maven
<dependency>
<groupId>com.github.actar233</groupId>
<artifactId>mbdpay-java</artifactId>
<version>1.0.2</version>
</dependency>
gradle
dependencies {
implementation 'com.github.actar233:mbdpay-java:1.0.2'
}
- 初始化
MbdPayClient client = new MbdPayClient("<app_id>", "<app_key>");
- 获取微信用户openid
private static void openid(MbdPayClient client){
System.out.println(client.openid("https://www.baidu.com"));
}
- 支付
// 支付宝
private static void aliPay(MbdPayClient client) {
try {
AliPayResult result = client.aliPay(AliPayParams.builder()
.url("<url>")
.amountTotal(1)
.callbackUrl("<callbackUrl>")
.description("<description>")
.outTradeNo("<可选:outTradeNo>")
.build());
if (result.isSuccess()) {
System.out.println("查询成功");
System.out.println(result);
} else {
System.out.println("查询失败");
System.out.println(result.getError());
}
} catch (MbdPayException e) {
System.out.println("请求异常");
e.printStackTrace();
}
}
// 微信H5
private static void wxH5(MbdPayClient client) {
try {
WxH5Result result = client.wxH5(WxH5Params.builder()
.amountTotal(1)
.description("<description>")
.channel("h5")
.outTradeNo("<可选:outTradeNo>")
.build());
if (result.isSuccess()) {
System.out.println("查询成功");
System.out.println(result);
} else {
System.out.println("查询失败");
System.out.println(result.getError());
}
} catch (MbdPayException e) {
System.out.println("请求异常");
e.printStackTrace();
}
}
// 微信JSAPI
private static void wxJsApi(MbdPayClient client) {
try {
WxJsApiResult result = client.wxJsApi(WxJsApiParams.builder()
.openid("<openid>")
.amountTotal(1)
.callbackUrl("<callbackUrl>")
.description("<description>")
.outTradeNo("<可选:outTradeNo>")
.build());
if (result.isSuccess()) {
System.out.println("查询成功");
System.out.println(result);
// 获取支付参数
System.out.println(result.getPayParams());
} else {
System.out.println("查询失败");
System.out.println(result.getError());
}
} catch (MbdPayException e) {
System.out.println("请求异常");
e.printStackTrace();
}
}
- 查单
private static void searchOrder(MbdPayClient client) {
try {
SearchOrderResult result = client.searchOrder(SearchOrderParams.builder()
.outTradeNo("<outTradeNo>")
.build());
if (result.isSuccess()) {
System.out.println("查询成功");
System.out.println(result);
} else {
System.out.println("查询失败");
System.out.println(result.getError());
}
} catch (MbdPayException e) {
System.out.println("请求异常");
e.printStackTrace();
}
}
- 退款
private static void refund(MbdPayClient client) {
try {
RefundResult result = client.refund(RefundParams.builder()
.orderId("<orderId>")
.build());
if (result.isSuccess()) {
System.out.println("查询成功");
System.out.println(result);
} else {
System.out.println("查询失败");
System.out.println(result.getError());
}
} catch (MbdPayException e) {
System.out.println("请求异常");
e.printStackTrace();
}
}
- 解析通知
@PostMapping("/callback")
public String callback(@RequestBody String json) throws MbdPayException {
Notice notice = mbdPayClient.parseNotice(json);
System.out.println(notice);
switch (notice.getType()) {
case charge_succeeded:
NoticeDataChargeSucceeded data_cs = (NoticeDataChargeSucceeded) notice.getData();
System.out.println("支付成功");
System.out.println("订单号:" + data_cs.getOutTradeNo());
break;
case complaint:
NoticeDataComplaint data_c = (NoticeDataComplaint) notice.getData();
System.out.println("投诉成功");
System.out.println("订单号:" + data_c.getOutTradeNo());
break;
}
return "success";
}