AppAbstractComponentSMO.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.java110.front.smo;
  2. import com.java110.core.component.AbstractComponentSMO;
  3. import com.java110.core.context.IPageData;
  4. import com.java110.core.factory.WechatFactory;
  5. import com.java110.dto.smallWeChat.SmallWeChatDto;
  6. import com.java110.front.properties.WechatAuthProperties;
  7. import com.java110.utils.cache.MappingCache;
  8. import com.java110.utils.constant.CommonConstant;
  9. import com.java110.utils.constant.WechatConstant;
  10. import com.java110.utils.util.Assert;
  11. import com.java110.utils.util.PayUtil;
  12. import com.java110.utils.util.StringUtil;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.http.*;
  17. import org.springframework.web.client.HttpStatusCodeException;
  18. import org.springframework.web.client.RestTemplate;
  19. import java.util.Map;
  20. import java.util.SortedMap;
  21. import java.util.TreeMap;
  22. public abstract class AppAbstractComponentSMO extends AbstractComponentSMO {
  23. private static final Logger logger = LoggerFactory.getLogger(AppAbstractComponentSMO.class);
  24. @Autowired
  25. private WechatAuthProperties wechatAuthProperties;
  26. @Autowired
  27. private RestTemplate restTemplate;
  28. //微信支付
  29. public static final String DOMAIN_WECHAT_PAY = "WECHAT_PAY";
  30. // 微信服务商支付开关
  31. public static final String WECHAT_SERVICE_PAY_SWITCH = "WECHAT_SERVICE_PAY_SWITCH";
  32. //开关ON打开
  33. public static final String WECHAT_SERVICE_PAY_SWITCH_ON = "ON";
  34. private static final String WECHAT_SERVICE_APP_ID = "SERVICE_APP_ID";
  35. private static final String WECHAT_SERVICE_MCH_ID = "SERVICE_MCH_ID";
  36. /**
  37. * 调用中心服务
  38. *
  39. * @return
  40. */
  41. @Override
  42. protected ResponseEntity<String> callCenterService(RestTemplate restTemplate, IPageData pd, String param, String url, HttpMethod httpMethod) {
  43. Assert.notNull(pd.getAppId(), "请求头中未包含应用信息");
  44. ResponseEntity<String> responseEntity = null;
  45. HttpHeaders header = new HttpHeaders();
  46. header.add(CommonConstant.HTTP_APP_ID.toLowerCase(), pd.getAppId());
  47. header.add(CommonConstant.HTTP_USER_ID.toLowerCase(), StringUtil.isEmpty(pd.getUserId()) ? CommonConstant.ORDER_DEFAULT_USER_ID : pd.getUserId());
  48. header.add(CommonConstant.HTTP_TRANSACTION_ID.toLowerCase(), pd.getTransactionId());
  49. header.add(CommonConstant.HTTP_REQ_TIME.toLowerCase(), pd.getRequestTime());
  50. header.add(CommonConstant.HTTP_SIGN.toLowerCase(), "");
  51. HttpEntity<String> httpEntity = new HttpEntity<String>(param, header);
  52. //logger.debug("请求中心服务信息,{}", httpEntity);
  53. try {
  54. responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
  55. } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
  56. responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
  57. } catch (Exception e) {
  58. responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
  59. } finally {
  60. logger.debug("请求地址为,{} 请求中心服务信息,{},中心服务返回信息,{}", url, httpEntity, responseEntity);
  61. return responseEntity;
  62. }
  63. }
  64. /**
  65. * 预下单
  66. *
  67. * @param orderNum
  68. * @param money
  69. * @param openId
  70. * @return
  71. * @throws Exception
  72. */
  73. protected Map<String, String> java110Payment(RestTemplate outRestTemplate,
  74. String feeName, String tradeType,
  75. String orderNum, double money,
  76. String openId, SmallWeChatDto smallWeChatDto) throws Exception {
  77. return java110Payment(outRestTemplate, feeName, tradeType, orderNum, money, openId, smallWeChatDto, "");
  78. }
  79. /**
  80. * 预下单
  81. *
  82. * @param orderNum
  83. * @param money
  84. * @param openId
  85. * @return
  86. * @throws Exception
  87. */
  88. protected Map<String, String> java110Payment(RestTemplate outRestTemplate,
  89. String feeName, String tradeType,
  90. String orderNum, double money,
  91. String openId, SmallWeChatDto smallWeChatDto, String notifyUrl) throws Exception {
  92. logger.info("【小程序支付】 统一下单开始, 订单编号=" + orderNum);
  93. SortedMap<String, String> resultMap = new TreeMap<String, String>();
  94. //生成支付金额,开发环境处理支付金额数到0.01、0.02、0.03元
  95. double payAmount = PayUtil.getPayAmountByEnv(MappingCache.getValue("HC_ENV"), money);
  96. //添加或更新支付记录(参数跟进自己业务需求添加)
  97. Map<String, String> resMap = null;
  98. if (StringUtil.isEmpty(notifyUrl)) {
  99. resMap = this.java110UnifieldOrder(outRestTemplate, feeName, orderNum, tradeType, payAmount, openId, smallWeChatDto);
  100. } else {
  101. resMap = this.java110UnifieldOrder(outRestTemplate, feeName, orderNum, tradeType, payAmount, openId, smallWeChatDto, notifyUrl);
  102. }
  103. if ("SUCCESS".equals(resMap.get("return_code")) && "SUCCESS".equals(resMap.get("result_code"))) {
  104. if (WechatAuthProperties.TRADE_TYPE_JSAPI.equals(tradeType)) {
  105. resultMap.put("appId", smallWeChatDto.getAppId());
  106. resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
  107. resultMap.put("nonceStr", PayUtil.makeUUID(32));
  108. resultMap.put("package", "prepay_id=" + resMap.get("prepay_id"));
  109. resultMap.put("signType", "MD5");
  110. resultMap.put("sign", PayUtil.createSign(resultMap, smallWeChatDto.getPayPassword()));
  111. } else if (WechatAuthProperties.TRADE_TYPE_APP.equals(tradeType)) {
  112. resultMap.put("appId", smallWeChatDto.getAppId());
  113. resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
  114. resultMap.put("nonceStr", PayUtil.makeUUID(32));
  115. resultMap.put("partnerid", smallWeChatDto.getMchId());
  116. resultMap.put("prepayid", resMap.get("prepay_id"));
  117. //resultMap.put("signType", "MD5");
  118. resultMap.put("sign", PayUtil.createSign(resultMap, smallWeChatDto.getPayPassword()));
  119. } else if (WechatAuthProperties.TRADE_TYPE_NATIVE.equals(tradeType)) {
  120. resultMap.put("prepayId", resMap.get("prepay_id"));
  121. resultMap.put("codeUrl", resMap.get("code_url"));
  122. }
  123. resultMap.put("code", "0");
  124. resultMap.put("msg", "下单成功");
  125. logger.info("【小程序支付】统一下单成功,返回参数:" + resultMap);
  126. } else {
  127. resultMap.put("code", resMap.get("return_code"));
  128. resultMap.put("msg", resMap.get("return_msg"));
  129. logger.info("【小程序支付】统一下单失败,失败原因:" + resMap.get("return_msg"));
  130. }
  131. return resultMap;
  132. }
  133. /**
  134. * 小程序支付统一下单
  135. */
  136. private Map<String, String> java110UnifieldOrder(RestTemplate outRestTemplate, String feeName, String orderNum,
  137. String tradeType, double payAmount, String openid,
  138. SmallWeChatDto smallWeChatDto) throws Exception {
  139. return java110UnifieldOrder(outRestTemplate, feeName, orderNum, tradeType, payAmount, openid, smallWeChatDto, wechatAuthProperties.getWxNotifyUrl());
  140. }
  141. /**
  142. * 小程序支付统一下单
  143. */
  144. private Map<String, String> java110UnifieldOrder(RestTemplate outRestTemplate, String feeName, String orderNum,
  145. String tradeType, double payAmount, String openid,
  146. SmallWeChatDto smallWeChatDto, String notifyUrl) throws Exception {
  147. String systemName = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_GOOD_NAME);
  148. SortedMap<String, String> paramMap = new TreeMap<String, String>();
  149. paramMap.put("appid", smallWeChatDto.getAppId());
  150. paramMap.put("mch_id", smallWeChatDto.getMchId());
  151. paramMap.put("nonce_str", PayUtil.makeUUID(32));
  152. paramMap.put("body", systemName + feeName);
  153. paramMap.put("out_trade_no", orderNum);
  154. paramMap.put("total_fee", PayUtil.moneyToIntegerStr(payAmount));
  155. paramMap.put("spbill_create_ip", PayUtil.getLocalIp());
  156. paramMap.put("notify_url", notifyUrl + "?wId=" + WechatFactory.getWId(smallWeChatDto.getAppId()));
  157. paramMap.put("trade_type", tradeType);
  158. paramMap.put("openid", openid);
  159. String paySwitch = MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_PAY_SWITCH);
  160. if (WECHAT_SERVICE_PAY_SWITCH_ON.equals(paySwitch)) {
  161. paramMap.put("appid", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_APP_ID)); //服务商appid,是服务商注册时公众号的id
  162. paramMap.put("mch_id", MappingCache.getValue(DOMAIN_WECHAT_PAY, WECHAT_SERVICE_MCH_ID)); //服务商商户号
  163. paramMap.put("sub_appid", smallWeChatDto.getAppId());//起调小程序appid
  164. paramMap.put("sub_mch_id", smallWeChatDto.getMchId());//起调小程序的商户号
  165. paramMap.put("sub_openid", openid);
  166. paramMap.remove("openid");
  167. }
  168. paramMap.put("sign", PayUtil.createSign(paramMap, smallWeChatDto.getPayPassword()));
  169. //转换为xml
  170. String xmlData = PayUtil.mapToXml(paramMap);
  171. logger.debug("调用支付统一下单接口" + xmlData);
  172. ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(
  173. wechatAuthProperties.getWxPayUnifiedOrder(), xmlData, String.class);
  174. logger.debug("统一下单返回" + responseEntity);
  175. //请求微信后台,获取预支付ID
  176. if (responseEntity.getStatusCode() != HttpStatus.OK) {
  177. throw new IllegalArgumentException("支付失败" + responseEntity.getBody());
  178. }
  179. return PayUtil.xmlStrToMap(responseEntity.getBody());
  180. }
  181. }