AlipayServiceImpl.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package com.fuint.common.service.impl;
  2. import com.alipay.api.AlipayApiException;
  3. import com.alipay.api.domain.AlipayTradePayModel;
  4. import com.alipay.api.domain.AlipayTradeQueryModel;
  5. import com.alipay.api.domain.AlipayTradeRefundModel;
  6. import com.alipay.api.internal.util.AlipaySignature;
  7. import com.alipay.api.response.AlipayTradePayResponse;
  8. import com.alipay.api.response.AlipayTradeQueryResponse;
  9. import com.alipay.api.response.AlipayTradeRefundResponse;
  10. import com.fuint.common.bean.AliPayBean;
  11. import com.fuint.common.dto.OrderDto;
  12. import com.fuint.common.dto.UserOrderDto;
  13. import com.fuint.common.enums.*;
  14. import com.fuint.common.service.*;
  15. import com.fuint.framework.exception.BusinessCheckException;
  16. import com.fuint.framework.web.ResponseObject;
  17. import com.fuint.repository.model.*;
  18. import com.fuint.utils.StringUtil;
  19. import com.ijpay.alipay.AliPayApi;
  20. import com.ijpay.alipay.AliPayApiConfig;
  21. import com.ijpay.alipay.AliPayApiConfigKit;
  22. import lombok.AllArgsConstructor;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import java.math.BigDecimal;
  28. import java.util.*;
  29. /**
  30. * 支付宝相关接口
  31. *
  32. * Created by FSQ
  33. * CopyRight https://www.fuint.cn
  34. */
  35. @Service
  36. @AllArgsConstructor
  37. public class AlipayServiceImpl implements AlipayService {
  38. private static final Logger logger = LoggerFactory.getLogger(WeixinServiceImpl.class);
  39. private AliPayBean aliPayBean;
  40. /**
  41. * 订单服务接口
  42. * */
  43. private OrderService orderService;
  44. /**
  45. * 店铺服务接口
  46. * */
  47. private StoreService storeService;
  48. /**
  49. * 创建支付订单
  50. * @return
  51. * */
  52. @Override
  53. @Transactional(rollbackFor = Exception.class)
  54. public ResponseObject createPrepayOrder(MtUser userInfo, MtOrder orderInfo, Integer payAmount, String authCode, Integer giveAmount, String ip, String platform) throws BusinessCheckException {
  55. logger.info("AlipayService createPrepayOrder inParams userInfo={} payAmount={} giveAmount={} goodsInfo={}", userInfo, payAmount, giveAmount, orderInfo);
  56. String goodsInfo = orderInfo.getOrderSn();
  57. if (orderInfo.getType().equals(OrderTypeEnum.PRESTORE.getKey())) {
  58. goodsInfo = OrderTypeEnum.PRESTORE.getValue();
  59. }
  60. // 更新支付金额
  61. BigDecimal payAmount1 = new BigDecimal(payAmount).divide(new BigDecimal("100"));
  62. OrderDto reqDto = new OrderDto();
  63. reqDto.setId(orderInfo.getId());
  64. reqDto.setPayAmount(payAmount1);
  65. reqDto.setPayType(orderInfo.getPayType());
  66. orderService.updateOrder(reqDto);
  67. getApiConfig(orderInfo.getStoreId());
  68. String notifyUrl = aliPayBean.getDomain();
  69. AlipayTradePayModel model = new AlipayTradePayModel();
  70. model.setAuthCode(authCode);
  71. model.setSubject(goodsInfo);
  72. model.setTotalAmount(payAmount1.toString());
  73. model.setOutTradeNo(orderInfo.getOrderSn());
  74. model.setStoreId(orderInfo.getStoreId().toString());
  75. model.setScene("bar_code");
  76. String code = "";
  77. try {
  78. AlipayTradePayResponse response = AliPayApi.tradePayToResponse(model, notifyUrl);
  79. code = response.getCode();
  80. String msg = response.getMsg();
  81. logger.info("AlipayService createPrepayOrder return code: {}, msg ", code, msg);
  82. if (!code.equals("10000") || !msg.equalsIgnoreCase("Success")) {
  83. if (code.equals("10003")) {
  84. // 需要会员输入支付密码,等待10秒后查询订单
  85. try {
  86. Thread.sleep(10000);
  87. } catch (InterruptedException e) {
  88. e.printStackTrace();
  89. }
  90. Map<String, String> payResult = queryPaidOrder(orderInfo.getStoreId(), response.getTradeNo(), orderInfo.getOrderSn());
  91. if (payResult == null) {
  92. throw new BusinessCheckException("支付宝支付失败");
  93. }
  94. } else {
  95. throw new BusinessCheckException("支付宝支付出错:" + msg);
  96. }
  97. }
  98. } catch (Exception e) {
  99. logger.error("AlipayService createPrepayOrder exception {}", e.getMessage());
  100. throw new BusinessCheckException("支付宝支付出错,请检查配置项");
  101. }
  102. Map<String, String> respData = new HashMap<>();
  103. respData.put("result", code);
  104. ResponseObject responseObject = new ResponseObject(200, "支付宝支付接口返回成功", respData);
  105. logger.info("AlipayService createPrepayOrder outParams {}", responseObject.toString());
  106. return responseObject;
  107. }
  108. @Override
  109. public Boolean checkCallBack(Map<String, String> params) throws Exception {
  110. String orderSn = params.get("out_trade_no") != null ? params.get("out_trade_no") : "";
  111. Integer storeId = 0;
  112. UserOrderDto orderDto = orderService.getOrderByOrderSn(orderSn);
  113. if (orderDto != null && orderDto.getStoreInfo() != null) {
  114. storeId = orderDto.getStoreInfo().getId();
  115. }
  116. getApiConfig(storeId);
  117. return AlipaySignature.rsaCheckV1(params, aliPayBean.getPublicKey(), "UTF-8", "RSA2");
  118. }
  119. /**
  120. * 获取支付配置
  121. * */
  122. public AliPayApiConfig getApiConfig(Integer storeId) throws BusinessCheckException {
  123. AliPayApiConfig aliPayApiConfig;
  124. String appId = aliPayBean.getAppId();
  125. String privateKey = aliPayBean.getPrivateKey();
  126. String publicKey = aliPayBean.getPublicKey();
  127. // 优先读取店铺的支付账号
  128. MtStore mtStore = storeService.queryStoreById(storeId);
  129. if (mtStore != null && StringUtil.isNotEmpty(mtStore.getAlipayAppId()) && StringUtil.isNotEmpty(mtStore.getAlipayPrivateKey()) && StringUtil.isNotEmpty(mtStore.getAlipayPublicKey())) {
  130. appId = mtStore.getAlipayAppId();
  131. privateKey = mtStore.getAlipayPrivateKey();
  132. publicKey = mtStore.getAlipayPublicKey();
  133. }
  134. aliPayApiConfig = AliPayApiConfig.builder()
  135. .setAppId(appId)
  136. .setAliPayPublicKey(publicKey)
  137. .setCharset("UTF-8")
  138. .setPrivateKey(privateKey)
  139. .setServiceUrl(aliPayBean.getServerUrl())
  140. .setSignType("RSA2")
  141. .build();
  142. AliPayApiConfigKit.setThreadLocalAppId(appId);
  143. AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayApiConfig);
  144. return aliPayApiConfig;
  145. }
  146. /**
  147. * 查询支付订单
  148. * */
  149. @Override
  150. public Map<String, String> queryPaidOrder(Integer storeId, String tradeNo, String orderSn) throws BusinessCheckException {
  151. try {
  152. AlipayTradeQueryModel model = new AlipayTradeQueryModel();
  153. if (StringUtil.isNotEmpty(orderSn)) {
  154. model.setOutTradeNo(orderSn);
  155. }
  156. if (StringUtil.isNotEmpty(tradeNo)) {
  157. model.setTradeNo(tradeNo);
  158. }
  159. getApiConfig(storeId);
  160. AlipayTradeQueryResponse response = AliPayApi.tradeQueryToResponse(model);
  161. if (response != null) {
  162. // TradeStatus:TRADE_SUCCESS(交易支付成功,可进行退款)或 TRADE_FINISHED(交易结束,不可退款)
  163. if (response.getTradeStatus() != null && response.getTradeStatus().equals("TRADE_SUCCESS")) {
  164. Map<String, String> result = new HashMap<>();
  165. result.put("tradeNo", response.getTradeNo());
  166. result.put("status", response.getTradeStatus());
  167. result.put("payAmount", response.getBuyerPayAmount());
  168. return result;
  169. }
  170. }
  171. } catch (AlipayApiException e) {
  172. logger.info("AlipayService queryPaidOrder response", e.getMessage());
  173. }
  174. return null;
  175. }
  176. /**
  177. * 支付宝发起退款
  178. *
  179. * @param storeId
  180. * @param orderSn
  181. * @param totalAmount
  182. * @param refundAmount
  183. * @param platform
  184. * @return
  185. * */
  186. public Boolean doRefund(Integer storeId, String orderSn, BigDecimal totalAmount, BigDecimal refundAmount, String platform) throws BusinessCheckException {
  187. try {
  188. logger.info("AlipayService.doRefund orderSn = {}, totalFee = {}, refundFee = {}", orderSn, totalAmount, refundAmount);
  189. if (StringUtil.isEmpty(orderSn)) {
  190. throw new BusinessCheckException("退款订单号不能为空...");
  191. }
  192. if (refundAmount.compareTo(totalAmount) > 0) {
  193. throw new BusinessCheckException("退款金额不能大于总金额...");
  194. }
  195. getApiConfig(storeId);
  196. AlipayTradeRefundModel model = new AlipayTradeRefundModel();
  197. model.setOutTradeNo(orderSn);
  198. model.setRefundAmount(refundAmount.toString());
  199. model.setRefundReason("申请退款");
  200. AlipayTradeRefundResponse refundResponse = AliPayApi.tradeRefundToResponse(model);
  201. String code = refundResponse.getCode();
  202. String msg = refundResponse.getMsg();
  203. String subMsg = refundResponse.getSubMsg() == null ? msg : refundResponse.getSubMsg();
  204. logger.info("AlipayService refundResult response Body = {}", refundResponse.getBody());
  205. if (!code.equals("10000") || !msg.equalsIgnoreCase("Success")) {
  206. throw new BusinessCheckException("支付宝退款失败," + subMsg);
  207. }
  208. } catch (AlipayApiException e) {
  209. logger.error("AlipayService.doRefund error = {}", e.getMessage());
  210. e.printStackTrace();
  211. }
  212. return true;
  213. }
  214. }