123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 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<StripeConfig> 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<PaypalConfig> list = paypalConfigService.list();
- for (PaypalConfig paypalConfig : list) {
- 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 {
- List<StripeConfig> 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<StripeConfig> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(StripeConfig::getId,stripeConfig.getId());
- wrapper.set(StripeConfig::getWebhookId,id);
- stripeConfigService.update(wrapper);
- }
- }
- }catch (Exception e){
- }
- }
- }
|