killbill-uncached

Details

diff --git a/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java b/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java
index 007a773..54f471b 100644
--- a/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java
+++ b/payment/src/main/java/com/ning/billing/payment/core/PaymentMethodProcessor.java
@@ -349,25 +349,24 @@ public class PaymentMethodProcessor extends ProcessorBase {
         });
     }
 
-    private void updateDefaultPaymentMethodIfNeeded(final String pluginName, final Account account, @Nullable final UUID defaultPaymentMethodId, final InternalCallContext context) throws PaymentApiException, AccountApiException {
+    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) {
-            return;
-        }
-
-        // Note that the code below is a no-op if the default payment method hasn't changed
-        if (defaultPaymentMethodId != null) {
-            accountInternalApi.updatePaymentMethod(account.getId(), defaultPaymentMethodId, context);
-        } else {
-            accountInternalApi.removePaymentMethod(account.getId(), context);
+            accountInternalApi.updatePaymentMethod(account.getId(), defaultPluginPaymentMethodId, context);
         }
     }
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java b/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java
index 62b4ca1..d530e5b 100644
--- a/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java
+++ b/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java
@@ -342,7 +342,12 @@ public class DefaultPaymentDao implements PaymentDao {
 
                 // Finally, all payment methods left in the existingPaymentMethods should be marked as deleted
                 for (final PaymentMethodModelDao existingPaymentMethod : existingPaymentMethods) {
-                    deletedPaymentMethodInTransaction(entitySqlDaoWrapperFactory, existingPaymentMethod.getId(), context);
+                    // Need to verify if this is active -- failure to do so would provide an exception down the stream because
+                    // the logic around audit/history will use getById to retrieve the entity and that method would not return
+                    // a marked as deleted object
+                    if (existingPaymentMethod.isActive()) {
+                        deletedPaymentMethodInTransaction(entitySqlDaoWrapperFactory, existingPaymentMethod.getId(), context);
+                    }
                 }
                 return transactional.getByAccountId(accountId.toString(), context);
             }