killbill-aplcache

#69 - Added effective date as attribute of PaymentAttempt -

7/1/2016 4:04:49 PM

Details

diff --git a/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentAttempt.java b/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentAttempt.java
index 03c5fa1..97ec78f 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentAttempt.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentAttempt.java
@@ -17,13 +17,12 @@
 package org.killbill.billing.payment.api;
 
 import java.math.BigDecimal;
-import java.util.Arrays;
+import java.util.List;
 import java.util.UUID;
 
 import org.joda.time.DateTime;
 import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.entity.EntityBase;
-import org.killbill.billing.payment.dao.PaymentAttemptModelDao;
 
 public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt {
 
@@ -33,15 +32,17 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
     private final UUID transactionId;
     private final String transactionExternalKey;
     private final TransactionType transactionType;
+    private final DateTime effectiveDate;
     private final String stateName;
     private final BigDecimal amount;
     private final Currency currency;
     private final String pluginName;
-    private final byte[] pluginProperties;
+    private final List<PluginProperty> pluginProperties;
 
     public DefaultPaymentAttempt(final UUID accountId, final UUID paymentMethodId, final UUID id, final DateTime createdDate, final DateTime updatedDate,
-                                 final String paymentExternalKey, final UUID transactionId, final String transactionExternalKey, final TransactionType transactionType,
-                                 final String stateName, final BigDecimal amount, final Currency currency, final String pluginName, final byte[] pluginProperties) {
+                                 final DateTime effectiveDate, final String paymentExternalKey, final UUID transactionId, final String transactionExternalKey,
+                                 final TransactionType transactionType, final String stateName, final BigDecimal amount, final Currency currency,
+                                 final String pluginName, final List<PluginProperty> pluginProperties) {
         super(id, createdDate, updatedDate);
         this.accountId = accountId;
         this.paymentMethodId = paymentMethodId;
@@ -49,6 +50,7 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
         this.transactionId = transactionId;
         this.transactionExternalKey = transactionExternalKey;
         this.transactionType = transactionType;
+        this.effectiveDate = effectiveDate;
         this.stateName = stateName;
         this.amount = amount;
         this.currency = currency;
@@ -56,24 +58,6 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
         this.pluginProperties = pluginProperties;
     }
 
-    public DefaultPaymentAttempt(final PaymentAttemptModelDao paymentAttemptModelDao) {
-        this(paymentAttemptModelDao.getAccountId(),
-            paymentAttemptModelDao.getPaymentMethodId(),
-            paymentAttemptModelDao.getId(),
-            paymentAttemptModelDao.getCreatedDate(),
-            paymentAttemptModelDao.getUpdatedDate(),
-            paymentAttemptModelDao.getPaymentExternalKey(),
-            paymentAttemptModelDao.getTransactionId(),
-            paymentAttemptModelDao.getTransactionExternalKey(),
-            paymentAttemptModelDao.getTransactionType(),
-            paymentAttemptModelDao.getStateName(),
-            paymentAttemptModelDao.getAmount(),
-            paymentAttemptModelDao.getCurrency(),
-            paymentAttemptModelDao.getPluginName(),
-            paymentAttemptModelDao.getPluginProperties()
-        );
-    }
-
     @Override
     public UUID getAccountId() {
         return accountId;
@@ -120,7 +104,7 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
     }
 
     @Override
-    public byte[] getPluginProperties() {
+    public List<PluginProperty> getPluginProperties() {
         return pluginProperties;
     }
 
@@ -142,7 +126,7 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
                ", amount=" + amount +
                ", currency=" + currency +
                ", pluginName='" + pluginName + '\'' +
-               ", pluginProperties=" + Arrays.toString(pluginProperties) +
+               ", pluginProperties=" + pluginProperties +
                '}';
     }
 
@@ -178,7 +162,10 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
         if (transactionType != that.transactionType) {
             return false;
         }
-        if (stateName != that.stateName) {
+        if (effectiveDate != null ? !effectiveDate.equals(that.effectiveDate) : that.effectiveDate != null) {
+            return false;
+        }
+        if (stateName != null ? !stateName.equals(that.stateName) : that.stateName != null) {
             return false;
         }
         if (amount != null ? !amount.equals(that.amount) : that.amount != null) {
@@ -190,7 +177,7 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
         if (pluginName != null ? !pluginName.equals(that.pluginName) : that.pluginName != null) {
             return false;
         }
-        return Arrays.equals(pluginProperties, that.pluginProperties);
+        return pluginProperties != null ? pluginProperties.equals(that.pluginProperties) : that.pluginProperties == null;
 
     }
 
@@ -207,7 +194,7 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
         result = 31 * result + (amount != null ? amount.hashCode() : 0);
         result = 31 * result + (currency != null ? currency.hashCode() : 0);
         result = 31 * result + (pluginName != null ? pluginName.hashCode() : 0);
-        result = 31 * result + Arrays.hashCode(pluginProperties);
+        result = 31 * result + (pluginProperties != null ? pluginProperties.hashCode() : 0);
         return result;
     }
 }
diff --git a/payment/src/main/java/org/killbill/billing/payment/core/PaymentProcessor.java b/payment/src/main/java/org/killbill/billing/payment/core/PaymentProcessor.java
index 6ad704f..54a234a 100644
--- a/payment/src/main/java/org/killbill/billing/payment/core/PaymentProcessor.java
+++ b/payment/src/main/java/org/killbill/billing/payment/core/PaymentProcessor.java
@@ -61,12 +61,12 @@ import org.killbill.billing.payment.glue.DefaultPaymentService;
 import org.killbill.billing.payment.plugin.api.PaymentPluginApi;
 import org.killbill.billing.payment.plugin.api.PaymentPluginApiException;
 import org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin;
+import org.killbill.billing.payment.retry.DefaultRetryService;
 import org.killbill.billing.payment.retry.PaymentRetryNotificationKey;
 import org.killbill.billing.tag.TagInternalApi;
 import org.killbill.billing.util.callcontext.CallContext;
 import org.killbill.billing.util.callcontext.InternalCallContextFactory;
 import org.killbill.billing.util.callcontext.TenantContext;
-import org.killbill.billing.util.config.PaymentConfig;
 import org.killbill.billing.util.entity.DefaultPagination;
 import org.killbill.billing.util.entity.Pagination;
 import org.killbill.billing.util.entity.dao.DefaultPaginationHelper.EntityPaginationBuilder;
@@ -99,11 +99,13 @@ public class PaymentProcessor extends ProcessorBase {
 
     private final PaymentAutomatonRunner paymentAutomatonRunner;
     private final IncompletePaymentTransactionTask incompletePaymentTransactionTask;
-    private final PaymentConfig paymentConfig;
     private final NotificationQueueService notificationQueueService;
 
     private static final Logger log = LoggerFactory.getLogger(PaymentProcessor.class);
 
+    public static final String SCHEDULED = "SCHEDULED";
+
+
     @Inject
     public PaymentProcessor(final OSGIServiceRegistration<PaymentPluginApi> pluginRegistry,
                             final AccountInternalApi accountUserApi,
@@ -114,13 +116,11 @@ public class PaymentProcessor extends ProcessorBase {
                             final GlobalLocker locker,
                             final PaymentAutomatonRunner paymentAutomatonRunner,
                             final IncompletePaymentTransactionTask incompletePaymentTransactionTask,
-                            final PaymentConfig paymentConfig,
                             final NotificationQueueService notificationQueueService,
                             final Clock clock) {
         super(pluginRegistry, accountUserApi, paymentDao, tagUserApi, locker, internalCallContextFactory, invoiceApi, clock);
         this.paymentAutomatonRunner = paymentAutomatonRunner;
         this.incompletePaymentTransactionTask = incompletePaymentTransactionTask;
-        this.paymentConfig = paymentConfig;
         this.notificationQueueService = notificationQueueService;
     }
 
@@ -491,45 +491,66 @@ public class PaymentProcessor extends ProcessorBase {
     }
 
     private List<PaymentAttempt> getPaymentAttempts(final List<PaymentTransaction> purchasedTransactions,
-                                                    final List<PaymentAttemptModelDao> paymentAttempts,
+                                                    final List<PaymentAttemptModelDao> pastPaymentAttempts,
                                                     final InternalTenantContext internalTenantContext) {
 
-        List<PaymentAttempt> result = new ArrayList<PaymentAttempt>();
+        List<PaymentAttempt> paymentAttempts = new ArrayList<PaymentAttempt>();
+
+        // Last Attempt from model dao
+        PaymentAttemptModelDao lastPaymentAttemptModelDao = pastPaymentAttempts.get(pastPaymentAttempts.size() - 1);
+        // Last Attempt from Transactions
+        PaymentTransaction lastPaymentTransaction = purchasedTransactions.get(purchasedTransactions.size() - 1);
 
         // Add Past Payment Attempts
-        for (PaymentAttemptModelDao pastPaymentAttempt : paymentAttempts) {
-            DefaultPaymentAttempt paymentAttempt = new DefaultPaymentAttempt(pastPaymentAttempt);
-            result.add(paymentAttempt);
+        for (PaymentAttemptModelDao pastPaymentAttempt : pastPaymentAttempts) {
+            DefaultPaymentAttempt paymentAttempt = new DefaultPaymentAttempt(
+                    pastPaymentAttempt.getAccountId(),
+                    pastPaymentAttempt.getPaymentMethodId(),
+                    pastPaymentAttempt.getId(),
+                    pastPaymentAttempt.getCreatedDate(),
+                    pastPaymentAttempt.getUpdatedDate(),
+                    pastPaymentAttempt.getCreatedDate(),
+                    pastPaymentAttempt.getPaymentExternalKey(),
+                    pastPaymentAttempt.getTransactionId(),
+                    pastPaymentAttempt.getTransactionExternalKey(),
+                    pastPaymentAttempt.getTransactionType(),
+                    pastPaymentAttempt.getStateName(),
+                    pastPaymentAttempt.getAmount(),
+                    pastPaymentAttempt.getCurrency(),
+                    pastPaymentAttempt.getPluginName(),
+                    (lastPaymentTransaction.getPaymentInfoPlugin() != null) ? lastPaymentTransaction.getPaymentInfoPlugin().getProperties() : null);
+            paymentAttempts.add(paymentAttempt);
         }
 
         // Get Future Payment Attempts from Notification Queue and add them to the list
         try {
-            final NotificationQueue retryQueue = notificationQueueService.getNotificationQueue(DefaultPaymentService.SERVICE_NAME, "retry");
+            final NotificationQueue retryQueue = notificationQueueService.getNotificationQueue(DefaultPaymentService.SERVICE_NAME, DefaultRetryService.QUEUE_NAME);
             final List<NotificationEventWithMetadata<NotificationEvent>> notificationEventWithMetadatas =
                     retryQueue.getFutureNotificationForSearchKeys(internalTenantContext.getAccountRecordId(), internalTenantContext.getTenantRecordId());
 
             for (NotificationEventWithMetadata<NotificationEvent> notificationEvent : notificationEventWithMetadatas) {
                 DefaultPaymentAttempt futurePaymentAttempt = new DefaultPaymentAttempt(
-                        null, //accountId,
-                        null, //paymentMethodId,
-                        ((PaymentRetryNotificationKey) notificationEvent.getEvent()).getAttemptId(), //id,
-                        notificationEvent.getEffectiveDate(), //createdDate,
-                        notificationEvent.getEffectiveDate(), //updatedDate,
-                        null, //paymentExternalKey,
-                        null, //transactionId,
-                        null, //transactionExternalKey,
-                        null, //transactionType,
-                        "SCHEDULED", //stateName,
-                        null, //amount,
-                        null, //currency,
-                        ((PaymentRetryNotificationKey) notificationEvent.getEvent()).getPaymentControlPluginNames().get(0), //pluginName,
-                        null);//pluginProperties
-                result.add(futurePaymentAttempt);
+                        lastPaymentAttemptModelDao.getAccountId(), // accountId
+                        lastPaymentAttemptModelDao.getPaymentMethodId(), // paymentMethodId
+                        ((PaymentRetryNotificationKey) notificationEvent.getEvent()).getAttemptId(), // id
+                        null, // createdDate
+                        null, // updatedDate
+                        notificationEvent.getEffectiveDate(), // effectiveDate
+                        lastPaymentAttemptModelDao.getPaymentExternalKey(), // paymentExternalKey
+                        null, // transactionId
+                        lastPaymentAttemptModelDao.getTransactionExternalKey(), // transactionExternalKey
+                        lastPaymentAttemptModelDao.getTransactionType(), // transactionType
+                        SCHEDULED, // stateName
+                        lastPaymentTransaction.getAmount(), // amount
+                        lastPaymentTransaction.getCurrency(), // currency
+                        ((PaymentRetryNotificationKey) notificationEvent.getEvent()).getPaymentControlPluginNames().get(0), // pluginName,
+                        (lastPaymentTransaction.getPaymentInfoPlugin() != null) ? lastPaymentTransaction.getPaymentInfoPlugin().getProperties() : null); // pluginProperties
+                paymentAttempts.add(futurePaymentAttempt);
             }
         } catch (NoSuchNotificationQueue noSuchNotificationQueue) {
             log.error("ERROR Loading Notification Queue - " + noSuchNotificationQueue.getMessage());
         }
-        return result;
+        return paymentAttempts;
     }
 
     private PaymentTransactionInfoPlugin findPaymentTransactionInfoPlugin(final PaymentTransactionModelDao paymentTransactionModelDao, @Nullable final Iterable<PaymentTransactionInfoPlugin> pluginTransactions) {