Search in sources :

Example 1 with TransactionStatus

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

the class PaymentEnteringStateCallback method enteringState.

@Override
public void enteringState(final State newState, final Operation.OperationCallback operationCallback, final OperationResult operationResult, final LeavingStateCallback leavingStateCallback) {
    logger.debug("Entering state {} with result {}", newState.getName(), operationResult);
    // If the transaction was not created -- for instance we had an exception in leavingState callback then we bail; if not, then update state:
    if (paymentStateContext.getPaymentTransactionModelDao() != null && paymentStateContext.getPaymentTransactionModelDao().getId() != null) {
        final PaymentTransactionInfoPlugin paymentInfoPlugin = paymentStateContext.getPaymentTransactionInfoPlugin();
        final TransactionStatus transactionStatus = PaymentTransactionInfoPluginConverter.toTransactionStatus(paymentInfoPlugin);
        // The bus event will be posted from the transaction
        daoHelper.processPaymentInfoPlugin(transactionStatus, paymentInfoPlugin, newState.getName());
    } else if (!paymentStateContext.isApiPayment()) {
        // 
        // If there is NO transaction to update (because payment transaction did not occur), then there is something wrong happening (maybe a missing defaultPaymentMethodId, ...)
        // so, if the call does NOT originates from api then we still want to send a bus event so the system can react to it if needed.
        // 
        final BusInternalEvent event = new DefaultPaymentErrorEvent(paymentStateContext.getAccount().getId(), null, null, paymentStateContext.getAmount(), paymentStateContext.getCurrency(), null, paymentStateContext.getTransactionType(), null, paymentStateContext.isApiPayment(), "Early abortion of payment transaction", paymentStateContext.getInternalCallContext().getAccountRecordId(), paymentStateContext.getInternalCallContext().getTenantRecordId(), paymentStateContext.getInternalCallContext().getUserToken());
        try {
            daoHelper.getEventBus().post(event);
        } catch (EventBusException e) {
            logger.warn("Failed to post event {}", event, e);
        }
    }
}
Also used : PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) TransactionStatus(org.killbill.billing.payment.api.TransactionStatus) EventBusException(org.killbill.bus.api.PersistentBus.EventBusException) BusInternalEvent(org.killbill.billing.events.BusInternalEvent) DefaultPaymentErrorEvent(org.killbill.billing.payment.api.DefaultPaymentErrorEvent)

Example 2 with TransactionStatus

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

the class IncompletePaymentTransactionTask method updatePaymentAndTransactionInternal.

private boolean updatePaymentAndTransactionInternal(final PaymentModelDao payment, @Nullable final Integer attemptNumber, @Nullable final UUID userToken, final PaymentTransactionModelDao paymentTransaction, final PaymentTransactionInfoPlugin paymentTransactionInfoPlugin, final InternalTenantContext internalTenantContext) {
    final CallContext callContext = createCallContext("IncompletePaymentTransactionTask", internalTenantContext);
    // First obtain the new transactionStatus,
    // Then compute the new paymentState; this one is mostly interesting in case of success (to compute the lastSuccessPaymentState below)
    final TransactionStatus transactionStatus = computeNewTransactionStatusFromPaymentTransactionInfoPlugin(paymentTransactionInfoPlugin, paymentTransaction.getTransactionStatus());
    final String newPaymentState;
    switch(transactionStatus) {
        case PENDING:
            newPaymentState = paymentStateMachineHelper.getPendingStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case SUCCESS:
            newPaymentState = paymentStateMachineHelper.getSuccessfulStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case PAYMENT_FAILURE:
            newPaymentState = paymentStateMachineHelper.getFailureStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case PLUGIN_FAILURE:
            newPaymentState = paymentStateMachineHelper.getErroredStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case UNKNOWN:
        default:
            if (transactionStatus != paymentTransaction.getTransactionStatus()) {
                log.info("Unable to repair paymentId='{}', paymentTransactionId='{}', currentTransactionStatus='{}', newTransactionStatus='{}'", payment.getId(), paymentTransaction.getId(), paymentTransaction.getTransactionStatus(), transactionStatus);
            }
            // We can't get anything interesting from the plugin...
            insertNewNotificationForUnresolvedTransactionIfNeeded(paymentTransaction.getId(), attemptNumber, userToken, internalTenantContext.getAccountRecordId(), internalTenantContext.getTenantRecordId());
            return false;
    }
    // Our status did not change, so we just insert a new notification (attemptNumber will be incremented)
    if (transactionStatus == paymentTransaction.getTransactionStatus()) {
        log.debug("Janitor IncompletePaymentTransactionTask repairing payment {}, transaction {}, transitioning transactionStatus from {} -> {}", payment.getId(), paymentTransaction.getId(), paymentTransaction.getTransactionStatus(), transactionStatus);
        insertNewNotificationForUnresolvedTransactionIfNeeded(paymentTransaction.getId(), attemptNumber, userToken, internalTenantContext.getAccountRecordId(), internalTenantContext.getTenantRecordId());
        return false;
    }
    // Recompute new lastSuccessPaymentState. This is important to be able to allow new operations on the state machine (for e.g an AUTH_SUCCESS would now allow a CAPTURE operation)
    final String lastSuccessPaymentState = paymentStateMachineHelper.isSuccessState(newPaymentState) ? newPaymentState : null;
    // Update processedAmount and processedCurrency
    final BigDecimal processedAmount;
    if (TransactionStatus.SUCCESS.equals(transactionStatus) || TransactionStatus.PENDING.equals(transactionStatus)) {
        if (paymentTransactionInfoPlugin == null || paymentTransactionInfoPlugin.getAmount() == null) {
            processedAmount = paymentTransaction.getProcessedAmount();
        } else {
            processedAmount = paymentTransactionInfoPlugin.getAmount();
        }
    } else {
        processedAmount = BigDecimal.ZERO;
    }
    final Currency processedCurrency;
    if (paymentTransactionInfoPlugin == null || paymentTransactionInfoPlugin.getCurrency() == null) {
        processedCurrency = paymentTransaction.getProcessedCurrency();
    } else {
        processedCurrency = paymentTransactionInfoPlugin.getCurrency();
    }
    // Update the gatewayErrorCode, gatewayError if we got a paymentTransactionInfoPlugin
    final String gatewayErrorCode = paymentTransactionInfoPlugin != null ? paymentTransactionInfoPlugin.getGatewayErrorCode() : paymentTransaction.getGatewayErrorCode();
    final String gatewayError = paymentTransactionInfoPlugin != null ? paymentTransactionInfoPlugin.getGatewayError() : paymentTransaction.getGatewayErrorMsg();
    log.info("Repairing paymentId='{}', paymentTransactionId='{}', currentTransactionStatus='{}', newTransactionStatus='{}'", payment.getId(), paymentTransaction.getId(), paymentTransaction.getTransactionStatus(), transactionStatus);
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(payment.getAccountId(), callContext);
    paymentDao.updatePaymentAndTransactionOnCompletion(payment.getAccountId(), paymentTransaction.getAttemptId(), payment.getId(), paymentTransaction.getTransactionType(), newPaymentState, lastSuccessPaymentState, paymentTransaction.getId(), transactionStatus, processedAmount, processedCurrency, gatewayErrorCode, gatewayError, internalCallContext);
    return true;
}
Also used : Currency(org.killbill.billing.catalog.api.Currency) TransactionStatus(org.killbill.billing.payment.api.TransactionStatus) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) CallContext(org.killbill.billing.util.callcontext.CallContext) BigDecimal(java.math.BigDecimal)

Example 3 with TransactionStatus

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

the class IncompletePaymentAttemptTask method updatePaymentAndTransactionIfNeeded.

private boolean updatePaymentAndTransactionIfNeeded(final UUID accountId, final UUID paymentTransactionId, @Nullable final TransactionStatus currentTransactionStatus, @Nullable final PaymentTransactionInfoPlugin paymentTransactionInfoPlugin, @Nullable final Integer attemptNumber, @Nullable final UUID userToken, final boolean isApiPayment, final InternalTenantContext internalTenantContext) throws LockFailedException {
    // First, fix the transaction itself
    final TransactionStatus latestTransactionStatus = incompletePaymentTransactionTask.updatePaymentAndTransactionIfNeeded2(accountId, paymentTransactionId, currentTransactionStatus, paymentTransactionInfoPlugin, isApiPayment, internalTenantContext);
    final boolean hasTransactionChanged = latestTransactionStatus == null;
    // Don't insert a notification for the on-the-fly Janitor
    final boolean shouldInsertNotification = attemptNumber != null;
    if (!hasTransactionChanged && shouldInsertNotification) {
        insertNewNotificationForUnresolvedTransactionIfNeeded(paymentTransactionId, latestTransactionStatus, attemptNumber, userToken, isApiPayment, internalTenantContext.getAccountRecordId(), internalTenantContext.getTenantRecordId());
    }
    // If there is a payment attempt associated with that transaction, we need to update it as well
    boolean hasAttemptChanged = false;
    final PaymentTransactionModelDao paymentTransactionModelDao = paymentDao.getPaymentTransaction(paymentTransactionId, internalTenantContext);
    if (paymentTransactionModelDao.getAttemptId() != null) {
        final PaymentAttemptModelDao paymentAttemptModelDao = paymentDao.getPaymentAttempt(paymentTransactionModelDao.getAttemptId(), internalTenantContext);
        if (paymentAttemptModelDao != null) {
            if (hasTransactionChanged || retrySMHelper.getInitialState().getName().equals(paymentAttemptModelDao.getStateName())) {
                // Run the completion part of the state machine to call the plugins and update the attempt in the right terminal state)
                hasAttemptChanged = doIteration(paymentAttemptModelDao, isApiPayment);
            }
        }
    }
    return hasTransactionChanged || hasAttemptChanged;
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) TransactionStatus(org.killbill.billing.payment.api.TransactionStatus)

Example 4 with TransactionStatus

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

the class IncompletePaymentTransactionTask method updatePaymentAndTransactionInternal.

// Return the latest transactionStatus in case the state wasn't updated, null otherwise
private TransactionStatus updatePaymentAndTransactionInternal(final UUID accountId, final PaymentTransactionModelDao paymentTransaction, final PaymentTransactionInfoPlugin paymentTransactionInfoPlugin, final boolean isApiPayment, final InternalTenantContext internalTenantContext) {
    final UUID paymentId = paymentTransaction.getPaymentId();
    // First obtain the new transactionStatus,
    // Then compute the new paymentState; this one is mostly interesting in case of success (to compute the lastSuccessPaymentState below)
    final TransactionStatus transactionStatus = computeNewTransactionStatusFromPaymentTransactionInfoPlugin(paymentTransactionInfoPlugin, paymentTransaction.getTransactionStatus());
    final String newPaymentState;
    switch(transactionStatus) {
        case PENDING:
            newPaymentState = paymentStateMachineHelper.getPendingStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case SUCCESS:
            newPaymentState = paymentStateMachineHelper.getSuccessfulStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case PAYMENT_FAILURE:
            newPaymentState = paymentStateMachineHelper.getFailureStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case PLUGIN_FAILURE:
            newPaymentState = paymentStateMachineHelper.getErroredStateForTransaction(paymentTransaction.getTransactionType());
            break;
        case UNKNOWN:
        default:
            // We can't get anything interesting from the plugin...
            log.info("Unable to repair paymentId='{}', paymentTransactionId='{}', currentTransactionStatus='{}', newTransactionStatus='{}'", paymentId, paymentTransaction.getId(), paymentTransaction.getTransactionStatus(), transactionStatus);
            return transactionStatus;
    }
    // Our status did not change, so we just insert a new notification (attemptNumber will be incremented)
    if (transactionStatus == paymentTransaction.getTransactionStatus()) {
        log.info("Unable to repair paymentId='{}', paymentTransactionId='{}', currentTransactionStatus='{}', newTransactionStatus='{}'", paymentId, paymentTransaction.getId(), paymentTransaction.getTransactionStatus(), transactionStatus);
        return transactionStatus;
    }
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(internalTenantContext.getTenantRecordId(), internalTenantContext.getAccountRecordId(), "IncompletePaymentTransactionTask", CallOrigin.INTERNAL, UserType.SYSTEM, UUIDs.randomUUID());
    final PaymentAutomatonDAOHelper paymentAutomatonDAOHelper = new PaymentAutomatonDAOHelper(paymentDao, internalCallContext, paymentStateMachineHelper);
    log.info("Repairing paymentId='{}', paymentTransactionId='{}', currentTransactionStatus='{}', newTransactionStatus='{}'", paymentId, paymentTransaction.getId(), paymentTransaction.getTransactionStatus(), transactionStatus);
    paymentAutomatonDAOHelper.processPaymentInfoPlugin(transactionStatus, paymentTransactionInfoPlugin, newPaymentState, paymentTransaction.getProcessedAmount(), paymentTransaction.getProcessedCurrency(), accountId, paymentTransaction.getAttemptId(), paymentId, paymentTransaction.getId(), paymentTransaction.getTransactionType(), isApiPayment);
    return null;
}
Also used : PaymentAutomatonDAOHelper(org.killbill.billing.payment.core.sm.PaymentAutomatonDAOHelper) TransactionStatus(org.killbill.billing.payment.api.TransactionStatus) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) UUID(java.util.UUID)

Aggregations

TransactionStatus (org.killbill.billing.payment.api.TransactionStatus)4 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 BigDecimal (java.math.BigDecimal)1 UUID (java.util.UUID)1 Currency (org.killbill.billing.catalog.api.Currency)1 BusInternalEvent (org.killbill.billing.events.BusInternalEvent)1 DefaultPaymentErrorEvent (org.killbill.billing.payment.api.DefaultPaymentErrorEvent)1 PaymentAutomatonDAOHelper (org.killbill.billing.payment.core.sm.PaymentAutomatonDAOHelper)1 PaymentAttemptModelDao (org.killbill.billing.payment.dao.PaymentAttemptModelDao)1 PaymentTransactionModelDao (org.killbill.billing.payment.dao.PaymentTransactionModelDao)1 PaymentTransactionInfoPlugin (org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin)1 CallContext (org.killbill.billing.util.callcontext.CallContext)1 EventBusException (org.killbill.bus.api.PersistentBus.EventBusException)1