killbill-aplcache

payment: integration fixes Signed-off-by: Pierre-Alexandre

6/17/2018 12:51:34 PM

Details

diff --git a/payment/src/main/java/org/killbill/billing/payment/api/DefaultApiBase.java b/payment/src/main/java/org/killbill/billing/payment/api/DefaultApiBase.java
index 690050e..20e99ad 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/DefaultApiBase.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/DefaultApiBase.java
@@ -25,6 +25,7 @@ import org.killbill.billing.callcontext.InternalTenantContext;
 import org.killbill.billing.payment.invoice.InvoicePaymentControlPluginApi;
 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.definition.PaymentConfig;
 
 import com.google.common.collect.ImmutableList;
@@ -39,7 +40,7 @@ public class DefaultApiBase {
         this.internalCallContextFactory = internalCallContextFactory;
     }
 
-    protected List<String> toPaymentControlPluginNames(final PaymentOptions paymentOptions, final CallContext callContext) {
+    protected List<String> toPaymentControlPluginNames(final PaymentOptions paymentOptions, final TenantContext callContext) {
         final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
 
         // Special path for JAX-RS InvoicePayment endpoints (see JaxRsResourceBase)
diff --git a/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java b/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java
index 23b45ff..0194518 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java
@@ -24,6 +24,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.annotation.Nullable;
+
 import org.joda.time.DateTime;
 import org.killbill.billing.account.api.Account;
 import org.killbill.billing.catalog.api.Currency;
@@ -158,7 +160,7 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
     }
 
     private Collection<PluginProperty> preparePluginPropertiesForRefundOrCredit(final boolean isAdjusted,
-                                                                                final Map<UUID, BigDecimal> adjustments,
+                                                                                @Nullable final Map<UUID, BigDecimal> adjustments,
                                                                                 final Iterable<PluginProperty> originalProperties) {
         final Collection<PluginProperty> pluginProperties = new LinkedList<PluginProperty>();
         if (originalProperties != null) {
@@ -167,7 +169,9 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
             }
         }
         pluginProperties.add(new PluginProperty("IPCD_REFUND_WITH_ADJUSTMENTS", isAdjusted, false));
-        pluginProperties.add(new PluginProperty("IPCD_REFUND_IDS_AMOUNTS", adjustments, false));
+        if (adjustments != null) {
+            pluginProperties.add(new PluginProperty("IPCD_REFUND_IDS_AMOUNTS", adjustments, false));
+        }
         return pluginProperties;
     }
 
diff --git a/payment/src/main/java/org/killbill/billing/payment/api/svcs/DefaultInvoicePaymentInternalApi.java b/payment/src/main/java/org/killbill/billing/payment/api/svcs/DefaultInvoicePaymentInternalApi.java
index ede6c16..22930b0 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/svcs/DefaultInvoicePaymentInternalApi.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/svcs/DefaultInvoicePaymentInternalApi.java
@@ -29,6 +29,7 @@ import org.killbill.billing.callcontext.InternalCallContext;
 import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.invoice.api.InvoiceInternalApi;
 import org.killbill.billing.invoice.api.InvoicePayment;
+import org.killbill.billing.payment.api.DefaultApiBase;
 import org.killbill.billing.payment.api.InvoicePaymentInternalApi;
 import org.killbill.billing.payment.api.Payment;
 import org.killbill.billing.payment.api.PaymentApiException;
@@ -36,11 +37,13 @@ import org.killbill.billing.payment.api.PaymentOptions;
 import org.killbill.billing.payment.api.PaymentTransaction;
 import org.killbill.billing.payment.api.PluginProperty;
 import org.killbill.billing.payment.api.TransactionType;
+import org.killbill.billing.payment.core.PaymentMethodProcessor;
 import org.killbill.billing.payment.core.PluginControlPaymentProcessor;
 import org.killbill.billing.util.UUIDs;
 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.definition.PaymentConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -50,21 +53,24 @@ import com.google.inject.Inject;
 import static org.killbill.billing.payment.logging.PaymentLoggingHelper.logEnterAPICall;
 import static org.killbill.billing.payment.logging.PaymentLoggingHelper.logExitAPICall;
 
-public class DefaultInvoicePaymentInternalApi implements InvoicePaymentInternalApi {
+public class DefaultInvoicePaymentInternalApi extends DefaultApiBase implements InvoicePaymentInternalApi {
 
     private static final Logger log = LoggerFactory.getLogger(DefaultInvoicePaymentInternalApi.class);
 
     private final InvoiceInternalApi invoiceInternalApi;
     private final PluginControlPaymentProcessor pluginControlPaymentProcessor;
-    private final InternalCallContextFactory internalCallContextFactory;
+    private final PaymentMethodProcessor paymentMethodProcessor;
 
     @Inject
     public DefaultInvoicePaymentInternalApi(final InvoiceInternalApi invoiceInternalApi,
                                             final PluginControlPaymentProcessor pluginControlPaymentProcessor,
+                                            final PaymentMethodProcessor paymentMethodProcessor,
+                                            final PaymentConfig paymentConfig,
                                             final InternalCallContextFactory internalCallContextFactory) {
+        super(paymentConfig, internalCallContextFactory);
         this.invoiceInternalApi = invoiceInternalApi;
         this.pluginControlPaymentProcessor = pluginControlPaymentProcessor;
-        this.internalCallContextFactory = internalCallContextFactory;
+        this.paymentMethodProcessor = paymentMethodProcessor;
     }
 
     @Override
@@ -77,10 +83,12 @@ public class DefaultInvoicePaymentInternalApi implements InvoicePaymentInternalA
                                                           final Currency currency,
                                                           final DateTime effectiveDate,
                                                           final String paymentExternalKey,
-                                                          final String originalPaymentTransactionExternalKey,
+                                                          final String paymentTransactionExternalKey,
                                                           final Iterable<PluginProperty> originalProperties,
                                                           final PaymentOptions paymentOptions,
                                                           final InternalCallContext internalCallContext) throws PaymentApiException {
+        checkExternalKeyLength(paymentTransactionExternalKey);
+
         final Collection<PluginProperty> pluginProperties = new LinkedList<PluginProperty>();
         if (originalProperties != null) {
             for (final PluginProperty pluginProperty : originalProperties) {
@@ -89,10 +97,14 @@ public class DefaultInvoicePaymentInternalApi implements InvoicePaymentInternalA
         }
         pluginProperties.add(new PluginProperty("IPCD_INVOICE_ID", invoiceId.toString(), false));
 
-        final String paymentTransactionExternalKey = MoreObjects.firstNonNull(originalPaymentTransactionExternalKey, UUIDs.randomUUID().toString());
+        // TODO should we add paymentConfig.getPaymentControlPluginNames(internalTenantContext)?
+        final List<String> paymentControlPluginNames = InvoicePaymentPaymentOptions.create(paymentOptions).getPaymentControlPluginNames();
+
         final CallContext callContext = internalCallContextFactory.createCallContext(internalCallContext);
 
-        final List<String> paymentControlPluginNames = InvoicePaymentPaymentOptions.create(paymentOptions).getPaymentControlPluginNames();
+        final UUID resolvedPaymentMethodId = (paymentMethodId == null && paymentOptions.isExternalPayment()) ?
+                                             paymentMethodProcessor.createOrGetExternalPaymentMethod(UUIDs.randomUUID().toString(), account, pluginProperties, callContext, internalCallContext) :
+                                             paymentMethodId;
 
         final String transactionType = TransactionType.PURCHASE.name();
         Payment payment = null;
@@ -103,7 +115,7 @@ public class DefaultInvoicePaymentInternalApi implements InvoicePaymentInternalA
 
             payment = pluginControlPaymentProcessor.createPurchase(isApiPayment,
                                                                    account,
-                                                                   paymentMethodId,
+                                                                   resolvedPaymentMethodId,
                                                                    paymentId,
                                                                    amount,
                                                                    currency,
@@ -135,7 +147,7 @@ public class DefaultInvoicePaymentInternalApi implements InvoicePaymentInternalA
                            exception);
         }
 
-        return getInvoicePayment(payment.getId(), paymentTransactionExternalKey, callContext);
+        return paymentTransaction != null ? getInvoicePayment(payment.getId(), paymentTransaction.getExternalKey(), callContext) : null;
     }
 
     private InvoicePayment getInvoicePayment(final UUID paymentId, final String paymentTransactionExternalKey, final TenantContext context) {
diff --git a/payment/src/test/java/org/killbill/billing/payment/PaymentTestSuiteNoDB.java b/payment/src/test/java/org/killbill/billing/payment/PaymentTestSuiteNoDB.java
index 5b69205..ed3df88 100644
--- a/payment/src/test/java/org/killbill/billing/payment/PaymentTestSuiteNoDB.java
+++ b/payment/src/test/java/org/killbill/billing/payment/PaymentTestSuiteNoDB.java
@@ -27,6 +27,7 @@ import org.killbill.billing.account.api.AccountInternalApi;
 import org.killbill.billing.invoice.api.InvoiceInternalApi;
 import org.killbill.billing.osgi.api.OSGIServiceRegistration;
 import org.killbill.billing.payment.api.InvoicePaymentApi;
+import org.killbill.billing.payment.api.InvoicePaymentInternalApi;
 import org.killbill.billing.payment.api.PaymentApi;
 import org.killbill.billing.payment.api.PaymentGatewayApi;
 import org.killbill.billing.payment.api.PaymentOptions;
@@ -83,6 +84,8 @@ public abstract class PaymentTestSuiteNoDB extends GuicyKillbillTestSuiteNoDB {
     @Inject
     protected InvoicePaymentApi invoicePaymentApi;
     @Inject
+    protected InvoicePaymentInternalApi invoicePaymentInternalApi;
+    @Inject
     protected PaymentGatewayApi paymentGatewayApi;
     @Inject
     protected AccountInternalApi accountInternalApi;
diff --git a/payment/src/test/java/org/killbill/billing/payment/TestRetryService.java b/payment/src/test/java/org/killbill/billing/payment/TestRetryService.java
index 1f2c90f..d1da4a7 100644
--- a/payment/src/test/java/org/killbill/billing/payment/TestRetryService.java
+++ b/payment/src/test/java/org/killbill/billing/payment/TestRetryService.java
@@ -118,18 +118,19 @@ public class TestRetryService extends PaymentTestSuiteNoDB {
         final String paymentExternalKey = UUID.randomUUID().toString();
         final String transactionExternalKey = UUID.randomUUID().toString();
         try {
-            invoicePaymentApi.createPurchaseForInvoicePayment(account,
-                                                              invoice.getId(),
-                                                              account.getPaymentMethodId(),
-                                                              null,
-                                                              amount,
-                                                              Currency.USD,
-                                                              null,
-                                                              paymentExternalKey,
-                                                              transactionExternalKey,
-                                                              NO_PROPERTIES,
-                                                              PAYMENT_OPTIONS,
-                                                              callContext);
+            invoicePaymentInternalApi.createPurchaseForInvoicePayment(false,
+                                                                      account,
+                                                                      invoice.getId(),
+                                                                      account.getPaymentMethodId(),
+                                                                      null,
+                                                                      amount,
+                                                                      Currency.USD,
+                                                                      null,
+                                                                      paymentExternalKey,
+                                                                      transactionExternalKey,
+                                                                      NO_PROPERTIES,
+                                                                      PAYMENT_OPTIONS,
+                                                                      internalCallContext);
         } catch (final PaymentApiException e) {
             failed = true;
         }
@@ -173,18 +174,19 @@ public class TestRetryService extends PaymentTestSuiteNoDB {
 
         final String paymentExternalKey = UUID.randomUUID().toString();
         final String transactionExternalKey = UUID.randomUUID().toString();
-        invoicePaymentApi.createPurchaseForInvoicePayment(account,
-                                                          invoice.getId(),
-                                                          account.getPaymentMethodId(),
-                                                          null,
-                                                          amount,
-                                                          Currency.USD,
-                                                          null,
-                                                          paymentExternalKey,
-                                                          transactionExternalKey,
-                                                          NO_PROPERTIES,
-                                                          PAYMENT_OPTIONS,
-                                                          callContext);
+        invoicePaymentInternalApi.createPurchaseForInvoicePayment(false,
+                                                                  account,
+                                                                  invoice.getId(),
+                                                                  account.getPaymentMethodId(),
+                                                                  null,
+                                                                  amount,
+                                                                  Currency.USD,
+                                                                  null,
+                                                                  paymentExternalKey,
+                                                                  transactionExternalKey,
+                                                                  NO_PROPERTIES,
+                                                                  PAYMENT_OPTIONS,
+                                                                  internalCallContext);
 
         Payment payment = getPaymentForExternalKey(paymentExternalKey);
         List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
@@ -256,18 +258,19 @@ public class TestRetryService extends PaymentTestSuiteNoDB {
 
         final String paymentExternalKey = UUID.randomUUID().toString();
         final String transactionExternalKey = UUID.randomUUID().toString();
-        invoicePaymentApi.createPurchaseForInvoicePayment(account,
-                                                          invoice.getId(),
-                                                          account.getPaymentMethodId(),
-                                                          null,
-                                                          amount,
-                                                          Currency.USD,
-                                                          null,
-                                                          paymentExternalKey,
-                                                          transactionExternalKey,
-                                                          NO_PROPERTIES,
-                                                          PAYMENT_OPTIONS,
-                                                          callContext);
+        invoicePaymentInternalApi.createPurchaseForInvoicePayment(false,
+                                                                  account,
+                                                                  invoice.getId(),
+                                                                  account.getPaymentMethodId(),
+                                                                  null,
+                                                                  amount,
+                                                                  Currency.USD,
+                                                                  null,
+                                                                  paymentExternalKey,
+                                                                  transactionExternalKey,
+                                                                  NO_PROPERTIES,
+                                                                  PAYMENT_OPTIONS,
+                                                                  internalCallContext);
 
         Payment payment = getPaymentForExternalKey(paymentExternalKey);
         List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
@@ -349,18 +352,19 @@ public class TestRetryService extends PaymentTestSuiteNoDB {
 
         final String paymentExternalKey = UUID.randomUUID().toString();
         final String transactionExternalKey = UUID.randomUUID().toString();
-        invoicePaymentApi.createPurchaseForInvoicePayment(account,
-                                                          invoice.getId(),
-                                                          account.getPaymentMethodId(),
-                                                          null,
-                                                          amount,
-                                                          Currency.USD,
-                                                          null,
-                                                          paymentExternalKey,
-                                                          transactionExternalKey,
-                                                          NO_PROPERTIES,
-                                                          PAYMENT_OPTIONS,
-                                                          callContext);
+        invoicePaymentInternalApi.createPurchaseForInvoicePayment(false,
+                                                                  account,
+                                                                  invoice.getId(),
+                                                                  account.getPaymentMethodId(),
+                                                                  null,
+                                                                  amount,
+                                                                  Currency.USD,
+                                                                  null,
+                                                                  paymentExternalKey,
+                                                                  transactionExternalKey,
+                                                                  NO_PROPERTIES,
+                                                                  PAYMENT_OPTIONS,
+                                                                  internalCallContext);
 
         Payment payment = getPaymentForExternalKey(paymentExternalKey);
         List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);