🎨 微信支付分相关接口代码重构规范化

This commit is contained in:
Binary Wang
2020-05-19 21:28:26 +08:00
parent 8709a9c5a7
commit 39cea92171
17 changed files with 587 additions and 546 deletions

View File

@@ -1,6 +1,6 @@
package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.payscore.NotifyData;
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult;
import com.github.binarywang.wxpay.exception.WxPayException;
@@ -11,154 +11,120 @@ import java.net.URISyntaxException;
* <pre>
* 支付分相关服务类.
* 微信支付分是对个人的身份特质、支付行为、使用历史等情况的综合计算分值,旨在为用户提供更简单便捷的生活方式。
* 微信用户可以在具体应用场景中,开通微信支付分。开通后,用户可以在【微信—>钱包—>支付分】中查看分数和使用记录。(即需在应用场景中使用过一次,钱包才会出现支付分入口)
* 微信用户可以在具体应用场景中,开通微信支付分。开通后,用户可以在【微信—>钱包—>支付分】中查看分数和使用记录。
* (即需在应用场景中使用过一次,钱包才会出现支付分入口)
*
* Created by doger.wang on 2020/05/12.
* </pre>
*
*
* @author doger.wang
*/
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
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter3_1.shtml
* 接口链接https://api.mch.weixin.qq.com/v3/payscore/serviceorder
* </pre>
*
* @param request 请求对象
* @return WxPayScoreResult
* @return WxPayScoreResult wx pay score result
* @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
* 接口链接https://api.mch.weixin.qq.com/v3/payscore/serviceorder
* </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;
* @param outOrderNo the out order no
* @param queryId the query id
* @return the wx pay score result
* @throws WxPayException the wx pay exception
* @throws URISyntaxException the uri syntax exception
*/
WxPayScoreResult queryServiceOrder(String outOrderNo, String queryId) 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
* 接口链接https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/cancel
* </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;
* @param outOrderNo the out order no
* @param reason the reason
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult wx pay score result
* @throws WxPayException the wx pay exception
*/
WxPayScoreResult cancelServiceOrder(String outOrderNo, 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
* 接口链接https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/modify
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param WxPayScoreRequest
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
* @param request the request
* @return the wx pay score result
* @throws WxPayException the wx pay exception
*/
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
* 请求URLhttps://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/complete
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param WxPayScoreRequest
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
* @param request the request
* @return the wx pay score result
* @throws WxPayException the wx pay exception
*/
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
* 请求URLhttps://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/pay
*
* </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;
* @param outOrderNo the out order no
* @return the wx pay score result
* @throws WxPayException the wx pay exception
*/
WxPayScoreResult payServiceOrder(String outOrderNo) 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
* 请求URL https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/sync
* </pre>
*
* @Author doger.wang
* @Description
* @Date 2020/5/14 15:40
* @Param WxPayScoreRequest
* @return com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult
**/
* @param request the request
* @return the wx pay score result
* @throws WxPayException the wx pay exception
*/
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
* @param data the data
* @return the wx pay score result
* @throws WxPayException the wx pay exception
*/
WxPayScoreResult decryptNotifyData(NotifyData data) throws WxPayException;
WxPayScoreResult parseNotifyData(PayScoreNotifyData data) throws WxPayException;
}

View File

@@ -1,7 +1,6 @@
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.PayScoreNotifyData;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest;
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
@@ -9,11 +8,13 @@ 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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.RequiredArgsConstructor;
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;
@@ -23,35 +24,31 @@ import java.util.Map;
* @author doger.wang
* @date 2020/5/14 9:43
*/
@RequiredArgsConstructor
public class PayScoreServiceImpl implements PayScoreService {
private WxPayService payService;
public PayScoreServiceImpl(WxPayService payService) {
this.payService = payService;
}
private static final Gson GSON = new GsonBuilder().create();
private final WxPayService payService;
@Override
public WxPayScoreResult createServiceOrder(WxPayScoreRequest request) throws WxPayException {
boolean need_user_confirm = request.isNeed_user_confirm();
boolean needUserConfirm = request.isNeedUserConfirm();
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);
request.setServiceId(config.getServiceId());
request.setNotifyUrl(config.getPayScoreNotifyUrl());
String result = payService.postV3(url, GSON.toJson(request));
WxPayScoreResult wxPayScoreCreateResult = GSON.fromJson(result, WxPayScoreResult.class);
//补充算一下签名给小程序跳转用
String currentTimeMillis = System.currentTimeMillis() + "";
Map<String, String> signMap = new HashMap<>();
Map<String, String> signMap = new HashMap<>(8);
signMap.put("mch_id", config.getMchId());
if (need_user_confirm){
if (needUserConfirm) {
signMap.put("package", wxPayScoreCreateResult.getPackageX());
}else {
} else {
signMap.put("service_id", config.getServiceId());
signMap.put("out_order_no", request.getOut_order_no());
signMap.put("out_order_no", request.getOutOrderNo());
}
signMap.put("timestamp", currentTimeMillis);
signMap.put("nonce_str", currentTimeMillis);
@@ -63,131 +60,96 @@ public class PayScoreServiceImpl implements PayScoreService {
}
@Override
public WxPayScoreResult queryServiceOrder(String out_order_no, String query_id) throws WxPayException, URISyntaxException {
public WxPayScoreResult queryServiceOrder(String outOrderNo, String queryId) 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)){
URIBuilder uriBuilder = new URIBuilder(url);
if (StringUtils.isAllEmpty(outOrderNo, queryId) || !StringUtils.isAnyEmpty(outOrderNo, queryId)) {
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(outOrderNo)) {
uriBuilder.setParameter("out_order_no", outOrderNo);
}
if (StringUtils.isNotEmpty(query_id)){
uriBuilder.setParameter("query_id", query_id);
if (StringUtils.isNotEmpty(queryId)) {
uriBuilder.setParameter("query_id", queryId);
}
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;
String result = payService.getV3(uriBuilder.build());
return GSON.fromJson(result, WxPayScoreResult.class);
}
@Override
public WxPayScoreResult cancelServiceOrder(String out_order_no, String reason) throws WxPayException {
public WxPayScoreResult cancelServiceOrder(String outOrderNo, 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;
String url = String.format("%s/v3/payscore/serviceorder/%s/cancel", this.payService.getPayBaseUrl(), outOrderNo);
HashMap<String, Object> map = new HashMap<>(4);
map.put("appid", config.getAppId());
map.put("service_id", config.getServiceId());
map.put("reason", reason);
String result = payService.postV3(url, GSON.toJson(map));
return GSON.fromJson(result, WxPayScoreResult.class);
}
@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";
String outOrderNo = request.getOutOrderNo();
String url = String.format("%s/v3/payscore/serviceorder/%s/modify", this.payService.getPayBaseUrl(), outOrderNo);
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;
request.setServiceId(config.getServiceId());
request.setOutOrderNo(null);
String result = payService.postV3(url, GSON.toJson(request));
return GSON.fromJson(result, WxPayScoreResult.class);
}
@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";
String outOrderNo = request.getOutOrderNo();
String url = String.format("%s/v3/payscore/serviceorder/%s/complete", this.payService.getPayBaseUrl(), outOrderNo);
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;
request.setServiceId(config.getServiceId());
request.setOutOrderNo(null);
String result = payService.postV3(url, GSON.toJson(request));
return GSON.fromJson(result, WxPayScoreResult.class);
}
@Override
public WxPayScoreResult payServiceOrder(String out_order_no) throws WxPayException {
public WxPayScoreResult payServiceOrder(String outOrderNo) 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;
String url = String.format("%s/v3/payscore/serviceorder/%s/pay", this.payService.getPayBaseUrl(), outOrderNo);
HashMap<String, Object> map = new HashMap<>(2);
map.put("appid", config.getAppId());
map.put("service_id", config.getServiceId());
String result = payService.postV3(url, GSON.toJson(map));
return GSON.fromJson(result, WxPayScoreResult.class);
}
@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";
String outOrderNo = request.getOutOrderNo();
String url = String.format("%s/v3/payscore/serviceorder/%s/sync", this.payService.getPayBaseUrl(), outOrderNo);
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;
request.setServiceId(config.getServiceId());
request.setOutOrderNo(null);
String result = payService.postV3(url, GSON.toJson(request));
return GSON.fromJson(result, WxPayScoreResult.class);
}
@Override
public WxPayScoreResult decryptNotifyData(NotifyData data) throws WxPayException{
NotifyData.Resource resource = data.getResource();
String ciphertext = resource.getCiphertext();
String associated_data = resource.getAssociated_data();
public WxPayScoreResult parseNotifyData(PayScoreNotifyData data) throws WxPayException {
PayScoreNotifyData.Resource resource = data.getResource();
String cipherText = resource.getCipherText();
String associatedData = resource.getAssociatedData();
String nonce = resource.getNonce();
String apiv3Key = this.payService.getConfig().getApiv3Key();
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);
String s = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key);
return GSON.fromJson(s, WxPayScoreResult.class);
} catch (GeneralSecurityException | IOException e) {
throw new WxPayException("解析报文异常!", e);
}
}
}

View File

@@ -1,18 +1,11 @@
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;
@@ -35,10 +28,6 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.util.Base64;
/**
* <pre>
* 微信支付请求实现类apache httpclient实现.
@@ -158,7 +147,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
}
private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
CloseableHttpClient apiv3HttpClient = this.getConfig().getApiv3HttpClient();
CloseableHttpClient apiv3HttpClient = this.getConfig().getApiV3HttpClient();
if (null==apiv3HttpClient){
return this.getConfig().initApiV3HttpClient();
}