Search in sources :

Example 6 with Subscription

use of org.killbill.billing.entitlement.api.Subscription in project killbill by killbill.

the class TestIntegrationWithCatalogUpdate method testWith$0RecurringPlan.

@Test(groups = "slow")
public void testWith$0RecurringPlan() throws Exception {
    // Create a per-tenant catalog with one plan
    final SimplePlanDescriptor zeroDesc = new DefaultSimplePlanDescriptor("zeroDesc-monthly", "Zero", ProductCategory.BASE, account.getCurrency(), BigDecimal.ZERO, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
    catalogUserApi.addSimplePlan(zeroDesc, init, testCallContext);
    StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
    assertEquals(catalog.getCurrentPlans().size(), 1);
    final PlanPhaseSpecifier specZero = new PlanPhaseSpecifier("zeroDesc-monthly", null);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final Entitlement baseEntitlement = entitlementApi.createBaseEntitlement(account.getId(), specZero, UUID.randomUUID().toString(), ImmutableList.<PlanPhasePriceOverride>of(), null, null, false, ImmutableList.<PluginProperty>of(), testCallContext);
    assertListenerStatus();
    Subscription refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 7, 1));
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    clock.addMonths(1);
    assertListenerStatus();
    refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 8, 1));
    busHandler.pushExpectedEvents(NextEvent.INVOICE);
    clock.addMonths(1);
    assertListenerStatus();
    refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 9, 1));
    // Add another Plan in the catalog
    final SimplePlanDescriptor descNonZero = new DefaultSimplePlanDescriptor("superfoo-monthly", "SuperFoo", ProductCategory.BASE, account.getCurrency(), new BigDecimal("20.00"), BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
    catalogUserApi.addSimplePlan(descNonZero, init, testCallContext);
    catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
    assertEquals(catalog.getCurrentPlans().size(), 2);
    final PlanPhaseSpecifier specNonZero = new PlanPhaseSpecifier("superfoo-monthly", null);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    final Entitlement baseEntitlement2 = entitlementApi.createBaseEntitlement(account.getId(), specNonZero, UUID.randomUUID().toString(), ImmutableList.<PlanPhasePriceOverride>of(), null, null, false, ImmutableList.<PluginProperty>of(), testCallContext);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 10, 1));
    Subscription refreshedBaseEntitlement2 = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement2.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement2.getChargedThroughDate(), new LocalDate(2016, 10, 1));
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 11, 1));
    refreshedBaseEntitlement2 = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement2.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement2.getChargedThroughDate(), new LocalDate(2016, 11, 1));
    busHandler.pushExpectedEvents(NextEvent.BLOCK);
    baseEntitlement.cancelEntitlementWithDateOverrideBillingPolicy(clock.getUTCToday(), BillingActionPolicy.END_OF_TERM, ImmutableList.<PluginProperty>of(), testCallContext);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.CANCEL, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 11, 1));
    refreshedBaseEntitlement2 = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement2.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement2.getChargedThroughDate(), new LocalDate(2016, 12, 1));
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    refreshedBaseEntitlement = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement.getChargedThroughDate(), new LocalDate(2016, 11, 1));
    refreshedBaseEntitlement2 = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement2.getId(), testCallContext);
    assertEquals(refreshedBaseEntitlement2.getChargedThroughDate(), new LocalDate(2017, 1, 1));
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Subscription(org.killbill.billing.entitlement.api.Subscription) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) LocalDate(org.joda.time.LocalDate) SimplePlanDescriptor(org.killbill.billing.catalog.api.SimplePlanDescriptor) DefaultSimplePlanDescriptor(org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor) DefaultSimplePlanDescriptor(org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 7 with Subscription

use of org.killbill.billing.entitlement.api.Subscription in project killbill by killbill.

the class SubscriptionResource method getTags.

@GET
@Path("/{subscriptionId:" + UUID_PATTERN + "}/" + TAGS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve subscription tags", response = TagJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid subscription id supplied"), @ApiResponse(code = 404, message = "Subscription not found") })
public Response getTags(@PathParam(ID_PARAM_NAME) final String subscriptionIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, SubscriptionApiException {
    final UUID subscriptionId = UUID.fromString(subscriptionIdString);
    final TenantContext tenantContext = context.createContext(request);
    final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(subscriptionId, tenantContext);
    return super.getTags(subscription.getAccountId(), subscriptionId, auditMode, includedDeleted, tenantContext);
}
Also used : TenantContext(org.killbill.billing.util.callcontext.TenantContext) UUID(java.util.UUID) Subscription(org.killbill.billing.entitlement.api.Subscription) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with Subscription

use of org.killbill.billing.entitlement.api.Subscription in project killbill by killbill.

the class SubscriptionResource method cancelEntitlementPlan.

@TimedResource
@DELETE
@Path("/{subscriptionId:" + UUID_PATTERN + "}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Cancel an entitlement plan")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid subscription id supplied"), @ApiResponse(code = 404, message = "Entitlement not found") })
public Response cancelEntitlementPlan(@PathParam("subscriptionId") final String subscriptionId, @QueryParam(QUERY_REQUESTED_DT) final String requestedDate, @QueryParam(QUERY_CALL_COMPLETION) @DefaultValue("false") final Boolean callCompletion, @QueryParam(QUERY_CALL_TIMEOUT) @DefaultValue("5") final long timeoutSec, @QueryParam(QUERY_ENTITLEMENT_POLICY) final String entitlementPolicyString, @QueryParam(QUERY_BILLING_POLICY) final String billingPolicyString, @QueryParam(QUERY_USE_REQUESTED_DATE_FOR_BILLING) @DefaultValue("false") final Boolean useRequestedDateForBilling, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final UriInfo uriInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws EntitlementApiException, AccountApiException, SubscriptionApiException {
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final EntitlementCallCompletionCallback<Response> callback = new EntitlementCallCompletionCallback<Response>() {

        private boolean isImmediateOp = true;

        @Override
        public Response doOperation(final CallContext ctx) throws EntitlementApiException, InterruptedException, TimeoutException, AccountApiException, SubscriptionApiException {
            final UUID uuid = UUID.fromString(subscriptionId);
            final Entitlement current = entitlementApi.getEntitlementForId(uuid, ctx);
            final LocalDate inputLocalDate = toLocalDate(requestedDate);
            final Entitlement newEntitlement;
            if (billingPolicyString == null && entitlementPolicyString == null) {
                newEntitlement = current.cancelEntitlementWithDate(inputLocalDate, useRequestedDateForBilling, pluginProperties, ctx);
            } else if (billingPolicyString == null && entitlementPolicyString != null) {
                final EntitlementActionPolicy entitlementPolicy = EntitlementActionPolicy.valueOf(entitlementPolicyString);
                newEntitlement = current.cancelEntitlementWithPolicy(entitlementPolicy, pluginProperties, ctx);
            } else if (billingPolicyString != null && entitlementPolicyString == null) {
                final BillingActionPolicy billingPolicy = BillingActionPolicy.valueOf(billingPolicyString.toUpperCase());
                newEntitlement = current.cancelEntitlementWithDateOverrideBillingPolicy(inputLocalDate, billingPolicy, pluginProperties, ctx);
            } else {
                final EntitlementActionPolicy entitlementPolicy = EntitlementActionPolicy.valueOf(entitlementPolicyString);
                final BillingActionPolicy billingPolicy = BillingActionPolicy.valueOf(billingPolicyString.toUpperCase());
                newEntitlement = current.cancelEntitlementWithPolicyOverrideBillingPolicy(entitlementPolicy, billingPolicy, pluginProperties, ctx);
            }
            final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(newEntitlement.getId(), ctx);
            final LocalDate nowInAccountTimeZone = new LocalDate(clock.getUTCNow(), subscription.getBillingEndDate().getChronology().getZone());
            isImmediateOp = subscription.getBillingEndDate() != null && !subscription.getBillingEndDate().isAfter(nowInAccountTimeZone);
            return Response.status(Status.OK).build();
        }

        @Override
        public boolean isImmOperation() {
            return isImmediateOp;
        }

        @Override
        public Response doResponseOk(final Response operationResponse) {
            return operationResponse;
        }
    };
    final EntitlementCallCompletion<Response> callCompletionCreation = new EntitlementCallCompletion<Response>();
    return callCompletionCreation.withSynchronization(callback, timeoutSec, callCompletion, callContext);
}
Also used : BillingActionPolicy(org.killbill.billing.catalog.api.BillingActionPolicy) CallContext(org.killbill.billing.util.callcontext.CallContext) LocalDate(org.joda.time.LocalDate) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) PluginProperty(org.killbill.billing.payment.api.PluginProperty) UUID(java.util.UUID) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Subscription(org.killbill.billing.entitlement.api.Subscription) EntitlementActionPolicy(org.killbill.billing.entitlement.api.Entitlement.EntitlementActionPolicy) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 9 with Subscription

use of org.killbill.billing.entitlement.api.Subscription in project killbill by killbill.

the class TestSubscription method testCancelSubscriptionInTrialWith_START_OF_TERM.

@Test(groups = "slow")
public void testCancelSubscriptionInTrialWith_START_OF_TERM() throws Exception {
    final LocalDate initialDate = new LocalDate(2015, 9, 1);
    clock.setDay(initialDate);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final Entitlement createdEntitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, account.getExternalKey(), null, initialDate, initialDate, false, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(createdEntitlement.getEffectiveStartDate().compareTo(initialDate), 0);
    assertEquals(createdEntitlement.getEffectiveEndDate(), null);
    assertListenerStatus();
    // Move clock a bit to make sure START_OF_TERM brings us back to initialDate
    clock.addDays(5);
    busHandler.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    final Entitlement cancelledEntitlement = createdEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.START_OF_TERM, null, callContext);
    assertListenerStatus();
    final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(cancelledEntitlement.getId(), callContext);
    assertEquals(subscription.getEffectiveEndDate().compareTo(new LocalDate(2015, 9, 6)), 0);
    assertEquals(subscription.getBillingEndDate().compareTo(initialDate), 0);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Subscription(org.killbill.billing.entitlement.api.Subscription) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 10 with Subscription

use of org.killbill.billing.entitlement.api.Subscription in project killbill by killbill.

the class TestWithBCDUpdate method testBCDChangeInTrial.

@Test(groups = "slow")
public void testBCDChangeInTrial() throws Exception {
    final DateTime initialDate = new DateTime(2016, 4, 1, 0, 13, 42, 0, testTimeZone);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(0));
    assertNotNull(account);
    // BP creation : Will set Account BCD to the first (2016-4-1 + 30 days = 2016-5-1)
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // 2016-4-4 : (BP still in TRIAL)
    clock.addDays(3);
    // Set next BCD to be the 15
    subscriptionBaseInternalApi.updateBCD(baseEntitlement.getId(), 15, null, internalCallContext);
    Thread.sleep(1000);
    assertListenerStatus();
    // 2016-5-15 : Catch BCD_CHANGE event
    busHandler.pushExpectedEvents(NextEvent.BCD_CHANGE, NextEvent.NULL_INVOICE);
    clock.addDays(11);
    assertListenerStatus();
    // 2016-5-1 : BP out of TRIAL
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(16);
    assertListenerStatus();
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2016, 5, 1), new LocalDate(2016, 5, 15), InvoiceItemType.RECURRING, new BigDecimal("116.64")));
    invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, expectedInvoices);
    expectedInvoices.clear();
    // 2016-5-15 : NEW BCD
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(14);
    assertListenerStatus();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2016, 5, 15), new LocalDate(2016, 6, 15), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    invoiceChecker.checkInvoice(invoices.get(2).getId(), callContext, expectedInvoices);
    expectedInvoices.clear();
    // Add cancellation with START_OF_TERM to verify BCD update is correctly interpreted
    clock.addDays(3);
    busHandler.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK, NextEvent.INVOICE);
    final Entitlement cancelledEntitlement = baseEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.START_OF_TERM, null, callContext);
    assertListenerStatus();
    final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(cancelledEntitlement.getId(), callContext);
    assertEquals(subscription.getEffectiveEndDate().compareTo(new LocalDate(2016, 5, 18)), 0);
    assertEquals(subscription.getBillingEndDate().compareTo(new LocalDate(2016, 5, 15)), 0);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) ArrayList(java.util.ArrayList) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Subscription(org.killbill.billing.entitlement.api.Subscription) Test(org.testng.annotations.Test)

Aggregations

Subscription (org.killbill.billing.entitlement.api.Subscription)11 LocalDate (org.joda.time.LocalDate)9 Entitlement (org.killbill.billing.entitlement.api.Entitlement)8 Test (org.testng.annotations.Test)8 Account (org.killbill.billing.account.api.Account)6 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)6 BigDecimal (java.math.BigDecimal)5 UUID (java.util.UUID)4 DateTime (org.joda.time.DateTime)4 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)4 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)3 Invoice (org.killbill.billing.invoice.api.Invoice)3 ArrayList (java.util.ArrayList)2 GET (javax.ws.rs.GET)2 SimplePlanDescriptor (org.killbill.billing.catalog.api.SimplePlanDescriptor)2