QrCodePlutusPaymentAdapt.java 8.0 KB

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