QrCodeSpdbPaymentAdapt.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.java110.acct.smo.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.acct.payment.adapt.spdb.SPDBApiClient;
  4. import com.java110.acct.payment.adapt.spdb.SPDBApiResponse;
  5. import com.java110.acct.payment.adapt.spdb.SPDBSecurity;
  6. import com.java110.acct.smo.IQrCodePaymentSMO;
  7. import com.java110.core.client.RestTemplate;
  8. import com.java110.core.factory.CommunitySettingFactory;
  9. import com.java110.core.log.LoggerFactory;
  10. import com.java110.dto.smallWeChat.SmallWeChatDto;
  11. import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
  12. import com.java110.utils.cache.CommonCache;
  13. import com.java110.utils.cache.MappingCache;
  14. import com.java110.utils.cache.UrlCache;
  15. import com.java110.utils.util.DateUtil;
  16. import com.java110.utils.util.PayUtil;
  17. import com.java110.vo.ResultVo;
  18. import org.slf4j.Logger;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.util.*;
  22. /**
  23. * 工商银行支付
  24. */
  25. @Service
  26. public class QrCodeSpdbPaymentAdapt implements IQrCodePaymentSMO {
  27. private static Logger logger = LoggerFactory.getLogger(QrCodeSpdbPaymentAdapt.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. public static final String wxPayUnifiedOrder = "https://api.spdb.com.cn/spdb/prd/api/acquiring/appPay/initiation";
  35. @Autowired
  36. private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
  37. @Autowired
  38. private RestTemplate outRestTemplate;
  39. @Override
  40. public ResultVo pay(String communityId, String orderNum, double money, String authCode, String feeName) throws Exception {
  41. logger.info("【浦发银行支付】 统一下单开始, 订单编号=" + orderNum);
  42. String notifyUrl = UrlCache.getOwnerUrl() + "/app/payment/notify/wechat/992020011134400001";
  43. //生成支付金额,开发环境处理支付金额数到0.01、0.02、0.03元
  44. // double payAmount = PayUtil.getPayAmountByEnv(MappingCache.getValue(MappingConstant.ENV_DOMAIN,"HC_ENV"), money);
  45. double payAmount = PayUtil.getPayAmountByEnv(MappingCache.getValue("HC_ENV"), money);
  46. //添加或更新支付记录(参数跟进自己业务需求添加)
  47. CommonCache.setValue("spdb_qrcode_payment_" + orderNum, payAmount + "", CommonCache.PAY_DEFAULT_EXPIRE_TIME);
  48. String clientId = CommunitySettingFactory.getValue(communityId, "SPDB_CLIENT_ID");
  49. String secret = CommunitySettingFactory.getValue(communityId, "SPDB_SECRET");
  50. String privateKey = CommunitySettingFactory.getRemark(communityId, "SPDB_PRIVATE_KEY");
  51. String apiPublicKey = CommunitySettingFactory.getRemark(communityId, "SPDB_PUBLIC_KEY");
  52. String terminalNo = CommunitySettingFactory.getValue(communityId, "SPDB_TERMINAL_NO");
  53. String spdbMrchNo = CommunitySettingFactory.getValue(communityId, "SPDB_MRCH_NO");
  54. SPDBSecurity spdbSecurity = new SPDBSecurity(clientId, secret, privateKey, apiPublicKey);
  55. SPDBApiClient spdbApiClient = new SPDBApiClient(spdbSecurity);
  56. if (feeName.length() > 127) {
  57. feeName = feeName.substring(0, 126);
  58. }
  59. JSONObject paramIn = new JSONObject();
  60. paramIn.put("ordTtl", feeName);
  61. paramIn.put("terminalNo", terminalNo);
  62. // paramIn.put("busnPckt", terminalNo);
  63. paramIn.put("spdbMrchNo", spdbMrchNo);
  64. paramIn.put("createTimep", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT));
  65. paramIn.put("mrchlInfmAdr", notifyUrl);
  66. paramIn.put("cmdtyDsc", feeName);
  67. paramIn.put("terminaIP", PayUtil.getLocalIp());
  68. paramIn.put("channelNo", "00");
  69. Calendar c = Calendar.getInstance();
  70. c.add(Calendar.HOUR_OF_DAY, 1);
  71. paramIn.put("trnsFnshStrtTm", DateUtil.getFormatTimeString(c.getTime(), DateUtil.DATE_FORMATE_STRING_DEFAULT));
  72. paramIn.put("frmrMrchOrdrNo", orderNum);
  73. paramIn.put("tranAmt", payAmount);
  74. paramIn.put("prblmNo", orderNum);
  75. int pre = Integer.parseInt(authCode.substring(0, 2));
  76. if (pre > 24 && pre < 31) { // 支付宝
  77. paramIn.put("tranType", "OF");
  78. } else {
  79. paramIn.put("tranType", "OC");
  80. }
  81. paramIn.put("authrCd", authCode);
  82. SPDBApiResponse response = spdbApiClient.post(wxPayUnifiedOrder, paramIn.toJSONString());
  83. if (response.getHttpStatus() != 0000) {
  84. throw new IllegalArgumentException(response.getResBody());
  85. }
  86. logger.debug("统一下单返回" + response.getResBody());
  87. JSONObject paramOut = JSONObject.parseObject(response.getResBody());
  88. if (!"0000".equals(paramOut.getString("statusCode"))) {
  89. throw new IllegalArgumentException(paramOut.getString("statusMsg"));
  90. }
  91. if (!"00".equals(paramOut.getString("ordrSt"))) {
  92. return new ResultVo(ResultVo.CODE_ERROR, "未知异常");
  93. }
  94. return new ResultVo(ResultVo.CODE_OK, "成功");
  95. }
  96. public ResultVo checkPayFinish(String communityId, String orderNum) {
  97. SmallWeChatDto shopSmallWeChatDto = null;
  98. Map<String, String> result = null;
  99. String clientId = CommunitySettingFactory.getValue(communityId, "SPDB_CLIENT_ID");
  100. String secret = CommunitySettingFactory.getValue(communityId, "SPDB_SECRET");
  101. String privateKey = CommunitySettingFactory.getRemark(communityId, "SPDB_PRIVATE_KEY");
  102. String apiPublicKey = CommunitySettingFactory.getRemark(communityId, "SPDB_PUBLIC_KEY");
  103. String terminalNo = CommunitySettingFactory.getValue(communityId, "SPDB_TERMINAL_NO");
  104. String spdbMrchNo = CommunitySettingFactory.getValue(communityId, "SPDB_MRCH_NO");
  105. try {
  106. SPDBSecurity spdbSecurity = new SPDBSecurity(clientId, secret, privateKey, apiPublicKey);
  107. SPDBApiClient spdbApiClient = new SPDBApiClient(spdbSecurity);
  108. JSONObject paramIn = new JSONObject();
  109. paramIn.put("mrchOrdrNo", orderNum);
  110. paramIn.put("spdbMrchNo", spdbMrchNo);
  111. paramIn.put("tranDate", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H));
  112. String payAmount = CommonCache.getValue("spdb_qrcode_payment_" + orderNum);
  113. paramIn.put("tranAmt", payAmount);
  114. SPDBApiResponse response = spdbApiClient.post(wxPayUnifiedOrder, paramIn.toJSONString());
  115. if (response.getHttpStatus() != 0000) {
  116. throw new IllegalArgumentException(response.getResBody());
  117. }
  118. logger.debug("统一下单返回" + response.getResBody());
  119. JSONObject paramOut = JSONObject.parseObject(response.getResBody());
  120. if (!"0000".equals(paramOut.getString("statusCode"))) {
  121. throw new IllegalArgumentException(paramOut.getString("statusMsg"));
  122. }
  123. if ("00".equals(paramOut.getString("ordrSt"))) {
  124. return new ResultVo(ResultVo.CODE_OK, "成功");
  125. } else if ("09".equals(paramOut.getString("ordrSt"))) {
  126. return new ResultVo(ResultVo.CODE_WAIT_PAY, "等待支付完成");
  127. } else {
  128. return new ResultVo(ResultVo.CODE_ERROR, "支付已经被取消,银行 状态码:" + paramOut.getString("ordrSt"));
  129. }
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. return new ResultVo(ResultVo.CODE_ERROR, "未知异常" + e.getMessage());
  133. }
  134. }
  135. }