Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信收付通支付增加关闭普通支付单接口 #1916

Merged
merged 24 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
123dbb6
Merge pull request #1 from Wechat-Group/develop
f00lish Sep 11, 2020
991f39e
Merge pull request #2 from Wechat-Group/develop
f00lish Sep 12, 2020
3cd7749
Merge pull request #3 from Wechat-Group/develop
f00lish Sep 14, 2020
8a89e1b
Merge pull request #4 from Wechat-Group/develop
f00lish Sep 16, 2020
43f96f4
Merge pull request #5 from Wechat-Group/develop
f00lish Sep 17, 2020
bdeadc0
Merge pull request #6 from Wechat-Group/develop
f00lish Sep 28, 2020
90b705f
Merge pull request #7 from Wechat-Group/develop
f00lish Oct 19, 2020
58671ad
:art: 重构部分包结构
binarywang Oct 25, 2020
5ff40eb
Merge pull request #8 from Wechat-Group/develop
f00lish Oct 26, 2020
d265065
增加微信收付通查询提现状态接口
f00lish Oct 27, 2020
5fdb8eb
fix 查询提现结果类的序列化
f00lish Oct 27, 2020
ed1c7c4
fix 收付通提现状态查询时接口地址错误
f00lish Oct 30, 2020
a2dd7f6
fix 微信收付通查询退款状态地址错误
f00lish Nov 5, 2020
b290713
Merge pull request #9 from Wechat-Group/develop
f00lish Nov 5, 2020
572c994
微信通用上传图片接口 增加使用流参数上传接口
f00lish Nov 6, 2020
3ff61a6
Merge pull request #10 from Wechat-Group/develop
f00lish Nov 18, 2020
869ac95
修改微信收付通资金账单下载接口
f00lish Nov 23, 2020
8ce7fe7
Merge pull request #11 from Wechat-Group/develop
f00lish Nov 23, 2020
d0c4668
fix 更新微信收付通账单接口时 误操作改名
f00lish Nov 23, 2020
1db5d88
回滚文件名的更改
f00lish Nov 24, 2020
315a0f2
Merge pull request #12 from Wechat-Group/develop
f00lish Dec 7, 2020
bc5db55
fix 增加微信收付通抛出的异常带状态码
f00lish Dec 8, 2020
cf412f1
微信收付通支付增加关闭普通支付单接口
f00lish Dec 10, 2020
8a8fb3c
fix v3的post接口有可能存在无返回的情况
f00lish Dec 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.binarywang.wxpay.bean.ecommerce;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* 关闭普通订单请求
* @author: f00lish
* @date: 2020/12/09
*/
@Data
@NoArgsConstructor
public class PartnerTransactionsCloseRequest implements Serializable {

private static final long serialVersionUID = -7602636370950088329L;

/**
* <pre>
* 字段名:服务商户号
* 变量名:sp_mchid
* 是否必填:是
* 类型:string(32)
* 描述:
* 服务商户号,由微信支付生成并下发
* 示例值:1230000109
* </pre>
*/
@SerializedName(value = "sp_mchid")
private String spMchid;

/**
* <pre>
* 字段名:二级商户号
* 变量名:sub_mchid
* 是否必填:是
* 类型:string(32)
* 描述:
* 二级商户的商户号,有微信支付生成并下发。
* 示例值:1900000109
* </pre>
*/
@SerializedName(value = "sub_mchid")
private String subMchid;

/**
* <pre>
* 字段名:商户订单号
* 变量名:out_trade_no
* 是否必填:是
* 类型:string(32)
* 描述:
* 商户系统内部订单号,只能是数字、大小写字母_-*且在同一个商户号下唯一,详见【商户订单号】。
* 特殊规则:最小字符长度为6
* 示例值:1217752501201407033233368018
* </pre>
*/
private transient String outTradeNo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ public interface EcommerceService {
*/
PartnerTransactionsResult queryPartnerTransactions(PartnerTransactionsQueryRequest request) throws WxPayException;

/**
* <pre>
* 关闭普通订单API
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/e_transactions/chapter3_6.shtml
* </pre>
*
* @param request 关闭普通订单请求
* @throws WxPayException the wx pay exception
*/
void closePartnerTransactions(PartnerTransactionsCloseRequest request) throws WxPayException;

/**
* <pre>
* 服务商账户实时余额
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public PartnerTransactionsResult queryPartnerTransactions(PartnerTransactionsQue
return GSON.fromJson(response, PartnerTransactionsResult.class);
}

@Override
public void closePartnerTransactions(PartnerTransactionsCloseRequest request) throws WxPayException {
String url = String.format("%s/v3/pay/partner/transactions/out-trade-no/%s/close", this.payService.getPayBaseUrl(), request.getOutTradeNo());
String response = this.payService.postV3(url, GSON.toJson(request));
}

@Override
public FundBalanceResult spNowBalance(SpAccountTypeEnum accountType) throws WxPayException {
String url = String.format("%s/v3/merchant/fund/balance/%s", this.payService.getPayBaseUrl(), accountType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -99,18 +100,24 @@ public String postV3(String url, String requestStr) throws WxPayException {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
//v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode();
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
//post方法有可能会没有返回值的情况
String responseString;
if (response.getEntity() == null) {
responseString = null;
}else {
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
return responseString;
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
Expand Down Expand Up @@ -141,12 +148,12 @@ public String postV3WithWechatpaySerial(String url, String requestStr) throws Wx
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
e.printStackTrace();
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
Expand All @@ -165,18 +172,24 @@ public String postV3(String url, HttpPost httpPost) throws WxPayException {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
//v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode();
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
//post方法有可能会没有返回值的情况
String responseString;
if (response.getEntity() == null) {
responseString = null;
}else {
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
this.log.info("\n【请求地址】:{}\n【响应数据】:{}", url, responseString);
return responseString;
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
Expand All @@ -198,11 +211,11 @@ public String getV3(URI url) throws WxPayException {
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpGet.releaseConnection();
}
Expand All @@ -223,11 +236,11 @@ public InputStream downloadV3(URI url) throws WxPayException {
//有错误提示信息返回
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpGet.releaseConnection();
}
Expand Down Expand Up @@ -291,4 +304,16 @@ private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayExc
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
}


private WxPayException convertException(JsonObject jsonObject) {
//todo 这里考虑使用新的适用于V3的异常
JsonElement codeElement = jsonObject.get("code");
String code = codeElement == null ? null : codeElement.getAsString();
String message = jsonObject.get("message").getAsString();
WxPayException wxPayException = new WxPayException(message);
wxPayException.setErrCode(code);
wxPayException.setErrCodeDes(message);
return wxPayException;
}

}