use of org.killbill.billing.account.api.Account in project killbill by killbill.
the class AccountResource method getInvoicePayments.
/*
* ************************** PAYMENTS ********************************
*/
// STEPH should refactor code since very similar to @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENTS)
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + INVOICE_PAYMENTS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account invoice payments", response = InvoicePaymentJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getInvoicePayments(@PathParam("accountId") final String accountIdStr, @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, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException, AccountApiException {
final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
final UUID accountId = UUID.fromString(accountIdStr);
final TenantContext tenantContext = context.createContext(request);
final Account account = accountUserApi.getAccountById(accountId, tenantContext);
final List<Payment> payments = paymentApi.getAccountPayments(account.getId(), withPluginInfo, withAttempts, pluginProperties, tenantContext);
final List<InvoicePayment> invoicePayments = invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext);
final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
final List<InvoicePaymentJson> result = new ArrayList<InvoicePaymentJson>(payments.size());
for (final Payment payment : payments) {
final UUID invoiceId = getInvoiceId(invoicePayments, payment);
result.add(new InvoicePaymentJson(payment, invoiceId, accountAuditLogs));
}
return Response.status(Status.OK).entity(result).build();
}
use of org.killbill.billing.account.api.Account in project killbill by killbill.
the class AccountResource method getEmailNotificationsForAccount.
/*
* ************************** EMAIL NOTIFICATIONS FOR INVOICES ********************************
*/
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + EMAIL_NOTIFICATIONS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account email notification", response = InvoiceEmailJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getEmailNotificationsForAccount(@PathParam("accountId") final String accountId, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException {
final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), context.createContext(request));
final InvoiceEmailJson invoiceEmailJson = new InvoiceEmailJson(accountId, account.isNotifiedForInvoices());
return Response.status(Status.OK).entity(invoiceEmailJson).build();
}
use of org.killbill.billing.account.api.Account in project killbill by killbill.
the class AccountResource method payAllInvoices.
@TimedResource
@POST
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@Path("/{accountId:" + UUID_PATTERN + "}/" + INVOICE_PAYMENTS)
@ApiOperation(value = "Trigger a payment for all unpaid invoices")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response payAllInvoices(@PathParam("accountId") final String accountId, @QueryParam(QUERY_PAYMENT_EXTERNAL) @DefaultValue("false") final Boolean externalPayment, @QueryParam(QUERY_PAYMENT_AMOUNT) final BigDecimal paymentAmount, @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 AccountApiException, PaymentApiException, InvoiceApiException {
final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
final CallContext callContext = context.createContext(createdBy, reason, comment, request);
final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
final Collection<Invoice> unpaidInvoices = invoiceApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext);
BigDecimal remainingRequestPayment = paymentAmount;
if (remainingRequestPayment == null) {
remainingRequestPayment = BigDecimal.ZERO;
for (final Invoice invoice : unpaidInvoices) {
remainingRequestPayment = remainingRequestPayment.add(invoice.getBalance());
}
}
for (final Invoice invoice : unpaidInvoices) {
final BigDecimal amountToPay = (remainingRequestPayment.compareTo(invoice.getBalance()) >= 0) ? invoice.getBalance() : remainingRequestPayment;
if (amountToPay.compareTo(BigDecimal.ZERO) > 0) {
final UUID paymentMethodId = externalPayment ? null : account.getPaymentMethodId();
createPurchaseForInvoice(account, invoice.getId(), amountToPay, paymentMethodId, externalPayment, null, null, pluginProperties, callContext);
}
remainingRequestPayment = remainingRequestPayment.subtract(amountToPay);
if (remainingRequestPayment.compareTo(BigDecimal.ZERO) == 0) {
break;
}
}
//
if (externalPayment && remainingRequestPayment.compareTo(BigDecimal.ZERO) > 0) {
invoiceApi.insertCredit(account.getId(), remainingRequestPayment, clock.getUTCToday(), account.getCurrency(), true, "pay all invoices", callContext);
}
return Response.status(Status.OK).build();
}
use of org.killbill.billing.account.api.Account in project killbill by killbill.
the class AccountResource method getAccounts.
@TimedResource
@GET
@Path("/" + PAGINATION)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "List accounts", response = AccountJson.class, responseContainer = "List")
@ApiResponses(value = {})
public Response getAccounts(@QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_ACCOUNT_WITH_BALANCE) @DefaultValue("false") final Boolean accountWithBalance, @QueryParam(QUERY_ACCOUNT_WITH_BALANCE_AND_CBA) @DefaultValue("false") final Boolean accountWithBalanceAndCBA, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException {
final TenantContext tenantContext = context.createContext(request);
final Pagination<Account> accounts = accountUserApi.getAccounts(offset, limit, tenantContext);
final URI nextPageUri = uriBuilder.nextPage(AccountResource.class, "getAccounts", accounts.getNextOffset(), limit, ImmutableMap.<String, String>of(QUERY_ACCOUNT_WITH_BALANCE, accountWithBalance.toString(), QUERY_ACCOUNT_WITH_BALANCE_AND_CBA, accountWithBalanceAndCBA.toString(), QUERY_AUDIT, auditMode.getLevel().toString()));
return buildStreamingPaginationResponse(accounts, new Function<Account, AccountJson>() {
@Override
public AccountJson apply(final Account account) {
final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(account.getId(), auditMode.getLevel(), tenantContext);
return getAccount(account, accountWithBalance, accountWithBalanceAndCBA, accountAuditLogs, tenantContext);
}
}, nextPageUri);
}
use of org.killbill.billing.account.api.Account in project killbill by killbill.
the class TestDefaultPaymentDao method testPaymentCRUDForAccount.
private void testPaymentCRUDForAccount(final int runNb) throws Exception {
final Account account = testHelper.createTestAccount(UUID.randomUUID().toString(), true);
final UUID accountId = account.getId();
final PaymentModelDao specifiedFirstPaymentModelDao = generatePaymentModelDao(accountId);
final PaymentTransactionModelDao specifiedFirstPaymentTransactionModelDao = generatePaymentTransactionModelDao(specifiedFirstPaymentModelDao.getId());
// Create and verify the payment and transaction
final PaymentModelDao firstPaymentModelDao = paymentDao.insertPaymentWithFirstTransaction(specifiedFirstPaymentModelDao, specifiedFirstPaymentTransactionModelDao, internalCallContext);
verifyPayment(firstPaymentModelDao, specifiedFirstPaymentModelDao);
verifyPaymentAndTransactions(internalCallContext, specifiedFirstPaymentModelDao, specifiedFirstPaymentTransactionModelDao);
// Create a second transaction for the same payment
final PaymentTransactionModelDao specifiedSecondPaymentTransactionModelDao = generatePaymentTransactionModelDao(specifiedFirstPaymentModelDao.getId());
final PaymentTransactionModelDao secondTransactionModelDao = paymentDao.updatePaymentWithNewTransaction(specifiedFirstPaymentTransactionModelDao.getPaymentId(), specifiedSecondPaymentTransactionModelDao, internalCallContext);
verifyPaymentTransaction(secondTransactionModelDao, specifiedSecondPaymentTransactionModelDao);
verifyPaymentAndTransactions(internalCallContext, specifiedFirstPaymentModelDao, specifiedFirstPaymentTransactionModelDao, specifiedSecondPaymentTransactionModelDao);
// Update the latest transaction
final BigDecimal processedAmount = new BigDecimal("902341.23232");
final Currency processedCurrency = Currency.USD;
final String gatewayErrorCode = UUID.randomUUID().toString().substring(0, 5);
final String gatewayErrorMsg = UUID.randomUUID().toString();
paymentDao.updatePaymentAndTransactionOnCompletion(accountId, specifiedSecondPaymentTransactionModelDao.getAttemptId(), specifiedSecondPaymentTransactionModelDao.getPaymentId(), specifiedFirstPaymentTransactionModelDao.getTransactionType(), PaymentStateMachineHelper.STATE_NAMES[0], PaymentStateMachineHelper.STATE_NAMES[0], specifiedSecondPaymentTransactionModelDao.getId(), TransactionStatus.PAYMENT_FAILURE, processedAmount, processedCurrency, gatewayErrorCode, gatewayErrorMsg, internalCallContext);
final PaymentTransactionModelDao updatedSecondPaymentTransactionModelDao = paymentDao.getPaymentTransaction(specifiedSecondPaymentTransactionModelDao.getId(), internalCallContext);
Assert.assertEquals(updatedSecondPaymentTransactionModelDao.getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
Assert.assertEquals(updatedSecondPaymentTransactionModelDao.getGatewayErrorMsg(), gatewayErrorMsg);
Assert.assertEquals(updatedSecondPaymentTransactionModelDao.getGatewayErrorMsg(), gatewayErrorMsg);
// Create multiple payments for that account
for (int i = 0; i < 3; i++) {
final PaymentModelDao paymentModelDao = generatePaymentModelDao(accountId);
final PaymentTransactionModelDao paymentTransactionModelDao = generatePaymentTransactionModelDao(paymentModelDao.getId());
final PaymentModelDao insertedPaymentModelDao = paymentDao.insertPaymentWithFirstTransaction(paymentModelDao, paymentTransactionModelDao, internalCallContext);
verifyPayment(insertedPaymentModelDao, paymentModelDao);
// Verify search APIs
Assert.assertEquals(ImmutableList.<PaymentModelDao>copyOf(paymentDao.searchPayments(paymentModelDao.getPaymentMethodId().toString(), 0L, 100L, internalCallContext).iterator()).size(), 1);
Assert.assertEquals(ImmutableList.<PaymentModelDao>copyOf(paymentDao.searchPayments(paymentModelDao.getExternalKey(), 0L, 100L, internalCallContext).iterator()).size(), 1);
}
Assert.assertEquals(paymentDao.getPaymentsForAccount(specifiedFirstPaymentModelDao.getAccountId(), internalCallContext).size(), 4);
// Verify search APIs
Assert.assertEquals(ImmutableList.<PaymentModelDao>copyOf(paymentDao.searchPayments(accountId.toString(), 0L, 100L, internalCallContext).iterator()).size(), 4);
Assert.assertEquals(ImmutableList.<PaymentModelDao>copyOf(paymentDao.searchPayments("_ERRORED", 0L, 100L, internalCallContext).iterator()).size(), runNb);
}
Aggregations