IEF001Service.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package com.miniframe.bisiness.system;
  2. import com.google.gson.Gson;
  3. import com.miniframe.service.mq.XIMQPayTimeoutQueue;
  4. import com.miniframe.spring.mq.MFMqUtils;
  5. import com.miniframe.core.ExecProcessFlow;
  6. import com.miniframe.core.exception.BusinessException;
  7. import com.miniframe.core.ext.UtilTools;
  8. import com.miniframe.generate.appcode.OrderStatus;
  9. import com.miniframe.generate.appcode.PayType;
  10. import com.miniframe.generate.appcode.Paystatus;
  11. import com.miniframe.generate.business.system.model.IEF001BaseModel;
  12. import com.miniframe.generate.business.pay.model.XP1001BaseModel;
  13. import com.miniframe.generate.comm.httppayapater.XP1001Client;
  14. import com.miniframe.generate.comm.pay.A_PAYHEAD;
  15. import com.miniframe.generate.comm.pay.A_XP1001;
  16. import com.miniframe.generate.comm.pay.D_XP1001;
  17. import com.miniframe.model.system.GeneralSet;
  18. import com.miniframe.model.system.Orders;
  19. import com.miniframe.model.system.Pay;
  20. import com.miniframe.model.system.SysUser;
  21. import com.miniframe.model.system.dao.GeneralSetMapper;
  22. import com.miniframe.model.system.dao.OrdersMapper;
  23. import com.miniframe.model.system.dao.PayMapper;
  24. import com.miniframe.model.system.dao.SysUserMapper;
  25. import com.miniframe.tools.XIDateTimeUtils;
  26. import org.json.JSONObject;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. /**
  32. * 基础系统,“支付下单”逻辑处理(重新生成不覆盖)。
  33. */
  34. public class IEF001Service extends IEF001BaseModel implements ExecProcessFlow {
  35. private static final long serialVersionUID = -7051358269847459502L;
  36. private static final Logger logger = LoggerFactory.getLogger(IEF001Service.class);
  37. /**
  38. * 基础系统,“支付下单”业务核心处理
  39. */
  40. public void transExecute() throws Exception {
  41. String payType = this.getA_ief001().getPayType();
  42. String payDesc = this.getA_ief001().getPayDesc();
  43. String orderId = this.getA_ief001().getOrderId();
  44. PayMapper payMapper = UtilTools.getBean(PayMapper.class);
  45. OrdersMapper ordersMapper = UtilTools.getBean(OrdersMapper.class);
  46. Orders orderInfo = ordersMapper.selectByPrimaryKey(orderId);
  47. if (orderInfo == null) {
  48. throw new BusinessException("EB2100008");
  49. }
  50. if (!orderInfo.getStatus().equals(OrderStatus.apply.getIndex())) {
  51. throw new BusinessException("EB2100009");
  52. }
  53. if (UtilTools.isNotNullAndBlank(orderInfo.getPayId())) {
  54. Pay payForOrder = payMapper.selectByPrimaryKey(orderInfo.getId());
  55. //只要不是支付成功的都可以再对该单申请支付
  56. if (payForOrder != null && payForOrder.getStatus().equals(Paystatus.paysuccess.getIndex())) {
  57. throw new BusinessException("EB2100009");
  58. }
  59. }
  60. String allAmount = orderInfo.getTotalAmount();
  61. allAmount = "0.01";//测试用
  62. Float allAmountYuan = Float.parseFloat(allAmount); //数值
  63. XP1001BaseModel model = new XP1001BaseModel();
  64. XP1001Client client = new XP1001Client();
  65. // 报文头
  66. A_PAYHEAD head = new A_PAYHEAD();
  67. head.setChannelNo("xipayment");
  68. head.setTransCode("XP1001");
  69. head.setClientToken(getA_systemhead().getClientToken());
  70. model.setA_payhead(head);
  71. // 报文体
  72. A_XP1001 a_xp1001 = new A_XP1001();
  73. a_xp1001.setUserId(getA_systemhead().getUserId());
  74. SysUserMapper userInfoMapper = UtilTools.getBean(SysUserMapper.class);
  75. SysUser userInfo = userInfoMapper.selectByPrimaryKey(getA_systemhead().getUserId());
  76. a_xp1001.setMobileNumber(userInfo.getUsermobnub());
  77. a_xp1001.setPackageName("com.xitech.xingben");
  78. // 拼接json参数
  79. String payTypeName = PayType.getPayType(payType).getName();
  80. String notify_url = "";
  81. try {
  82. if (payTypeName.contains(PayType.alipay.getName())) {
  83. GeneralSet generalSet = UtilTools.getBean(GeneralSetMapper.class).selectByPrimaryKey("pay_alipay_notify_url");
  84. if (generalSet != null) {
  85. notify_url = generalSet.getParamValue();
  86. }
  87. } else if (payTypeName.contains(PayType.wechatpay.getName())) {
  88. GeneralSet generalSet = UtilTools.getBean(GeneralSetMapper.class).selectByPrimaryKey("pay_wechat_notify_url");
  89. if (generalSet != null) {
  90. notify_url = generalSet.getParamValue();
  91. }
  92. }
  93. } catch (Exception e) {
  94. }
  95. JSONObject jsonObject = new JSONObject();
  96. jsonObject.put("notify_url", notify_url);
  97. if (payType.equals(PayType.wechatpayProgram.getIndex())) {// 1: 微信小程序支付
  98. // jsonObject.put("openid", userInfo.getOpenId());
  99. a_xp1001.setServerChannel("02");
  100. jsonObject.put("total_fee", allAmountYuan * 100);
  101. jsonObject.put("body", payDesc);
  102. jsonObject.put("attach", orderId);
  103. jsonObject.put("trade_type", "JSAPI");
  104. jsonObject.put("attach", PayType.getPayType(payType).getName());
  105. } else if (payType.equals(PayType.wechatpayApp.getIndex())) {// 微信APP支付
  106. a_xp1001.setServerChannel("02");
  107. jsonObject.put("total_fee", allAmountYuan * 100);
  108. jsonObject.put("body", payDesc);
  109. jsonObject.put("attach", orderId);
  110. jsonObject.put("trade_type", "APP");
  111. jsonObject.put("attach", PayType.getPayType(payType).getName());
  112. } else if (payType.equals(PayType.wechatpay.getIndex())) {// 微信扫码支付
  113. a_xp1001.setServerChannel("02");
  114. jsonObject.put("total_fee", allAmountYuan * 100);
  115. jsonObject.put("body", payDesc);
  116. jsonObject.put("product_id", orderId);
  117. jsonObject.put("trade_type", "NATIVE");
  118. jsonObject.put("attach", PayType.getPayType(payType).getName());
  119. } else if (payType.equals(PayType.alipay.getIndex())) {// 支付宝扫码支付
  120. a_xp1001.setServerChannel("03");
  121. jsonObject.put("totalAmount", allAmount);
  122. jsonObject.put("subject", payDesc);
  123. jsonObject.put("body", orderId);
  124. jsonObject.put("trade_type", "QR_CODE");//输入QR_CODE 为二维码扫码支付,否则是本地app支付
  125. } else if (payType.equals(PayType.alipayApp.getIndex())) {// 支付宝APP支付
  126. a_xp1001.setServerChannel("03");
  127. jsonObject.put("totalAmount", allAmount);
  128. jsonObject.put("subject", payDesc);
  129. jsonObject.put("body", orderId);
  130. jsonObject.put("trade_type", "APP");
  131. } else {
  132. throw new BusinessException("EB2100000");
  133. }
  134. a_xp1001.setJsonString(jsonObject.toString());
  135. model.setA_xp1001(a_xp1001);
  136. // 发送请求
  137. model = client.execute(model);
  138. D_XP1001 d_xp1001 = model.getD_xp1001();
  139. /**=BEGIN============== 下行返回并插入记录 =============== 所有金额打折数据都用数据库查询模式**/
  140. Pay pay = new Pay();
  141. pay.setId(UtilTools.getUUid());
  142. pay.setCat(payType);
  143. pay.setAmount(allAmountYuan);
  144. pay.setCreateTime(XIDateTimeUtils.getNowDate());
  145. pay.setUpdateTime(pay.getCreateTime());
  146. pay.setUid(getA_systemhead().getUserId());
  147. pay.setOrderId(orderInfo.getId());
  148. String platFormOrderNo = "";
  149. try {
  150. platFormOrderNo = new JSONObject(d_xp1001.getJsonString()).getString("out_trade_no");
  151. } catch (Exception e) {
  152. logger.error("", e);
  153. }
  154. pay.setPlatformOrderNo(platFormOrderNo);
  155. // 下行赋值
  156. if (UtilTools.isNullOrBlank(platFormOrderNo) || !d_xp1001.getReturn_code().equals("SUCCESS")) {// 失败
  157. pay.setStatus(Paystatus.orderfail.getIndex());
  158. payMapper.insertSelective(pay);
  159. throw new BusinessException("EB2100005");
  160. } else {
  161. pay.setStatus(Paystatus.ordersuccess.getIndex());
  162. payMapper.insertSelective(pay);
  163. orderInfo.setPayId(pay.getId());
  164. ordersMapper.updateByPrimaryKeySelective(orderInfo);
  165. String payInfo = new JSONObject(d_xp1001.getJsonString()).getString("payinfo");
  166. getD_ief001().setPayInfo(payInfo);
  167. getD_ief001().setPayType(payType);
  168. getD_ief001().setOrderNo(platFormOrderNo);
  169. if (payTypeName.contains(PayType.alipay.getName())) {
  170. if (payTypeName.equals(PayType.alipay.getName())) { //只有扫码支付才解析二维码
  171. Gson gson = new Gson();
  172. try {
  173. AlipayPayInfoBean payBean = gson.fromJson(payInfo, AlipayPayInfoBean.class);
  174. if (null != payBean) {
  175. this.getD_ief001().setQrUrl(payBean.getAlipay_trade_precreate_response().getQr_code());
  176. }
  177. } catch (Exception e) {
  178. }
  179. }
  180. } else if (payTypeName.contains(PayType.wechatpay.getName())) {
  181. this.getD_ief001().setQrUrl(payInfo);
  182. }
  183. //设置支付超时
  184. {
  185. Map<String, String> opDataMap = new HashMap<>();
  186. opDataMap.put("opIndex", "");
  187. opDataMap.put("opType", "pay");
  188. opDataMap.put("opData", pay.getId());
  189. opDataMap.put("opTime", XIDateTimeUtils.getNowStr());
  190. if (!MFMqUtils.get(XIMQPayTimeoutQueue.class).sendPayData(UtilTools.obj2Json(opDataMap))) {
  191. throw new BusinessException("EB8000105");
  192. }
  193. }
  194. }
  195. /**=END================ 下行返回并插入记录 ===============**/
  196. }
  197. public class AlipayPayInfoBean {
  198. private Alipay_trade_precreate_response alipay_trade_precreate_response;
  199. private String sign;
  200. public void setAlipay_trade_precreate_response(Alipay_trade_precreate_response alipay_trade_precreate_response) {
  201. this.alipay_trade_precreate_response = alipay_trade_precreate_response;
  202. }
  203. public Alipay_trade_precreate_response getAlipay_trade_precreate_response() {
  204. return alipay_trade_precreate_response;
  205. }
  206. public void setSign(String sign) {
  207. this.sign = sign;
  208. }
  209. public String getSign() {
  210. return sign;
  211. }
  212. }
  213. public class Alipay_trade_precreate_response {
  214. private String code;
  215. private String msg;
  216. private String out_trade_no;
  217. private String qr_code;
  218. public void setCode(String code) {
  219. this.code = code;
  220. }
  221. public String getCode() {
  222. return code;
  223. }
  224. public void setMsg(String msg) {
  225. this.msg = msg;
  226. }
  227. public String getMsg() {
  228. return msg;
  229. }
  230. public void setOut_trade_no(String out_trade_no) {
  231. this.out_trade_no = out_trade_no;
  232. }
  233. public String getOut_trade_no() {
  234. return out_trade_no;
  235. }
  236. public void setQr_code(String qr_code) {
  237. this.qr_code = qr_code;
  238. }
  239. public String getQr_code() {
  240. return qr_code;
  241. }
  242. }
  243. /**
  244. * 基础系统,“支付下单”业务前处理
  245. */
  246. public void preTransFlow() throws Exception {
  247. this.validater();
  248. }
  249. /**
  250. * 基础系统,“支付下单”业务后处理
  251. */
  252. public void afterTransFlow() throws Exception {
  253. }
  254. /**
  255. * 基础系统,“支付下单”逻辑入口处理方法
  256. */
  257. @SuppressWarnings("rawtypes")
  258. @Override
  259. public Map execute(Map vars) throws Exception {
  260. this.setTransMap(vars);
  261. preTransFlow();// 执行业务开始的规则检查和校验
  262. transExecute();// 执行核心业务段
  263. afterTransFlow();// 执行核心逻辑完成后的收尾逻辑
  264. return this.getTransMap();
  265. }
  266. }