QrCodePlutusPaymentAdapt.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.java110.acct.smo.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.java110.acct.smo.IQrCodePaymentSMO;
  5. import com.java110.core.client.RestTemplate;
  6. import com.java110.core.factory.PlutusFactory;
  7. import com.java110.dto.smallWeChat.SmallWeChatDto;
  8. import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
  9. import com.java110.utils.cache.MappingCache;
  10. import com.java110.utils.constant.WechatConstant;
  11. import com.java110.utils.util.PayUtil;
  12. import com.java110.vo.ResultVo;
  13. import org.bouncycastle.util.encoders.Base64;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.SortedMap;
  21. import java.util.TreeMap;
  22. /**
  23. * 微信支付
  24. */
  25. @Service
  26. public class QrCodePlutusPaymentAdapt implements IQrCodePaymentSMO {
  27. private static Logger logger = LoggerFactory.getLogger(QrCodePlutusPaymentAdapt.class);
  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. public static final String PAY_UNIFIED_ORDER_URL = "https://api.plutuspay.com/open/v2/pay";
  37. @Autowired
  38. private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
  39. @Autowired
  40. private RestTemplate outRestTemplate;
  41. @Override
  42. public ResultVo pay(String communityId, String orderNum, double money, String authCode, String feeName) throws Exception {
  43. logger.info("【小程序支付】 统一下单开始, 订单编号=" + orderNum);
  44. SortedMap<String, String> resultMap = new TreeMap<String, String>();
  45. //生成支付金额,开发环境处理支付金额数到0.01、0.02、0.03元
  46. double payAmount = PayUtil.getPayAmountByEnv(MappingCache.getValue("HC_ENV"), money);
  47. //添加或更新支付记录(参数跟进自己业务需求添加)
  48. String systemName = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_GOOD_NAME);
  49. SmallWeChatDto shopSmallWeChatDto = null;
  50. SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
  51. smallWeChatDto.setObjId(communityId);
  52. List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
  53. if (smallWeChatDtos == null && smallWeChatDtos.size() < 1) {
  54. shopSmallWeChatDto = new SmallWeChatDto();
  55. shopSmallWeChatDto.setObjId(communityId);
  56. shopSmallWeChatDto.setAppId(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId"));
  57. shopSmallWeChatDto.setAppSecret(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret"));
  58. shopSmallWeChatDto.setMchId(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "mchId"));
  59. shopSmallWeChatDto.setPayPassword(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "payPassword"));
  60. } else {
  61. shopSmallWeChatDto = smallWeChatDtos.get(0);
  62. }
  63. JSONObject paramMap = new JSONObject();
  64. paramMap.put("sn", smallWeChatDto.getMchId()); // 富友分配给二级商户的商户号
  65. paramMap.put("outTradeId", orderNum);
  66. paramMap.put("authCode", authCode);
  67. paramMap.put("tradeAmount", PayUtil.moneyToIntegerStr(payAmount));
  68. paramMap.put("payTypeId", "0");
  69. String param = PlutusFactory.Encryption(paramMap.toJSONString());
  70. System.out.println(param);
  71. String str = PlutusFactory.post(PAY_UNIFIED_ORDER_URL, param);
  72. System.out.println(str);
  73. JSONObject json = JSON.parseObject(str);
  74. String signature = json.getString("signature");
  75. String content = json.getString("content");
  76. //验签
  77. Boolean verify = PlutusFactory.verify256(content, Base64.decode(signature));
  78. //验签成功
  79. if (!verify) {
  80. throw new IllegalArgumentException("支付失败签名失败");
  81. }
  82. //解密
  83. byte[] bb = PlutusFactory.decrypt(Base64.decode(content), PlutusFactory.SECRET_KEY);
  84. //服务器返回内容
  85. String paramOut = new String(bb);
  86. JSONObject paramObj = JSONObject.parseObject(paramOut);
  87. if ("1".equals(paramObj.get("status"))) {
  88. return new ResultVo(ResultVo.CODE_OK, "成功");
  89. } else {
  90. return new ResultVo(ResultVo.CODE_ERROR, paramObj.getString("remark"));
  91. }
  92. }
  93. public ResultVo checkPayFinish(String communityId, String orderNum) {
  94. SmallWeChatDto shopSmallWeChatDto = null;
  95. Map<String, String> result = null;
  96. SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
  97. smallWeChatDto.setObjId(communityId);
  98. List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
  99. if (smallWeChatDtos == null && smallWeChatDtos.size() < 1) {
  100. shopSmallWeChatDto = new SmallWeChatDto();
  101. shopSmallWeChatDto.setObjId(communityId);
  102. shopSmallWeChatDto.setAppId(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId"));
  103. shopSmallWeChatDto.setAppSecret(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret"));
  104. shopSmallWeChatDto.setMchId(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "mchId"));
  105. shopSmallWeChatDto.setPayPassword(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "payPassword"));
  106. } else {
  107. shopSmallWeChatDto = smallWeChatDtos.get(0);
  108. }
  109. JSONObject paramMap = new JSONObject();
  110. paramMap.put("sn", smallWeChatDto.getMchId()); // 富友分配给二级商户的商户号
  111. paramMap.put("outTradeId", orderNum);
  112. String param = PlutusFactory.Encryption(paramMap.toJSONString());
  113. System.out.println(param);
  114. String str = PlutusFactory.post(PAY_UNIFIED_ORDER_URL, param);
  115. System.out.println(str);
  116. JSONObject json = JSON.parseObject(str);
  117. String signature = json.getString("signature");
  118. String content = json.getString("content");
  119. //验签
  120. Boolean verify = PlutusFactory.verify256(content, Base64.decode(signature));
  121. //验签成功
  122. if (!verify) {
  123. throw new IllegalArgumentException("支付失败签名失败");
  124. }
  125. //解密
  126. byte[] bb = PlutusFactory.decrypt(Base64.decode(content), PlutusFactory.SECRET_KEY);
  127. //服务器返回内容
  128. String paramOut = new String(bb);
  129. JSONObject paramObj = JSONObject.parseObject(paramOut);
  130. if ("1".equals(paramObj.get("status"))) {
  131. return new ResultVo(ResultVo.CODE_OK, "成功");
  132. } else if ("0".equals(paramObj.get("status"))) {
  133. return new ResultVo(ResultVo.CODE_WAIT_PAY, "等待支付完成");
  134. } else {
  135. return new ResultVo(ResultVo.CODE_ERROR, paramObj.getString("remark"));
  136. }
  137. }
  138. }