Search in sources :

Example 6 with PaymentMethod

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

the class ComboPaymentResource method getOrCreatePaymentMethod.

protected UUID getOrCreatePaymentMethod(final Account account, @Nullable final PaymentMethodJson paymentMethodJson, final Iterable<PluginProperty> pluginProperties, final CallContext callContext) throws PaymentApiException {
    // No info about payment method was passed, we default to null payment Method ID (which is allowed to be overridden in payment control plugins)
    if (paymentMethodJson == null || paymentMethodJson.getPluginName() == null) {
        return null;
    }
    // Get all payment methods for account
    final List<PaymentMethod> accountPaymentMethods = paymentApi.getAccountPaymentMethods(account.getId(), false, ImmutableList.<PluginProperty>of(), callContext);
    // If we were specified a paymentMethod id and we find it, we return it
    if (paymentMethodJson.getPaymentMethodId() != null) {
        final UUID match = UUID.fromString(paymentMethodJson.getPaymentMethodId());
        if (Iterables.any(accountPaymentMethods, new Predicate<PaymentMethod>() {

            @Override
            public boolean apply(final PaymentMethod input) {
                return input.getId().equals(match);
            }
        })) {
            return match;
        }
        throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, match);
    }
    // If we were specified a paymentMethod externalKey and we find it, we return it
    if (paymentMethodJson.getExternalKey() != null) {
        final PaymentMethod match = Iterables.tryFind(accountPaymentMethods, new Predicate<PaymentMethod>() {

            @Override
            public boolean apply(final PaymentMethod input) {
                return input.getExternalKey().equals(paymentMethodJson.getExternalKey());
            }
        }).orNull();
        if (match != null) {
            return match.getId();
        }
    }
    // Only set as default if this is the first paymentMethod on the account
    final boolean isDefault = accountPaymentMethods.isEmpty();
    final PaymentMethod paymentData = paymentMethodJson.toPaymentMethod(account.getId().toString());
    return paymentApi.addPaymentMethod(account, paymentMethodJson.getExternalKey(), paymentMethodJson.getPluginName(), isDefault, paymentData.getPluginDetail(), pluginProperties, callContext);
}
Also used : PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) UUID(java.util.UUID) Predicate(com.google.common.base.Predicate)

Example 7 with PaymentMethod

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

the class PaymentMethodResource method deletePaymentMethod.

@TimedResource
@DELETE
@Produces(APPLICATION_JSON)
@Path("/{paymentMethodId:" + UUID_PATTERN + "}")
@ApiOperation(value = "Delete a payment method")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid paymentMethodId supplied"), @ApiResponse(code = 404, message = "Account or payment method not found") })
public Response deletePaymentMethod(@PathParam("paymentMethodId") final String paymentMethodId, @QueryParam(QUERY_DELETE_DEFAULT_PM_WITH_AUTO_PAY_OFF) @DefaultValue("false") final Boolean deleteDefaultPaymentMethodWithAutoPayOff, @QueryParam(QUERY_FORCE_DEFAULT_PM_DELETION) @DefaultValue("false") final Boolean forceDefaultPaymentMethodDeletion, @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 HttpServletRequest request) throws PaymentApiException, AccountApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final PaymentMethod paymentMethod = paymentApi.getPaymentMethodById(UUID.fromString(paymentMethodId), false, false, pluginProperties, callContext);
    final Account account = accountUserApi.getAccountById(paymentMethod.getAccountId(), callContext);
    paymentApi.deletePaymentMethod(account, UUID.fromString(paymentMethodId), deleteDefaultPaymentMethodWithAutoPayOff, forceDefaultPaymentMethodDeletion, pluginProperties, callContext);
    return Response.status(Status.OK).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) CallContext(org.killbill.billing.util.callcontext.CallContext) 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 8 with PaymentMethod

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

the class PaymentMethodResource method searchPaymentMethods.

@TimedResource
@GET
@Path("/" + SEARCH + "/{searchKey:" + ANYTHING_PATTERN + "}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Search payment methods", response = PaymentMethodJson.class, responseContainer = "List")
@ApiResponses(value = {})
public Response searchPaymentMethods(@PathParam("searchKey") final String searchKey, @QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_PAYMENT_METHOD_PLUGIN_NAME) final String pluginName, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final TenantContext tenantContext = context.createContext(request);
    // Search the plugin(s)
    final Pagination<PaymentMethod> paymentMethods;
    if (Strings.isNullOrEmpty(pluginName)) {
        paymentMethods = paymentApi.searchPaymentMethods(searchKey, offset, limit, withPluginInfo, pluginProperties, tenantContext);
    } else {
        paymentMethods = paymentApi.searchPaymentMethods(searchKey, offset, limit, pluginName, withPluginInfo, pluginProperties, tenantContext);
    }
    final URI nextPageUri = uriBuilder.nextPage(PaymentMethodResource.class, "searchPaymentMethods", paymentMethods.getNextOffset(), limit, ImmutableMap.<String, String>of("searchKey", searchKey, QUERY_PAYMENT_METHOD_PLUGIN_NAME, Strings.nullToEmpty(pluginName), QUERY_AUDIT, auditMode.getLevel().toString()));
    final AtomicReference<Map<UUID, AccountAuditLogs>> accountsAuditLogs = new AtomicReference<Map<UUID, AccountAuditLogs>>(new HashMap<UUID, AccountAuditLogs>());
    final Map<UUID, Account> accounts = new HashMap<UUID, Account>();
    return buildStreamingPaginationResponse(paymentMethods, new Function<PaymentMethod, PaymentMethodJson>() {

        @Override
        public PaymentMethodJson apply(final PaymentMethod paymentMethod) {
            // Cache audit logs per account
            if (accountsAuditLogs.get().get(paymentMethod.getAccountId()) == null) {
                accountsAuditLogs.get().put(paymentMethod.getAccountId(), auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext));
            }
            // Lookup the associated account(s)
            if (accounts.get(paymentMethod.getAccountId()) == null) {
                final Account account;
                try {
                    account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext);
                    accounts.put(paymentMethod.getAccountId(), account);
                } catch (final AccountApiException e) {
                    log.warn("Error retrieving accountId='{}'", paymentMethod.getAccountId(), e);
                    return null;
                }
            }
            return PaymentMethodJson.toPaymentMethodJson(accounts.get(paymentMethod.getAccountId()), paymentMethod, accountsAuditLogs.get().get(paymentMethod.getAccountId()));
        }
    }, nextPageUri);
}
Also used : Account(org.killbill.billing.account.api.Account) HashMap(java.util.HashMap) TenantContext(org.killbill.billing.util.callcontext.TenantContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) PluginProperty(org.killbill.billing.payment.api.PluginProperty) PaymentMethodJson(org.killbill.billing.jaxrs.json.PaymentMethodJson) AccountApiException(org.killbill.billing.account.api.AccountApiException) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) UUID(java.util.UUID) AccountAuditLogs(org.killbill.billing.util.audit.AccountAuditLogs) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 9 with PaymentMethod

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

the class PaymentMethodResource method getPaymentMethod.

@TimedResource(name = "getPaymentMethod")
@GET
@Path("/{paymentMethodId:" + UUID_PATTERN + "}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve a payment method by id", response = PaymentMethodJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid paymentMethodId supplied"), @ApiResponse(code = 404, message = "Account or payment method not found") })
public Response getPaymentMethod(@PathParam("paymentMethodId") final String paymentMethodId, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final TenantContext tenantContext = context.createContext(request);
    final PaymentMethod paymentMethod = paymentApi.getPaymentMethodById(UUID.fromString(paymentMethodId), false, withPluginInfo, pluginProperties, tenantContext);
    final Account account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext);
    final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext);
    final PaymentMethodJson json = PaymentMethodJson.toPaymentMethodJson(account, paymentMethod, accountAuditLogs);
    return Response.status(Status.OK).entity(json).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) PaymentMethodJson(org.killbill.billing.jaxrs.json.PaymentMethodJson) TenantContext(org.killbill.billing.util.callcontext.TenantContext) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) AccountAuditLogs(org.killbill.billing.util.audit.AccountAuditLogs) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 10 with PaymentMethod

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

the class PaymentMethodProcessor method addPaymentMethod.

public UUID addPaymentMethod(final String paymentMethodExternalKey, final String paymentPluginServiceName, final Account account, final boolean setDefault, final PaymentMethodPlugin paymentMethodProps, final Iterable<PluginProperty> properties, final CallContext callContext, final InternalCallContext context) throws PaymentApiException {
    return dispatchWithExceptionHandling(account, paymentPluginServiceName, new CallableWithAccountLock<UUID, PaymentApiException>(locker, account.getId(), paymentConfig, new DispatcherCallback<PluginDispatcherReturnType<UUID>, PaymentApiException>() {

        @Override
        public PluginDispatcherReturnType<UUID> doOperation() throws PaymentApiException {
            PaymentMethod pm = null;
            try {
                validateUniqueExternalPaymentMethod(account.getId(), paymentPluginServiceName);
                pm = new DefaultPaymentMethod(paymentMethodExternalKey, account.getId(), paymentPluginServiceName, paymentMethodProps);
                final PaymentPluginApi pluginApi = getPaymentPluginApi(paymentPluginServiceName);
                pluginApi.addPaymentMethod(account.getId(), pm.getId(), paymentMethodProps, setDefault, properties, callContext);
                final String actualPaymentMethodExternalKey = retrieveActualPaymentMethodExternalKey(account, pm, pluginApi, properties, callContext, context);
                final PaymentMethodModelDao pmModel = new PaymentMethodModelDao(pm.getId(), actualPaymentMethodExternalKey, pm.getCreatedDate(), pm.getUpdatedDate(), pm.getAccountId(), pm.getPluginName(), pm.isActive());
                paymentDao.insertPaymentMethod(pmModel, context);
                if (setDefault) {
                    accountInternalApi.updatePaymentMethod(account.getId(), pm.getId(), context);
                }
            } catch (final PaymentPluginApiException e) {
                throw new PaymentApiException(ErrorCode.PAYMENT_ADD_PAYMENT_METHOD, account.getId(), e.getErrorMessage());
            } catch (final AccountApiException e) {
                throw new PaymentApiException(e);
            }
            return PluginDispatcher.createPluginDispatcherReturnType(pm.getId());
        }

        private void validateUniqueExternalPaymentMethod(final UUID accountId, final String pluginName) throws PaymentApiException {
            if (ExternalPaymentProviderPlugin.PLUGIN_NAME.equals(pluginName)) {
                final List<PaymentMethodModelDao> accountPaymentMethods = paymentDao.getPaymentMethods(context);
                if (Iterables.any(accountPaymentMethods, new Predicate<PaymentMethodModelDao>() {

                    @Override
                    public boolean apply(final PaymentMethodModelDao input) {
                        return ExternalPaymentProviderPlugin.PLUGIN_NAME.equals(input.getPluginName());
                    }
                })) {
                    throw new PaymentApiException(ErrorCode.PAYMENT_EXTERNAL_PAYMENT_METHOD_ALREADY_EXISTS, accountId);
                }
            }
        }
    }), uuidPluginNotificationDispatcher);
}
Also used : PaymentPluginApiException(org.killbill.billing.payment.plugin.api.PaymentPluginApiException) PaymentMethodModelDao(org.killbill.billing.payment.dao.PaymentMethodModelDao) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) DefaultPaymentMethod(org.killbill.billing.payment.api.DefaultPaymentMethod) PaymentPluginApi(org.killbill.billing.payment.plugin.api.PaymentPluginApi) AccountApiException(org.killbill.billing.account.api.AccountApiException) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) DefaultPaymentMethod(org.killbill.billing.payment.api.DefaultPaymentMethod) UUID(java.util.UUID)

Aggregations

PaymentMethod (org.killbill.billing.payment.api.PaymentMethod)17 Account (org.killbill.billing.account.api.Account)11 UUID (java.util.UUID)10 PluginProperty (org.killbill.billing.payment.api.PluginProperty)10 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponses (io.swagger.annotations.ApiResponses)7 Produces (javax.ws.rs.Produces)7 TimedResource (org.killbill.commons.metrics.TimedResource)7 Path (javax.ws.rs.Path)6 GET (javax.ws.rs.GET)5 PaymentMethodJson (org.killbill.billing.jaxrs.json.PaymentMethodJson)5 AccountAuditLogs (org.killbill.billing.util.audit.AccountAuditLogs)5 TenantContext (org.killbill.billing.util.callcontext.TenantContext)5 AccountApiException (org.killbill.billing.account.api.AccountApiException)4 DefaultPaymentMethod (org.killbill.billing.payment.api.DefaultPaymentMethod)4 PaymentMethodModelDao (org.killbill.billing.payment.dao.PaymentMethodModelDao)4 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)3 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)3 DefaultNoOpPaymentMethodPlugin (org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin)3