PayService.java 7.3 KB

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