AdapayService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import com.huifu.adapay.core.exception.BaseAdaPayException;
  5. import com.huifu.adapay.model.AdapayCommon;
  6. import com.huifu.adapay.model.SettleAccount;
  7. import com.izouma.nineth.config.AdapayProperties;
  8. import com.izouma.nineth.dto.Balance;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import lombok.AllArgsConstructor;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.collections.MapUtils;
  13. import org.springframework.stereotype.Service;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. @AllArgsConstructor
  17. @Service
  18. @Slf4j
  19. public class AdapayService {
  20. private final AdapayProperties adapayProperties;
  21. public String createMember(Long id, String tel, String name, String idNo) throws BaseAdaPayException {
  22. Map<String, Object> memberParams = new HashMap<>();
  23. memberParams.put("adapay_func_code", "members.realname");
  24. memberParams.put("member_id", id.toString());
  25. memberParams.put("app_id", adapayProperties.getAppId());
  26. memberParams.put("tel_no", tel);
  27. memberParams.put("user_name", name);
  28. memberParams.put("cert_type", "00");
  29. memberParams.put("cert_id", idNo);
  30. Map<String, Object> member = AdapayCommon.requestAdapay(memberParams);
  31. log.info("createMember\n{}", JSON.toJSONString(member, SerializerFeature.PrettyFormat));
  32. try {
  33. checkSuccess(member);
  34. } catch (BusinessException e) {
  35. if ("member_id_exists".equals(e.getMessage())) {
  36. return id.toString();
  37. } else {
  38. throw e;
  39. }
  40. }
  41. return MapUtils.getString(member, "member_id");
  42. }
  43. public String createSettleAccount(String memberId, String realName, String idNo, String phone, String bankNo) throws BaseAdaPayException {
  44. Map<String, Object> settleCountParams = new HashMap<>();
  45. Map<String, Object> accountInfo = new HashMap<>();
  46. accountInfo.put("card_id", bankNo);
  47. accountInfo.put("card_name", realName);
  48. accountInfo.put("cert_id", idNo);
  49. accountInfo.put("cert_type", "00");
  50. accountInfo.put("tel_no", phone);
  51. accountInfo.put("bank_acct_type", "2");
  52. settleCountParams.put("member_id", memberId);
  53. settleCountParams.put("app_id", adapayProperties.getAppId());
  54. settleCountParams.put("channel", "bank_account");
  55. settleCountParams.put("account_info", accountInfo);
  56. Map<String, Object> settleCount = SettleAccount.create(settleCountParams);
  57. log.info("createSettleAccount\n{}", JSON.toJSONString(settleCount, SerializerFeature.PrettyFormat));
  58. checkSuccess(settleCount);
  59. return MapUtils.getString(settleCount, "id");
  60. }
  61. public void delSettleAccount(String memberId, String accountId) throws BaseAdaPayException {
  62. Map<String, Object> settleCountParams = new HashMap<>();
  63. settleCountParams.put("settle_account_id", accountId);
  64. settleCountParams.put("member_id", memberId);
  65. settleCountParams.put("app_id", adapayProperties.getAppId());
  66. Map<String, Object> settleCount = SettleAccount.delete(settleCountParams);
  67. log.info("delSettleAccount\n{}", JSON.toJSONString(settleCount, SerializerFeature.PrettyFormat));
  68. checkSuccess(settleCount);
  69. }
  70. public Balance queryBalance(String memberId, String settleAccountId) throws BaseAdaPayException {
  71. Map<String, Object> queryParams = new HashMap<>();
  72. queryParams.put("settle_account_id", settleAccountId);
  73. queryParams.put("member_id", memberId);
  74. queryParams.put("app_id", adapayProperties.getAppId());
  75. Map<String, Object> settleCount = SettleAccount.balance(queryParams);
  76. String json = JSON.toJSONString(settleCount, SerializerFeature.PrettyFormat);
  77. log.info("queryBalance\n{}", JSON.toJSONString(settleCount, SerializerFeature.PrettyFormat));
  78. checkSuccess(settleCount);
  79. return JSON.parseObject(json, Balance.class);
  80. }
  81. public static void checkSuccess(Map<String, Object> map) {
  82. if (!"succeeded".equals(MapUtils.getString(map, "status"))) {
  83. if ("member_id_exists".equals(MapUtils.getString(map, "error_code"))) {
  84. throw new BusinessException("member_id_exists");
  85. }
  86. String errMsg = MapUtils.getString(map, "error_msg");
  87. String errCode = MapUtils.getString(map, "error_code");
  88. throw new BusinessException(errMsg + "(" + errCode + ")");
  89. }
  90. }
  91. }