|
@@ -0,0 +1,165 @@
|
|
|
+package com.fdkankan.pay.config;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.fdkankan.pay.entity.AliConfig;
|
|
|
+import com.fdkankan.pay.entity.PaypalConfig;
|
|
|
+import com.fdkankan.pay.entity.StripeConfig;
|
|
|
+import com.fdkankan.pay.entity.WxConfig;
|
|
|
+import com.fdkankan.pay.service.IAliConfigService;
|
|
|
+import com.fdkankan.pay.service.IPaypalConfigService;
|
|
|
+import com.fdkankan.pay.service.IStripeConfigService;
|
|
|
+import com.fdkankan.pay.service.IWxConfigService;
|
|
|
+import com.fdkankan.pay.util.paypal.restApi.vo.EventTypeVo;
|
|
|
+import com.fdkankan.pay.util.paypal.restApi.vo.WebhookVo;
|
|
|
+import com.stripe.Stripe;
|
|
|
+import com.stripe.model.WebhookEndpoint;
|
|
|
+import com.stripe.param.WebhookEndpointCreateParams;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@Slf4j
|
|
|
+public class PayConfig {
|
|
|
+ public static HashMap<Integer,AliConfig> aliConfigMap = new HashMap<>();
|
|
|
+ public static HashMap<Integer,WxConfig> wxConfigMap = new HashMap<>();
|
|
|
+ public static HashMap<Integer,PaypalConfig> paypalConfigMap = new HashMap<>();
|
|
|
+ public static HashMap<Integer,StripeConfig> stripeConfigMap = new HashMap<>();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ IAliConfigService aliConfigService;
|
|
|
+ @Autowired
|
|
|
+ IWxConfigService wxConfigService;
|
|
|
+ @Autowired
|
|
|
+ IPaypalConfigService paypalConfigService;
|
|
|
+ @Autowired
|
|
|
+ IStripeConfigService stripeConfigService;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init(){
|
|
|
+ setConfig();
|
|
|
+ createPayPalWebhooks();
|
|
|
+ createStripeWebhooks();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setConfig() {
|
|
|
+ log.info("启动初始化支付配置");
|
|
|
+ List<AliConfig> aliConfigList = aliConfigService.list();
|
|
|
+ if (!aliConfigList.isEmpty()) {
|
|
|
+ log.info("初始化AliConfig配置");
|
|
|
+ aliConfigList.forEach(e -> aliConfigMap.put(e.getServeId(),e));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<WxConfig> wxConfigList = wxConfigService.list();
|
|
|
+ if (!wxConfigList.isEmpty()) {
|
|
|
+ log.info("初始化wxConfig配置");
|
|
|
+ wxConfigList.forEach(e -> wxConfigMap.put(e.getServeId(),e));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<PaypalConfig> paypalConfigList = paypalConfigService.list();
|
|
|
+ if (!paypalConfigList.isEmpty()) {
|
|
|
+ log.info("初始化PaypalConfig配置");
|
|
|
+ paypalConfigList.forEach(e -> paypalConfigMap.put(e.getServeId(),e));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<StripeConfig> list = stripeConfigService.list();
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ log.info("初始化StripeConfig配置");
|
|
|
+ list.forEach(e -> stripeConfigMap.put(e.getServeId(),e));
|
|
|
+ list.forEach(e -> Stripe.apiKey = e.getApiKey());
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动创建webhooks
|
|
|
+ * https://testeur.4dkankan.com/service/pay/paypal/webhook
|
|
|
+ */
|
|
|
+ public void createPayPalWebhooks() {
|
|
|
+ Set<Integer> list = paypalConfigMap.keySet();
|
|
|
+ for (Integer key : list) {
|
|
|
+ PaypalConfig paypalConfig = paypalConfigMap.get(key);
|
|
|
+ if(StringUtils.isBlank(paypalConfig.getWebhookId())){
|
|
|
+ log.info("--------------项目启动初始化paypal-webhook-------------");
|
|
|
+ try {
|
|
|
+ Map<String,String> map = new HashMap<>(4);
|
|
|
+ map.put("Content-Type","application/json");
|
|
|
+ WebhookVo webhookVo = new WebhookVo();
|
|
|
+ webhookVo.setUrl(paypalConfig.getWebhookHost() +"/service/pay/paypal/webhook");
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("PAYMENT.SALE.COMPLETED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("CATALOG.PRODUCT.CREATED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.PLAN.CREATED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.PLAN.ACTIVATED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.PLAN.DEACTIVATED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.CREATED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.EXPIRED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.SUSPENDED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.UPDATED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.PAYMENT.FAILED"));
|
|
|
+ webhookVo.getEvent_types().add(new EventTypeVo("BILLING.SUBSCRIPTION.CANCELLED"));
|
|
|
+
|
|
|
+ String string = JSONObject.toJSONString(webhookVo);
|
|
|
+ String body = HttpRequest.post(paypalConfig.getBaseUrl() + "/v1/notifications/webhooks")
|
|
|
+ .addHeaders(map)
|
|
|
+ .basicAuth(paypalConfig.getClientId(), paypalConfig.getSecret())
|
|
|
+ .body(string)
|
|
|
+ .execute().body();
|
|
|
+ log.info("paypal-createWebhooks:{}",body);
|
|
|
+ JSONObject resp = JSONObject.parseObject(body);
|
|
|
+ String webhookId = resp.getString("id");
|
|
|
+ paypalConfig.setWebhookId(webhookId);
|
|
|
+
|
|
|
+ LambdaUpdateWrapper<PaypalConfig> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(PaypalConfig::getId,paypalConfig.getId());
|
|
|
+ wrapper.set(PaypalConfig::getWebhookId,webhookId);
|
|
|
+ paypalConfigService.update(wrapper);
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("paypal-createWebhooks:error:",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void createStripeWebhooks(){
|
|
|
+ try {
|
|
|
+ Set<Integer> keySet = stripeConfigMap.keySet();
|
|
|
+ for (Integer key : keySet) {
|
|
|
+ StripeConfig stripeConfig = stripeConfigMap.get(key);
|
|
|
+ if(StringUtils.isBlank(stripeConfig.getWebhookId())){
|
|
|
+
|
|
|
+ Stripe.apiKey = stripeConfig.getApiKey();
|
|
|
+ WebhookEndpointCreateParams params =
|
|
|
+ WebhookEndpointCreateParams.builder()
|
|
|
+ .addEnabledEvent(WebhookEndpointCreateParams.EnabledEvent.ALL)
|
|
|
+ .setUrl(stripeConfig.getWebhookHost()+"/service/pay/stripe/webhook")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ WebhookEndpoint webhookEndpoint = WebhookEndpoint.create(params);
|
|
|
+ String id = webhookEndpoint.getId();
|
|
|
+
|
|
|
+ LambdaUpdateWrapper<StripeConfig> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(StripeConfig::getId,stripeConfig.getId());
|
|
|
+ wrapper.set(StripeConfig::getWebhookId,id);
|
|
|
+ stripeConfigService.update(wrapper);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|