Search in sources :

Example 61 with Payment

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

the class PaymentResource method getPayments.

@TimedResource
@GET
@Path("/" + PAGINATION)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get payments", response = PaymentJson.class, responseContainer = "List")
@ApiResponses(value = {})
public Response getPayments(@QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_PAYMENT_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, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final TenantContext tenantContext = context.createContext(request);
    final Pagination<Payment> payments;
    if (Strings.isNullOrEmpty(pluginName)) {
        payments = paymentApi.getPayments(offset, limit, withPluginInfo, withAttempts, pluginProperties, tenantContext);
    } else {
        payments = paymentApi.getPayments(offset, limit, pluginName, withPluginInfo, withAttempts, pluginProperties, tenantContext);
    }
    final URI nextPageUri = uriBuilder.nextPage(PaymentResource.class, "getPayments", payments.getNextOffset(), limit, ImmutableMap.<String, String>of(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>());
    return buildStreamingPaginationResponse(payments, new Function<Payment, PaymentJson>() {

        @Override
        public PaymentJson apply(final Payment payment) {
            // Cache audit logs per account
            if (accountsAuditLogs.get().get(payment.getAccountId()) == null) {
                accountsAuditLogs.get().put(payment.getAccountId(), auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext));
            }
            final AccountAuditLogs accountAuditLogs = accountsAuditLogs.get().get(payment.getAccountId());
            return new PaymentJson(payment, accountAuditLogs);
        }
    }, nextPageUri);
}
Also used : TenantContext(org.killbill.billing.util.callcontext.TenantContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) PluginProperty(org.killbill.billing.payment.api.PluginProperty) Payment(org.killbill.billing.payment.api.Payment) PaymentJson(org.killbill.billing.jaxrs.json.PaymentJson) 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 62 with Payment

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

the class AccountResource method getPayments.

/*
     * ************************* PAYMENTS *****************************
     */
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENTS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account payments", response = PaymentJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied") })
public Response getPayments(@PathParam("accountId") final String accountIdStr, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException {
    final UUID accountId = UUID.fromString(accountIdStr);
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final TenantContext tenantContext = context.createContext(request);
    final List<Payment> payments = paymentApi.getAccountPayments(accountId, withPluginInfo, withAttempts, pluginProperties, tenantContext);
    final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
    final List<PaymentJson> result = ImmutableList.copyOf(Iterables.transform(payments, new Function<Payment, PaymentJson>() {

        @Override
        public PaymentJson apply(final Payment payment) {
            return new PaymentJson(payment, accountAuditLogs);
        }
    }));
    return Response.status(Response.Status.OK).entity(result).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Function(com.google.common.base.Function) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) TenantContext(org.killbill.billing.util.callcontext.TenantContext) InvoicePaymentJson(org.killbill.billing.jaxrs.json.InvoicePaymentJson) PaymentJson(org.killbill.billing.jaxrs.json.PaymentJson) UUID(java.util.UUID) 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 63 with Payment

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

the class AccountResource method processPayment.

private Response processPayment(final PaymentTransactionJson json, final Account account, final String paymentMethodIdStr, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final UriInfo uriInfo, final CallContext callContext, final HttpServletRequest request) throws PaymentApiException {
    verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified");
    verifyNonNullOrEmpty(json.getTransactionType(), "PaymentTransactionJson transactionType needs to be set", json.getAmount(), "PaymentTransactionJson amount needs to be set");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final Currency currency = json.getCurrency() == null ? account.getCurrency() : Currency.valueOf(json.getCurrency());
    final UUID paymentId = json.getPaymentId() == null ? null : UUID.fromString(json.getPaymentId());
    //
    // If paymentId was specified, it means we are attempting a payment completion. The preferred way is to use the PaymentResource
    // (PUT /1.0/kb/payments/{paymentId}/completeTransaction), but for backward compatibility we still allow the call to proceed
    // as long as the request/existing state is healthy (i.e there is a matching PENDING transaction)
    //
    final UUID paymentMethodId;
    if (paymentId != null) {
        final Payment initialPayment = paymentApi.getPayment(paymentId, false, false, pluginProperties, callContext);
        final PaymentTransaction pendingOrSuccessTransaction = lookupPendingOrSuccessTransaction(initialPayment, json != null ? json.getTransactionId() : null, json != null ? json.getTransactionExternalKey() : null, json != null ? json.getTransactionType() : null);
        // If transaction was already completed, return early (See #626)
        if (pendingOrSuccessTransaction.getTransactionStatus() == TransactionStatus.SUCCESS) {
            return uriBuilder.buildResponse(uriInfo, PaymentResource.class, "getPayment", pendingOrSuccessTransaction.getPaymentId(), request);
        }
        paymentMethodId = initialPayment.getPaymentMethodId();
    } else {
        paymentMethodId = paymentMethodIdStr == null ? account.getPaymentMethodId() : UUID.fromString(paymentMethodIdStr);
    }
    validatePaymentMethodForAccount(account.getId(), paymentMethodId, callContext);
    final TransactionType transactionType = TransactionType.valueOf(json.getTransactionType());
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment result;
    switch(transactionType) {
        case AUTHORIZE:
            result = paymentApi.createAuthorizationWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case PURCHASE:
            result = paymentApi.createPurchaseWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case CREDIT:
            result = paymentApi.createCreditWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, 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, json.getTransactionExternalKey(), request);
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) TransactionType(org.killbill.billing.payment.api.TransactionType) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions)

Example 64 with Payment

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

the class TestPaymentProcessor method testVoid.

@Test(groups = "slow")
public void testVoid() throws Exception {
    final String paymentExternalKey = UUID.randomUUID().toString();
    // AUTH
    final String authorizationKey = UUID.randomUUID().toString();
    final Payment authorization = paymentProcessor.createAuthorization(true, null, account, null, null, TEN, CURRENCY, paymentExternalKey, authorizationKey, null, null, SHOULD_LOCK_ACCOUNT, PLUGIN_PROPERTIES, callContext, internalCallContext);
    verifyPayment(authorization, paymentExternalKey, TEN, ZERO, ZERO, 1);
    final UUID paymentId = authorization.getId();
    verifyPaymentTransaction(authorization.getTransactions().get(0), authorizationKey, TransactionType.AUTHORIZE, TEN, paymentId);
    paymentBusListener.verify(1, account.getId(), paymentId, TEN, TransactionStatus.SUCCESS);
    // VOID
    final String voidKey = UUID.randomUUID().toString();
    final Payment voidTransaction = paymentProcessor.createVoid(true, null, account, paymentId, voidKey, null, SHOULD_LOCK_ACCOUNT, PLUGIN_PROPERTIES, callContext, internalCallContext);
    verifyPayment(voidTransaction, paymentExternalKey, ZERO, ZERO, ZERO, 2);
    verifyPaymentTransaction(voidTransaction.getTransactions().get(1), voidKey, TransactionType.VOID, null, paymentId);
    paymentBusListener.verify(2, account.getId(), paymentId, null, TransactionStatus.SUCCESS);
}
Also used : Payment(org.killbill.billing.payment.api.Payment) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 65 with Payment

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

the class TestPaymentProcessor method testPurchase.

@Test(groups = "slow")
public void testPurchase() throws Exception {
    final String paymentExternalKey = UUID.randomUUID().toString();
    // PURCHASE
    final String purchaseKey = UUID.randomUUID().toString();
    final Payment purchase = paymentProcessor.createPurchase(true, null, account, null, null, TEN, CURRENCY, paymentExternalKey, purchaseKey, null, null, SHOULD_LOCK_ACCOUNT, PLUGIN_PROPERTIES, callContext, internalCallContext);
    verifyPayment(purchase, paymentExternalKey, ZERO, ZERO, ZERO, 1);
    final UUID paymentId = purchase.getId();
    verifyPaymentTransaction(purchase.getTransactions().get(0), purchaseKey, TransactionType.PURCHASE, TEN, paymentId);
    paymentBusListener.verify(1, account.getId(), paymentId, TEN, TransactionStatus.SUCCESS);
}
Also used : Payment(org.killbill.billing.payment.api.Payment) UUID(java.util.UUID) Test(org.testng.annotations.Test)

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