Search in sources :

Example 6 with PaymentPluginApi

use of org.killbill.billing.payment.plugin.api.PaymentPluginApi in project killbill by killbill.

the class PaymentMethodProcessor method addPaymentMethod.

public UUID addPaymentMethod(final String paymentMethodExternalKey, final String paymentPluginServiceName, final Account account, final boolean setDefault, final PaymentMethodPlugin paymentMethodProps, final Iterable<PluginProperty> properties, final CallContext callContext, final InternalCallContext context) throws PaymentApiException {
    return dispatchWithExceptionHandling(account, paymentPluginServiceName, new CallableWithAccountLock<UUID, PaymentApiException>(locker, account.getId(), paymentConfig, new DispatcherCallback<PluginDispatcherReturnType<UUID>, PaymentApiException>() {

        @Override
        public PluginDispatcherReturnType<UUID> doOperation() throws PaymentApiException {
            PaymentMethod pm = null;
            try {
                validateUniqueExternalPaymentMethod(account.getId(), paymentPluginServiceName);
                pm = new DefaultPaymentMethod(paymentMethodExternalKey, account.getId(), paymentPluginServiceName, paymentMethodProps);
                final PaymentPluginApi pluginApi = getPaymentPluginApi(paymentPluginServiceName);
                pluginApi.addPaymentMethod(account.getId(), pm.getId(), paymentMethodProps, setDefault, properties, callContext);
                final String actualPaymentMethodExternalKey = retrieveActualPaymentMethodExternalKey(account, pm, pluginApi, properties, callContext, context);
                final PaymentMethodModelDao pmModel = new PaymentMethodModelDao(pm.getId(), actualPaymentMethodExternalKey, pm.getCreatedDate(), pm.getUpdatedDate(), pm.getAccountId(), pm.getPluginName(), pm.isActive());
                paymentDao.insertPaymentMethod(pmModel, context);
                if (setDefault) {
                    accountInternalApi.updatePaymentMethod(account.getId(), pm.getId(), context);
                }
            } catch (final PaymentPluginApiException e) {
                throw new PaymentApiException(ErrorCode.PAYMENT_ADD_PAYMENT_METHOD, account.getId(), e.getErrorMessage());
            } catch (final AccountApiException e) {
                throw new PaymentApiException(e);
            }
            return PluginDispatcher.createPluginDispatcherReturnType(pm.getId());
        }

        private void validateUniqueExternalPaymentMethod(final UUID accountId, final String pluginName) throws PaymentApiException {
            if (ExternalPaymentProviderPlugin.PLUGIN_NAME.equals(pluginName)) {
                final List<PaymentMethodModelDao> accountPaymentMethods = paymentDao.getPaymentMethods(context);
                if (Iterables.any(accountPaymentMethods, new Predicate<PaymentMethodModelDao>() {

                    @Override
                    public boolean apply(final PaymentMethodModelDao input) {
                        return ExternalPaymentProviderPlugin.PLUGIN_NAME.equals(input.getPluginName());
                    }
                })) {
                    throw new PaymentApiException(ErrorCode.PAYMENT_EXTERNAL_PAYMENT_METHOD_ALREADY_EXISTS, accountId);
                }
            }
        }
    }), uuidPluginNotificationDispatcher);
}
Also used : PaymentPluginApiException(org.killbill.billing.payment.plugin.api.PaymentPluginApiException) PaymentMethodModelDao(org.killbill.billing.payment.dao.PaymentMethodModelDao) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) DefaultPaymentMethod(org.killbill.billing.payment.api.DefaultPaymentMethod) PaymentPluginApi(org.killbill.billing.payment.plugin.api.PaymentPluginApi) AccountApiException(org.killbill.billing.account.api.AccountApiException) PaymentMethod(org.killbill.billing.payment.api.PaymentMethod) DefaultPaymentMethod(org.killbill.billing.payment.api.DefaultPaymentMethod) UUID(java.util.UUID)

Example 7 with PaymentPluginApi

use of org.killbill.billing.payment.plugin.api.PaymentPluginApi in project killbill by killbill.

the class PaymentProcessor method performOperation.

private Payment performOperation(final boolean isApiPayment, final boolean runJanitor, @Nullable final UUID attemptId, final TransactionType transactionType, final Account account, @Nullable final UUID paymentMethodId, @Nullable final UUID paymentId, @Nullable final UUID transactionId, @Nullable final BigDecimal amount, @Nullable final Currency currency, @Nullable final String paymentExternalKey, @Nullable final String paymentTransactionExternalKey, @Nullable final UUID paymentIdForNewPayment, @Nullable final UUID paymentTransactionIdForNewPaymentTransaction, final boolean shouldLockAccountAndDispatch, @Nullable final OperationResult overridePluginOperationResult, final Iterable<PluginProperty> properties, final CallContext callContext, final InternalCallContext internalCallContext) throws PaymentApiException {
    final PaymentStateContext paymentStateContext = paymentAutomatonRunner.buildPaymentStateContext(isApiPayment, transactionType, account, attemptId, paymentMethodId != null ? paymentMethodId : account.getPaymentMethodId(), paymentId, transactionId, paymentExternalKey, paymentTransactionExternalKey, amount, currency, paymentIdForNewPayment, paymentTransactionIdForNewPaymentTransaction, shouldLockAccountAndDispatch, overridePluginOperationResult, properties, callContext, internalCallContext);
    final PaymentAutomatonDAOHelper daoHelper = paymentAutomatonRunner.buildDaoHelper(paymentStateContext, internalCallContext);
    String currentStateName = null;
    if (paymentStateContext.getPaymentId() != null) {
        PaymentModelDao paymentModelDao = daoHelper.getPayment();
        // Sanity: verify the payment belongs to the right account (in case it was looked-up by payment or transaction external key)
        if (!paymentModelDao.getAccountRecordId().equals(internalCallContext.getAccountRecordId())) {
            throw new PaymentApiException(ErrorCode.PAYMENT_DIFFERENT_ACCOUNT_ID, paymentStateContext.getPaymentId());
        }
        // Note: the list needs to be modifiable for invokeJanitor
        final Collection<PaymentTransactionModelDao> paymentTransactionsForCurrentPayment = new LinkedList<PaymentTransactionModelDao>(daoHelper.getPaymentDao().getTransactionsForPayment(paymentStateContext.getPaymentId(), paymentStateContext.getInternalCallContext()));
        // prevent disallowed transitions in case the state couldn't be fixed (or if it's already in a final state).
        if (runJanitor) {
            final PaymentPluginApi plugin = getPaymentProviderPlugin(paymentModelDao.getPaymentMethodId(), true, internalCallContext);
            final List<PaymentTransactionInfoPlugin> pluginTransactions = getPaymentTransactionInfoPlugins(plugin, paymentModelDao, properties, callContext);
            paymentModelDao = invokeJanitor(paymentModelDao, paymentTransactionsForCurrentPayment, pluginTransactions, internalCallContext);
        }
        if (paymentStateContext.getPaymentTransactionExternalKey() != null) {
            final List<PaymentTransactionModelDao> allPaymentTransactionsForKey = daoHelper.getPaymentDao().getPaymentTransactionsByExternalKey(paymentStateContext.getPaymentTransactionExternalKey(), internalCallContext);
            runSanityOnTransactionExternalKey(allPaymentTransactionsForKey, paymentStateContext, internalCallContext);
        }
        if (paymentStateContext.getTransactionId() != null || paymentStateContext.getPaymentTransactionExternalKey() != null) {
            // If a transaction id or key is passed, we are maybe completing an existing transaction (unless a new key was provided)
            PaymentTransactionModelDao transactionToComplete = findTransactionToCompleteAndRunSanityChecks(paymentModelDao, paymentTransactionsForCurrentPayment, paymentStateContext, internalCallContext);
            if (transactionToComplete != null) {
                final UUID transactionToCompleteId = transactionToComplete.getId();
                transactionToComplete = Iterables.<PaymentTransactionModelDao>find(paymentTransactionsForCurrentPayment, new Predicate<PaymentTransactionModelDao>() {

                    @Override
                    public boolean apply(final PaymentTransactionModelDao input) {
                        return transactionToCompleteId.equals(input.getId());
                    }
                });
                // We can't tell where we should be in the state machine - bail (cannot be enforced by the state machine unfortunately because UNKNOWN and PLUGIN_FAILURE are both treated as EXCEPTION)
                if (transactionToComplete.getTransactionStatus() == TransactionStatus.UNKNOWN) {
                    throw new PaymentApiException(ErrorCode.PAYMENT_INVALID_OPERATION, paymentStateContext.getTransactionType(), transactionToComplete.getTransactionStatus());
                }
                paymentStateContext.setPaymentTransactionModelDao(transactionToComplete);
            }
        }
        // Use the original payment method id of the payment being completed
        paymentStateContext.setPaymentMethodId(paymentModelDao.getPaymentMethodId());
        // We always take the last successful state name to permit retries on failures
        currentStateName = paymentModelDao.getLastSuccessStateName();
    }
    final UUID nonNullPaymentId = paymentAutomatonRunner.run(paymentStateContext, daoHelper, currentStateName, transactionType);
    return getPayment(nonNullPaymentId, true, false, properties, callContext, internalCallContext);
}
Also used : PaymentModelDao(org.killbill.billing.payment.dao.PaymentModelDao) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) LinkedList(java.util.LinkedList) Predicate(com.google.common.base.Predicate) PaymentPluginApi(org.killbill.billing.payment.plugin.api.PaymentPluginApi) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) PaymentAutomatonDAOHelper(org.killbill.billing.payment.core.sm.PaymentAutomatonDAOHelper) PaymentStateContext(org.killbill.billing.payment.core.sm.PaymentStateContext) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) UUID(java.util.UUID)

Example 8 with PaymentPluginApi

use of org.killbill.billing.payment.plugin.api.PaymentPluginApi in project killbill by killbill.

the class PaymentGatewayProcessor method buildFormDescriptor.

public HostedPaymentPageFormDescriptor buildFormDescriptor(final boolean shouldDispatch, final Account account, final UUID paymentMethodId, final Iterable<PluginProperty> customFields, final Iterable<PluginProperty> properties, final CallContext callContext, final InternalCallContext internalCallContext) throws PaymentApiException {
    final String pluginName = getPaymentMethodById(paymentMethodId, true, internalCallContext).getPluginName();
    final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
    if (shouldDispatch) {
        return dispatchWithExceptionHandling(account, pluginName, new Callable<PluginDispatcherReturnType<HostedPaymentPageFormDescriptor>>() {

            @Override
            public PluginDispatcherReturnType<HostedPaymentPageFormDescriptor> call() throws PaymentApiException {
                try {
                    final HostedPaymentPageFormDescriptor result = plugin.buildFormDescriptor(account.getId(), customFields, properties, callContext);
                    return PluginDispatcher.createPluginDispatcherReturnType(result == null ? new DefaultNoOpHostedPaymentPageFormDescriptor(account.getId()) : result);
                } catch (final RuntimeException e) {
                    throw new PaymentApiException(e, ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), ""));
                } catch (final PaymentPluginApiException e) {
                    throw new PaymentApiException(e, ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
                }
            }
        }, paymentPluginFormDispatcher);
    } else {
        try {
            return plugin.buildFormDescriptor(account.getId(), customFields, properties, callContext);
        } catch (final PaymentPluginApiException e) {
            throw new PaymentApiException(e, ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
        }
    }
}
Also used : PaymentPluginApi(org.killbill.billing.payment.plugin.api.PaymentPluginApi) PluginDispatcherReturnType(org.killbill.billing.payment.dispatcher.PluginDispatcher.PluginDispatcherReturnType) DefaultNoOpHostedPaymentPageFormDescriptor(org.killbill.billing.payment.provider.DefaultNoOpHostedPaymentPageFormDescriptor) HostedPaymentPageFormDescriptor(org.killbill.billing.payment.plugin.api.HostedPaymentPageFormDescriptor) DefaultNoOpHostedPaymentPageFormDescriptor(org.killbill.billing.payment.provider.DefaultNoOpHostedPaymentPageFormDescriptor) PaymentPluginApiException(org.killbill.billing.payment.plugin.api.PaymentPluginApiException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException)

Aggregations

PaymentPluginApi (org.killbill.billing.payment.plugin.api.PaymentPluginApi)8 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)5 PaymentPluginApiException (org.killbill.billing.payment.plugin.api.PaymentPluginApiException)4 UUID (java.util.UUID)3 DefaultPaymentMethod (org.killbill.billing.payment.api.DefaultPaymentMethod)3 PaymentTransactionInfoPlugin (org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin)3 Predicate (com.google.common.base.Predicate)2 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 AccountApiException (org.killbill.billing.account.api.AccountApiException)2 PaymentMethod (org.killbill.billing.payment.api.PaymentMethod)2 PaymentMethodModelDao (org.killbill.billing.payment.dao.PaymentMethodModelDao)2 PaymentModelDao (org.killbill.billing.payment.dao.PaymentModelDao)2 PaymentTransactionModelDao (org.killbill.billing.payment.dao.PaymentTransactionModelDao)2 PluginDispatcherReturnType (org.killbill.billing.payment.dispatcher.PluginDispatcher.PluginDispatcherReturnType)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 PaymentMethodPlugin (org.killbill.billing.payment.api.PaymentMethodPlugin)1