private void updateDefaultPaymentMethodIfNeeded(final String pluginName, final Account account, @Nullable final UUID defaultPluginPaymentMethodId, final InternalCallContext context) throws PaymentApiException, AccountApiException { // If the plugin does not have a default payment gateway, we keep the current default payment method in KB account as it is. if (defaultPluginPaymentMethodId == null) { return; } // Some gateways have the concept of default payment methods. Kill Bill has also its own default payment method // and is authoritative on this matter. However, if the default payment method is associated with a given plugin, // and if the default payment method in that plugin has changed, we will reflect this change in Kill Bill as well. boolean shouldUpdateDefaultPaymentMethod = true; if (account.getPaymentMethodId() != null) { final PaymentMethodModelDao currentDefaultPaymentMethod = paymentDao.getPaymentMethod(account.getPaymentMethodId(), context); shouldUpdateDefaultPaymentMethod = pluginName.equals(currentDefaultPaymentMethod.getPluginName()); } if (shouldUpdateDefaultPaymentMethod) { accountInternalApi.updatePaymentMethod(account.getId(), defaultPluginPaymentMethodId, context); } } }
@Override public Void doOperation() throws PaymentApiException { final PaymentMethodModelDao paymentMethodModel = paymentDao.getPaymentMethod(paymentMethodId, context); if (paymentMethodModel == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, paymentMethodId); } try { final PaymentPluginApi pluginApi = getPluginApi(paymentMethodId, context); pluginApi.setDefaultPaymentMethod(account.getId(), paymentMethodId, context.toCallContext(tenantId)); accountInternalApi.updatePaymentMethod(account.getId(), paymentMethodId, context); return null; } catch (PaymentPluginApiException e) { throw new PaymentApiException(ErrorCode.PAYMENT_UPD_PAYMENT_METHOD, account.getId(), e.getErrorMessage()); } catch (AccountApiException e) { throw new PaymentApiException(e); } } });
@Override public UUID doOperation() throws PaymentApiException { PaymentMethod pm = null; PaymentPluginApi pluginApi = null; try { pluginApi = getPaymentPluginApi(paymentPluginServiceName); pm = new DefaultPaymentMethod(account.getId(), paymentPluginServiceName, paymentMethodProps); pluginApi.addPaymentMethod(account.getId(), pm.getId(), paymentMethodProps, setDefault, context.toCallContext(tenantId)); final PaymentMethodModelDao pmModel = new PaymentMethodModelDao(pm.getId(), pm.getCreatedDate(), pm.getUpdatedDate(), pm.getAccountId(), pm.getPluginName(), pm.isActive()); paymentDao.insertPaymentMethod(pmModel, context); if (setDefault) { accountInternalApi.updatePaymentMethod(account.getId(), pm.getId(), context); } } catch (PaymentPluginApiException e) { log.warn("Error adding payment method " + pm.getId() + " for plugin " + paymentPluginServiceName, e); // STEPH all errors should also take a pluginName throw new PaymentApiException(ErrorCode.PAYMENT_ADD_PAYMENT_METHOD, account.getId(), e.getErrorMessage()); } catch (AccountApiException e) { throw new PaymentApiException(e); } return pm.getId(); } });