配置文件 :
<!-- 代扣策略工厂 -->
<bean id="withholdStrategyFactory" class="*******WithholdStrategyFactory" >
<property name="withholdStrategyMap" >
<map>
<entry key="default" value-ref="jydWithholdHandleService"></entry>
<entry key="1" value-ref="jydWithholdHandleService"></entry>
<entry key="2" value-ref="lsdWithholdHandleService"></entry>
<entry key="3" value-ref="yxdWithholdHandleService"></entry>
<entry key="4" value-ref="htPsWithholdHandleService"></entry>
</map>
</property>
</bean>
代码:
1.策略工厂:
public class WithholdStrategyFactory {
private Map<String, WithholdHandleService> withholdStrategyMap;
public Map<String, WithholdHandleService> getWithholdStrategyMap() {
return withholdStrategyMap;
}
public void setWithholdStrategyMap(Map<String, WithholdHandleService> withholdStrategyMap) {
this.withholdStrategyMap = withholdStrategyMap;
}
public WithholdHandleService getInstance(String productProvider) {
if (null == productProvider) {
throw new RuntimeException("资金方不能为空");
}
if (null == withholdStrategyMap.get(productProvider)) {
throw new RuntimeException("代扣未找到对应的实例bean:" + productProvider);
}
return withholdStrategyMap.get(productProvider);
}
}
2.接口
public interface WithholdHandleService {
/**
*代扣接口
*/
WithholdAcceptResult withHoldFusionpay(TradeOperFlowBean tradeFlow, IouBean iou, String signNo, ProviderPaymentParams params);
}
3.具体运用:
withholdStrategyFactory.getInstance(params.getProductProvider()).withHoldFusionpay(tradeFlow, iou, signNo,params);
4.具体实现代码的实现 :
public class JydWithholdHandleServiceImpl implements WithholdHandleService {
public WithholdAcceptResult withHoldFusionpay(TradeOperFlowBean tradeFlow, IouBean iou, String signNo,
ProviderPaymentParams params) {
iou.getIouId(), signNo, JsonUtil.toJSONString(params));
WithholdAcceptResult r = null;
try {
WithholdRequestParam payRequestParam = createWithholdRequestParam(tradeFlow, iou, signNo, params);
if (StringUtils.isBlank(payRequestParam.getSign())) {
r = WithholdAcceptResult.fail(ErrCode.PARAM_ERR.name(), "sign异常,发送代扣请求终止");
}
logger.info("http代扣请求入参:{}", JsonUtil.toJSONString(payRequestParam));
PayBizResponse res = paymentHttpService.withHoldFusionpay(payRequestParam);
logger.info("http代扣请求结果:{}", JsonUtil.toJSONString(res));
tradeFlow.setReturnMsg(res.getRetMsg());
if (PayResult.SUCCESS.getCode().equals(res.getRetCode())) {
// 受理成功
updateTradeFlowToProcessing(tradeFlow);
r = WithholdAcceptResult.ok("受理成功");
} else {
// 受理失败
updateTradeFlowToFailure(tradeFlow);
r = WithholdAcceptResult.fail(ErrCode.WITHHOLD_NOT_ACCEPTED.name(), "http代扣请求受理失败");
}
} catch (Exception e) {
String msg = e.getMessage();
if (msg != null && msg.length() > 256) {
msg = msg.substring(0, 256);
}
tradeFlow.setRemarks(msg);
updateTradeFlowToRetry(tradeFlow);
r = WithholdAcceptResult.ok("处理中");
} finally {
iouMapper.increaseVersion(iou.getIouId());
}
return r;
}
}