Search in sources :

Example 11 with PaymentTransaction

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

the class AuditChecker method checkPaymentCreated.

/**
     * ********************************************  PAYMENT *******************************************************
     */
public void checkPaymentCreated(final Payment payment, final CallContext context) {
    final List<AuditLog> paymentLogs = getAuditLogForPayment(payment, context);
    Assert.assertEquals(paymentLogs.size(), 2);
    checkAuditLog(ChangeType.INSERT, context, paymentLogs.get(0), payment.getId(), PaymentSqlDao.class, true, false);
    checkAuditLog(ChangeType.UPDATE, context, paymentLogs.get(1), payment.getId(), PaymentSqlDao.class, true, false);
    for (PaymentTransaction cur : payment.getTransactions()) {
        final List<AuditLog> auditLogs = getAuditLogForPaymentTransaction(payment, cur, context);
        Assert.assertEquals(auditLogs.size(), 2);
        checkAuditLog(ChangeType.INSERT, context, auditLogs.get(0), cur.getId(), TransactionSqlDao.class, true, false);
        checkAuditLog(ChangeType.UPDATE, context, auditLogs.get(1), cur.getId(), TransactionSqlDao.class, true, false);
    }
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) AuditLog(org.killbill.billing.util.audit.AuditLog)

Example 12 with PaymentTransaction

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

the class RefundChecker method checkRefund.

public PaymentTransaction checkRefund(final UUID paymentId, final CallContext context, ExpectedRefundCheck expected) throws PaymentApiException {
    final Payment payment = paymentApi.getPayment(paymentId, false, false, ImmutableList.<PluginProperty>of(), context);
    final PaymentTransaction refund = Iterables.tryFind(payment.getTransactions(), new Predicate<PaymentTransaction>() {

        @Override
        public boolean apply(final PaymentTransaction input) {
            return input.getTransactionType() == TransactionType.REFUND;
        }
    }).orNull();
    Assert.assertNotNull(refund);
    final InvoicePayment refundInvoicePayment = getInvoicePaymentEntry(paymentId, InvoicePaymentType.REFUND, context);
    final InvoicePayment invoicePayment = getInvoicePaymentEntry(paymentId, InvoicePaymentType.ATTEMPT, context);
    Assert.assertEquals(refund.getPaymentId(), expected.getPaymentId());
    Assert.assertEquals(refund.getCurrency(), expected.getCurrency());
    Assert.assertEquals(refund.getAmount().compareTo(expected.getRefundAmount()), 0);
    Assert.assertEquals(refundInvoicePayment.getPaymentId(), paymentId);
    Assert.assertEquals(refundInvoicePayment.getLinkedInvoicePaymentId(), invoicePayment.getId());
    Assert.assertEquals(refundInvoicePayment.getPaymentCookieId(), refund.getExternalKey());
    Assert.assertEquals(refundInvoicePayment.getInvoiceId(), invoicePayment.getInvoiceId());
    Assert.assertEquals(refundInvoicePayment.getAmount().compareTo(expected.getRefundAmount().negate()), 0);
    Assert.assertEquals(refundInvoicePayment.getCurrency(), expected.getCurrency());
    return refund;
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) Predicate(com.google.common.base.Predicate)

Example 13 with PaymentTransaction

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

the class PaymentProcessor method toPayment.

// Used in bulk get API (getAccountPayments)
private Payment toPayment(final PaymentModelDao curPaymentModelDao, final Collection<PaymentTransactionModelDao> curTransactionsModelDao, @Nullable final Iterable<PaymentTransactionInfoPlugin> pluginTransactions, final boolean withAttempts, final InternalTenantContext internalTenantContext) {
    final Collection<PaymentTransactionModelDao> transactionsModelDao = new LinkedList<PaymentTransactionModelDao>(curTransactionsModelDao);
    invokeJanitor(curPaymentModelDao, transactionsModelDao, pluginTransactions, internalTenantContext);
    final Collection<PaymentTransaction> transactions = new LinkedList<PaymentTransaction>();
    for (final PaymentTransactionModelDao newPaymentTransactionModelDao : transactionsModelDao) {
        final PaymentTransactionInfoPlugin paymentTransactionInfoPlugin = findPaymentTransactionInfoPlugin(newPaymentTransactionModelDao, pluginTransactions);
        final PaymentTransaction transaction = new DefaultPaymentTransaction(newPaymentTransactionModelDao.getId(), newPaymentTransactionModelDao.getAttemptId(), newPaymentTransactionModelDao.getTransactionExternalKey(), newPaymentTransactionModelDao.getCreatedDate(), newPaymentTransactionModelDao.getUpdatedDate(), newPaymentTransactionModelDao.getPaymentId(), newPaymentTransactionModelDao.getTransactionType(), newPaymentTransactionModelDao.getEffectiveDate(), newPaymentTransactionModelDao.getTransactionStatus(), newPaymentTransactionModelDao.getAmount(), newPaymentTransactionModelDao.getCurrency(), newPaymentTransactionModelDao.getProcessedAmount(), newPaymentTransactionModelDao.getProcessedCurrency(), newPaymentTransactionModelDao.getGatewayErrorCode(), newPaymentTransactionModelDao.getGatewayErrorMsg(), paymentTransactionInfoPlugin);
        transactions.add(transaction);
    }
    final Ordering<PaymentTransaction> perPaymentTransactionOrdering = Ordering.<PaymentTransaction>from(new Comparator<PaymentTransaction>() {

        @Override
        public int compare(final PaymentTransaction o1, final PaymentTransaction o2) {
            return o1.getEffectiveDate().compareTo(o2.getEffectiveDate());
        }
    });
    final List<PaymentTransaction> sortedTransactions = perPaymentTransactionOrdering.immutableSortedCopy(transactions);
    return new DefaultPayment(curPaymentModelDao.getId(), curPaymentModelDao.getCreatedDate(), curPaymentModelDao.getUpdatedDate(), curPaymentModelDao.getAccountId(), curPaymentModelDao.getPaymentMethodId(), curPaymentModelDao.getPaymentNumber(), curPaymentModelDao.getExternalKey(), sortedTransactions, (withAttempts && !sortedTransactions.isEmpty()) ? getPaymentAttempts(paymentDao.getPaymentAttempts(curPaymentModelDao.getExternalKey(), internalTenantContext), internalTenantContext) : null);
}
Also used : DefaultPaymentTransaction(org.killbill.billing.payment.api.DefaultPaymentTransaction) PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) DefaultPayment(org.killbill.billing.payment.api.DefaultPayment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) DefaultPaymentTransaction(org.killbill.billing.payment.api.DefaultPaymentTransaction) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) LinkedList(java.util.LinkedList)

Example 14 with PaymentTransaction

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

the class PaymentBusEventHandler method processInvoiceEvent.

@AllowConcurrentEvents
@Subscribe
public void processInvoiceEvent(final InvoiceCreationInternalEvent event) {
    log.info("Received invoice creation notification for accountId='{}', invoiceId='{}'", event.getAccountId(), event.getInvoiceId());
    final Collection<PluginProperty> properties = new ArrayList<PluginProperty>();
    final PluginProperty propertyInvoiceId = new PluginProperty(InvoicePaymentControlPluginApi.PROP_IPCD_INVOICE_ID, event.getInvoiceId().toString(), false);
    properties.add(propertyInvoiceId);
    final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(event.getSearchKey2(), event.getSearchKey1(), "PaymentRequestProcessor", CallOrigin.INTERNAL, UserType.SYSTEM, event.getUserToken());
    final CallContext callContext = internalCallContextFactory.createCallContext(internalContext);
    // We let the plugin compute how much should be paid
    final BigDecimal amountToBePaid = null;
    final List<String> paymentControlPluginNames = paymentConfig.getPaymentControlPluginNames(internalContext) != null ? new LinkedList<String>(paymentConfig.getPaymentControlPluginNames(internalContext)) : new LinkedList<String>();
    paymentControlPluginNames.add(InvoicePaymentControlPluginApi.PLUGIN_NAME);
    final String transactionType = TransactionType.PURCHASE.name();
    Account account = null;
    Payment payment = null;
    PaymentTransaction paymentTransaction = null;
    try {
        account = accountApi.getAccountById(event.getAccountId(), internalContext);
        logEnterAPICall(log, transactionType, account, account.getPaymentMethodId(), null, null, amountToBePaid, account.getCurrency(), null, null, null, paymentControlPluginNames);
        payment = pluginControlPaymentProcessor.createPurchase(false, account, account.getPaymentMethodId(), null, amountToBePaid, account.getCurrency(), null, null, properties, paymentControlPluginNames, callContext, internalContext);
        paymentTransaction = payment.getTransactions().get(payment.getTransactions().size() - 1);
    } catch (final AccountApiException e) {
        log.warn("Failed to process invoice payment", e);
    } catch (final PaymentApiException e) {
        // Log as warn unless nothing left to be paid
        if (e.getCode() != ErrorCode.PAYMENT_PLUGIN_API_ABORTED.getCode()) {
            log.warn("Failed to process invoice payment {}", e.toString());
        }
    } finally {
        logExitAPICall(log, transactionType, account, payment != null ? payment.getPaymentMethodId() : null, payment != null ? payment.getId() : null, paymentTransaction != null ? paymentTransaction.getId() : null, paymentTransaction != null ? paymentTransaction.getProcessedAmount() : null, paymentTransaction != null ? paymentTransaction.getProcessedCurrency() : null, payment != null ? payment.getExternalKey() : null, paymentTransaction != null ? paymentTransaction.getExternalKey() : null, paymentTransaction != null ? paymentTransaction.getTransactionStatus() : null, paymentControlPluginNames, null);
    }
}
Also used : Account(org.killbill.billing.account.api.Account) ArrayList(java.util.ArrayList) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) CallContext(org.killbill.billing.util.callcontext.CallContext) BigDecimal(java.math.BigDecimal) PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) Payment(org.killbill.billing.payment.api.Payment) AccountApiException(org.killbill.billing.account.api.AccountApiException) AllowConcurrentEvents(com.google.common.eventbus.AllowConcurrentEvents) Subscribe(com.google.common.eventbus.Subscribe)

Example 15 with PaymentTransaction

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

the class JaxRsResourceBase method findCreatedTransaction.

private PaymentTransaction findCreatedTransaction(final Payment payment, final TransactionType transactionType, @Nullable final String transactionExternalKey) {
    // Make sure we start looking from the latest transaction created
    final List<PaymentTransaction> reversedTransactions = Lists.reverse(payment.getTransactions());
    final Iterable<PaymentTransaction> matchingTransactions = Iterables.filter(reversedTransactions, new Predicate<PaymentTransaction>() {

        @Override
        public boolean apply(final PaymentTransaction input) {
            return input.getTransactionType() == transactionType;
        }
    });
    if (transactionExternalKey != null) {
        for (final PaymentTransaction transaction : matchingTransactions) {
            if (transactionExternalKey.equals(transaction.getExternalKey())) {
                return transaction;
            }
        }
    }
    // If nothing is found, return the latest transaction of given type
    return Iterables.getFirst(matchingTransactions, null);
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction)

Aggregations

PaymentTransaction (org.killbill.billing.payment.api.PaymentTransaction)20 Payment (org.killbill.billing.payment.api.Payment)12 PluginProperty (org.killbill.billing.payment.api.PluginProperty)7 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)6 BigDecimal (java.math.BigDecimal)5 UUID (java.util.UUID)5 Account (org.killbill.billing.account.api.Account)5 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)4 CallContext (org.killbill.billing.util.callcontext.CallContext)4 Predicate (com.google.common.base.Predicate)3 ArrayList (java.util.ArrayList)3 LocalDate (org.joda.time.LocalDate)3 Invoice (org.killbill.billing.invoice.api.Invoice)3 Test (org.testng.annotations.Test)3 AccountApiException (org.killbill.billing.account.api.AccountApiException)2 AccountData (org.killbill.billing.account.api.AccountData)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 Currency (org.killbill.billing.catalog.api.Currency)2 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)2 DefaultPayment (org.killbill.billing.payment.api.DefaultPayment)2