killbill-aplcache

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java
index faaccb5..a4f079d 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/PaymentResource.java
@@ -25,6 +25,7 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.core.Response.Status;
@@ -114,11 +115,12 @@ public class PaymentResource extends JaxRsResourceBase {
             @javax.ws.rs.core.Context final UriInfo uriInfo) {
 
         try {
+
             final UUID paymentUuid = UUID.fromString(paymentId);
             final Payment payment = paymentApi.getPayment(paymentUuid);
-
             final Account account = accountApi.getAccountById(payment.getAccountId());
 
+
             Refund result = paymentApi.createRefund(account, paymentUuid, json.getRefundAmount(), json.isAdjusted(), context.createContext(createdBy, reason, comment));
             return uriBuilder.buildResponse(RefundResource.class, "getRefund", result.getId(), uriInfo.getBaseUri().toString());
         } catch (AccountApiException e) {
@@ -128,7 +130,7 @@ public class PaymentResource extends JaxRsResourceBase {
                 return Response.status(Status.BAD_REQUEST).build();
             }
         } catch (PaymentApiException e) {
-            return Response.status(Status.BAD_REQUEST).build();
+            return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
         }
     }
 
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 3cee48a..d5ca7e9 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
@@ -94,13 +94,16 @@ public class RefundProcessor extends ProcessorBase {
                     if (payment == null) {
                         throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_SUCCESS_PAYMENT, paymentId);
                     }
-                    if (payment.getAmount().compareTo(refundAmount) < 0) {
-                        throw new PaymentApiException(ErrorCode.PAYMENT_REFUND_AMOUNT_TOO_LARGE);
-                    }
 
-                    // Look for that refund entry and count any 'similar' refund left in CREATED state (same amount, same paymentId)
+                    //
+                    // We are looking for multiple things:
+                    // 1. Compute totalAmountRefunded based on all Refund entries that made it to the plugin.
+                    // 2. If we find a CREATED entry (that did not make it to the plugin) with the same amount, we reuse the entry
+                    // 3. Compute foundPluginCompletedRefunds, number of refund entries for that amount that made it to the plugin
+                    //
                     int foundPluginCompletedRefunds = 0;
                     RefundModelDao refundInfo = null;
+                    BigDecimal totalAmountRefunded = BigDecimal.ZERO;
                     List<RefundModelDao> existingRefunds = paymentDao.getRefundsForPayment(paymentId);
                     for (RefundModelDao cur : existingRefunds) {
 
@@ -114,7 +117,15 @@ public class RefundProcessor extends ProcessorBase {
                                 foundPluginCompletedRefunds++;
                             }
                         }
+                        if (cur.getRefundStatus() != RefundStatus.CREATED) {
+                            totalAmountRefunded = totalAmountRefunded.add(existingPositiveAmount);
+                        }
                     }
+
+                    if (payment.getAmount().subtract(totalAmountRefunded).compareTo(refundAmount) < 0) {
+                        throw new PaymentApiException(ErrorCode.PAYMENT_REFUND_AMOUNT_TOO_LARGE);
+                    }
+
                     if (refundInfo == null) {
                         refundInfo = new RefundModelDao(account.getId(), paymentId, refundAmount, account.getCurrency(), isAdjusted);
                         paymentDao.insertRefund(refundInfo, context);
@@ -122,7 +133,7 @@ public class RefundProcessor extends ProcessorBase {
 
                     final PaymentPluginApi plugin = getPaymentProviderPlugin(payment.getPaymentMethodId());
                     int nbExistingRefunds = plugin.getNbRefundForPaymentAmount(account, paymentId, refundAmount);
-                    log.info(String.format("STEPH debug : found %d pluginRefunds for paymentId %s and amount %s", nbExistingRefunds, paymentId, refundAmount));
+                    log.debug(String.format("found %d pluginRefunds for paymentId %s and amount %s", nbExistingRefunds, paymentId, refundAmount));
 
                     if (nbExistingRefunds > foundPluginCompletedRefunds) {
                         log.info("Found existing plugin refund for paymentId {}, skip plugin", paymentId);