Skip to content

Commit

Permalink
#255 - Implementing external refund on a paymentMethod sent by query …
Browse files Browse the repository at this point in the history
…param.
  • Loading branch information
matias-aguero-hs committed Aug 25, 2016
1 parent 0a4b284 commit b470aa1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
Expand Up @@ -73,6 +73,7 @@
import org.killbill.commons.metrics.TimedResource;

import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
Expand Down Expand Up @@ -149,6 +150,7 @@ public boolean apply(final InvoicePayment input) {
public Response createRefundWithAdjustments(final InvoicePaymentTransactionJson json,
@PathParam("paymentId") final String paymentId,
@QueryParam(QUERY_PAYMENT_EXTERNAL) @DefaultValue("false") final Boolean externalPayment,
@QueryParam(QUERY_PAYMENT_METHOD_ID) @DefaultValue("") final String paymentMethodId,
@QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString,
@HeaderParam(HDR_CREATED_BY) final String createdBy,
@HeaderParam(HDR_REASON) final String reason,
Expand Down Expand Up @@ -183,16 +185,8 @@ public Response createRefundWithAdjustments(final InvoicePaymentTransactionJson
}

final Payment result;
PaymentMethod paymentMethod = null;
try {
paymentMethod = paymentApi.getPaymentMethodById(payment.getPaymentMethodId(), false, false, pluginProperties, callContext);
} catch (PaymentApiException e) {
log.warn("Payment method {} does not found", payment.getPaymentMethodId());
}

if (externalPayment && (paymentMethod == null)) {
// TODO to complete when a different PM is passed
UUID externalPaymentMethodId = null;
if (externalPayment) {
UUID externalPaymentMethodId = Strings.isNullOrEmpty(paymentMethodId) ? null : UUID.fromString(paymentMethodId);

final Collection<PluginProperty> pluginPropertiesForExternalRefund = new LinkedList<PluginProperty>();
Iterables.addAll(pluginPropertiesForExternalRefund, pluginProperties);
Expand Down
Expand Up @@ -303,7 +303,7 @@ public Payment createPurchaseWithPaymentControl(final Account account, @Nullable
// TODO validate if the code is located properly here
// The code should understand that the external payment method needs to be created
// if it doesn't exist yet and trigger a CREDIT using the (built-in) external payment plugin.
final UUID nonNulPaymentMethodId = (paymentMethodId != null) ?
final UUID nonNullPaymentMethodId = (paymentMethodId != null) ?
paymentMethodId :
paymentMethodProcessor.createOrGetExternalPaymentMethod(UUIDs.randomUUID().toString(), account, properties, callContext, internalCallContext);

Expand All @@ -314,7 +314,7 @@ public Payment createPurchaseWithPaymentControl(final Account account, @Nullable
try {
logEnterAPICall(log, transactionType, account, paymentMethodId, paymentId, null, amount, currency, paymentExternalKey, paymentTransactionExternalKey, null, paymentControlPluginNames);

payment = pluginControlPaymentProcessor.createPurchase(IS_API_PAYMENT, account, nonNulPaymentMethodId, paymentId, amount, currency, paymentExternalKey, paymentTransactionExternalKey,
payment = pluginControlPaymentProcessor.createPurchase(IS_API_PAYMENT, account, nonNullPaymentMethodId, paymentId, amount, currency, paymentExternalKey, paymentTransactionExternalKey,
properties, paymentControlPluginNames, callContext, internalCallContext);

paymentTransaction = findPaymentTransaction(payment, paymentTransactionExternalKey);
Expand Down Expand Up @@ -569,11 +569,11 @@ public Payment createCreditWithPaymentControl(final Account account, final UUID
// TODO validate if the code is located properly here
// The code should understand that the external payment method needs to be created
// if it doesn't exist yet and trigger a CREDIT using the (built-in) external payment plugin.
final UUID nonNulPaymentMethodId = (paymentMethodId != null) ?
final UUID nonNullPaymentMethodId = (paymentMethodId != null) ?
paymentMethodId :
paymentMethodProcessor.createOrGetExternalPaymentMethod(UUIDs.randomUUID().toString(), account, properties, callContext, internalCallContext);

payment = pluginControlPaymentProcessor.createCredit(IS_API_PAYMENT, account, nonNulPaymentMethodId, paymentId, amount, currency, paymentExternalKey, paymentTransactionExternalKey,
payment = pluginControlPaymentProcessor.createCredit(IS_API_PAYMENT, account, nonNullPaymentMethodId, paymentId, amount, currency, paymentExternalKey, paymentTransactionExternalKey,
properties, paymentControlPluginNames, callContext, internalCallContext);

paymentTransaction = findPaymentTransaction(payment, paymentTransactionExternalKey);
Expand Down
Expand Up @@ -31,6 +31,8 @@
import org.killbill.billing.client.model.InvoicePayments;
import org.killbill.billing.client.model.Invoices;
import org.killbill.billing.client.model.Payment;
import org.killbill.billing.client.model.PaymentMethod;
import org.killbill.billing.client.model.PaymentMethodPluginDetail;
import org.killbill.billing.client.model.Payments;
import org.killbill.billing.invoice.api.InvoiceItemType;
import org.killbill.billing.payment.api.TransactionType;
Expand Down Expand Up @@ -120,7 +122,7 @@ public void testManualPaymentAndExternalRefund() throws Exception {
final InvoicePaymentTransaction invoicePaymentTransactionRequest = new InvoicePaymentTransaction();
invoicePaymentTransactionRequest.setAmount(BigDecimal.valueOf(249.95));
invoicePaymentTransactionRequest.setPaymentId(invoicePayment.getPaymentId());
final InvoicePayment invoicePaymentRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, true, requestOptions);
final InvoicePayment invoicePaymentRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, requestOptions);
assertNotNull(invoicePaymentRefund);

assertSingleInvoicePaymentRefund(invoicePaymentRefund);
Expand Down Expand Up @@ -180,7 +182,7 @@ public void testAutomaticPaymentAndExternalRefund() throws Exception {
invoicePaymentTransactionRequest.setAmount(BigDecimal.valueOf(249.95));
invoicePaymentTransactionRequest.setCurrency(accountJson.getCurrency().toString());
invoicePaymentTransactionRequest.setPaymentId(payment.getPaymentId());
final InvoicePayment invoicePaymentExternalRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, true, requestOptions);
final InvoicePayment invoicePaymentExternalRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, true, null, requestOptions);
assertNotNull(invoicePaymentExternalRefund);

assertInvoicePaymentsExternalRefund(accountJson.getAccountId(), invoicePaymentExternalRefund);
Expand All @@ -197,6 +199,7 @@ public void testAutomaticPaymentAndExternalRefundWithAdjustments() throws Except
final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();
// delete PM
killBillClient.deletePaymentMethod(accountJson.getPaymentMethodId(), true, true, requestOptions);

final Payments paymentsForAccount = killBillClient.getPaymentsForAccount(accountJson.getAccountId(), requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);

Expand All @@ -210,7 +213,7 @@ public void testAutomaticPaymentAndExternalRefundWithAdjustments() throws Except
invoicePaymentTransactionRequest.setPaymentId(payment.getPaymentId());
invoicePaymentTransactionRequest.setIsAdjusted(true);
invoicePaymentTransactionRequest.setAdjustments(itemsToBeAdjusted);
final InvoicePayment invoicePaymentExternalRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, true, requestOptions);
final InvoicePayment invoicePaymentExternalRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, true, null, requestOptions);
assertNotNull(invoicePaymentExternalRefund);

assertInvoicePaymentsExternalRefund(accountJson.getAccountId(), invoicePaymentExternalRefund);
Expand All @@ -220,13 +223,40 @@ public void testAutomaticPaymentAndExternalRefundWithAdjustments() throws Except
}

@Test(groups = "slow", description = "#255 - Scenario 2b - Can refund an automatic payment though another existing payment method")
public void testAutomaticPaymentAndRefundWithDifferentPM() throws Exception {
public void testAutomaticPaymentAndExternalRefundWithDifferentPM() throws Exception {
final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());

final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();

// TODO complete test
// delete PM
killBillClient.deletePaymentMethod(accountJson.getPaymentMethodId(), true, true, requestOptions);

// create another PM
final PaymentMethodPluginDetail info = new PaymentMethodPluginDetail();
final PaymentMethod paymentMethodJson = new PaymentMethod(null, UUID.randomUUID().toString(), accountJson.getAccountId(), false, PLUGIN_NAME, info);
final PaymentMethod otherPaymentMethod = killBillClient.createPaymentMethod(paymentMethodJson, requestOptions);

final Payments paymentsForAccount = killBillClient.getPaymentsForAccount(accountJson.getAccountId(), requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);

final Invoices invoices = killBillClient.getInvoicesForAccount(accountJson.getAccountId(), true, true, requestOptions);
final List<InvoiceItem> itemsToBeAdjusted = invoices.get(1).getItems();

// external refund
final InvoicePaymentTransaction invoicePaymentTransactionRequest = new InvoicePaymentTransaction();
invoicePaymentTransactionRequest.setAmount(BigDecimal.valueOf(249.95));
invoicePaymentTransactionRequest.setCurrency(accountJson.getCurrency().toString());
invoicePaymentTransactionRequest.setPaymentId(payment.getPaymentId());
invoicePaymentTransactionRequest.setIsAdjusted(true);
invoicePaymentTransactionRequest.setAdjustments(itemsToBeAdjusted);
final InvoicePayment invoicePaymentExternalRefund = killBillClient.createInvoicePaymentRefund(invoicePaymentTransactionRequest, true, otherPaymentMethod.getPaymentMethodId(), requestOptions);
assertNotNull(invoicePaymentExternalRefund);
assertEquals(invoicePaymentExternalRefund.getPaymentMethodId(), otherPaymentMethod.getPaymentMethodId());

assertInvoicePaymentsExternalRefund(accountJson.getAccountId(), invoicePaymentExternalRefund);
assertRefundInvoiceAdjustments(accountJson.getAccountId());
assertRefundAccountBalance(accountJson.getAccountId(), BigDecimal.ZERO, BigDecimal.ZERO);

}

Expand Down

0 comments on commit b470aa1

Please sign in to comment.