Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work for payment control plugin fixes #1131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.killbill.billing.payment.core.sm.PluginControlPaymentAutomatonRunner;
import org.killbill.billing.payment.dao.PaymentAttemptModelDao;
import org.killbill.billing.payment.dao.PaymentDao;
import org.killbill.billing.payment.dao.PaymentMethodModelDao;
import org.killbill.billing.payment.dao.PaymentModelDao;
import org.killbill.billing.payment.dao.PaymentTransactionModelDao;
import org.killbill.billing.payment.dao.PluginPropertySerializer;
Expand Down Expand Up @@ -101,11 +102,17 @@ public void leavingState(final State state) throws OperationException {
stateContext.setPaymentTransactionExternalKey(paymentTransactionIdForNewPaymentTransaction.toString());
}

if (stateContext.getPaymentMethodId() == null) {
// Similar logic in PaymentAutomatonRunner
stateContext.setPaymentMethodId(stateContext.getAccount().getPaymentMethodId());
// The user specified the payment Method to use for a new payment or we computed which one to use for a subsequent payment
// In this case, we also want to provide the associated plugin name
if (stateContext.getPaymentMethodId() != null) {
final PaymentMethodModelDao pm = paymentDao.getPaymentMethod(stateContext.getPaymentMethodId(), stateContext.getInternalCallContext());
// Payment method was deleted
if (pm != null) {
stateContext.setOriginalPaymentPluginName(pm.getPluginName());
}
}


if (state.getName().equals(initialState.getName()) || state.getName().equals(retriedState.getName())) {
try {
final PaymentAttemptModelDao attempt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public PluginDispatcherReturnType<OperationResult> doOperation() throws Operatio

final PaymentControlContext paymentControlContext = new DefaultPaymentControlContext(paymentStateContext.getAccount(),
paymentStateContext.getPaymentMethodId(),
null,
paymentStateControlContext.getOriginalPaymentPluginName(),
paymentStateControlContext.getAttemptId(),
paymentStateContext.getPaymentId(),
paymentStateContext.getPaymentExternalKey(),
Expand Down Expand Up @@ -175,7 +175,7 @@ private PriorPaymentControlResult executePluginPriorCalls(final List<String> pay

final PriorPaymentControlResult result = controlPluginRunner.executePluginPriorCalls(paymentStateContext.getAccount(),
paymentControlContextArg.getPaymentMethodId(),
null,
paymentControlContextArg.getPaymentPluginName(),
paymentStateControlContext.getAttemptId(),
paymentStateContext.getPaymentId(),
paymentStateContext.getPaymentExternalKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class PaymentStateControlContext extends PaymentStateContext {
private BigDecimal processedAmount;
private Currency processedCurrency;

private String originalPaymentPluginName;


public PaymentStateControlContext(@Nullable final List<String> paymentControlPluginNames,
final boolean isApiPayment,
final Boolean isSuccess,
Expand Down Expand Up @@ -115,6 +118,14 @@ public void setProcessedCurrency(final Currency processedCurrency) {
this.processedCurrency = processedCurrency;
}

public String getOriginalPaymentPluginName() {
return originalPaymentPluginName;
}

public void setOriginalPaymentPluginName(final String originalPaymentPluginName) {
this.originalPaymentPluginName = originalPaymentPluginName;
}

public PaymentTransaction getCurrentTransaction() {
if (result == null || result.getTransactions() == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void testCreateAuthWithControl() throws PaymentApiException {
final UUID newPaymentMethodId = paymentApi.addPaymentMethod(account, null, MockPaymentProviderPlugin.PLUGIN_NAME, false, paymentMethodInfo, ImmutableList.<PluginProperty>of(), callContext);
testPaymentControlPluginApi.setNewPaymentMethodId(newPaymentMethodId);

final Payment payment = paymentApi.createAuthorizationWithPaymentControl(account, account.getPaymentMethodId(), null, BigDecimal.TEN, Currency.USD, null,null, UUID.randomUUID().toString(),
final Payment payment = paymentApi.createAuthorizationWithPaymentControl(account, null, null, BigDecimal.TEN, Currency.USD, null,null, UUID.randomUUID().toString(),
ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext);
Assert.assertEquals(payment.getPaymentMethodId(), newPaymentMethodId);

Expand Down Expand Up @@ -961,6 +961,11 @@ public Currency getActualOnFailureCallProcessedCurrency() {

@Override
public PriorPaymentControlResult priorCall(final PaymentControlContext context, final Iterable<PluginProperty> properties) throws PaymentControlApiException {

if (context.getPaymentMethodId() != null) {
Assert.assertNotNull(context.getPaymentPluginName());
}

actualPriorCallPaymentId = context.getPaymentId();
actualPriorCallPaymentExternalKey = context.getPaymentExternalKey();
actualPriorCallTransactionId = context.getTransactionId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public void testBuildFormDescriptorWithPaymentControl() throws PaymentApiExcepti

validationPlugin.setExpectedProperties(expectedProperties);

// Set a random UUID to verify the plugin will successfully override it
paymentGatewayApi.buildFormDescriptorWithPaymentControl(account, UUID.randomUUID(), ImmutableList.<PluginProperty>of(), initialProperties, paymentOptions, callContext);
// Set a null paymentMethodId to verify the plugin will successfully override it
paymentGatewayApi.buildFormDescriptorWithPaymentControl(account, null, ImmutableList.<PluginProperty>of(), initialProperties, paymentOptions, callContext);

}

Expand All @@ -161,7 +161,7 @@ public void testBuildFormDescriptorWithPaymentControlAbortedPayment() throws Pay

// Set a random UUID to verify the plugin will successfully override it
try {
paymentGatewayApi.buildFormDescriptorWithPaymentControl(account, UUID.randomUUID(), ImmutableList.<PluginProperty>of(), ImmutableList.<PluginProperty>of(), paymentOptions, callContext);
paymentGatewayApi.buildFormDescriptorWithPaymentControl(account, null, ImmutableList.<PluginProperty>of(), ImmutableList.<PluginProperty>of(), paymentOptions, callContext);
Assert.fail();
} catch (PaymentApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.PAYMENT_PLUGIN_API_ABORTED.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.killbill.billing.payment.core.sm.control.PaymentStateControlContext;
import org.killbill.billing.payment.dao.PaymentAttemptModelDao;
import org.killbill.billing.payment.dao.PaymentDao;
import org.killbill.billing.payment.dao.PaymentMethodModelDao;
import org.killbill.billing.payment.dao.PaymentModelDao;
import org.killbill.billing.payment.dao.PaymentTransactionModelDao;
import org.killbill.billing.payment.dao.PluginPropertySerializer;
Expand Down Expand Up @@ -164,6 +165,13 @@ public void beforeMethod() throws Exception {
super.beforeMethod();
this.utcNow = clock.getUTCNow();


final PaymentMethodModelDao method = new PaymentMethodModelDao(paymentMethodId, UUID.randomUUID().toString(), null, null,
account.getId(), "PAYMENT_PLUGIN", true);
// Add the mock payment method
final PaymentMethodModelDao savedMethod = paymentDao.insertPaymentMethod(method, internalCallContext);


runner = new MockRetryablePaymentAutomatonRunner(
paymentDao,
locker,
Expand Down