🆕 #1090 增加微信支付分和免押租借相关接口

This commit is contained in:
spvycf
2020-05-19 16:25:18 +08:00
committed by GitHub
parent 0bc2cf9ade
commit 8709a9c5a7
26 changed files with 1762 additions and 12 deletions

View File

@@ -0,0 +1,164 @@
package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.payscore.NotifyData;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import java.net.URISyntaxException;
/**
* <pre>
* 支付分相关服务类.
* 微信支付分是对个人的身份特质、支付行为、使用历史等情况的综合计算分值,旨在为用户提供更简单便捷的生活方式。
* 微信用户可以在具体应用场景中,开通微信支付分。开通后,用户可以在【微信—>钱包—>支付分】中查看分数和使用记录。(即需在应用场景中使用过一次,钱包才会出现支付分入口)
*
* Created by doger.wang on 2020/05/12.
* </pre>
*
*
*/
public interface PayScoreService {
/**
* <pre>
* 支付分创建订单API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter1_1.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_1.shtml
* </pre>
*
* @param request 请求对象
* @return WxPayScoreResult
* @throws WxPayException the wx pay exception
*/
WxPayScoreResult createServiceOrder(WxPayScoreRequest request) throws WxPayException;
/**
* <pre>
* 支付分查询订单API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_2.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_2.shtml
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param out_order_no, query_id选填一个
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
WxPayScoreResult queryServiceOrder( String out_order_no,String query_id ) throws WxPayException, URISyntaxException;
/**
* <pre>
* 支付分取消订单API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_3.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_3.shtml
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param out_order_no reason
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
WxPayScoreResult cancelServiceOrder(String out_order_no, String reason) throws WxPayException;
/**
* <pre>
* 支付分修改订单金额API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_4.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_4.shtml
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param WxPayScoreRequest
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
WxPayScoreResult modifyServiceOrder(WxPayScoreRequest request) throws WxPayException;
/**
* <pre>
* 支付分完结订单API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_5.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_5.shtml
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param WxPayScoreRequest
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
WxPayScoreResult completeServiceOrder(WxPayScoreRequest request) throws WxPayException;
/**
* <pre>
* 支付分订单收款API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_6.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_6.shtml
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param out_order_no
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
WxPayScoreResult payServiceOrder(String out_order_no) throws WxPayException;
/**
* <pre>
* 支付分订单收款API.
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_7.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_7.shtml
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param WxPayScoreRequest
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
WxPayScoreResult syncServiceOrder(WxPayScoreRequest request) throws WxPayException;
/**
* <pre>
* 支付分回调内容解密方法
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter5_2.shtml
* 接口链接https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter5_2.shtml
* </pre>
*
* @param NotifyData 请求对象
* @return WxPayScoreResult
*/
WxPayScoreResult decryptNotifyData(NotifyData data) throws WxPayException;
}

View File

@@ -12,6 +12,7 @@ import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import java.io.File;
import java.net.URI;
import java.util.Date;
import java.util.Map;
@@ -54,6 +55,27 @@ public interface WxPayService {
*/
String post(String url, String requestStr, boolean useKey) throws WxPayException;
/**
* 发送post请求得到响应字符串.
*
* @param url 请求地址
* @param requestStr 请求信息
* @return 返回请求结果字符串 string
* @throws WxPayException the wx pay exception
*/
String postV3(String url, String requestStr) throws WxPayException;
/**
* 发送get V3请求得到响应字符串.
*
* @param url 请求地址
* @param param 请求信息
* @return 返回请求结果字符串 string
* @throws WxPayException the wx pay exception
*/
String getV3(URI url) throws WxPayException;
/**
* 获取企业付款服务类.
*
@@ -75,6 +97,14 @@ public interface WxPayService {
*/
ProfitSharingService getProfitSharingService();
/**
* 获取支付分服务类.
*
* @return the ent pay service
*/
PayScoreService getPayScoreService();
/**
* 设置企业付款服务类,允许开发者自定义实现类.
*
@@ -729,4 +759,5 @@ public interface WxPayService {
*/
WxPayFacepayResult facepay(WxPayFacepayRequest request) throws WxPayException;
}

View File

@@ -17,10 +17,7 @@ import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.ProfitSharingService;
import com.github.binarywang.wxpay.service.RedpackService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.*;
import com.github.binarywang.wxpay.util.SignUtils;
import com.github.binarywang.wxpay.util.XmlConfig;
import com.google.common.base.Joiner;
@@ -64,6 +61,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
private EntPayService entPayService = new EntPayServiceImpl(this);
private ProfitSharingService profitSharingService = new ProfitSharingServiceImpl(this);
private RedpackService redpackService = new RedpackServiceImpl(this);
private PayScoreService payScoreService = new PayScoreServiceImpl(this);
/**
* The Config.
@@ -80,6 +78,11 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return profitSharingService;
}
@Override
public PayScoreService getPayScoreService() {
return payScoreService;
}
@Override
public RedpackService getRedpackService() {
return this.redpackService;

View File

@@ -0,0 +1,193 @@
package com.github.binarywang.wxpay.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.payscore.NotifyData;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.PayScoreService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.v3.util.AesUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
/**
* @author doger.wang
* @date 2020/5/14 9:43
*/
public class PayScoreServiceImpl implements PayScoreService {
private WxPayService payService;
public PayScoreServiceImpl(WxPayService payService) {
this.payService = payService;
}
@Override
public WxPayScoreResult createServiceOrder(WxPayScoreRequest request) throws WxPayException {
boolean need_user_confirm = request.isNeed_user_confirm();
WxPayConfig config = this.payService.getConfig();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder";
request.setAppid(config.getAppId());
request.setService_id(config.getServiceId());
request.setNotify_url(config.getPayScoreNotifyUrl());
String result = payService.postV3(url, JSONObject.toJSONString(request));
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
//补充算一下签名给小程序跳转用
String currentTimeMillis = System.currentTimeMillis() + "";
Map<String, String> signMap = new HashMap<>();
signMap.put("mch_id", config.getMchId());
if (need_user_confirm){
signMap.put("package", wxPayScoreCreateResult.getPackageX());
}else {
signMap.put("service_id", config.getServiceId());
signMap.put("out_order_no", request.getOut_order_no());
}
signMap.put("timestamp", currentTimeMillis);
signMap.put("nonce_str", currentTimeMillis);
signMap.put("sign_type", "HMAC-SHA256");
String sign = AesUtils.createSign(signMap, config.getMchKey());
signMap.put("sign", sign);
wxPayScoreCreateResult.setPayScoreSignInfo(signMap);
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult queryServiceOrder(String out_order_no, String query_id) throws WxPayException, URISyntaxException {
WxPayConfig config = this.payService.getConfig();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder";
URIBuilder uriBuilder = new URIBuilder(url);
if (StringUtils.isAllEmpty(out_order_no,query_id) || !StringUtils.isAnyEmpty(out_order_no,query_id)){
throw new WxPayException("out_order_no,query_id不允许都填写或都不填写");
}
if (StringUtils.isNotEmpty(out_order_no)){
uriBuilder.setParameter("out_order_no", out_order_no);
}
if (StringUtils.isNotEmpty(query_id)){
uriBuilder.setParameter("query_id", query_id);
}
uriBuilder.setParameter("service_id", config.getServiceId());
uriBuilder.setParameter("appid", config.getAppId());
URI build = uriBuilder.build();
String result = payService.getV3(build);
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
//补充一下加密跳转信息
/* String currentTimeMillis = System.currentTimeMillis() + "";
Map<String, String> signMap = new HashMap();
signMap.put("mch_id", config.getMchId());
signMap.put("service_id", config.getServiceId());
signMap.put("out_order_no", out_order_no);
signMap.put("timestamp", currentTimeMillis);
signMap.put("nonce_str", currentTimeMillis);
signMap.put("sign_type", "HMAC-SHA256");
String sign = AesUtil.createSign(signMap, config.getMchKey());
signMap.put("sign", sign);
wxPayScoreCreateResult.setPayScoreSignInfo(signMap);*/
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult cancelServiceOrder(String out_order_no, String reason) throws WxPayException {
WxPayConfig config = this.payService.getConfig();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/cancel";
HashMap<String, Object> map = new HashMap<>();
map.put("appid",config.getAppId());
map.put("service_id",config.getServiceId());
map.put("reason",reason);
String result = payService.postV3(url, JSONObject.toJSONString(map));
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult modifyServiceOrder(WxPayScoreRequest request) throws WxPayException {
WxPayConfig config = this.payService.getConfig();
String out_order_no = request.getOut_order_no();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/modify";
request.setAppid(config.getAppId());
request.setService_id(config.getServiceId());
request.setOut_order_no(null);
//request.setNotify_url(config.getPayScoreNotifyUrl());
String result = payService.postV3(url, JSONObject.toJSONString(request));
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult completeServiceOrder(WxPayScoreRequest request) throws WxPayException {
WxPayConfig config = this.payService.getConfig();
String out_order_no = request.getOut_order_no();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/complete";
request.setAppid(config.getAppId());
request.setService_id(config.getServiceId());
//request.setNotify_url(config.getPayScoreNotifyUrl());
request.setOut_order_no(null);
String result = payService.postV3(url, JSONObject.toJSONString(request));
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult payServiceOrder(String out_order_no) throws WxPayException {
WxPayConfig config = this.payService.getConfig();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/pay";
HashMap<String, Object> map = new HashMap<>();
map.put("appid",config.getAppId());
map.put("service_id",config.getServiceId());
String result = payService.postV3(url, JSONObject.toJSONString(map));
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult syncServiceOrder(WxPayScoreRequest request) throws WxPayException {
WxPayConfig config = this.payService.getConfig();
String out_order_no = request.getOut_order_no();
String url = this.payService.getPayBaseUrl() + "/v3/payscore/serviceorder/"+out_order_no+"/sync";
request.setAppid(config.getAppId());
request.setService_id(config.getServiceId());
request.setOut_order_no(null);
//request.setNotify_url(config.getPayScoreNotifyUrl());
String result = payService.postV3(url, JSONObject.toJSONString(request));
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(result, WxPayScoreResult.class);
return wxPayScoreCreateResult;
}
@Override
public WxPayScoreResult decryptNotifyData(NotifyData data) throws WxPayException{
NotifyData.Resource resource = data.getResource();
String ciphertext = resource.getCiphertext();
String associated_data = resource.getAssociated_data();
String nonce = resource.getNonce();
String apiv3Key = this.payService.getConfig().getApiv3Key();
try {
String s = AesUtils.decryptToString(associated_data, nonce, ciphertext, apiv3Key);
WxPayScoreResult wxPayScoreCreateResult = JSONObject.parseObject(s, WxPayScoreResult.class);
return wxPayScoreCreateResult;
} catch (GeneralSecurityException e) {
throw new WxPayException("解析报文异常",e);
} catch (IOException e) {
throw new WxPayException("解析报文异常",e);
}
}
}

View File

@@ -1,26 +1,33 @@
package com.github.binarywang.wxpay.service.impl;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.SSLContext;
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.request.WxPayQueryCommentRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRedpackQueryRequest;
import com.github.binarywang.wxpay.bean.result.WxPayCommonResult;
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.util.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
@@ -90,14 +97,81 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
}
}
private StringEntity createEntry(String requestStr) {
try {
return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
} catch (UnsupportedEncodingException e) {
//cannot happen
this.log.error(e.getMessage(), e);
return null;
@Override
public String postV3(String url, String requestStr) throws WxPayException {
CloseableHttpClient httpClient = this.createApiV3HttpClient();
HttpPost httpPost = this.createHttpPost(url, requestStr);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json");
try (CloseableHttpResponse response = httpClient.execute(httpPost)){
//v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode();
String 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 = JSONObject.parseObject(responseString);
String message = jsonObject.getString("message");
throw new WxPayException(message);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
throw new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
}
@Override
public String getV3(URI url) throws WxPayException {
CloseableHttpClient httpClient = this.createApiV3HttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Accept", "application/json");
httpGet.addHeader("Content-Type", "application/json");
try (CloseableHttpResponse response = httpClient.execute(httpGet)){
//v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode();
String 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 = JSONObject.parseObject(responseString);
String message = jsonObject.getString("message");
throw new WxPayException(message);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【异常信息】{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
} finally {
httpGet.releaseConnection();
}
}
private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
CloseableHttpClient apiv3HttpClient = this.getConfig().getApiv3HttpClient();
if (null==apiv3HttpClient){
return this.getConfig().initApiV3HttpClient();
}
return apiv3HttpClient;
}
private StringEntity createEntry(String requestStr) {
return new StringEntity(requestStr, ContentType.create("application/json", "utf-8"));
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {

View File

@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service.impl;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.SSLContext;
@@ -67,6 +68,16 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
}
}
@Override
public String postV3(String url, String requestStr) throws WxPayException {
return null;
}
@Override
public String getV3(URI url) throws WxPayException {
return null;
}
private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey) throws WxPayException {
HttpRequest request = HttpRequest
.post(url)