解决回调的部分,改为策略模式
This commit is contained in:
parent
feb38f18c3
commit
f5202904f9
|
|
@ -2,7 +2,7 @@ package com.ghy.web.controller.customer;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.ghy.common.adapay.AdapayConfig;
|
import com.ghy.common.adapay.AdapayConfig;
|
||||||
import com.ghy.common.adapay.AdapayService;
|
import com.ghy.payment.service.AdapayService;
|
||||||
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
||||||
import com.ghy.common.adapay.model.Merchant;
|
import com.ghy.common.adapay.model.Merchant;
|
||||||
import com.ghy.common.core.domain.AjaxResult;
|
import com.ghy.common.core.domain.AjaxResult;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
package com.ghy.common.adapay.callback;
|
package com.ghy.web.controller.pay;
|
||||||
|
|
||||||
import com.ghy.common.adapay.callback.mapping.DrawCashReplyMapping;
|
import com.ghy.common.adapay.callback.Event;
|
||||||
import com.ghy.common.adapay.callback.mapping.PayReplyMapping;
|
|
||||||
import com.ghy.common.adapay.callback.mapping.RefundReplyMapping;
|
|
||||||
import com.ghy.common.core.controller.BaseController;
|
import com.ghy.common.core.controller.BaseController;
|
||||||
|
import com.ghy.common.utils.spring.SpringUtils;
|
||||||
|
import com.ghy.payment.service.CallBackService;
|
||||||
import com.huifu.adapay.core.AdapayCore;
|
import com.huifu.adapay.core.AdapayCore;
|
||||||
import com.huifu.adapay.core.util.AdapaySign;
|
import com.huifu.adapay.core.util.AdapaySign;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
@ -19,6 +19,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RestController
|
@RestController
|
||||||
public class AdapayCallbackController extends BaseController {
|
public class AdapayCallbackController extends BaseController {
|
||||||
|
|
||||||
|
private CallBackService callBackService;
|
||||||
|
|
||||||
@PostMapping("/adapay/callback")
|
@PostMapping("/adapay/callback")
|
||||||
public String callback(Event event) {
|
public String callback(Event event) {
|
||||||
logger.info(" 收单返回消息event" + event);
|
logger.info(" 收单返回消息event" + event);
|
||||||
|
|
@ -36,26 +38,31 @@ public class AdapayCallbackController extends BaseController {
|
||||||
if (verifySign) {
|
if (verifySign) {
|
||||||
//Event事件类型
|
//Event事件类型
|
||||||
String type = event.getType();
|
String type = event.getType();
|
||||||
|
String beanName;
|
||||||
if (StringUtils.isBlank(type)) {
|
if (StringUtils.isBlank(type)) {
|
||||||
return "NG";
|
return "NG";
|
||||||
}
|
}
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "payment.succeeded":
|
case "payment.succeeded":
|
||||||
case "payment.failed":
|
case "payment.failed":
|
||||||
PayReplyMapping.putReply(event);
|
beanName = "payCallBackService";
|
||||||
break;
|
break;
|
||||||
case "payment.close.succeeded":
|
case "payment.close.succeeded":
|
||||||
case "payment.close.failed":
|
case "payment.close.failed":
|
||||||
RefundReplyMapping.putReply(event);
|
beanName = "refundCallBackService";
|
||||||
break;
|
break;
|
||||||
case "cash.succeeded":
|
case "cash.succeeded":
|
||||||
case "cash.failed":
|
case "cash.failed":
|
||||||
DrawCashReplyMapping.putReply(event);
|
beanName = "drawCashCallBackService";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
logger.warn("UNKNOWN EVENT TYPE [{}]", type);
|
logger.warn("UNKNOWN EVENT TYPE [{}]", type);
|
||||||
return "NG";
|
return "NG";
|
||||||
}
|
}
|
||||||
|
logger.info("handler bean is {} ", beanName);
|
||||||
|
|
||||||
|
callBackService = SpringUtils.getBean(beanName);
|
||||||
|
callBackService.onCallback(event);
|
||||||
return "OK";
|
return "OK";
|
||||||
}
|
}
|
||||||
logger.warn("签名验证失败 data=[{}], sign=[{}]", data, sign);
|
logger.warn("签名验证失败 data=[{}], sign=[{}]", data, sign);
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package com.ghy.web.controller.pay;
|
package com.ghy.web.controller.pay;
|
||||||
|
|
||||||
import com.ghy.common.adapay.AdapayService;
|
import com.ghy.payment.service.AdapayService;
|
||||||
import com.ghy.common.adapay.model.PayParam;
|
import com.ghy.common.adapay.model.PayParam;
|
||||||
import com.ghy.common.core.controller.BaseController;
|
import com.ghy.common.core.controller.BaseController;
|
||||||
import com.ghy.common.core.domain.AjaxResult;
|
import com.ghy.common.core.domain.AjaxResult;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package com.ghy.web.controller.pay;
|
package com.ghy.web.controller.pay;
|
||||||
|
|
||||||
import com.ghy.common.adapay.AdapayService;
|
import com.ghy.payment.service.AdapayService;
|
||||||
import com.ghy.common.adapay.model.PayParam;
|
import com.ghy.common.adapay.model.PayParam;
|
||||||
import com.ghy.common.adapay.model.WxpayExpend;
|
import com.ghy.common.adapay.model.WxpayExpend;
|
||||||
import com.ghy.common.config.WxConfig;
|
import com.ghy.common.config.WxConfig;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package com.ghy.web.controller.worker;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.ghy.common.adapay.AdapayConfig;
|
import com.ghy.common.adapay.AdapayConfig;
|
||||||
import com.ghy.common.adapay.AdapayService;
|
import com.ghy.payment.service.AdapayService;
|
||||||
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
||||||
import com.ghy.common.adapay.model.Merchant;
|
import com.ghy.common.adapay.model.Merchant;
|
||||||
import com.ghy.common.core.controller.BaseController;
|
import com.ghy.common.core.controller.BaseController;
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,11 @@ public class DrawCashReplyMapping {
|
||||||
*/
|
*/
|
||||||
private final static ConcurrentHashMap<String, IDrawCashCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);
|
private final static ConcurrentHashMap<String, IDrawCashCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);
|
||||||
|
|
||||||
public static void putReply(Event event) {
|
public static void putReply(Event event, IDrawCashCallback callback) {
|
||||||
String data = event.getData();
|
String data = event.getData();
|
||||||
DrawCashReply reply = JSON.parseObject(data, DrawCashReply.class);
|
DrawCashReply reply = JSON.parseObject(data, DrawCashReply.class);
|
||||||
Assert.hasText(reply.getOrderNo(), "orderNo is blank !!!");
|
Assert.hasText(reply.getOrderNo(), "orderNo is blank !!!");
|
||||||
IDrawCashCallback callback = PAY_RESULT_CALLBACK_MAP.remove(reply.getOrderNo());
|
PAY_RESULT_CALLBACK_MAP.remove(reply.getOrderNo());
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.onCallback(reply);
|
callback.onCallback(reply);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,12 +26,12 @@ public class PayReplyMapping {
|
||||||
*/
|
*/
|
||||||
private final static ConcurrentHashMap<String, IPayCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);
|
private final static ConcurrentHashMap<String, IPayCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);
|
||||||
|
|
||||||
public static void putReply(Event event) {
|
public static void putReply(Event event, IPayCallback callback) {
|
||||||
String data = event.getData();
|
String data = event.getData();
|
||||||
PayReply payment = JSON.parseObject(data, PayReply.class);
|
PayReply payment = JSON.parseObject(data, PayReply.class);
|
||||||
Assert.hasText(payment.getOrderNo(), "orderNo is blank !!!");
|
Assert.hasText(payment.getOrderNo(), "orderNo is blank !!!");
|
||||||
payment.setPayTime(event.getCreatedTime());
|
payment.setPayTime(event.getCreatedTime());
|
||||||
IPayCallback callback = PAY_RESULT_CALLBACK_MAP.remove(payment.getOrderNo());
|
PAY_RESULT_CALLBACK_MAP.remove(payment.getOrderNo());
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.onCallback(payment);
|
callback.onCallback(payment);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ public class RefundReplyMapping {
|
||||||
*/
|
*/
|
||||||
private final static ConcurrentHashMap<String, IRefundCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);
|
private final static ConcurrentHashMap<String, IRefundCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);
|
||||||
|
|
||||||
public static void putReply(Event event) {
|
public static void putReply(Event event, IRefundCallback refundCallback) {
|
||||||
String data = event.getData();
|
String data = event.getData();
|
||||||
RefundReply payment = JSON.parseObject(data, RefundReply.class);
|
RefundReply payment = JSON.parseObject(data, RefundReply.class);
|
||||||
Assert.hasText(payment.getPaymentId(), "paymentId is blank !!!");
|
Assert.hasText(payment.getPaymentId(), "paymentId is blank !!!");
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
package com.ghy.common.adapay;
|
package com.ghy.payment.service;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ghy.common.adapay.AdapayConfig;
|
||||||
|
import com.ghy.common.adapay.AdapayProperties;
|
||||||
import com.ghy.common.adapay.callback.IDrawCashCallback;
|
import com.ghy.common.adapay.callback.IDrawCashCallback;
|
||||||
import com.ghy.common.adapay.callback.IPayCallback;
|
import com.ghy.common.adapay.callback.IPayCallback;
|
||||||
import com.ghy.common.adapay.callback.IRefundCallback;
|
import com.ghy.common.adapay.callback.IRefundCallback;
|
||||||
import com.ghy.common.adapay.callback.mapping.DrawCashReplyMapping;
|
import com.ghy.common.adapay.callback.mapping.DrawCashReplyMapping;
|
||||||
import com.ghy.common.adapay.callback.mapping.PayReplyMapping;
|
|
||||||
import com.ghy.common.adapay.callback.mapping.RefundReplyMapping;
|
import com.ghy.common.adapay.callback.mapping.RefundReplyMapping;
|
||||||
import com.ghy.common.adapay.model.*;
|
import com.ghy.common.adapay.model.*;
|
||||||
|
import com.ghy.payment.service.impl.PayCallBackServiceImpl;
|
||||||
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
||||||
import com.huifu.adapay.model.*;
|
import com.huifu.adapay.model.*;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
@ -33,6 +35,8 @@ public class AdapayService {
|
||||||
@Resource
|
@Resource
|
||||||
private IPayCallback payCallback;
|
private IPayCallback payCallback;
|
||||||
@Resource
|
@Resource
|
||||||
|
private PayCallBackServiceImpl payCallBackService;
|
||||||
|
@Resource
|
||||||
private IDrawCashCallback drawCashCallback;
|
private IDrawCashCallback drawCashCallback;
|
||||||
@Resource
|
@Resource
|
||||||
private IRefundCallback refundCallback;
|
private IRefundCallback refundCallback;
|
||||||
|
|
@ -289,8 +293,7 @@ public class AdapayService {
|
||||||
paymentParams.put("expend", expend);
|
paymentParams.put("expend", expend);
|
||||||
logger.debug("paymentParams: {}", paymentParams.toJSONString());
|
logger.debug("paymentParams: {}", paymentParams.toJSONString());
|
||||||
JSONObject response = (JSONObject) Payment.create(paymentParams, deptId.toString());
|
JSONObject response = (JSONObject) Payment.create(paymentParams, deptId.toString());
|
||||||
payCallback.onResponse(response);
|
payCallBackService.onResponse(response);
|
||||||
PayReplyMapping.putCallback(payParam.getOrderNo(), payCallback);
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.ghy.payment.service;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ghy.common.adapay.callback.Event;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
public interface CallBackService {
|
||||||
|
|
||||||
|
public final static ConcurrentMap<String, List<String>> resultCallBackMap = new ConcurrentHashMap<>(1024);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param event 回调
|
||||||
|
*/
|
||||||
|
void onCallback(Event event);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param response 同步响应
|
||||||
|
*/
|
||||||
|
void onResponse(JSONObject response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.ghy.payment.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ghy.common.adapay.callback.Event;
|
||||||
|
import com.ghy.payment.service.CallBackService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@Service("drawCashCallBackService")
|
||||||
|
public class DrawCashCallBackServiceImpl implements CallBackService {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(DrawCashCallBackServiceImpl.class);
|
||||||
|
|
||||||
|
private final static List<String> drawCashList = new ArrayList<>(1024);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时保存支付结果
|
||||||
|
* key: orderNo
|
||||||
|
* value: 处理支付结果的回调接口
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCallback(Event event) {
|
||||||
|
logger.info("draw callback is {}", event.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(JSONObject response) {
|
||||||
|
logger.info("draw response is {}", response.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.ghy.payment.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ghy.common.adapay.callback.Event;
|
||||||
|
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
||||||
|
import com.ghy.common.adapay.model.PayReply;
|
||||||
|
import com.ghy.common.adapay.model.PaymentDTO;
|
||||||
|
import com.ghy.payment.domain.FinancialMaster;
|
||||||
|
import com.ghy.payment.service.CallBackService;
|
||||||
|
import com.ghy.payment.service.FinancialMasterService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service("payCallBackService")
|
||||||
|
public class PayCallBackServiceImpl implements CallBackService {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(PayCallBackServiceImpl.class);
|
||||||
|
|
||||||
|
private final static List<String> payList = new ArrayList<>(1024);
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
FinancialMasterService financialMasterService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCallback(Event event) {
|
||||||
|
String data = event.getData();
|
||||||
|
PayReply payment = JSON.parseObject(data, PayReply.class);
|
||||||
|
Assert.hasText(payment.getOrderNo(), "orderNo is blank !!!");
|
||||||
|
// 校验是否是本系统发出去的回调请求
|
||||||
|
if(payList.contains(payment.getOrderNo())){
|
||||||
|
// 更新交易记录
|
||||||
|
PaymentDTO param = new PaymentDTO();
|
||||||
|
param.setId(payment.getId());
|
||||||
|
param.setStatus(payment.getStatus());
|
||||||
|
param.setPayTime(payment.getPayTime());
|
||||||
|
financialMasterService.updatePayment(param);
|
||||||
|
|
||||||
|
if (AdapayStatusEnum.succeeded.code.equals(payment.getStatus())) {
|
||||||
|
// 修改主财务单状态
|
||||||
|
financialMasterService.paySucceeded(payment.getOrderNo(), payment.getPayChannel());
|
||||||
|
} else {
|
||||||
|
logger.warn("支付失败 : {}", payment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info("pay callback is {}", event.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(JSONObject response) {
|
||||||
|
// 保存一条支付记录
|
||||||
|
PaymentDTO payment = response.toJavaObject(PaymentDTO.class);
|
||||||
|
payment.setStatus("pending");
|
||||||
|
financialMasterService.insertPayment(payment);
|
||||||
|
|
||||||
|
if (AdapayStatusEnum.succeeded.code.equals(response.getString("status"))) {
|
||||||
|
FinancialMaster update = new FinancialMaster();
|
||||||
|
update.setOrderMasterCode(response.getString("order_no"));
|
||||||
|
update.setPaymentId(response.getString("id"));
|
||||||
|
financialMasterService.updateFinancialMaster(update);
|
||||||
|
} else {
|
||||||
|
logger.warn("请求支付失败 : {}", response.toJSONString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将记录保存到临时会话中
|
||||||
|
payList.add(response.getString("order_no"));
|
||||||
|
resultCallBackMap.put("pay", payList);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.ghy.payment.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ghy.common.adapay.callback.Event;
|
||||||
|
import com.ghy.payment.service.CallBackService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service("refundCallBackService")
|
||||||
|
public class RefundCallBackServiceImpl implements CallBackService {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(RefundCallBackServiceImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCallback(Event event) {
|
||||||
|
logger.info("refund callback is {}", event.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(JSONObject response) {
|
||||||
|
logger.info("refund response is {}", response.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue