AppAbstractComponentSMO.java 7.5 KB

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