8 changed files with 210 additions and 31 deletions
@ -0,0 +1,182 @@ |
|||||
|
//package com.bnyer.pay.manager;
|
||||
|
//
|
||||
|
//import com.alibaba.fastjson.JSON;
|
||||
|
//import com.alibaba.fastjson.JSONObject;
|
||||
|
//import com.bnyer.common.core.constant.RedisKeyConstant;
|
||||
|
//import com.bnyer.common.core.domain.DypayConfig;
|
||||
|
//import com.bnyer.common.core.utils.SpringUtils;
|
||||
|
//import com.bnyer.common.core.utils.StringUtils;
|
||||
|
//import com.bnyer.common.redis.service.RedisService;
|
||||
|
//import com.bnyer.pay.constant.DYPayConstants;
|
||||
|
//import com.bnyer.pay.exception.PayException;
|
||||
|
//import com.google.gson.JsonElement;
|
||||
|
//import com.google.gson.JsonObject;
|
||||
|
//import lombok.extern.slf4j.Slf4j;
|
||||
|
//import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
|
//import org.springframework.http.HttpEntity;
|
||||
|
//import org.springframework.http.HttpHeaders;
|
||||
|
//import org.springframework.http.MediaType;
|
||||
|
//import org.springframework.web.client.RestTemplate;
|
||||
|
//
|
||||
|
//import java.text.MessageFormat;
|
||||
|
//import java.util.Objects;
|
||||
|
//import java.util.concurrent.TimeUnit;
|
||||
|
//
|
||||
|
///**
|
||||
|
// * @author :WXC
|
||||
|
// * @Date :2023/04/24
|
||||
|
// * @description :
|
||||
|
// */
|
||||
|
//@Slf4j
|
||||
|
//public class DyPayManagerBack {
|
||||
|
//
|
||||
|
// private RestTemplate restTemplate;
|
||||
|
//
|
||||
|
// private RedisService redisService;
|
||||
|
//
|
||||
|
// private final DypayConfig dypayConfig;
|
||||
|
//
|
||||
|
// private final int MAX_RETRY_TIMES = 2;
|
||||
|
//
|
||||
|
// public DyPayManagerBack(DypayConfig dypayConfig){
|
||||
|
// this.dypayConfig = dypayConfig;
|
||||
|
// init();
|
||||
|
// }
|
||||
|
//
|
||||
|
// private void init() {
|
||||
|
// this.restTemplate = SpringUtils.getBean(RestTemplate.class);
|
||||
|
// this.redisService = SpringUtils.getBean(RedisService.class);
|
||||
|
// }
|
||||
|
//
|
||||
|
// /**
|
||||
|
// * 抖音小程序获取accessToken
|
||||
|
// * 为了保障应用的数据安全,只能在开发者服务器使用 AppSecret,如果小程序存在泄露 AppSecret 的问题,字节小程序平台将有可能下架该小程序,并暂停该小程序相关服务。
|
||||
|
// *
|
||||
|
// * access_token 是小程序的全局唯一调用凭据,开发者调用小程序支付时需要使用 access_token。access_token 的有效期为 2 个小时,需要定时刷新 access_token,重复获取会导致之前一次获取的 access_token 的有效期缩短为 5 分钟。
|
||||
|
// */
|
||||
|
// public String getAccessToken(boolean isRefresh) throws PayException {
|
||||
|
// if (!isRefresh){
|
||||
|
// Object cacheObject = redisService.getCacheObject(RedisKeyConstant.DY_ACCESS_TOKEN_KEY);
|
||||
|
// if (Objects.nonNull(cacheObject)){
|
||||
|
// return cacheObject.toString();
|
||||
|
// }
|
||||
|
// }
|
||||
|
// String requestStr = "";
|
||||
|
// try {
|
||||
|
// HttpHeaders headers = new HttpHeaders();
|
||||
|
// headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
|
// HttpEntity<String> formEntity = new HttpEntity<>(MessageFormat.format("app_id={0}&app_secret={1}&&grant_type={2}"
|
||||
|
// , dypayConfig.getAppid(), dypayConfig.getAppSecret(), "client_credentials"), headers);
|
||||
|
// String resultStr = restTemplate.postForObject(DYPayConstants.GET_ACCESS_TOKEN, formEntity, String.class);
|
||||
|
// String url = DYPayConstants.GET_ACCESS_TOKEN;
|
||||
|
// requestStr = formEntity.getBody();
|
||||
|
// if (StringUtils.isBlank(resultStr) || !resultStr.startsWith("{")){
|
||||
|
// log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, resultStr);
|
||||
|
// throw new PayException("第三方返回格式有误!");
|
||||
|
// }
|
||||
|
// JSONObject resultObj = JSONObject.parseObject(resultStr);
|
||||
|
// String errNo = StringUtils.isNotBlank(resultObj.getString("err_no"))?resultObj.getString("err_no"):resultObj.getString("err_code");
|
||||
|
// if ("0".equals(errNo)) {
|
||||
|
// log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, resultObj, resultStr);
|
||||
|
// //存入redis
|
||||
|
// String accessToken = resultObj.getJSONObject("data").getString("access_token");
|
||||
|
// String expiresIn = resultObj.getJSONObject("data").getString("expires_in");
|
||||
|
// redisService.setCacheObject(RedisKeyConstant.DY_ACCESS_TOKEN_KEY,accessToken,Long.parseLong(expiresIn), TimeUnit.SECONDS);
|
||||
|
// return accessToken;
|
||||
|
// }else {
|
||||
|
// throw convertDyException(GsonParser.parse(resultStr));
|
||||
|
// }
|
||||
|
// } catch (Exception e) {
|
||||
|
// log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", DYPayConstants.GET_ACCESS_TOKEN, requestStr, e.getMessage());
|
||||
|
// throw (e instanceof PayException) ? (PayException) e : new PayException(e.getMessage(), e);
|
||||
|
// }
|
||||
|
// }
|
||||
|
//
|
||||
|
// /**
|
||||
|
// * post请求
|
||||
|
// */
|
||||
|
// public String postRequest(String requestStr, String url) throws PayException {
|
||||
|
// return postRequest(0,requestStr,url);
|
||||
|
// }
|
||||
|
//
|
||||
|
// /**
|
||||
|
// * post请求
|
||||
|
// */
|
||||
|
// public String postRequest(int retryTimes, String requestStr, String url) throws PayException {
|
||||
|
// String resultStr = "";
|
||||
|
// try {
|
||||
|
// HttpHeaders headers = new HttpHeaders();
|
||||
|
// //所有的请求需要用JSON格式发送
|
||||
|
// headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
// HttpEntity<Object> formEntity = new HttpEntity<>(requestStr, headers);
|
||||
|
// resultStr = restTemplate.postForObject(url, formEntity, String.class);
|
||||
|
// if (StringUtils.isBlank(resultStr) || !resultStr.startsWith("{")){
|
||||
|
// log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, resultStr);
|
||||
|
// throw new PayException("第三方返回格式有误!");
|
||||
|
// }
|
||||
|
// JSONObject resultObj = JSONObject.parseObject(resultStr);
|
||||
|
// String errNo = StringUtils.isNotBlank(resultObj.getString("err_no"))?resultObj.getString("err_no"):resultObj.getString("err_code");
|
||||
|
// if ("0".equals(errNo)) {
|
||||
|
// log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, resultObj, resultStr);
|
||||
|
// return resultStr;
|
||||
|
// }else {
|
||||
|
// throw convertDyException(GsonParser.parse(resultStr));
|
||||
|
// }
|
||||
|
// }catch (PayException e){
|
||||
|
// log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||
|
// if (retryTimes > MAX_RETRY_TIMES){
|
||||
|
// throw new PayException("抖音接口调用失败,已超过最大重试次数");
|
||||
|
// }
|
||||
|
// //如果是access_token过期,刷新token在重试
|
||||
|
// if ("40004".equals(e.getErrCode())){
|
||||
|
// String accessToken = refreshAccessToken();
|
||||
|
// JSONObject requestJsonObj = JSON.parseObject(requestStr);
|
||||
|
// requestJsonObj.put("access_token",accessToken);
|
||||
|
// requestStr = requestJsonObj.toJSONString();
|
||||
|
// }
|
||||
|
// retryTimes ++;
|
||||
|
// postRequest(retryTimes,requestStr,url);
|
||||
|
// }
|
||||
|
// return resultStr;
|
||||
|
// }
|
||||
|
//
|
||||
|
// /**
|
||||
|
// * 刷新access_token
|
||||
|
// */
|
||||
|
// public String refreshAccessToken() throws PayException {
|
||||
|
// return refreshAccessToken(0);
|
||||
|
// }
|
||||
|
//
|
||||
|
// /**
|
||||
|
// * 刷新access_token
|
||||
|
// * @param retryTimes
|
||||
|
// */
|
||||
|
// public String refreshAccessToken(int retryTimes) throws PayException {
|
||||
|
// String accessToken = "";
|
||||
|
// try {
|
||||
|
// accessToken = getAccessToken( true);
|
||||
|
// } catch (PayException e) {
|
||||
|
// if (retryTimes > MAX_RETRY_TIMES){
|
||||
|
// throw new PayException("抖音接口获取AccessToken调用失败,已超过最大重试次数");
|
||||
|
// }
|
||||
|
// retryTimes ++;
|
||||
|
// refreshAccessToken(retryTimes);
|
||||
|
// }
|
||||
|
// return accessToken;
|
||||
|
// }
|
||||
|
//
|
||||
|
// /**
|
||||
|
// * 转换异常
|
||||
|
// * @param jsonObject
|
||||
|
// * @return
|
||||
|
// */
|
||||
|
// private PayException convertDyException(JsonObject jsonObject) {
|
||||
|
// JsonElement codeElement = jsonObject.get("err_no") != null ? jsonObject.get("err_no"):jsonObject.get("err_code");
|
||||
|
// String code = codeElement == null ? null : codeElement.getAsString();
|
||||
|
// String message = jsonObject.get("error_msg").getAsString();
|
||||
|
// PayException payException = new PayException(message);
|
||||
|
// payException.setErrCode(code);
|
||||
|
// payException.setErrCodeDes(message);
|
||||
|
// return payException;
|
||||
|
// }
|
||||
|
//}
|
||||
Loading…
Reference in new issue