新增儲值成功和失敗接口

This commit is contained in:
liaozetao
2024-01-06 15:11:20 +08:00
parent aec67b669d
commit 62fd8f2cf7
4 changed files with 103 additions and 62 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
@@ -64,6 +65,7 @@ public class MyCardStrategy extends AbstractPayStrategy {
if (paymentArray.length > 1) {
itemCode = paymentArray[1];
}
String chargeRecordId = chargeRecord.getChargeRecordId();
Long uid = chargeRecord.getUid();
String productName = null != chargeProd.getChargeGoldNum() && chargeProd.getChargeGoldNum() > 0L ?
chargeProd.getChargeGoldNum() + "鉆石" :
@@ -71,10 +73,9 @@ public class MyCardStrategy extends AbstractPayStrategy {
String amount = BigDecimal.valueOf(chargeRecord.getLocalAmount()).divide(Constant.HUNDRED, 2, RoundingMode.HALF_UP).toString();
String currency = chargeRecord.getLocalCurrencyCode();
//获取myCard的授权码和H5支付页面地址
//h5贪图方便用get拼接参数的形式跳转后会叠加导致长度过长
String successUrl = CommonPayUtils.retainParams(context.getSuccessUrl(), "channelType", "deviceId", "countryCode");
String failureUrl = CommonPayUtils.retainParams(context.getFailureUrl(), "channelType", "deviceId", "countryCode");
AuthGlobalResp authGlobalResp = myCardService.authGlobal(chargeRecord.getChargeRecordId(), String.valueOf(uid), String.valueOf(uid), paymentType, itemCode, productName, amount, currency);
String successUrl = CommonPayUtils.retainParams(context.getSuccessUrl(), "channelType", "deviceId", "countryCode") + "&chargeRecordId=" + chargeRecordId;
String failureUrl = CommonPayUtils.retainParams(context.getFailureUrl(), "channelType", "deviceId", "countryCode") + "&chargeRecordId=" + chargeRecordId;
AuthGlobalResp authGlobalResp = myCardService.authGlobal(chargeRecordId, String.valueOf(uid), String.valueOf(uid), paymentType, itemCode, productName, amount, currency);
authGlobalResp.setSuccessUrl(successUrl);
authGlobalResp.setFailureUrl(failureUrl);
chargeRecord.setExtra(authGlobalResp.getAuthCode());

View File

@@ -10,7 +10,7 @@ import java.util.*;
@Slf4j
public class CommonPayUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* 构造sign内容
@@ -53,7 +53,7 @@ public class CommonPayUtils {
return MD5.getMD5(signSb.toString()).toUpperCase();
}
public static Map parseToMap(String json) {
public static Map<?, ?> parseToMap(String json) {
return parseToObject(json, Map.class);
}
@@ -61,7 +61,6 @@ public class CommonPayUtils {
try {
return objectMapper.readValue(json, toClass);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@@ -73,7 +72,6 @@ public class CommonPayUtils {
try {
return objectMapper.writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@@ -100,27 +98,26 @@ public class CommonPayUtils {
* @return
*/
public static LinkedHashMap<String, String> getParamsMap(String url) {
if (org.apache.commons.lang3.StringUtils.isBlank(url)) {
return null;
LinkedHashMap<String, String> paramsMap = new LinkedHashMap<String, String>();
if (StringUtils.isBlank(url)) {
return paramsMap;
}
url = url.trim();
int length = url.length();
int index = url.indexOf("?");
if (index > -1) {//url说明有问号
if ((length - 1) == index) {//url最后一个符号为http://wwww.baidu.com?
return null;
return paramsMap;
} else {
//情况为http://wwww.baidu.com?aa=11或http://wwww.baidu.com?aa=或http://wwww.baidu.com?aa
String baseUrl = url.substring(0, index);
String paramsString = url.substring(index + 1);
if (!org.apache.commons.lang3.StringUtils.isBlank(paramsString)) {
LinkedHashMap<String, String> paramsMap = new LinkedHashMap<String, String>();
if (!StringUtils.isBlank(paramsString)) {
String[] params = paramsString.split("&");
for (String param : params) {
if (!org.apache.commons.lang3.StringUtils.isBlank(param)) {
if (!StringUtils.isBlank(param)) {
String[] oneParam = param.split("=");
String paramName = oneParam[0];
if (!org.apache.commons.lang3.StringUtils.isBlank(paramName)) {
if (!StringUtils.isBlank(paramName)) {
if (oneParam.length > 1) {
paramsMap.put(paramName.trim(), oneParam[1]);//键可以去空格,值不能去空格
} else {
@@ -133,7 +130,7 @@ public class CommonPayUtils {
}
}
}
return null;
return paramsMap;
}
/**
@@ -144,7 +141,7 @@ public class CommonPayUtils {
* @return
*/
public static String retainParams(String url, String... paramNames) {
if (org.apache.commons.lang3.StringUtils.isBlank(url)) {
if (StringUtils.isBlank(url)) {
return "";
} else if (paramNames == null || paramNames.length < 1) {
return url.trim();
@@ -160,9 +157,9 @@ public class CommonPayUtils {
LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
LinkedHashMap<String, String> oldParamsMap = getParamsMap(url);
//删除参数
if (oldParamsMap != null && !oldParamsMap.isEmpty()) {
if (!oldParamsMap.isEmpty()) {
for (String paramName : paramNames) {
if (!org.apache.commons.lang3.StringUtils.isBlank(paramName)) {
if (!StringUtils.isBlank(paramName)) {
paramsMap.put(paramName, oldParamsMap.get(paramName));
}
}

View File

@@ -13,6 +13,7 @@ import com.accompany.core.exception.ServiceException;
import com.accompany.payment.constant.PayChannelConstant;
import com.accompany.payment.constant.PayConstant;
import com.accompany.payment.mapper.ChargeRecordMapper;
import com.accompany.payment.model.ChargeProd;
import com.accompany.payment.model.ChargeRecord;
import com.accompany.payment.model.ChargeRecordExample;
import com.accompany.payment.mycard.BaseResult;
@@ -23,6 +24,7 @@ import com.accompany.payment.mycard.dto.ReplenishDto;
import com.accompany.payment.mycard.resp.AuthGlobalResp;
import com.accompany.payment.mycard.resp.QueryOrderResp;
import com.accompany.payment.mycard.resp.TradeQueryResp;
import com.accompany.payment.service.ChargeProdService;
import com.accompany.payment.service.ChargeRecordService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@@ -63,6 +65,9 @@ public class MyCardBizServiceImpl implements MyCardBizService {
@Autowired
private ChargeRecordService chargeRecordService;
@Autowired
private ChargeProdService chargeProdService;
@Autowired
private ChargeService chargeService;
@@ -146,38 +151,45 @@ public class MyCardBizServiceImpl implements MyCardBizService {
if (!hash.equals(callback.getHash())) {
throw new ApiException("Hash校验失败");
}
String returnCode = callback.getReturnCode();
String chargeRecordId = callback.getFacTradeSeq();
String currency = callback.getCurrency();
String amount = callback.getAmount();
String myCardTradeNo = callback.getMyCardTradeNo();
if (!callback.isSuccess()) {
String returnMsg = callback.getReturnMsg();
try {
returnMsg = URLDecoder.decode(returnMsg, StandardCharsets.UTF_8.name());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
throw new ApiException(returnMsg);
}
String returnMsg = callback.getReturnMsg();
String payResult = callback.getPayResult();
if (StrUtil.isEmpty(payResult) || !payResult.equals(PAY_SUCCESS)) {
log.error("支付非成功状态, payResult : {}", payResult);
throw new ApiException("交易失敗");
try {
returnMsg = URLDecoder.decode(returnMsg, StandardCharsets.UTF_8.name());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
ChargeRecord chargeRecord = chargeRecordMapper.selectByPrimaryKey(chargeRecordId);
if (chargeRecord == null) {
throw new ApiException("交易失敗");
throw new ApiException("儲值失敗");
}
String metadata = chargeRecord.getMetadata();
if (StrUtil.isEmpty(metadata)) {
throw new ApiException("交易失敗");
throw new ApiException("儲值失敗");
}
AuthGlobalResp authGlobalResp = JSONObject.parseObject(metadata, AuthGlobalResp.class);
//更新订单
boolean isSuccess = updateOrder(chargeRecordId, currency, amount, myCardTradeNo);
authGlobalResp.setReturnCode(returnCode);
authGlobalResp.setReturnMsg(returnMsg);
//更新结果信息
chargeRecord.setMetadata(JSONObject.toJSONString(authGlobalResp));
chargeRecordMapper.updateByPrimaryKey(chargeRecord);
//回调链接
String successUrl = authGlobalResp.getSuccessUrl();
String failureUrl = authGlobalResp.getFailureUrl();
log.info("MyCard callback successUrl : {}, failureUrl : {}", successUrl, failureUrl);
if (!callback.isSuccess()) {
return failureUrl;
}
if (StrUtil.isEmpty(payResult) || !payResult.equals(PAY_SUCCESS)) {
log.error("支付非成功状态, payResult : {}", payResult);
return failureUrl;
}
//更新订单
boolean isSuccess = updateOrder(chargeRecordId, currency, amount, myCardTradeNo);
return isSuccess ? successUrl : failureUrl;
}
@@ -222,6 +234,8 @@ public class MyCardBizServiceImpl implements MyCardBizService {
log.error("【MyCard支付回调】订单 {} 不存在", chargeRecordId);
return false;
}
String chargeProdId = chargeRecord.getChargeProdId();
String country = chargeRecord.getCountry();
Byte chargeStatus = chargeRecord.getChargeStatus();
if (Constant.ChargeRecordStatus.finish.equals(chargeStatus)) {
log.info("订单 {} 已完成", chargeRecordId);
@@ -239,7 +253,13 @@ public class MyCardBizServiceImpl implements MyCardBizService {
log.error("【MyCard支付回调】订单 {} 金额 {} 校验失败", chargeRecordId, payAmount);
return false;
}
chargeRecord.setCountry("TW");
if (StrUtil.isEmpty(country)) {
ChargeProd chargeProd = chargeProdService.getChargeProdById(chargeProdId);
if (chargeProd != null) {
country = chargeProd.getCountry();
}
}
chargeRecord.setCountry(country);
chargeRecord.setLocalCurrencyCode(currency);
chargeRecord.setLocalAmount(payAmount);
chargeRecord.setPingxxChargeId(myCardTradeNo);

View File

@@ -1,63 +1,86 @@
package com.accompany.business.controller.charge;
import cn.hutool.core.util.StrUtil;
import com.accompany.business.common.BaseController;
import com.accompany.business.model.request.ChargeRecordQueryReq;
import com.accompany.common.constant.Constant;
import com.accompany.common.result.BusiResult;
import com.accompany.payment.model.ChargeRecord;
import com.accompany.payment.mycard.resp.AuthGlobalResp;
import com.accompany.payment.service.ChargeRecordService;
import com.accompany.payment.vo.ChargeRecordCountVo;
import com.accompany.payment.vo.ChargeRecordParentVo;
import com.accompany.payment.vo.ChargeRecordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;
@Controller
@Api(tags = "充值记录")
@Slf4j
@RestController
@RequestMapping("/chargeRecord")
public class ChargeRecordController extends BaseController{
private static final Logger logger = LoggerFactory.getLogger(ChargeRecordController.class);
public class ChargeRecordController extends BaseController {
@Autowired
ChargeRecordService chargeRecordService;
private ChargeRecordService chargeRecordService;
/**
* 通过时间,平台号通过特殊渠道获取对应的充值记录
*
* @param erbanNo
* @param startDateStr
* @param endDateStr
* @return
*/
@RequestMapping("/getSpecialList")
@ResponseBody
public ChargeRecordParentVo getUserChargeRecordFromSpecialChannel(Long erbanNo, String startDateStr, String endDateStr, Integer pageNum, Integer pageSize, String channel){
// if(erbanNo == null){
// return null;
// }
try{
public ChargeRecordParentVo getUserChargeRecordFromSpecialChannel(Long erbanNo, String startDateStr, String endDateStr, Integer pageNum, Integer pageSize, String channel) {
try {
List<ChargeRecordVo> list = chargeRecordService.getUserChargeRecordFromSpecialChannel(erbanNo, startDateStr, endDateStr, pageNum, pageSize, channel);
ChargeRecordParentVo chargeRecordParentVo = new ChargeRecordParentVo();
chargeRecordParentVo.setRows(list);
ChargeRecordCountVo chargeRecordCountVo = chargeRecordService.getChargeRecordCount(erbanNo, startDateStr, endDateStr, channel);
Integer total = chargeRecordCountVo.getCountNum();
Long ammount = chargeRecordCountVo.getAmount();
Long amount = chargeRecordCountVo.getAmount();
chargeRecordParentVo.setTotal(total);
chargeRecordParentVo.setAmmount(ammount);
chargeRecordParentVo.setAmmount(amount);
return chargeRecordParentVo;
}catch (Exception e){
logger.error("getUserChargeRecordFromSpecialChannel error",e);
} catch (Exception e) {
log.error("getUserChargeRecordFromSpecialChannel error", e);
return null;
}
}
//@ResponseBody
//@RequestMapping(value = "query")
//public BusiResult query(@Valid ChargeRecordQueryReq req) throws Exception {
// return chargeRecordService.query(req);
//}
/**
* 获取充值结果
*
* @param chargeRecordId
* @return
*/
@ApiOperation("获取充值结果")
@GetMapping("/getChargeResult")
public BusiResult<String> getChargeResult(@RequestParam("chargeRecordId") String chargeRecordId) {
ChargeRecord chargeRecord = chargeRecordService.getChargeRecordById(chargeRecordId);
if (chargeRecord == null) {
return BusiResult.fail("儲值失敗");
}
String paymentType = chargeRecord.getPaymentType();
String metadata = chargeRecord.getMetadata();
if (StrUtil.isNotEmpty(metadata)) {
if (Constant.ChargeChannel.my_card.equals(paymentType)) {
AuthGlobalResp authGlobalResp = JSONObject.parseObject(metadata, AuthGlobalResp.class);
if (!authGlobalResp.isSuccess()) {
return BusiResult.fail(authGlobalResp.getReturnMsg());
}
}
}
return BusiResult.success("儲值成功", chargeRecordId);
}
}