ApiWxGlobalPayService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.kmall.api.service.wxGlobal;
  2. import com.google.common.collect.Maps;
  3. import com.kmall.common.constant.Dict;
  4. import com.kmall.api.dao.*;
  5. import com.kmall.api.entity.*;
  6. import com.kmall.api.service.ApiOrderService;
  7. import com.kmall.api.service.PayerChildOrderRealService;
  8. import com.kmall.api.service.pay.wxpay.WxPayPropertiesBuilder;
  9. import com.kmall.common.entity.FormIdsEntity;
  10. import com.kmall.common.service.FormIdsService;
  11. import com.kmall.common.utils.Constant;
  12. import com.kmall.common.utils.DateUtils;
  13. import com.kmall.common.utils.wechat.WechatRefundApiResult;
  14. import com.kmall.common.utils.wechat.WechatUtil;
  15. import com.kmall.common.utils.wechat.wxglobal.WechatGlobalUtil;
  16. import com.kmall.common.utils.wechat.wxglobal.dto.WechatGlobalNotifyResult;
  17. import com.kmall.common.utils.wechat.wxglobal.dto.WechatGlobalQueryApiResult;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.apache.log4j.Logger;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import java.math.BigDecimal;
  25. import java.util.*;
  26. /**
  27. * 小程序form_id收集Service实现类
  28. *
  29. * @author Scott
  30. * @email
  31. * @date 2017-12-16 15:32:50
  32. */
  33. @Service
  34. public class ApiWxGlobalPayService {
  35. private Log logger = LogFactory.getLog(ApiWxGlobalPayService.class);
  36. @Autowired
  37. private ApiOrderService orderService;
  38. @Autowired
  39. private FormIdsService formIdsService;
  40. @Autowired
  41. private PayerChildOrderRealService payerChildOrderRealService;
  42. @Autowired
  43. private ApiOrderExceptionRecordMapper mallOrderExceptionRecordMapper;
  44. @Autowired
  45. private ApiOrderProcessRecordMapper orderProcessRecordMapper;
  46. @Autowired
  47. private ApiOrderRefundMapper mallOrderRefundMapper;
  48. @Transactional
  49. public void payPrepay(Map<Object, Object> resultObj, WechatGlobalQueryApiResult queryApiResult, String nonceStr, List<OrderVo> orderVoList) {
  50. logger.info("payPrepay》》》》更新订单状态");
  51. String prepay_id = queryApiResult.getPrepay_id();
  52. resultObj.put("appId", WxPayPropertiesBuilder.instance().getAppId());
  53. resultObj.put("timeStamp", DateUtils.timeToStr(System.currentTimeMillis() / 1000, DateUtils.DATE_TIME_PATTERN));
  54. resultObj.put("nonceStr", nonceStr);
  55. resultObj.put("package", "prepay_id=" + prepay_id);
  56. resultObj.put("signType", "MD5");
  57. String paySign = WechatUtil.arraySign(resultObj, WxPayPropertiesBuilder.instance().getPaySignKey());
  58. resultObj.put("paySign", paySign);
  59. for (OrderVo orderInfo: orderVoList) {
  60. // 业务处理
  61. orderInfo.setPay_id(prepay_id);
  62. orderInfo.setPayFlag(Dict.payFlag.item_wxglobalpay.getItem());
  63. orderInfo.setOrder_status(Integer.parseInt(Dict.orderStatus.item_100.getItem()));
  64. orderInfo.setPay_status(Integer.parseInt(Dict.payStatus.item_1.getItem())); // 付款中
  65. orderService.update(orderInfo);
  66. Map map = Maps.newHashMap();
  67. map.put("orderSn", orderInfo.getOrder_sn());
  68. List<PayerChildOrderRealEntity> list = payerChildOrderRealService.queryList(map);
  69. PayerChildOrderRealEntity entity = new PayerChildOrderRealEntity();
  70. entity.setGoodsBizType(orderInfo.getOrderBizType());
  71. entity.setOrderSn(orderInfo.getOrder_sn());
  72. entity.setMerchSn(orderInfo.getMerchSn());
  73. entity.setMerchOrderSn(orderInfo.getMerchOrderSn());
  74. entity.setPayNo(orderInfo.getPay_id());
  75. entity.setPayPrice(orderInfo.getActual_price());
  76. entity.setCreateTime(new Date());
  77. entity.setModTime(new Date());
  78. entity.setUserId(Integer.parseInt(orderInfo.getUser_id() + ""));
  79. if(list.size() > 0) {
  80. for (PayerChildOrderRealEntity payerChildOrderRealEntity:list) {
  81. entity.setId(payerChildOrderRealEntity.getId());
  82. payerChildOrderRealService.update(entity);
  83. }
  84. }else{
  85. payerChildOrderRealService.save(entity);//记录微信返回的支付单与子订单信息
  86. }
  87. FormIdsEntity formIdsEntity = formIdsService.getFormIdsByMerchOrderSn(orderInfo.getMerchOrderSn());
  88. if(formIdsEntity != null){
  89. FormIdsEntity formIds = new FormIdsEntity();
  90. formIds.setMerchOrderSn(orderInfo.getMerchOrderSn());
  91. formIds.setFormId(prepay_id);
  92. formIds.setValidNum(formIdsEntity.getValidNum()+1);
  93. formIds.setId(formIdsEntity.getId());
  94. formIdsService.releaseFormIds(formIds);
  95. }else{
  96. // 保存form_id
  97. formIdsService.save(orderInfo.getUser_id(), prepay_id, 1, orderInfo.getMerchOrderSn());
  98. }
  99. }
  100. }
  101. @Transactional
  102. public void notify(WechatGlobalNotifyResult result) {
  103. String time_end = result.getTime_end();
  104. String out_trade_no = result.getOut_trade_no();//商户订单号
  105. String transaction_id = result.getTransaction_id();//微信支付订单号
  106. String total_fee = result.getTotal_fee();//订单编号
  107. // 支付成功业务处理
  108. List<OrderVo> orderVoList = orderService.queryObjectByMerchOrderSn(out_trade_no);
  109. List<OrderProcessRecordEntity> processRecordEntityList = new ArrayList<>();
  110. Date timeEnd = DateUtils.convertStringToDate(time_end,DateUtils.DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS);
  111. if(orderVoList != null && orderVoList.size()>0) {
  112. for(OrderVo orderInfo:orderVoList) {
  113. //校验返回的订单金额是否与商户侧的订单金额一致
  114. String actualPrice = String.valueOf(orderInfo.getActual_price().multiply(Constant.ONE_HUNDRED));
  115. if(!total_fee.equalsIgnoreCase(actualPrice)){//不一致
  116. MallOrderExceptionRecord mallOrderExceptionRecord = new MallOrderExceptionRecord();
  117. mallOrderExceptionRecord.setUserId(Integer.parseInt(orderInfo.getUser_id()+""));
  118. mallOrderExceptionRecord.setOrderSn(orderInfo.getOrder_sn());
  119. mallOrderExceptionRecord.setExceptionStatus(Dict.exceptionStatus.item_04.getItem());
  120. mallOrderExceptionRecord.setCreateTime(new Date());
  121. mallOrderExceptionRecord.setExceptionContent("微信支付回调返回的订单金额与商户订单金额不一致");
  122. mallOrderExceptionRecordMapper.save(mallOrderExceptionRecord);//支付订单异常记录
  123. }
  124. if(Dict.orderStatus.item_100.getItem().equalsIgnoreCase(orderInfo.getOrder_status()+"")
  125. && Dict.payStatus.item_1.getItem().equalsIgnoreCase(orderInfo.getPay_status()+"")){
  126. OrderProcessRecordEntity processRecordEntity = orderProcessRecordMapper.queryObjectByOrderSn(orderInfo.getOrder_sn());
  127. if(processRecordEntity != null){
  128. OrderProcessRecordEntity entity = new OrderProcessRecordEntity();
  129. entity.setOrderSn(orderInfo.getOrder_sn());
  130. entity.setUserId(Integer.valueOf(orderInfo.getUser_id()+""));
  131. entity.setPaySuccTime(timeEnd);//微信下单完成时间
  132. entity.setId(processRecordEntity.getId());
  133. processRecordEntityList.add(entity);
  134. }
  135. orderInfo.setPayTransactionId(transaction_id);
  136. orderInfo.setPay_status(Integer.parseInt(Dict.payStatus.item_2.getItem()));
  137. orderInfo.setPay_time(timeEnd);//微信下单完成时间
  138. if (orderInfo.getOrder_status() == Integer.parseInt(Dict.orderStatus.item_100.getItem())) {
  139. orderInfo.setOrder_status(Integer.parseInt(Dict.orderStatus.item_201.getItem()));
  140. }
  141. orderInfo.setTotalFee(Integer.parseInt(result.getTotal_fee()));//标价金额
  142. orderInfo.setFeeType(result.getFee_type());//标价币种
  143. orderInfo.setCashFee(Integer.parseInt(result.getCash_fee()));//用户支付金额
  144. orderInfo.setCashFeeType(result.getCash_fee_type());//用户支付金额币种
  145. orderInfo.setRate(result.getRate_value());//汇率
  146. orderService.update(orderInfo);
  147. orderProcessRecordMapper.updateBatch(processRecordEntityList);//更新订单流转信息
  148. // 微信支付成功通知
  149. orderService.notifyPaySuccess(orderInfo);
  150. }
  151. }
  152. }
  153. }
  154. @Transactional
  155. public void refundNotify(WechatRefundApiResult refundApiResult) {
  156. String success_time = refundApiResult.getSuccess_time();
  157. String out_trade_no = refundApiResult.getOut_trade_no();//商户订单号
  158. // 业务处理
  159. List<OrderVo> orderVoList = orderService.queryOrderByMerchOrderSnAndRefundStatus(out_trade_no);
  160. Date successTime = DateUtils.strToDate(success_time);
  161. if(orderVoList != null && orderVoList.size()>0) {
  162. for(OrderVo orderInfo:orderVoList) {
  163. if(orderInfo.getOrder_status() == Integer.parseInt(Dict.orderStatus.item_201.getItem())
  164. || orderInfo.getOrder_status() == Integer.parseInt(Dict.orderStatus.item_401.getItem())){
  165. MallOrderRefund mallOrderRefund = mallOrderRefundMapper.queryObjectByOrderId(orderInfo.getId()+"");
  166. MallOrderRefund orderRefund = new MallOrderRefund();
  167. OrderVo orderVo = new OrderVo();
  168. if(mallOrderRefund != null){
  169. orderRefund.setOutRefundNo(refundApiResult.getOut_refund_no());
  170. orderRefund.setRefundTime(successTime);//退款成功时间
  171. orderRefund.setRefundId(refundApiResult.getRefund_id());
  172. orderRefund.setRefundMoney(BigDecimal.valueOf(Long.valueOf(refundApiResult.getRefund_fee())).divide(Constant.ONE_HUNDRED));
  173. if (refundApiResult.getRefund_status().equalsIgnoreCase(WechatGlobalUtil.WXTradeState.SUCCESS.getCode())) {
  174. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_2.getItem()));
  175. orderRefund.setWechatRefundStatusDes("退款成功");
  176. orderVo.setPay_status(Integer.parseInt(Dict.payStatus.item_4.getItem()));
  177. } else if (refundApiResult.getRefund_status().equalsIgnoreCase("PROCESSING")) {
  178. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_5.getItem()));
  179. orderRefund.setWechatRefundStatusDes("退款处理中");
  180. orderVo.setPay_status(Integer.parseInt(Dict.payStatus.item_3.getItem()));
  181. } else if (refundApiResult.getRefund_status().equalsIgnoreCase("REFUNDCLOSE")) {
  182. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  183. orderRefund.setWechatRefundStatusDes("退款关闭");
  184. orderVo.setPay_status(Integer.parseInt(Dict.payStatus.item_5.getItem()));
  185. } else if (refundApiResult.getRefund_status().equalsIgnoreCase("CHANGE")) {
  186. orderRefund.setRefundStatus(Integer.parseInt(Dict.RefundStatus.item_4.getItem()));
  187. orderRefund.setWechatRefundStatusDes("退款异常");
  188. orderVo.setPay_status(Integer.parseInt(Dict.payStatus.item_6.getItem()));
  189. }
  190. orderRefund.setRefundRecvAccout(refundApiResult.getRefund_recv_accout());
  191. orderRefund.setId(mallOrderRefund.getId());
  192. mallOrderRefundMapper.update(orderRefund);//更新订单退款信息
  193. orderVo.setOrder_status(Integer.parseInt(Dict.orderStatus.item_401.getItem()));
  194. orderVo.setId(orderInfo.getId());
  195. orderService.update(orderVo);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }