CashierCmd.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.java110.acct.cmd.payment;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.acct.payment.IPaymentBusiness;
  4. import com.java110.acct.payment.IPaymentFactoryAdapt;
  5. import com.java110.core.annotation.Java110Cmd;
  6. import com.java110.core.context.ICmdDataFlowContext;
  7. import com.java110.core.event.cmd.Cmd;
  8. import com.java110.core.event.cmd.CmdEvent;
  9. import com.java110.core.log.LoggerFactory;
  10. import com.java110.doc.annotation.*;
  11. import com.java110.dto.fee.FeeDto;
  12. import com.java110.dto.fee.PayFeeDto;
  13. import com.java110.dto.payment.PaymentOrderDto;
  14. import com.java110.dto.paymentPool.PaymentPoolDto;
  15. import com.java110.dto.paymentPoolConfig.PaymentPoolConfigDto;
  16. import com.java110.intf.acct.IPaymentPoolConfigV1InnerServiceSMO;
  17. import com.java110.intf.acct.IPaymentPoolV1InnerServiceSMO;
  18. import com.java110.intf.fee.IPayFeeV1InnerServiceSMO;
  19. import com.java110.utils.cache.CommonCache;
  20. import com.java110.utils.cache.MappingCache;
  21. import com.java110.utils.constant.MappingConstant;
  22. import com.java110.utils.constant.WechatConstant;
  23. import com.java110.utils.exception.CmdException;
  24. import com.java110.utils.factory.ApplicationContextFactory;
  25. import com.java110.utils.util.Assert;
  26. import com.java110.utils.util.StringUtil;
  27. import org.slf4j.Logger;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.http.HttpStatus;
  30. import org.springframework.http.ResponseEntity;
  31. import java.text.ParseException;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * 统一支付接口
  36. */
  37. @Java110Cmd(serviceCode = "payment.cashier")
  38. public class CashierCmd extends Cmd {
  39. private static final Logger logger = LoggerFactory.getLogger(CashierCmd.class);
  40. protected static final String DEFAULT_PAYMENT_ADAPT = "wechatPaymentFactory";// 默认微信通用支付
  41. @Autowired
  42. private IPaymentPoolConfigV1InnerServiceSMO paymentPoolConfigV1InnerServiceSMOImpl;
  43. @Autowired
  44. private IPaymentPoolV1InnerServiceSMO paymentPoolV1InnerServiceSMOImpl;
  45. @Autowired
  46. private IPayFeeV1InnerServiceSMO payFeeV1InnerServiceSMOImpl;
  47. /**
  48. * 校验
  49. *
  50. * @param event 事件对象
  51. * @param context 请求报文数据
  52. * @param reqJson
  53. * @throws CmdException
  54. */
  55. @Override
  56. public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
  57. Assert.hasKeyAndValue(reqJson, "business", "未包含业务");
  58. Assert.hasKeyAndValue(reqJson, "cashierUserId", "未包含收银人员");
  59. Assert.hasKeyAndValue(reqJson, "openId", "未包含openId");
  60. Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区ID");
  61. context.getReqHeaders().put("user-id", reqJson.getString("cashierUserId"));
  62. }
  63. @Override
  64. public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
  65. logger.debug(">>>>>>>>>>>>>>>>支付参数报文,{}", reqJson.toJSONString());
  66. String appId = context.getReqHeaders().get("app-id");
  67. String userId = reqJson.getString("cashierUserId");
  68. //1.0 查询当前支付的业务
  69. IPaymentBusiness paymentBusiness = ApplicationContextFactory.getBean(reqJson.getString("business"), IPaymentBusiness.class);
  70. if (paymentBusiness == null) {
  71. throw new CmdException("当前支付业务不支持");
  72. }
  73. //2.0 相应业务 下单 返回 单号 ,金额,
  74. PaymentOrderDto paymentOrderDto = paymentBusiness.unified(context, reqJson);
  75. paymentOrderDto.setAppId(appId);
  76. paymentOrderDto.setUserId(userId);
  77. logger.debug(">>>>>>>>>>>>>>>>支付业务下单返回,{}", JSONObject.toJSONString(paymentOrderDto));
  78. String env = MappingCache.getValue(MappingConstant.ENV_DOMAIN, "HC_ENV");
  79. // 这里 演示环境不向微信下单
  80. if ("DEV".equals(env) || "TEST".equals(env)) {
  81. paymentBusiness.notifyPayment(paymentOrderDto, reqJson);
  82. JSONObject param = new JSONObject();
  83. param.put("code", "100");
  84. param.put("msg", "演示环境不触发支付");
  85. context.setResponseEntity(new ResponseEntity(JSONObject.toJSONString(param), HttpStatus.OK));
  86. return;
  87. }
  88. // 3.0 如果支付金额为0 直接调用 支付完通知接口
  89. if (paymentOrderDto.getMoney() <= 0) {
  90. paymentBusiness.notifyPayment(paymentOrderDto, reqJson);
  91. JSONObject param = new JSONObject();
  92. param.put("code", "100");
  93. param.put("msg", "扣费为0回调成功");
  94. context.setResponseEntity(new ResponseEntity(JSONObject.toJSONString(param), HttpStatus.OK));
  95. return;
  96. }
  97. // todo 3.0 寻找当前支付适配器
  98. String payAdapt = computeAdapt(reqJson.getString("business"), reqJson);
  99. //MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAYMENT_ADAPT);
  100. payAdapt = StringUtil.isEmpty(payAdapt) ? DEFAULT_PAYMENT_ADAPT : payAdapt;
  101. if (reqJson.containsKey("payAdapt") && !StringUtil.isEmpty(reqJson.getString("payAdapt"))) {
  102. payAdapt = reqJson.getString("payAdapt");
  103. }
  104. IPaymentFactoryAdapt tPayAdapt = ApplicationContextFactory.getBean(payAdapt, IPaymentFactoryAdapt.class);
  105. // 4.0 相应支付厂家下单
  106. Map result = null;
  107. try {
  108. result = tPayAdapt.java110Payment(paymentOrderDto, reqJson, context);
  109. } catch (Exception e) {
  110. logger.error("支付异常", e);
  111. throw new CmdException(e.getLocalizedMessage());
  112. }
  113. ResponseEntity<String> responseEntity = new ResponseEntity(JSONObject.toJSONString(result), HttpStatus.OK);
  114. logger.debug("调用支付厂家返回,{}", responseEntity);
  115. context.setResponseEntity(responseEntity);
  116. // redis 中 保存 请求参数
  117. CommonCache.setValue("unifiedPayment_" + paymentOrderDto.getOrderId(), reqJson.toJSONString(), CommonCache.PAY_DEFAULT_EXPIRE_TIME);
  118. }
  119. /**
  120. * 计算适配器
  121. *
  122. * @param business
  123. * @param reqJson
  124. * @return
  125. */
  126. private String computeAdapt(String business, JSONObject reqJson) {
  127. String communityId = reqJson.getString("communityId");
  128. //todo 如果是单个费用缴费
  129. PaymentPoolDto paymentPoolDto = ifPayFeeBusiness(business, reqJson);
  130. if (paymentPoolDto != null) {
  131. reqJson.put("paymentPoolId", paymentPoolDto.getPpId());
  132. return paymentPoolDto.getBeanJsapi();
  133. }
  134. //todo 如果是临时车
  135. paymentPoolDto = ifTempCarFeeBusiness(business, communityId);
  136. if (paymentPoolDto != null) {
  137. reqJson.put("paymentPoolId", paymentPoolDto.getPpId());
  138. return paymentPoolDto.getBeanJsapi();
  139. }
  140. //todo 按小区查询 支付信息
  141. paymentPoolDto = new PaymentPoolDto();
  142. paymentPoolDto.setCommunityId(communityId);
  143. paymentPoolDto.setPayType(PaymentPoolDto.PAY_TYPE_COMMUNITY);
  144. List<PaymentPoolDto> paymentPoolDtos = paymentPoolV1InnerServiceSMOImpl.queryPaymentPools(paymentPoolDto);
  145. if (paymentPoolDtos == null || paymentPoolDtos.isEmpty()) {
  146. throw new IllegalArgumentException("小区未配置支付信息");
  147. }
  148. reqJson.put("paymentPoolId", paymentPoolDtos.get(0).getPpId());
  149. return paymentPoolDtos.get(0).getBeanJsapi();
  150. }
  151. /**
  152. * 临时车场景处理
  153. *
  154. * @param business
  155. * @param communityId
  156. * @return
  157. */
  158. private PaymentPoolDto ifTempCarFeeBusiness(String business, String communityId) {
  159. if (!"tempCarFee".equals(business)) {
  160. return null;
  161. }
  162. //todo 按小区查询 支付信息
  163. PaymentPoolDto paymentPoolDto = new PaymentPoolDto();
  164. paymentPoolDto.setCommunityId(communityId);
  165. paymentPoolDto.setPayType(PaymentPoolDto.PAY_TYPE_TEMP_CAT);
  166. List<PaymentPoolDto> paymentPoolDtos = paymentPoolV1InnerServiceSMOImpl.queryPaymentPools(paymentPoolDto);
  167. if (paymentPoolDtos == null || paymentPoolDtos.isEmpty()) {
  168. return null;
  169. }
  170. return paymentPoolDtos.get(0);
  171. }
  172. private PaymentPoolDto ifPayFeeBusiness(String business, JSONObject reqJson) {
  173. String feeId = "";
  174. if (!"payFee".equals(business) || !reqJson.containsKey("feeId")) {
  175. return null;
  176. }
  177. feeId = reqJson.getString("feeId");
  178. if (StringUtil.isNumber(feeId)) {
  179. return null;
  180. }
  181. PayFeeDto feeDto = new PayFeeDto();
  182. feeDto.setFeeId(feeId);
  183. feeDto.setCommunityId(reqJson.getString("communityId"));
  184. List<PayFeeDto> feeDtos = payFeeV1InnerServiceSMOImpl.queryPayFees(feeDto);
  185. if (feeDtos == null || feeDtos.isEmpty()) {
  186. return null;
  187. }
  188. PaymentPoolConfigDto paymentPoolConfigDto = new PaymentPoolConfigDto();
  189. paymentPoolConfigDto.setConfigId(feeDtos.get(0).getConfigId());
  190. paymentPoolConfigDto.setCommunityId(feeDtos.get(0).getCommunityId());
  191. List<PaymentPoolConfigDto> paymentPoolConfigDtos = paymentPoolConfigV1InnerServiceSMOImpl.queryPaymentPoolConfigs(paymentPoolConfigDto);
  192. if (paymentPoolConfigDtos == null || paymentPoolConfigDtos.isEmpty()) {
  193. return null;
  194. }
  195. PaymentPoolDto paymentPoolDto = new PaymentPoolDto();
  196. paymentPoolDto.setPpId(paymentPoolConfigDtos.get(0).getPpId());
  197. paymentPoolDto.setCommunityId(paymentPoolConfigDtos.get(0).getCommunityId());
  198. paymentPoolDto.setPayType(PaymentPoolDto.PAY_TYPE_FEE_CONFIG);
  199. List<PaymentPoolDto> paymentPoolDtos = paymentPoolV1InnerServiceSMOImpl.queryPaymentPools(paymentPoolDto);
  200. if (paymentPoolDtos == null || paymentPoolDtos.isEmpty()) {
  201. return null;
  202. }
  203. return paymentPoolDtos.get(0);
  204. }
  205. }