killbill-memoizeit

Implement new API notifyPendingRefundOfStateChanged and

1/5/2014 4:52:51 PM

Details

diff --git a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
index d54d44a..0f81037 100644
--- a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
+++ b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
@@ -75,6 +75,8 @@ public class DefaultPaymentApi implements PaymentApi {
 
     @Override
     public void notifyPendingPaymentOfStateChanged(final Account account, final UUID paymentId, final boolean isSuccess, final CallContext context) throws PaymentApiException {
+        paymentProcessor.notifyPendingPaymentOfStateChanged(account, paymentId, isSuccess,
+                                                            internalCallContextFactory.createInternalCallContext(account.getId(), context));
     }
 
     @Override
@@ -129,7 +131,9 @@ public class DefaultPaymentApi implements PaymentApi {
     }
 
     @Override
-    public void notifyPendingRefundOfStateChanged(final Account account, final UUID paymentId, final boolean isSuccess, final CallContext context) throws PaymentApiException {
+    public void notifyPendingRefundOfStateChanged(final Account account, final UUID refundId, final boolean isSuccess, final CallContext context) throws PaymentApiException {
+        return refundProcessor.notifyPendingRefundOfStateChanged(account, refundId, isSuccess,
+                                                                 internalCallContextFactory.createInternalCallContext(account.getId(), context);
     }
 
     @Override
diff --git a/payment/src/main/java/com/ning/billing/payment/core/PaymentProcessor.java b/payment/src/main/java/com/ning/billing/payment/core/PaymentProcessor.java
index 195e6de..db6fcad 100644
--- a/payment/src/main/java/com/ning/billing/payment/core/PaymentProcessor.java
+++ b/payment/src/main/java/com/ning/billing/payment/core/PaymentProcessor.java
@@ -388,31 +388,25 @@ public class PaymentProcessor extends ProcessorBase {
     public void notifyPendingPaymentOfStateChanged(final Account account, final UUID paymentId, final boolean isSuccess, final InternalCallContext context)
             throws PaymentApiException {
 
-        try {
-            voidPluginDispatcher.dispatchWithAccountLock(
-                    new CallableWithAccountLock<Void>(locker,
-                                                      account.getExternalKey(),
-                                                      new WithAccountLockCallback<Void>() {
-                                                          @Override
-                                                          public Void doOperation() throws PaymentApiException {
-
-                                                              final PaymentModelDao payment = paymentDao.getPayment(paymentId, context);
-                                                              if (payment == null) {
-                                                                  throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT, paymentId);
-                                                              }
-                                                              if (payment.getPaymentStatus() != PaymentStatus.PENDING) {
-                                                                  throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, paymentId);
-                                                              }
-
-                                                              final List<PaymentAttemptModelDao> attempts = paymentDao.getAttemptsForPayment(paymentId, context);
-                                                              final PaymentAttemptModelDao lastAttempt = attempts.get(attempts.size() - 1);
-                                                              final PaymentStatus newPaymentStatus = isSuccess ? PaymentStatus.SUCCESS : PaymentStatus.PAYMENT_FAILURE_ABORTED;
-                                                              paymentDao.updatePaymentAndAttemptOnCompletion(paymentId, newPaymentStatus, payment.getProcessedAmount(), payment.getProcessedCurrency(), lastAttempt.getId(),null, null, context);
-                                                              return null;
-                                                          }
-                                                      }));
-        } catch (TimeoutException e) {
-        }
+        new WithAccountLock<Void>().processAccountWithLock(locker, account.getExternalKey(), new WithAccountLockCallback<Void>() {
+
+            @Override
+            public Void doOperation() throws PaymentApiException {
+                final PaymentModelDao payment = paymentDao.getPayment(paymentId, context);
+                if (payment == null) {
+                    throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT, paymentId);
+                }
+                if (payment.getPaymentStatus() != PaymentStatus.PENDING) {
+                    throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, paymentId);
+                }
+
+                final List<PaymentAttemptModelDao> attempts = paymentDao.getAttemptsForPayment(paymentId, context);
+                final PaymentAttemptModelDao lastAttempt = attempts.get(attempts.size() - 1);
+                final PaymentStatus newPaymentStatus = isSuccess ? PaymentStatus.SUCCESS : PaymentStatus.PAYMENT_FAILURE_ABORTED;
+                paymentDao.updatePaymentAndAttemptOnCompletion(paymentId, newPaymentStatus, payment.getProcessedAmount(), payment.getProcessedCurrency(), lastAttempt.getId(),null, null, context);
+                return null;
+            }
+        });
     }
 
     private void setUnsaneAccount_AUTO_PAY_OFFWithAccountLock(final UUID accountId, final UUID paymentMethodId, final boolean isAccountAutoPayOff,
diff --git a/payment/src/main/java/com/ning/billing/payment/core/RefundProcessor.java b/payment/src/main/java/com/ning/billing/payment/core/RefundProcessor.java
index 4077fb6..b2a62b1 100644
--- a/payment/src/main/java/com/ning/billing/payment/core/RefundProcessor.java
+++ b/payment/src/main/java/com/ning/billing/payment/core/RefundProcessor.java
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeoutException;
 
 import javax.annotation.Nullable;
 import javax.inject.Inject;
@@ -46,8 +47,10 @@ import com.ning.billing.invoice.api.InvoiceItem;
 import com.ning.billing.osgi.api.OSGIServiceRegistration;
 import com.ning.billing.payment.api.DefaultRefund;
 import com.ning.billing.payment.api.PaymentApiException;
+import com.ning.billing.payment.api.PaymentStatus;
 import com.ning.billing.payment.api.Refund;
 import com.ning.billing.payment.api.RefundStatus;
+import com.ning.billing.payment.dao.PaymentAttemptModelDao;
 import com.ning.billing.payment.dao.PaymentDao;
 import com.ning.billing.payment.dao.PaymentModelDao;
 import com.ning.billing.payment.dao.RefundModelDao;
@@ -55,6 +58,7 @@ import com.ning.billing.payment.plugin.api.PaymentPluginApi;
 import com.ning.billing.payment.plugin.api.PaymentPluginApiException;
 import com.ning.billing.payment.plugin.api.RefundInfoPlugin;
 import com.ning.billing.tag.TagInternalApi;
+import com.ning.billing.util.callcontext.CallContext;
 import com.ning.billing.util.callcontext.CallOrigin;
 import com.ning.billing.util.callcontext.InternalCallContextFactory;
 import com.ning.billing.util.callcontext.UserType;
@@ -158,6 +162,34 @@ public class RefundProcessor extends ProcessorBase {
         });
     }
 
+
+    public void notifyPendingRefundOfStateChanged(final Account account, final UUID refundId, final boolean isSuccess, final InternalCallContext context)
+            throws PaymentApiException {
+
+        new WithAccountLock<Void>().processAccountWithLock(locker, account.getExternalKey(), new WithAccountLockCallback<Void>() {
+
+            @Override
+            public Void doOperation() throws PaymentApiException {
+                try {
+                    final RefundModelDao refund = paymentDao.getRefund(refundId, context);
+                    if (refund == null) {
+                        throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_REFUND, refundId);
+                    }
+                    if (refund.getRefundStatus() != RefundStatus.PENDING) {
+                        throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, refundId);
+                    }
+
+                    // TODO STEPH : Model is broken if we had an invoice item adjustements as we lost track of them
+                    invoiceApi.createRefund(refund.getPaymentId(), refund.getAmount(), refund.isAdjusted(), Collections.<UUID, BigDecimal>emptyMap(), refund.getId(), context);
+                    paymentDao.updateRefundStatus(refund.getId(), RefundStatus.COMPLETED, refund.getAmount(), refund.getCurrency(), context);
+                } catch (InvoiceApiException e) {
+                }
+                return null;
+            }
+        });
+
+    }
+
     /**
      * Compute the refund amount (computed from the invoice or invoice items as necessary).
      *