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 { @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 list = stripeConfigService.list(); if (!list.isEmpty()) { log.info("初始化StripeConfig配置"); list.forEach(e -> Stripe.apiKey = e.getApiKey()); } } /** * 启动创建webhooks * https://testeur.4dkankan.com/service/pay/paypal/webhook */ public void createPayPalWebhooks() { List list = paypalConfigService.list(); for (PaypalConfig paypalConfig : list) { if(StringUtils.isBlank(paypalConfig.getWebhookId())){ log.info("--------------项目启动初始化paypal-webhook-------------"); try { Map 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 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 { List list = stripeConfigService.list(); for (StripeConfig stripeConfig : list) { 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 wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(StripeConfig::getId,stripeConfig.getId()); wrapper.set(StripeConfig::getWebhookId,id); stripeConfigService.update(wrapper); } } }catch (Exception e){ } } }