Search in sources :

Example 91 with PluginProperty

use of org.killbill.billing.payment.api.PluginProperty 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 92 with PluginProperty

use of org.killbill.billing.payment.api.PluginProperty in project killbill by killbill.

the class AccountResource method createPaymentMethod.

@TimedResource
@POST
@Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Add a payment method")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response createPaymentMethod(final PaymentMethodJson json, @PathParam("accountId") final String accountId, @QueryParam(QUERY_PAYMENT_METHOD_IS_DEFAULT) @DefaultValue("false") final Boolean isDefault, @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false") final Boolean payAllUnpaidInvoices, @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 AccountApiException, PaymentApiException {
    verifyNonNullOrEmpty(json, "PaymentMethodJson body should be specified");
    verifyNonNullOrEmpty(json.getPluginName(), "PaymentMethodJson pluginName should be specified");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final PaymentMethod data = json.toPaymentMethod(accountId);
    final Account account = accountUserApi.getAccountById(data.getAccountId(), callContext);
    final boolean hasDefaultPaymentMethod = account.getPaymentMethodId() != null || isDefault;
    final Collection<Invoice> unpaidInvoices = payAllUnpaidInvoices ? invoiceApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext) : Collections.<Invoice>emptyList();
    if (payAllUnpaidInvoices && unpaidInvoices.size() > 0 && !hasDefaultPaymentMethod) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    final UUID paymentMethodId = paymentApi.addPaymentMethod(account, data.getExternalKey(), data.getPluginName(), isDefault, data.getPluginDetail(), pluginProperties, callContext);
    if (payAllUnpaidInvoices && unpaidInvoices.size() > 0) {
        for (final Invoice invoice : unpaidInvoices) {
            createPurchaseForInvoice(account, invoice.getId(), invoice.getBalance(), paymentMethodId, false, null, null, pluginProperties, callContext);
        }
    }
    return uriBuilder.buildResponse(uriInfo, PaymentMethodResource.class, "getPaymentMethod", paymentMethodId, request);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 93 with PluginProperty

use of org.killbill.billing.payment.api.PluginProperty in project killbill by killbill.

the class TestPaymentOperation method setUp.

private void setUp(final PaymentPluginStatus paymentPluginStatus) throws Exception {
    final GlobalLocker locker = new MemoryGlobalLocker();
    final PluginDispatcher<OperationResult> paymentPluginDispatcher = new PluginDispatcher<OperationResult>(1, paymentExecutors);
    paymentStateContext = new PaymentStateContext(true, UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), TransactionType.CAPTURE, Mockito.mock(Account.class), UUID.randomUUID(), new BigDecimal("192.3920111"), Currency.BRL, null, null, false, null, ImmutableList.<PluginProperty>of(), internalCallContext, callContext);
    final PaymentMethodModelDao paymentMethodModelDao = new PaymentMethodModelDao(paymentStateContext.getPaymentMethodId(), UUID.randomUUID().toString(), clock.getUTCNow(), clock.getUTCNow(), paymentStateContext.getAccount().getId(), MockPaymentProviderPlugin.PLUGIN_NAME, true);
    final PaymentDao paymentDao = Mockito.mock(PaymentDao.class);
    Mockito.when(paymentDao.getPaymentMethod(paymentStateContext.getPaymentMethodId(), internalCallContext)).thenReturn(paymentMethodModelDao);
    final PaymentPluginServiceRegistration paymentPluginServiceRegistration = new PaymentPluginServiceRegistration(paymentDao, registry);
    final PaymentAutomatonDAOHelper daoHelper = new PaymentAutomatonDAOHelper(paymentStateContext, clock.getUTCNow(), paymentDao, paymentPluginServiceRegistration, internalCallContext, eventBus, paymentSMHelper);
    paymentOperation = new PaymentOperationTest(paymentPluginStatus, daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
}
Also used : GlobalLocker(org.killbill.commons.locker.GlobalLocker) MemoryGlobalLocker(org.killbill.commons.locker.memory.MemoryGlobalLocker) PaymentMethodModelDao(org.killbill.billing.payment.dao.PaymentMethodModelDao) OperationResult(org.killbill.automaton.OperationResult) BigDecimal(java.math.BigDecimal) PluginProperty(org.killbill.billing.payment.api.PluginProperty) MemoryGlobalLocker(org.killbill.commons.locker.memory.MemoryGlobalLocker) PluginDispatcher(org.killbill.billing.payment.dispatcher.PluginDispatcher) PaymentPluginServiceRegistration(org.killbill.billing.payment.core.PaymentPluginServiceRegistration) PaymentDao(org.killbill.billing.payment.dao.PaymentDao)

Example 94 with PluginProperty

use of org.killbill.billing.payment.api.PluginProperty in project killbill by killbill.

the class TestRetryablePayment method beforeClass.

@BeforeClass(groups = "fast")
public void beforeClass() throws Exception {
    super.beforeClass();
    account = testHelper.createTestAccount("lolo@gmail.com", false);
    Mockito.when(accountInternalApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account);
    //Mockito.when(nonEntityDao.retrieveIdFromObject(Mockito.<Long>any(), Mockito.<ObjectType>any())).thenReturn(uuid);
    retryPluginRegistry.registerService(new OSGIServiceDescriptor() {

        @Override
        public String getPluginSymbolicName() {
            return null;
        }

        @Override
        public String getPluginName() {
            return MockPaymentControlProviderPlugin.PLUGIN_NAME;
        }

        @Override
        public String getRegistrationName() {
            return MockPaymentControlProviderPlugin.PLUGIN_NAME;
        }
    }, mockRetryProviderPlugin);
    EMPTY_PROPERTIES = PluginPropertySerializer.serialize(ImmutableList.<PluginProperty>of());
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) OSGIServiceDescriptor(org.killbill.billing.osgi.api.OSGIServiceDescriptor) BeforeClass(org.testng.annotations.BeforeClass)

Example 95 with PluginProperty

use of org.killbill.billing.payment.api.PluginProperty in project killbill by killbill.

the class TestPaymentMethodProcessorRefreshWithDB method testRefreshWithNewPaymentMethod.

@Test(groups = "slow")
public void testRefreshWithNewPaymentMethod() throws Exception {
    final Account account = testHelper.createTestAccount("foo@bar.com", true);
    Assert.assertEquals(getPluginApi().getPaymentMethods(account.getId(), true, PLUGIN_PROPERTIES, callContext).size(), 1);
    final UUID existingPMId = account.getPaymentMethodId();
    // Add new payment in plugin directly
    final UUID newPmId = UUID.randomUUID();
    getPluginApi().addPaymentMethod(account.getId(), newPmId, new DefaultNoOpPaymentMethodPlugin(UUID.randomUUID().toString(), false, ImmutableList.<PluginProperty>of()), false, PLUGIN_PROPERTIES, callContext);
    // Verify that the refresh does indeed show 2 PMs
    final List<PaymentMethod> methods = paymentMethodProcessor.refreshPaymentMethods(MockPaymentProviderPlugin.PLUGIN_NAME, account, PLUGIN_PROPERTIES, callContext, internalCallContext);
    Assert.assertEquals(methods.size(), 2);
    checkPaymentMethodExistsWithStatus(methods, existingPMId, true);
    checkPaymentMethodExistsWithStatus(methods, newPmId, true);
}
Also used : Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) UUID(java.util.UUID) DefaultNoOpPaymentMethodPlugin(org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin) Test(org.testng.annotations.Test)

Aggregations

PluginProperty (org.killbill.billing.payment.api.PluginProperty)105 UUID (java.util.UUID)45 Account (org.killbill.billing.account.api.Account)42 ApiOperation (io.swagger.annotations.ApiOperation)35 ApiResponses (io.swagger.annotations.ApiResponses)35 Produces (javax.ws.rs.Produces)35 TimedResource (org.killbill.commons.metrics.TimedResource)35 Test (org.testng.annotations.Test)33 Path (javax.ws.rs.Path)32 Payment (org.killbill.billing.payment.api.Payment)30 CallContext (org.killbill.billing.util.callcontext.CallContext)29 ArrayList (java.util.ArrayList)19 LocalDate (org.joda.time.LocalDate)19 Consumes (javax.ws.rs.Consumes)17 BigDecimal (java.math.BigDecimal)16 TenantContext (org.killbill.billing.util.callcontext.TenantContext)15 GET (javax.ws.rs.GET)14 POST (javax.ws.rs.POST)13 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)13 AccountAuditLogs (org.killbill.billing.util.audit.AccountAuditLogs)13