Skip to content

Commit

Permalink
#69
Browse files Browse the repository at this point in the history
- Added effective date as attribute of PaymentAttempt
- Changed type of pluginProperties attribute
- Using last payment attempt to complete past and future attempts in the list
  • Loading branch information
Javier Gómez committed Jul 1, 2016
1 parent d92ff70 commit 27af4f1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 54 deletions.
Expand Up @@ -17,13 +17,12 @@
package org.killbill.billing.payment.api; package org.killbill.billing.payment.api;


import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Arrays; import java.util.List;
import java.util.UUID; import java.util.UUID;


import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.killbill.billing.catalog.api.Currency; import org.killbill.billing.catalog.api.Currency;
import org.killbill.billing.entity.EntityBase; import org.killbill.billing.entity.EntityBase;
import org.killbill.billing.payment.dao.PaymentAttemptModelDao;


public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt { public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt {


Expand All @@ -33,47 +32,32 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt
private final UUID transactionId; private final UUID transactionId;
private final String transactionExternalKey; private final String transactionExternalKey;
private final TransactionType transactionType; private final TransactionType transactionType;
private final DateTime effectiveDate;
private final String stateName; private final String stateName;
private final BigDecimal amount; private final BigDecimal amount;
private final Currency currency; private final Currency currency;
private final String pluginName; 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, 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 DateTime effectiveDate, final String paymentExternalKey, final UUID transactionId, final String transactionExternalKey,
final String stateName, final BigDecimal amount, final Currency currency, final String pluginName, final byte[] pluginProperties) { final TransactionType transactionType, final String stateName, final BigDecimal amount, final Currency currency,
final String pluginName, final List<PluginProperty> pluginProperties) {
super(id, createdDate, updatedDate); super(id, createdDate, updatedDate);
this.accountId = accountId; this.accountId = accountId;
this.paymentMethodId = paymentMethodId; this.paymentMethodId = paymentMethodId;
this.paymentExternalKey = paymentExternalKey; this.paymentExternalKey = paymentExternalKey;
this.transactionId = transactionId; this.transactionId = transactionId;
this.transactionExternalKey = transactionExternalKey; this.transactionExternalKey = transactionExternalKey;
this.transactionType = transactionType; this.transactionType = transactionType;
this.effectiveDate = effectiveDate;
this.stateName = stateName; this.stateName = stateName;
this.amount = amount; this.amount = amount;
this.currency = currency; this.currency = currency;
this.pluginName = pluginName; this.pluginName = pluginName;
this.pluginProperties = pluginProperties; 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 @Override
public UUID getAccountId() { public UUID getAccountId() {
return accountId; return accountId;
Expand Down Expand Up @@ -120,7 +104,7 @@ public String getPluginName() {
} }


@Override @Override
public byte[] getPluginProperties() { public List<PluginProperty> getPluginProperties() {
return pluginProperties; return pluginProperties;
} }


Expand All @@ -142,7 +126,7 @@ public String toString() {
", amount=" + amount + ", amount=" + amount +
", currency=" + currency + ", currency=" + currency +
", pluginName='" + pluginName + '\'' + ", pluginName='" + pluginName + '\'' +
", pluginProperties=" + Arrays.toString(pluginProperties) + ", pluginProperties=" + pluginProperties +
'}'; '}';
} }


Expand Down Expand Up @@ -178,7 +162,10 @@ public boolean equals(final Object o) {
if (transactionType != that.transactionType) { if (transactionType != that.transactionType) {
return false; 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; return false;
} }
if (amount != null ? !amount.equals(that.amount) : that.amount != null) { if (amount != null ? !amount.equals(that.amount) : that.amount != null) {
Expand All @@ -190,7 +177,7 @@ public boolean equals(final Object o) {
if (pluginName != null ? !pluginName.equals(that.pluginName) : that.pluginName != null) { if (pluginName != null ? !pluginName.equals(that.pluginName) : that.pluginName != null) {
return false; return false;
} }
return Arrays.equals(pluginProperties, that.pluginProperties); return pluginProperties != null ? pluginProperties.equals(that.pluginProperties) : that.pluginProperties == null;


} }


Expand All @@ -207,7 +194,7 @@ public int hashCode() {
result = 31 * result + (amount != null ? amount.hashCode() : 0); result = 31 * result + (amount != null ? amount.hashCode() : 0);
result = 31 * result + (currency != null ? currency.hashCode() : 0); result = 31 * result + (currency != null ? currency.hashCode() : 0);
result = 31 * result + (pluginName != null ? pluginName.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; return result;
} }
} }
Expand Up @@ -61,12 +61,12 @@
import org.killbill.billing.payment.plugin.api.PaymentPluginApi; import org.killbill.billing.payment.plugin.api.PaymentPluginApi;
import org.killbill.billing.payment.plugin.api.PaymentPluginApiException; import org.killbill.billing.payment.plugin.api.PaymentPluginApiException;
import org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin; 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.payment.retry.PaymentRetryNotificationKey;
import org.killbill.billing.tag.TagInternalApi; import org.killbill.billing.tag.TagInternalApi;
import org.killbill.billing.util.callcontext.CallContext; import org.killbill.billing.util.callcontext.CallContext;
import org.killbill.billing.util.callcontext.InternalCallContextFactory; import org.killbill.billing.util.callcontext.InternalCallContextFactory;
import org.killbill.billing.util.callcontext.TenantContext; 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.DefaultPagination;
import org.killbill.billing.util.entity.Pagination; import org.killbill.billing.util.entity.Pagination;
import org.killbill.billing.util.entity.dao.DefaultPaginationHelper.EntityPaginationBuilder; import org.killbill.billing.util.entity.dao.DefaultPaginationHelper.EntityPaginationBuilder;
Expand Down Expand Up @@ -99,11 +99,13 @@ public class PaymentProcessor extends ProcessorBase {


private final PaymentAutomatonRunner paymentAutomatonRunner; private final PaymentAutomatonRunner paymentAutomatonRunner;
private final IncompletePaymentTransactionTask incompletePaymentTransactionTask; private final IncompletePaymentTransactionTask incompletePaymentTransactionTask;
private final PaymentConfig paymentConfig;
private final NotificationQueueService notificationQueueService; private final NotificationQueueService notificationQueueService;


private static final Logger log = LoggerFactory.getLogger(PaymentProcessor.class); private static final Logger log = LoggerFactory.getLogger(PaymentProcessor.class);


public static final String SCHEDULED = "SCHEDULED";


@Inject @Inject
public PaymentProcessor(final OSGIServiceRegistration<PaymentPluginApi> pluginRegistry, public PaymentProcessor(final OSGIServiceRegistration<PaymentPluginApi> pluginRegistry,
final AccountInternalApi accountUserApi, final AccountInternalApi accountUserApi,
Expand All @@ -114,13 +116,11 @@ public PaymentProcessor(final OSGIServiceRegistration<PaymentPluginApi> pluginRe
final GlobalLocker locker, final GlobalLocker locker,
final PaymentAutomatonRunner paymentAutomatonRunner, final PaymentAutomatonRunner paymentAutomatonRunner,
final IncompletePaymentTransactionTask incompletePaymentTransactionTask, final IncompletePaymentTransactionTask incompletePaymentTransactionTask,
final PaymentConfig paymentConfig,
final NotificationQueueService notificationQueueService, final NotificationQueueService notificationQueueService,
final Clock clock) { final Clock clock) {
super(pluginRegistry, accountUserApi, paymentDao, tagUserApi, locker, internalCallContextFactory, invoiceApi, clock); super(pluginRegistry, accountUserApi, paymentDao, tagUserApi, locker, internalCallContextFactory, invoiceApi, clock);
this.paymentAutomatonRunner = paymentAutomatonRunner; this.paymentAutomatonRunner = paymentAutomatonRunner;
this.incompletePaymentTransactionTask = incompletePaymentTransactionTask; this.incompletePaymentTransactionTask = incompletePaymentTransactionTask;
this.paymentConfig = paymentConfig;
this.notificationQueueService = notificationQueueService; this.notificationQueueService = notificationQueueService;
} }


Expand Down Expand Up @@ -491,45 +491,66 @@ public boolean apply(final PaymentTransactionModelDao curPaymentTransactionModel
} }


private List<PaymentAttempt> getPaymentAttempts(final List<PaymentTransaction> purchasedTransactions, private List<PaymentAttempt> getPaymentAttempts(final List<PaymentTransaction> purchasedTransactions,
final List<PaymentAttemptModelDao> paymentAttempts, final List<PaymentAttemptModelDao> pastPaymentAttempts,
final InternalTenantContext internalTenantContext) { 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 // Add Past Payment Attempts
for (PaymentAttemptModelDao pastPaymentAttempt : paymentAttempts) { for (PaymentAttemptModelDao pastPaymentAttempt : pastPaymentAttempts) {
DefaultPaymentAttempt paymentAttempt = new DefaultPaymentAttempt(pastPaymentAttempt); DefaultPaymentAttempt paymentAttempt = new DefaultPaymentAttempt(
result.add(paymentAttempt); 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 // Get Future Payment Attempts from Notification Queue and add them to the list
try { 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 = final List<NotificationEventWithMetadata<NotificationEvent>> notificationEventWithMetadatas =
retryQueue.getFutureNotificationForSearchKeys(internalTenantContext.getAccountRecordId(), internalTenantContext.getTenantRecordId()); retryQueue.getFutureNotificationForSearchKeys(internalTenantContext.getAccountRecordId(), internalTenantContext.getTenantRecordId());


for (NotificationEventWithMetadata<NotificationEvent> notificationEvent : notificationEventWithMetadatas) { for (NotificationEventWithMetadata<NotificationEvent> notificationEvent : notificationEventWithMetadatas) {
DefaultPaymentAttempt futurePaymentAttempt = new DefaultPaymentAttempt( DefaultPaymentAttempt futurePaymentAttempt = new DefaultPaymentAttempt(
null, //accountId, lastPaymentAttemptModelDao.getAccountId(), // accountId
null, //paymentMethodId, lastPaymentAttemptModelDao.getPaymentMethodId(), // paymentMethodId
((PaymentRetryNotificationKey) notificationEvent.getEvent()).getAttemptId(), //id, ((PaymentRetryNotificationKey) notificationEvent.getEvent()).getAttemptId(), // id
notificationEvent.getEffectiveDate(), //createdDate, null, // createdDate
notificationEvent.getEffectiveDate(), //updatedDate, null, // updatedDate
null, //paymentExternalKey, notificationEvent.getEffectiveDate(), // effectiveDate
null, //transactionId, lastPaymentAttemptModelDao.getPaymentExternalKey(), // paymentExternalKey
null, //transactionExternalKey, null, // transactionId
null, //transactionType, lastPaymentAttemptModelDao.getTransactionExternalKey(), // transactionExternalKey
"SCHEDULED", //stateName, lastPaymentAttemptModelDao.getTransactionType(), // transactionType
null, //amount, SCHEDULED, // stateName
null, //currency, lastPaymentTransaction.getAmount(), // amount
((PaymentRetryNotificationKey) notificationEvent.getEvent()).getPaymentControlPluginNames().get(0), //pluginName, lastPaymentTransaction.getCurrency(), // currency
null);//pluginProperties ((PaymentRetryNotificationKey) notificationEvent.getEvent()).getPaymentControlPluginNames().get(0), // pluginName,
result.add(futurePaymentAttempt); (lastPaymentTransaction.getPaymentInfoPlugin() != null) ? lastPaymentTransaction.getPaymentInfoPlugin().getProperties() : null); // pluginProperties
paymentAttempts.add(futurePaymentAttempt);
} }
} catch (NoSuchNotificationQueue noSuchNotificationQueue) { } catch (NoSuchNotificationQueue noSuchNotificationQueue) {
log.error("ERROR Loading Notification Queue - " + noSuchNotificationQueue.getMessage()); log.error("ERROR Loading Notification Queue - " + noSuchNotificationQueue.getMessage());
} }
return result; return paymentAttempts;
} }


private PaymentTransactionInfoPlugin findPaymentTransactionInfoPlugin(final PaymentTransactionModelDao paymentTransactionModelDao, @Nullable final Iterable<PaymentTransactionInfoPlugin> pluginTransactions) { private PaymentTransactionInfoPlugin findPaymentTransactionInfoPlugin(final PaymentTransactionModelDao paymentTransactionModelDao, @Nullable final Iterable<PaymentTransactionInfoPlugin> pluginTransactions) {
Expand Down

0 comments on commit 27af4f1

Please sign in to comment.