Search in sources :

Example 1 with EnteringStateCallback

use of org.killbill.automaton.State.EnteringStateCallback in project killbill by killbill.

the class PluginControlPaymentAutomatonRunner method run.

public Payment run(final State state, final boolean isApiPayment, final Boolean isSuccess, final TransactionType transactionType, final ControlOperation controlOperation, final Account account, @Nullable final UUID paymentMethodId, @Nullable final UUID paymentId, @Nullable final String paymentExternalKey, @Nullable final UUID transactionId, final String paymentTransactionExternalKey, @Nullable final BigDecimal amount, @Nullable final Currency currency, final Iterable<PluginProperty> properties, @Nullable final List<String> paymentControlPluginNames, final CallContext callContext, final InternalCallContext internalCallContext) throws PaymentApiException {
    final PaymentStateControlContext paymentStateContext = createContext(isApiPayment, isSuccess, transactionType, account, paymentMethodId, paymentId, paymentExternalKey, transactionId, paymentTransactionExternalKey, amount, currency, properties, paymentControlPluginNames, callContext, internalCallContext);
    try {
        final OperationCallback callback = createOperationCallback(controlOperation, paymentStateContext);
        final LeavingStateCallback leavingStateCallback = new DefaultControlInitiated(this, paymentStateContext, paymentDao, paymentControlStateMachineHelper.getInitialState(), paymentControlStateMachineHelper.getRetriedState(), transactionType);
        final EnteringStateCallback enteringStateCallback = new DefaultControlCompleted(this, paymentStateContext, paymentControlStateMachineHelper.getRetriedState(), retryServiceScheduler);
        state.runOperation(paymentControlStateMachineHelper.getOperation(), callback, enteringStateCallback, leavingStateCallback);
    } catch (final MissingEntryException e) {
        throw new PaymentApiException(e.getCause(), ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), ""));
    } catch (final OperationException e) {
        if (e.getCause() instanceof PaymentApiException) {
            throw (PaymentApiException) e.getCause();
        // If the control plugin tries to pass us back a PaymentApiException we throw it
        } else if (e.getCause() instanceof PaymentControlApiException && e.getCause().getCause() instanceof PaymentApiException) {
            throw (PaymentApiException) e.getCause().getCause();
        } else if (e.getCause() != null || paymentStateContext.getResult() == null) {
            throw new PaymentApiException(e.getCause(), ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), ""));
        }
    }
    // we don't throw, and return the failed Payment instead to be consistent with what happens when we don't go through control api.
    return paymentStateContext.getResult();
}
Also used : DefaultControlInitiated(org.killbill.billing.payment.core.sm.control.DefaultControlInitiated) OperationCallback(org.killbill.automaton.Operation.OperationCallback) DefaultControlCompleted(org.killbill.billing.payment.core.sm.control.DefaultControlCompleted) MissingEntryException(org.killbill.automaton.MissingEntryException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) LeavingStateCallback(org.killbill.automaton.State.LeavingStateCallback) EnteringStateCallback(org.killbill.automaton.State.EnteringStateCallback) OperationException(org.killbill.automaton.OperationException) PaymentControlApiException(org.killbill.billing.control.plugin.api.PaymentControlApiException) PaymentStateControlContext(org.killbill.billing.payment.core.sm.control.PaymentStateControlContext)

Example 2 with EnteringStateCallback

use of org.killbill.automaton.State.EnteringStateCallback in project killbill by killbill.

the class PluginControlPaymentAutomatonRunner method completeRun.

public Payment completeRun(final PaymentStateControlContext paymentStateContext) throws PaymentApiException {
    try {
        final OperationCallback callback = new CompletionControlOperation(locker, paymentPluginDispatcher, paymentConfig, paymentStateContext, paymentProcessor, controlPluginRunner);
        final LeavingStateCallback leavingStateCallback = new NoopControlInitiated();
        final EnteringStateCallback enteringStateCallback = new DefaultControlCompleted(this, paymentStateContext, paymentControlStateMachineHelper.getRetriedState(), retryServiceScheduler);
        paymentControlStateMachineHelper.getInitialState().runOperation(paymentControlStateMachineHelper.getOperation(), callback, enteringStateCallback, leavingStateCallback);
    } catch (final MissingEntryException e) {
        throw new PaymentApiException(e.getCause(), ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), ""));
    } catch (final OperationException e) {
        if (e.getCause() instanceof PaymentApiException) {
            throw (PaymentApiException) e.getCause();
        // If the control plugin tries to pass us back a PaymentApiException we throw it
        } else if (e.getCause() instanceof PaymentControlApiException && e.getCause().getCause() instanceof PaymentApiException) {
            throw (PaymentApiException) e.getCause().getCause();
        } else if (e.getCause() != null || paymentStateContext.getResult() == null) {
            throw new PaymentApiException(e.getCause(), ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), ""));
        }
    }
    // we don't throw, and return the failed Payment instead to be consistent with what happens when we don't go through control api.
    return paymentStateContext.getResult();
}
Also used : OperationCallback(org.killbill.automaton.Operation.OperationCallback) CompletionControlOperation(org.killbill.billing.payment.core.sm.control.CompletionControlOperation) NoopControlInitiated(org.killbill.billing.payment.core.sm.control.NoopControlInitiated) DefaultControlCompleted(org.killbill.billing.payment.core.sm.control.DefaultControlCompleted) MissingEntryException(org.killbill.automaton.MissingEntryException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) LeavingStateCallback(org.killbill.automaton.State.LeavingStateCallback) EnteringStateCallback(org.killbill.automaton.State.EnteringStateCallback) OperationException(org.killbill.automaton.OperationException) PaymentControlApiException(org.killbill.billing.control.plugin.api.PaymentControlApiException)

Example 3 with EnteringStateCallback

use of org.killbill.automaton.State.EnteringStateCallback in project killbill by killbill.

the class PaymentAutomatonRunner method run.

public UUID run(final PaymentStateContext paymentStateContext, final PaymentAutomatonDAOHelper daoHelper, @Nullable final String currentStateNameOrNull, final TransactionType transactionType) throws PaymentApiException {
    final String currentStateName = MoreObjects.firstNonNull(currentStateNameOrNull, paymentSMHelper.getInitStateNameForTransaction());
    final OperationCallback operationCallback;
    final LeavingStateCallback leavingStateCallback;
    final EnteringStateCallback enteringStateCallback;
    Boolean includeDeletedPaymentMethod = Boolean.FALSE;
    switch(transactionType) {
        case PURCHASE:
            operationCallback = new PurchaseOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new PurchaseInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new PurchaseCompleted(daoHelper, paymentStateContext);
            break;
        case AUTHORIZE:
            operationCallback = new AuthorizeOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new AuthorizeInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new AuthorizeCompleted(daoHelper, paymentStateContext);
            break;
        case CAPTURE:
            operationCallback = new CaptureOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new CaptureInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new CaptureCompleted(daoHelper, paymentStateContext);
            break;
        case VOID:
            operationCallback = new VoidOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new VoidInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new VoidCompleted(daoHelper, paymentStateContext);
            includeDeletedPaymentMethod = Boolean.TRUE;
            break;
        case REFUND:
            operationCallback = new RefundOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new RefundInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new RefundCompleted(daoHelper, paymentStateContext);
            includeDeletedPaymentMethod = Boolean.TRUE;
            break;
        case CREDIT:
            operationCallback = new CreditOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new CreditInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new CreditCompleted(daoHelper, paymentStateContext);
            break;
        case CHARGEBACK:
            operationCallback = new ChargebackOperation(daoHelper, locker, paymentPluginDispatcher, paymentConfig, paymentStateContext);
            leavingStateCallback = new ChargebackInitiated(daoHelper, paymentStateContext);
            enteringStateCallback = new ChargebackCompleted(daoHelper, paymentStateContext);
            includeDeletedPaymentMethod = Boolean.TRUE;
            break;
        default:
            throw new IllegalStateException("Unsupported transaction type " + transactionType);
    }
    runStateMachineOperation(currentStateName, transactionType, leavingStateCallback, operationCallback, enteringStateCallback, includeDeletedPaymentMethod, paymentStateContext, daoHelper);
    return paymentStateContext.getPaymentId();
}
Also used : RefundOperation(org.killbill.billing.payment.core.sm.payments.RefundOperation) PurchaseOperation(org.killbill.billing.payment.core.sm.payments.PurchaseOperation) VoidOperation(org.killbill.billing.payment.core.sm.payments.VoidOperation) CaptureCompleted(org.killbill.billing.payment.core.sm.payments.CaptureCompleted) RefundInitiated(org.killbill.billing.payment.core.sm.payments.RefundInitiated) AuthorizeCompleted(org.killbill.billing.payment.core.sm.payments.AuthorizeCompleted) AuthorizeOperation(org.killbill.billing.payment.core.sm.payments.AuthorizeOperation) CreditCompleted(org.killbill.billing.payment.core.sm.payments.CreditCompleted) ChargebackInitiated(org.killbill.billing.payment.core.sm.payments.ChargebackInitiated) PurchaseInitiated(org.killbill.billing.payment.core.sm.payments.PurchaseInitiated) CreditInitiated(org.killbill.billing.payment.core.sm.payments.CreditInitiated) RefundCompleted(org.killbill.billing.payment.core.sm.payments.RefundCompleted) CreditOperation(org.killbill.billing.payment.core.sm.payments.CreditOperation) PurchaseCompleted(org.killbill.billing.payment.core.sm.payments.PurchaseCompleted) CaptureOperation(org.killbill.billing.payment.core.sm.payments.CaptureOperation) LeavingStateCallback(org.killbill.automaton.State.LeavingStateCallback) VoidCompleted(org.killbill.billing.payment.core.sm.payments.VoidCompleted) EnteringStateCallback(org.killbill.automaton.State.EnteringStateCallback) CaptureInitiated(org.killbill.billing.payment.core.sm.payments.CaptureInitiated) ChargebackOperation(org.killbill.billing.payment.core.sm.payments.ChargebackOperation) VoidInitiated(org.killbill.billing.payment.core.sm.payments.VoidInitiated) ChargebackCompleted(org.killbill.billing.payment.core.sm.payments.ChargebackCompleted) OperationCallback(org.killbill.automaton.Operation.OperationCallback) AuthorizeInitiated(org.killbill.billing.payment.core.sm.payments.AuthorizeInitiated)

Aggregations

OperationCallback (org.killbill.automaton.Operation.OperationCallback)3 EnteringStateCallback (org.killbill.automaton.State.EnteringStateCallback)3 LeavingStateCallback (org.killbill.automaton.State.LeavingStateCallback)3 MissingEntryException (org.killbill.automaton.MissingEntryException)2 OperationException (org.killbill.automaton.OperationException)2 PaymentControlApiException (org.killbill.billing.control.plugin.api.PaymentControlApiException)2 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)2 DefaultControlCompleted (org.killbill.billing.payment.core.sm.control.DefaultControlCompleted)2 CompletionControlOperation (org.killbill.billing.payment.core.sm.control.CompletionControlOperation)1 DefaultControlInitiated (org.killbill.billing.payment.core.sm.control.DefaultControlInitiated)1 NoopControlInitiated (org.killbill.billing.payment.core.sm.control.NoopControlInitiated)1 PaymentStateControlContext (org.killbill.billing.payment.core.sm.control.PaymentStateControlContext)1 AuthorizeCompleted (org.killbill.billing.payment.core.sm.payments.AuthorizeCompleted)1 AuthorizeInitiated (org.killbill.billing.payment.core.sm.payments.AuthorizeInitiated)1 AuthorizeOperation (org.killbill.billing.payment.core.sm.payments.AuthorizeOperation)1 CaptureCompleted (org.killbill.billing.payment.core.sm.payments.CaptureCompleted)1 CaptureInitiated (org.killbill.billing.payment.core.sm.payments.CaptureInitiated)1 CaptureOperation (org.killbill.billing.payment.core.sm.payments.CaptureOperation)1 ChargebackCompleted (org.killbill.billing.payment.core.sm.payments.ChargebackCompleted)1 ChargebackInitiated (org.killbill.billing.payment.core.sm.payments.ChargebackInitiated)1