PayService.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package com.qxgmat.service.inline;
  2. import com.alibaba.fastjson.JSON;
  3. import com.github.pagehelper.Page;
  4. import com.nuliji.tools.AbstractService;
  5. import com.nuliji.tools.Tools;
  6. import com.nuliji.tools.exception.ParameterException;
  7. import com.nuliji.tools.exception.SystemException;
  8. import com.nuliji.tools.mybatis.Example;
  9. import com.nuliji.tools.pay.common.PayInfo;
  10. import com.nuliji.tools.pay.common.ResultInfo;
  11. import com.nuliji.tools.pay.enums.TradeStatus;
  12. import com.qxgmat.data.dao.PayMapper;
  13. import com.qxgmat.data.dao.entity.Pay;
  14. import com.qxgmat.service.annotation.PayModule;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.math.BigDecimal;
  20. import java.util.Collection;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.UUID;
  24. @Service
  25. public class PayService extends AbstractService {
  26. private static final Logger logger = LoggerFactory.getLogger(PayService.class);
  27. @Resource
  28. private PayMapper payMapper;
  29. /**
  30. * 发起充值
  31. * @param pay
  32. * @param info
  33. * @return
  34. */
  35. public int start(Pay pay, PayInfo info){
  36. pay.setPayInfo(JSON.toJSONString(info));
  37. pay.setTradeStatus(TradeStatus.WAIT_BUYER_PAY);
  38. return payMapper.updateByPrimaryKey(pay);
  39. }
  40. /**
  41. * 系统支付错误
  42. * @param pay
  43. * @param info
  44. * @return
  45. */
  46. public int system(Pay pay, PayInfo info){
  47. pay.setPayInfo(JSON.toJSONString(info));
  48. pay.setTradeStatus(TradeStatus.SYSTEM);
  49. return payMapper.updateByPrimaryKey(pay);
  50. }
  51. /**
  52. * 系统结果错误
  53. * @param pay
  54. * @param info
  55. * @return
  56. */
  57. public int system(Pay pay, ResultInfo info){
  58. pay.setResultInfo(JSON.toJSONString(info));
  59. pay.setTradeStatus(TradeStatus.SYSTEM);
  60. return payMapper.updateByPrimaryKey(pay);
  61. }
  62. /**
  63. * 支付流程异常
  64. * @param pay
  65. * @param info
  66. * @return
  67. */
  68. public int exception(Pay pay, ResultInfo info){
  69. pay.setPayInfo(JSON.toJSONString(info));
  70. pay.setTradeStatus(TradeStatus.EXCEPTION);
  71. return payMapper.updateByPrimaryKey(pay);
  72. }
  73. /**
  74. * 支付关闭
  75. * @param pay
  76. * @param info
  77. * @return
  78. */
  79. public int close(Pay pay, ResultInfo info){
  80. pay.setPayInfo(JSON.toJSONString(info));
  81. pay.setTransactionNo(info.getTransaction_no());
  82. pay.setTradeStatus(TradeStatus.CLOSE);
  83. return payMapper.updateByPrimaryKey(pay);
  84. }
  85. /**
  86. * 支付错误,需要重新支付
  87. * @param pay
  88. * @param info
  89. * @return
  90. */
  91. public int error(Pay pay, ResultInfo info){
  92. pay.setPayInfo(JSON.toJSONString(info));
  93. pay.setTransactionNo(info.getTransaction_no());
  94. pay.setTradeStatus(TradeStatus.ERROR);
  95. return payMapper.updateByPrimaryKey(pay);
  96. }
  97. /**
  98. * 支付取消
  99. * @param pay
  100. * @param info
  101. * @return
  102. */
  103. public int cancel(Pay pay, ResultInfo info){
  104. pay.setPayInfo(JSON.toJSONString(info));
  105. pay.setTransactionNo(info.getTransaction_no());
  106. pay.setTradeStatus(TradeStatus.CANCEL);
  107. return payMapper.updateByPrimaryKey(pay);
  108. }
  109. /**
  110. * 支付完成
  111. * @param pay
  112. * @param info
  113. * @param finish
  114. * @return
  115. */
  116. public int payed(Pay pay, ResultInfo info, boolean finish){
  117. // int timestamp = Math.toIntExact(System.currentTimeMillis() / 1000);
  118. pay.setTransactionNo(info.getTransaction_no());
  119. pay.setResultInfo(JSON.toJSONString(info));
  120. pay.setPayTime(new Date());
  121. pay.setTradeStatus(finish ? TradeStatus.FINISH : TradeStatus.SUCCESS);
  122. return payMapper.updateByPrimaryKey(pay);
  123. }
  124. /**
  125. * 已支付
  126. * @param pay
  127. * @return boolean
  128. */
  129. public boolean isPayed(Pay pay){
  130. return TradeStatus.isPayed(pay.getTradeStatus());
  131. }
  132. /**
  133. * 支付中
  134. * @param pay
  135. * @return boolean
  136. */
  137. public boolean isPaying(Pay pay){
  138. return TradeStatus.isPaying(pay.getTradeStatus());
  139. }
  140. /**
  141. * 无效支付
  142. * @param pay
  143. * @return boolean
  144. */
  145. public boolean isInvalid(Pay pay){
  146. return TradeStatus.isInvalid(pay.getTradeStatus());
  147. }
  148. /**
  149. * 支付错误
  150. * @param pay
  151. * @return boolean
  152. */
  153. public boolean isError(Pay pay){
  154. return TradeStatus.isError(pay.getTradeStatus());
  155. }
  156. /**
  157. * 获取支付信息
  158. * @param pay
  159. * @return boolean
  160. */
  161. public PayInfo getInfo(Pay pay){
  162. return JSON.parseObject(pay.getPayInfo(), PayInfo.class);
  163. }
  164. /**
  165. * 创建支付记录
  166. * @param userId
  167. * @param payType
  168. * @param channel
  169. * @param money
  170. * @param module
  171. * @param moduleExtend
  172. * @param pid
  173. * @param subject
  174. * @param body
  175. * @param clientIp
  176. * @return Pay
  177. */
  178. public Pay make(Integer userId, String payType, String channel, BigDecimal money, String module, String moduleExtend, String pid, String subject, String body, String clientIp){
  179. Pay pay = Pay.builder()
  180. .subject(subject)
  181. .body(body)
  182. .userId(userId)
  183. .module(module)
  184. .moduleExtend(moduleExtend)
  185. .payType(payType)
  186. .money(money)
  187. .clientIp(clientIp)
  188. .currency("cny")
  189. .channel(channel)
  190. .no(UUID.randomUUID().toString().substring(0, 32))
  191. .pid(pid)
  192. .transactionNo("") // 发起支付后得到
  193. .tradeStatus(com.qxgmat.data.constants.enums.trade.TradeStatus.WAIT.index)
  194. .resultInfo("") // 支付返回后得到
  195. .payInfo("") // 发起支付的请求参数
  196. .build();
  197. int result = insert(payMapper, pay);
  198. return pay;
  199. }
  200. public Pay getByNo(String no){
  201. Example example = new Example(Pay.class);
  202. example.and(
  203. example.createCriteria()
  204. .andEqualTo("no", no)
  205. );
  206. return one(payMapper, example);
  207. }
  208. public Pay add(Pay pay){
  209. int result = insert(payMapper, pay);
  210. pay = one(payMapper, pay.getId());
  211. if(pay == null){
  212. throw new SystemException("添加失败");
  213. }
  214. return pay;
  215. }
  216. public Pay edit(Pay pay){
  217. Pay in = one(payMapper, pay.getId());
  218. if(in == null){
  219. throw new ParameterException("支付不存在");
  220. }
  221. int result = update(payMapper, pay);
  222. return pay;
  223. }
  224. public boolean delete(Number id){
  225. Pay in = payMapper.selectByPrimaryKey(id);
  226. if(in == null){
  227. throw new ParameterException("支付不存在");
  228. }
  229. int result = payMapper.deleteByPrimaryKey(id);
  230. return result > 0;
  231. }
  232. public Pay get(Number id){
  233. Pay in = payMapper.selectByPrimaryKey(id);
  234. if(in == null){
  235. throw new ParameterException("支付不存在");
  236. }
  237. return in;
  238. }
  239. public Page<Pay> select(int page, int pageSize){
  240. return select(payMapper, page, pageSize);
  241. }
  242. public Page<Pay> select(Integer[] ids){
  243. return page(()->select(payMapper, ids), 1, ids.length);
  244. }
  245. public List<Pay> select(Collection ids){
  246. return select(payMapper, ids);
  247. }
  248. }