Skip to content

Commit

Permalink
Entitlement plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
reshmabidikar committed Apr 30, 2024
1 parent 082b044 commit 91967f4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<artifactId>killbill-platform-osgi-bundles-lib-killbill</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.billing.plugin</groupId>
<artifactId>killbill-plugin-api-entitlement</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.billing.plugin</groupId>
<artifactId>killbill-plugin-api-invoice</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<String, String> props = new Hashtable<String, String>();
props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME);
registrar.registerService(context, EntitlementPluginApi.class, api, props);
}
}
Original file line number Diff line number Diff line change
@@ -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<PluginProperty> properties) throws EntitlementPluginApiException {
if (context.getOperationType() == OperationType.CREATE_SHOPPING_CART_SUBSCRIPTIONS || context.getOperationType() == OperationType.CREATE_SUBSCRIPTION || context.getOperationType() == OperationType.CREATE_SUBSCRIPTIONS_WITH_AO) {
Iterable<BaseEntitlementWithAddOnsSpecifier> baseEntList = context.getBaseEntitlementWithAddOnsSpecifiers();
String newplanName = baseEntList.iterator().next().getEntitlementSpecifier().iterator().next().getPlanPhaseSpecifier().getPlanName();
logger.info("New planName: {}", newplanName);
List<SubscriptionBundle> bundles = null;
try {
bundles = killbillAPI.getSubscriptionApi().getSubscriptionBundlesForAccountId(context.getAccountId(), context);
for (SubscriptionBundle bundle : bundles) {
List<Subscription> 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<PluginProperty> properties) throws EntitlementPluginApiException {
return null;
}

@Override
public OnFailureEntitlementResult onFailureCall(final EntitlementContext context, final Iterable<PluginProperty> properties) throws EntitlementPluginApiException {
return null;
}
}

0 comments on commit 91967f4

Please sign in to comment.