AppAbstractComponentSMO.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.java110.app.smo;
  2. import com.java110.app.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 restTemplate,String feeName, String orderNum, double money, String openId) throws Exception {
  65. logger.info("【小程序支付】 统一下单开始, 订单编号=" + orderNum);
  66. SortedMap<String, String> resultMap = new TreeMap<String, String>();
  67. //生成支付金额,开发环境处理支付金额数到0.01、0.02、0.03元
  68. double payAmount = PayUtil.getPayAmountByEnv(MappingCache.getValue("HC_ENV"), money);
  69. //添加或更新支付记录(参数跟进自己业务需求添加)
  70. Map<String, String> resMap = this.java110UnifieldOrder(restTemplate,feeName, orderNum, wechatAuthProperties.TRADE_TYPE_JSAPI, payAmount, openId);
  71. if ("SUCCESS".equals(resMap.get("return_code")) && "SUCCESS".equals(resMap.get("result_code"))) {
  72. resultMap.put("appId", wechatAuthProperties.getAppId());
  73. resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
  74. resultMap.put("nonceStr", PayUtil.makeUUID(32));
  75. resultMap.put("package", "prepay_id=" + resMap.get("prepay_id"));
  76. resultMap.put("signType", "MD5");
  77. resultMap.put("sign", PayUtil.createSign(resultMap, wechatAuthProperties.getKey()));
  78. resultMap.put("code", "0");
  79. resultMap.put("msg", "下单成功");
  80. logger.info("【小程序支付】统一下单成功,返回参数:" + resultMap);
  81. } else {
  82. resultMap.put("code", resMap.get("return_code"));
  83. resultMap.put("msg", resMap.get("return_msg"));
  84. logger.info("【小程序支付】统一下单失败,失败原因:" + resMap.get("return_msg"));
  85. }
  86. return resultMap;
  87. }
  88. /**
  89. * 小程序支付统一下单
  90. */
  91. private Map<String, String> java110UnifieldOrder(RestTemplate restTemplate, String feeName, String orderNum, String tradeType, double payAmount, String openid) throws Exception {
  92. //封装参数
  93. SortedMap<String, String> paramMap = new TreeMap<String, String>();
  94. paramMap.put("appid", wechatAuthProperties.getAppId());
  95. paramMap.put("mch_id", wechatAuthProperties.getMchId());
  96. paramMap.put("nonce_str", PayUtil.makeUUID(32));
  97. paramMap.put("body", "HC智慧家园-" + feeName);
  98. paramMap.put("out_trade_no", orderNum);
  99. paramMap.put("total_fee", PayUtil.moneyToIntegerStr(payAmount));
  100. paramMap.put("spbill_create_ip", PayUtil.getLocalIp());
  101. paramMap.put("notify_url", wechatAuthProperties.getWxNotifyUrl());
  102. paramMap.put("trade_type", tradeType);
  103. paramMap.put("openid", openid);
  104. paramMap.put("sign", PayUtil.createSign(paramMap, wechatAuthProperties.getKey()));
  105. //转换为xml
  106. String xmlData = PayUtil.mapToXml(paramMap);
  107. logger.debug("调用支付统一下单接口" + xmlData);
  108. ResponseEntity<String> responseEntity = restTemplate.postForEntity(
  109. wechatAuthProperties.getWxPayUnifiedOrder(), xmlData, String.class);
  110. logger.debug("统一下单返回" + responseEntity);
  111. //请求微信后台,获取预支付ID
  112. if (responseEntity.getStatusCode() != HttpStatus.OK) {
  113. throw new IllegalArgumentException("支付失败" + responseEntity.getBody());
  114. }
  115. return PayUtil.xmlStrToMap(responseEntity.getBody());
  116. }
  117. }