From 91967f43dd802a8ee4345f2b6f3b2bcf8ca8f16e Mon Sep 17 00:00:00 2001 From: reshmabidikar Date: Tue, 30 Apr 2024 13:46:07 +0530 Subject: [PATCH] Entitlement plugin --- pom.xml | 5 ++ .../helloworld/HelloWorldActivator.java | 11 +++ .../HelloWorldEntitlementPluginApi.java | 86 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldEntitlementPluginApi.java diff --git a/pom.xml b/pom.xml index 63177f5..484d9d8 100644 --- a/pom.xml +++ b/pom.xml @@ -101,6 +101,11 @@ killbill-platform-osgi-bundles-lib-killbill provided + + org.kill-bill.billing.plugin + killbill-plugin-api-entitlement + provided + org.kill-bill.billing.plugin killbill-plugin-api-invoice diff --git a/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldActivator.java b/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldActivator.java index 1f3ca97..a46d854 100644 --- a/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldActivator.java +++ b/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldActivator.java @@ -25,6 +25,7 @@ import javax.servlet.Servlet; import javax.servlet.http.HttpServlet; +import org.killbill.billing.entitlement.plugin.api.EntitlementPluginApi; import org.killbill.billing.invoice.plugin.api.InvoicePluginApi; import org.killbill.billing.osgi.api.Healthcheck; import org.killbill.billing.osgi.api.OSGIPluginProperties; @@ -91,6 +92,10 @@ public void start(final BundleContext context) throws Exception { final InvoicePluginApi invoicePluginApi = new HelloWorldInvoicePluginApi(killbillAPI, configProperties, null); registerInvoicePluginApi(context, invoicePluginApi); + final EntitlementPluginApi entitlementPluginApi = new HelloWorldEntitlementPluginApi(killbillAPI); + registerEntitlementPluginApi(context, entitlementPluginApi); + + // Register a servlet (optional) final PluginApp pluginApp = new PluginAppBuilder(PLUGIN_NAME, killbillAPI, dataSource, super.clock, configProperties).withRouteClass(HelloWorldServlet.class) @@ -139,4 +144,10 @@ private void registerHealthcheck(final BundleContext context, final Healthcheck props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME); registrar.registerService(context, Healthcheck.class, healthcheck, props); } + + private void registerEntitlementPluginApi(final BundleContext context, final EntitlementPluginApi api) { + final Hashtable props = new Hashtable(); + props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME); + registrar.registerService(context, EntitlementPluginApi.class, api, props); + } } diff --git a/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldEntitlementPluginApi.java b/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldEntitlementPluginApi.java new file mode 100644 index 0000000..3fea875 --- /dev/null +++ b/src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldEntitlementPluginApi.java @@ -0,0 +1,86 @@ +/* + * Copyright 2020-2024 Equinix, Inc + * Copyright 2014-2024 The Billing Project, LLC + * + * The Billing Project licenses this file to you under the Apache License, version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.killbill.billing.plugin.helloworld; + +import java.util.List; + +import org.killbill.billing.catalog.api.Plan; +import org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier; +import org.killbill.billing.entitlement.api.Subscription; +import org.killbill.billing.entitlement.api.SubscriptionApiException; +import org.killbill.billing.entitlement.api.SubscriptionBundle; +import org.killbill.billing.entitlement.plugin.api.EntitlementContext; +import org.killbill.billing.entitlement.plugin.api.EntitlementPluginApi; +import org.killbill.billing.entitlement.plugin.api.EntitlementPluginApiException; +import org.killbill.billing.entitlement.plugin.api.OnFailureEntitlementResult; +import org.killbill.billing.entitlement.plugin.api.OnSuccessEntitlementResult; +import org.killbill.billing.entitlement.plugin.api.OperationType; +import org.killbill.billing.entitlement.plugin.api.PriorEntitlementResult; +import org.killbill.billing.entitlement.plugin.api.boilerplate.plugin.PriorEntitlementResultImp; +import org.killbill.billing.osgi.libs.killbill.OSGIKillbillAPI; +import org.killbill.billing.payment.api.PluginProperty; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HelloWorldEntitlementPluginApi implements EntitlementPluginApi { + + public static final Logger logger = LoggerFactory.getLogger(HelloWorldEntitlementPluginApi.class); + + private final OSGIKillbillAPI killbillAPI; + + public HelloWorldEntitlementPluginApi(final OSGIKillbillAPI killbillAPI) { + this.killbillAPI = killbillAPI; + } + + @Override + public PriorEntitlementResult priorCall(final EntitlementContext context, final Iterable properties) throws EntitlementPluginApiException { + if (context.getOperationType() == OperationType.CREATE_SHOPPING_CART_SUBSCRIPTIONS || context.getOperationType() == OperationType.CREATE_SUBSCRIPTION || context.getOperationType() == OperationType.CREATE_SUBSCRIPTIONS_WITH_AO) { + Iterable baseEntList = context.getBaseEntitlementWithAddOnsSpecifiers(); + String newplanName = baseEntList.iterator().next().getEntitlementSpecifier().iterator().next().getPlanPhaseSpecifier().getPlanName(); + logger.info("New planName: {}", newplanName); + List bundles = null; + try { + bundles = killbillAPI.getSubscriptionApi().getSubscriptionBundlesForAccountId(context.getAccountId(), context); + for (SubscriptionBundle bundle : bundles) { + List subscriptions = bundle.getSubscriptions(); + for (Subscription subscription : subscriptions) { + Plan plan = subscription.getLastActivePlan(); + logger.info("Plan is {}", plan.getName()); + if (plan.getName().equals(newplanName)) { + logger.info("Subscription already exists, blocking operation"); + return new PriorEntitlementResultImp.Builder<>().withIsAborted(true).build(); + } + } + } + } catch (SubscriptionApiException e) { + throw new RuntimeException(e); + } + } + return null; + } + + @Override + public OnSuccessEntitlementResult onSuccessCall(final EntitlementContext context, final Iterable properties) throws EntitlementPluginApiException { + return null; + } + + @Override + public OnFailureEntitlementResult onFailureCall(final EntitlementContext context, final Iterable properties) throws EntitlementPluginApiException { + return null; + } +}