PayConfig.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.fdkankan.pay.config;
  2. import cn.hutool.http.HttpRequest;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.pay.entity.AliConfig;
  6. import com.fdkankan.pay.entity.PaypalConfig;
  7. import com.fdkankan.pay.entity.StripeConfig;
  8. import com.fdkankan.pay.entity.WxConfig;
  9. import com.fdkankan.pay.service.IAliConfigService;
  10. import com.fdkankan.pay.service.IPaypalConfigService;
  11. import com.fdkankan.pay.service.IStripeConfigService;
  12. import com.fdkankan.pay.service.IWxConfigService;
  13. import com.fdkankan.pay.util.paypal.restApi.vo.EventTypeVo;
  14. import com.fdkankan.pay.util.paypal.restApi.vo.WebhookVo;
  15. import com.stripe.Stripe;
  16. import com.stripe.model.WebhookEndpoint;
  17. import com.stripe.param.WebhookEndpointCreateParams;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.context.annotation.Configuration;
  22. import javax.annotation.PostConstruct;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. @Configuration
  28. @Slf4j
  29. public class PayConfig {
  30. @Autowired
  31. IAliConfigService aliConfigService;
  32. @Autowired
  33. IWxConfigService wxConfigService;
  34. @Autowired
  35. IPaypalConfigService paypalConfigService;
  36. @Autowired
  37. IStripeConfigService stripeConfigService;
  38. @PostConstruct
  39. public void init(){
  40. setConfig();
  41. createPayPalWebhooks();
  42. createStripeWebhooks();
  43. }
  44. public void setConfig() {
  45. log.info("启动初始化支付配置");
  46. List<StripeConfig> list = stripeConfigService.list();
  47. if (!list.isEmpty()) {
  48. log.info("初始化StripeConfig配置");
  49. list.forEach(e -> Stripe.apiKey = e.getApiKey());
  50. }
  51. }
  52. /**
  53. * 启动创建webhooks
  54. * https://testeur.4dkankan.com/service/pay/paypal/webhook
  55. */
  56. public void createPayPalWebhooks() {
  57. List<PaypalConfig> list = paypalConfigService.list();
  58. for (PaypalConfig paypalConfig : list) {
  59. if(StringUtils.isBlank(paypalConfig.getWebhookId())){
  60. log.info("--------------项目启动初始化paypal-webhook-------------");
  61. try {
  62. Map<String,String> map = new HashMap<>(4);
  63. map.put("Content-Type","application/json");
  64. WebhookVo webhookVo = new WebhookVo();
  65. webhookVo.setUrl(paypalConfig.getWebhookHost() +"/service/pay/paypal/webhook");
  66. webhookVo.getEvent_types().add(new EventTypeVo("PAYMENT.SALE.COMPLETED"));
  67. webhookVo.getEvent_types().add(new EventTypeVo("CATALOG.PRODUCT.CREATED"));
  68. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.PLAN.CREATED"));
  69. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.PLAN.ACTIVATED"));
  70. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.PLAN.DEACTIVATED"));
  71. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.CREATED"));
  72. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.EXPIRED"));
  73. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.SUSPENDED"));
  74. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.UPDATED"));
  75. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.PAYMENT.FAILED"));
  76. webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.CANCELLED"));
  77. String string = JSONObject.toJSONString(webhookVo);
  78. String body = HttpRequest.post(paypalConfig.getBaseUrl() + "/v1/notifications/webhooks")
  79. .addHeaders(map)
  80. .basicAuth(paypalConfig.getClientId(), paypalConfig.getSecret())
  81. .body(string)
  82. .execute().body();
  83. log.info("paypal-createWebhooks:{}",body);
  84. JSONObject resp = JSONObject.parseObject(body);
  85. String webhookId = resp.getString("id");
  86. paypalConfig.setWebhookId(webhookId);
  87. LambdaUpdateWrapper<PaypalConfig> wrapper = new LambdaUpdateWrapper<>();
  88. wrapper.eq(PaypalConfig::getId,paypalConfig.getId());
  89. wrapper.set(PaypalConfig::getWebhookId,webhookId);
  90. paypalConfigService.update(wrapper);
  91. }catch (Exception e){
  92. log.info("paypal-createWebhooks:error:",e);
  93. }
  94. }
  95. }
  96. }
  97. public void createStripeWebhooks(){
  98. try {
  99. List<StripeConfig> list = stripeConfigService.list();
  100. for (StripeConfig stripeConfig : list) {
  101. if(StringUtils.isBlank(stripeConfig.getWebhookId())){
  102. Stripe.apiKey = stripeConfig.getApiKey();
  103. WebhookEndpointCreateParams params =
  104. WebhookEndpointCreateParams.builder()
  105. .addEnabledEvent(WebhookEndpointCreateParams.EnabledEvent.ALL)
  106. .setUrl(stripeConfig.getWebhookHost()+"/service/pay/stripe/webhook")
  107. .build();
  108. WebhookEndpoint webhookEndpoint = WebhookEndpoint.create(params);
  109. String id = webhookEndpoint.getId();
  110. LambdaUpdateWrapper<StripeConfig> wrapper = new LambdaUpdateWrapper<>();
  111. wrapper.eq(StripeConfig::getId,stripeConfig.getId());
  112. wrapper.set(StripeConfig::getWebhookId,id);
  113. stripeConfigService.update(wrapper);
  114. }
  115. }
  116. }catch (Exception e){
  117. }
  118. }
  119. }