Search in sources :

Example 56 with Payment

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

the class AccountResource method getAccountTimeline.

@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + TIMELINE)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account timeline", response = AccountTimelineJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getAccountTimeline(@PathParam("accountId") final String accountIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_PARALLEL) @DefaultValue("false") final Boolean parallel, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException, SubscriptionApiException, InvoiceApiException, CatalogApiException {
    final TenantContext tenantContext = context.createContext(request);
    final UUID accountId = UUID.fromString(accountIdString);
    final Account account = accountUserApi.getAccountById(accountId, tenantContext);
    final Callable<List<SubscriptionBundle>> bundlesCallable = new Callable<List<SubscriptionBundle>>() {

        @Override
        public List<SubscriptionBundle> call() throws Exception {
            return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, tenantContext);
        }
    };
    final Callable<List<Invoice>> invoicesCallable = new Callable<List<Invoice>>() {

        @Override
        public List<Invoice> call() throws Exception {
            return invoiceApi.getInvoicesByAccount(accountId, false, tenantContext);
        }
    };
    final Callable<List<InvoicePayment>> invoicePaymentsCallable = new Callable<List<InvoicePayment>>() {

        @Override
        public List<InvoicePayment> call() throws Exception {
            return invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext);
        }
    };
    final Callable<List<Payment>> paymentsCallable = new Callable<List<Payment>>() {

        @Override
        public List<Payment> call() throws Exception {
            return paymentApi.getAccountPayments(accountId, false, false, ImmutableList.<PluginProperty>of(), tenantContext);
        }
    };
    final Callable<AccountAuditLogs> auditsCallable = new Callable<AccountAuditLogs>() {

        @Override
        public AccountAuditLogs call() throws Exception {
            return auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
        }
    };
    final AccountTimelineJson json;
    List<Invoice> invoices = null;
    List<SubscriptionBundle> bundles = null;
    List<InvoicePayment> invoicePayments = null;
    List<Payment> payments = null;
    AccountAuditLogs accountAuditLogs = null;
    if (parallel) {
        final ExecutorService executor = jaxrsExecutors.getJaxrsExecutorService();
        final Future<List<SubscriptionBundle>> futureBundlesCallable = executor.submit(bundlesCallable);
        final Future<List<Invoice>> futureInvoicesCallable = executor.submit(invoicesCallable);
        final Future<List<InvoicePayment>> futureInvoicePaymentsCallable = executor.submit(invoicePaymentsCallable);
        final Future<List<Payment>> futurePaymentsCallable = executor.submit(paymentsCallable);
        final Future<AccountAuditLogs> futureAuditsCallable = executor.submit(auditsCallable);
        final ImmutableList<Future> toBeCancelled = ImmutableList.<Future>of(futureBundlesCallable, futureInvoicesCallable, futureInvoicePaymentsCallable, futurePaymentsCallable, futureAuditsCallable);
        final int timeoutMsec = 100;
        final long ini = System.currentTimeMillis();
        do {
            bundles = (bundles == null) ? waitOnFutureAndHandleTimeout("bundles", futureBundlesCallable, timeoutMsec, toBeCancelled) : bundles;
            invoices = (invoices == null) ? waitOnFutureAndHandleTimeout("invoices", futureInvoicesCallable, timeoutMsec, toBeCancelled) : invoices;
            invoicePayments = (invoicePayments == null) ? waitOnFutureAndHandleTimeout("invoicePayments", futureInvoicePaymentsCallable, timeoutMsec, toBeCancelled) : invoicePayments;
            payments = (payments == null) ? waitOnFutureAndHandleTimeout("payments", futurePaymentsCallable, timeoutMsec, toBeCancelled) : payments;
            accountAuditLogs = (accountAuditLogs == null) ? waitOnFutureAndHandleTimeout("accountAuditLogs", futureAuditsCallable, timeoutMsec, toBeCancelled) : accountAuditLogs;
        } while ((System.currentTimeMillis() - ini < jaxrsConfig.getJaxrsTimeout().getMillis()) && (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null));
        if (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null) {
            Response.status(Status.SERVICE_UNAVAILABLE).build();
        }
    } else {
        invoices = runCallable("invoices", invoicesCallable);
        payments = runCallable("payments", paymentsCallable);
        bundles = runCallable("bundles", bundlesCallable);
        accountAuditLogs = runCallable("accountAuditLogs", auditsCallable);
        invoicePayments = runCallable("invoicePayments", invoicePaymentsCallable);
    }
    json = new AccountTimelineJson(account, invoices, payments, invoicePayments, bundles, accountAuditLogs);
    return Response.status(Status.OK).entity(json).build();
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) TenantContext(org.killbill.billing.util.callcontext.TenantContext) Callable(java.util.concurrent.Callable) SubscriptionBundle(org.killbill.billing.entitlement.api.SubscriptionBundle) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) AccountTimelineJson(org.killbill.billing.jaxrs.json.AccountTimelineJson) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) 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 57 with Payment

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

the class PaymentResource method getPaymentByExternalKey.

@TimedResource(name = "getPayment")
@GET
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve a payment by external key", response = PaymentJson.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Payment not found") })
public Response getPaymentByExternalKey(@QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @QueryParam(QUERY_EXTERNAL_KEY) final String paymentExternalKey, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException {
    verifyNonNullOrEmpty(paymentExternalKey, "Payment externalKey needs to be specified");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final TenantContext tenantContext = context.createContext(request);
    final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, withPluginInfo, withAttempts, pluginProperties, tenantContext);
    final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext);
    final PaymentJson result = new PaymentJson(payment, accountAuditLogs);
    return Response.status(Response.Status.OK).entity(result).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Payment(org.killbill.billing.payment.api.Payment) TenantContext(org.killbill.billing.util.callcontext.TenantContext) PaymentJson(org.killbill.billing.jaxrs.json.PaymentJson) AccountAuditLogs(org.killbill.billing.util.audit.AccountAuditLogs) 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 58 with Payment

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

the class PaymentResource method createComboPayment.

@TimedResource
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("/" + COMBO)
@ApiOperation(value = "Combo api to create a new payment transaction on a existing (or not) account ")
@ApiResponses(value = { @ApiResponse(code = 201, message = "Payment transaction created successfully"), @ApiResponse(code = 400, message = "Invalid data for Account or PaymentMethod"), @ApiResponse(code = 402, message = "Transaction declined by gateway"), @ApiResponse(code = 422, message = "Payment is aborted by a control plugin"), @ApiResponse(code = 502, message = "Failed to submit payment transaction"), @ApiResponse(code = 503, message = "Payment in unknown status, failed to receive gateway response"), @ApiResponse(code = 504, message = "Payment operation timeout") })
public Response createComboPayment(@MetricTag(tag = "type", property = "transactionType") final ComboPaymentTransactionJson json, @QueryParam(QUERY_PAYMENT_CONTROL_PLUGIN_NAME) final List<String> paymentControlPluginNames, @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 PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "ComboPaymentTransactionJson body should be specified");
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Account account = getOrCreateAccount(json.getAccount(), callContext);
    final Iterable<PluginProperty> paymentMethodPluginProperties = extractPluginProperties(json.getPaymentMethodPluginProperties());
    final UUID paymentMethodId = getOrCreatePaymentMethod(account, json.getPaymentMethod(), paymentMethodPluginProperties, callContext);
    final PaymentTransactionJson paymentTransactionJson = json.getTransaction();
    final TransactionType transactionType = TransactionType.valueOf(paymentTransactionJson.getTransactionType());
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment result;
    final Iterable<PluginProperty> transactionPluginProperties = extractPluginProperties(json.getTransactionPluginProperties());
    final Currency currency = paymentTransactionJson.getCurrency() == null ? account.getCurrency() : Currency.valueOf(paymentTransactionJson.getCurrency());
    // If we need to specify a paymentId (e.g 3DS authorization, we can use regular API, no need for combo call)
    final UUID paymentId = null;
    switch(transactionType) {
        case AUTHORIZE:
            result = paymentApi.createAuthorizationWithPaymentControl(account, paymentMethodId, paymentId, paymentTransactionJson.getAmount(), currency, paymentTransactionJson.getPaymentExternalKey(), paymentTransactionJson.getTransactionExternalKey(), transactionPluginProperties, paymentOptions, callContext);
            break;
        case PURCHASE:
            result = paymentApi.createPurchaseWithPaymentControl(account, paymentMethodId, paymentId, paymentTransactionJson.getAmount(), currency, paymentTransactionJson.getPaymentExternalKey(), paymentTransactionJson.getTransactionExternalKey(), transactionPluginProperties, paymentOptions, callContext);
            break;
        case CREDIT:
            result = paymentApi.createCreditWithPaymentControl(account, paymentMethodId, paymentId, paymentTransactionJson.getAmount(), currency, paymentTransactionJson.getPaymentExternalKey(), paymentTransactionJson.getTransactionExternalKey(), transactionPluginProperties, paymentOptions, callContext);
            break;
        default:
            return Response.status(Status.PRECONDITION_FAILED).entity("TransactionType " + transactionType + " is not allowed for an account").build();
    }
    return createPaymentResponse(uriInfo, result, transactionType, paymentTransactionJson.getTransactionExternalKey(), request);
}
Also used : Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) TransactionType(org.killbill.billing.payment.api.TransactionType) Payment(org.killbill.billing.payment.api.Payment) ComboPaymentTransactionJson(org.killbill.billing.jaxrs.json.ComboPaymentTransactionJson) PaymentTransactionJson(org.killbill.billing.jaxrs.json.PaymentTransactionJson) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) 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 59 with Payment

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

the class PaymentResource method voidPaymentInternal.

private Response voidPaymentInternal(final PaymentTransactionJson json, @Nullable final String paymentIdStr, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Payment initialPayment = getPaymentByIdOrKey(paymentIdStr, json.getPaymentExternalKey(), pluginProperties, callContext);
    final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext);
    final String transactionExternalKey = json != null ? json.getTransactionExternalKey() : null;
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment payment = paymentApi.createVoidWithPaymentControl(account, initialPayment.getId(), transactionExternalKey, pluginProperties, paymentOptions, callContext);
    return createPaymentResponse(uriInfo, payment, TransactionType.VOID, json.getTransactionExternalKey(), request);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext)

Example 60 with Payment

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

the class PaymentResource method chargebackReversalPaymentInternal.

private Response chargebackReversalPaymentInternal(final PaymentTransactionJson json, @Nullable final String paymentIdStr, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified");
    verifyNonNullOrEmpty(json.getTransactionExternalKey(), "PaymentTransactionJson transactionExternalKey needs to be set");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Payment initialPayment = getPaymentByIdOrKey(paymentIdStr, json.getPaymentExternalKey(), pluginProperties, callContext);
    final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext);
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment payment = paymentApi.createChargebackReversalWithPaymentControl(account, initialPayment.getId(), json.getTransactionExternalKey(), paymentOptions, callContext);
    return createPaymentResponse(uriInfo, payment, TransactionType.CHARGEBACK, json.getTransactionExternalKey(), request);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext)

Aggregations

Payment (org.killbill.billing.payment.api.Payment)75 Test (org.testng.annotations.Test)42 Account (org.killbill.billing.account.api.Account)40 UUID (java.util.UUID)39 BigDecimal (java.math.BigDecimal)32 PluginProperty (org.killbill.billing.payment.api.PluginProperty)30 Invoice (org.killbill.billing.invoice.api.Invoice)27 LocalDate (org.joda.time.LocalDate)24 ApiOperation (io.swagger.annotations.ApiOperation)20 ApiResponses (io.swagger.annotations.ApiResponses)20 Produces (javax.ws.rs.Produces)20 Path (javax.ws.rs.Path)19 TimedResource (org.killbill.commons.metrics.TimedResource)19 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)17 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)15 CallContext (org.killbill.billing.util.callcontext.CallContext)15 AccountData (org.killbill.billing.account.api.AccountData)14 GET (javax.ws.rs.GET)13 DateTime (org.joda.time.DateTime)13 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)13