Search in sources :

Example 1 with KillBillClientException

use of org.killbill.billing.client.KillBillClientException in project killbill by killbill.

the class TestTenantKV method testPerTenantPluginPaymentStateMachineConfig.

@Test(groups = "slow", description = "Upload and retrieve a per plugin payment state machine config")
public void testPerTenantPluginPaymentStateMachineConfig() throws Exception {
    // Create another tenant - it will have a different state machine
    final Tenant otherTenantWithDifferentStateMachine = new Tenant();
    otherTenantWithDifferentStateMachine.setApiKey(UUID.randomUUID().toString());
    otherTenantWithDifferentStateMachine.setApiSecret(UUID.randomUUID().toString());
    killBillClient.createTenant(otherTenantWithDifferentStateMachine, true, requestOptions);
    final RequestOptions requestOptionsOtherTenant = requestOptions.extend().withTenantApiKey(otherTenantWithDifferentStateMachine.getApiKey()).withTenantApiSecret(otherTenantWithDifferentStateMachine.getApiSecret()).build();
    // Verify initial state
    final TenantKey emptyTenantKey = killBillClient.getPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, requestOptions);
    Assert.assertEquals(emptyTenantKey.getValues().size(), 0);
    final TenantKey emptyTenantKeyOtherTenant = killBillClient.getPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, requestOptionsOtherTenant);
    Assert.assertEquals(emptyTenantKeyOtherTenant.getValues().size(), 0);
    final String stateMachineConfigPath = Resources.getResource("SimplePaymentStates.xml").getPath();
    final TenantKey tenantKey0 = killBillClient.registerPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, stateMachineConfigPath, requestOptionsOtherTenant);
    Assert.assertEquals(tenantKey0.getKey(), TenantKV.TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_.toString() + PLUGIN_NAME);
    // Verify only the other tenant has the new state machine
    final TenantKey emptyTenantKey1 = killBillClient.getPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, requestOptions);
    Assert.assertEquals(emptyTenantKey1.getValues().size(), 0);
    final TenantKey tenantKey1OtherTenant = killBillClient.getPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, requestOptionsOtherTenant);
    Assert.assertEquals(tenantKey1OtherTenant.getKey(), TenantKV.TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_.toString() + PLUGIN_NAME);
    Assert.assertEquals(tenantKey1OtherTenant.getValues().size(), 1);
    // Create an auth in both tenant
    final Payment payment = createComboPaymentTransaction(requestOptions);
    final Payment paymentOtherTenant = createComboPaymentTransaction(requestOptionsOtherTenant);
    // Void in the first tenant (allowed by the default state machine)
    final Payment voidPayment = killBillClient.voidPayment(payment.getPaymentId(), payment.getPaymentExternalKey(), UUID.randomUUID().toString(), ImmutableList.<String>of(), ImmutableMap.<String, String>of(), requestOptions);
    Assert.assertEquals(voidPayment.getTransactions().get(0).getStatus(), TransactionStatus.SUCCESS.toString());
    Assert.assertEquals(voidPayment.getTransactions().get(1).getStatus(), TransactionStatus.SUCCESS.toString());
    // Void in the other tenant (disallowed)
    try {
        killBillClient.voidPayment(paymentOtherTenant.getPaymentId(), paymentOtherTenant.getPaymentExternalKey(), UUID.randomUUID().toString(), ImmutableList.<String>of(), ImmutableMap.<String, String>of(), requestOptionsOtherTenant);
        Assert.fail();
    } catch (final KillBillClientException e) {
        Assert.assertEquals((int) e.getBillingException().getCode(), ErrorCode.PAYMENT_INVALID_OPERATION.getCode());
    }
    // Remove the custom state machine
    killBillClient.unregisterPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, requestOptionsOtherTenant);
    final TenantKey tenantKey2 = killBillClient.getPluginPaymentStateMachineConfigurationForTenant(PLUGIN_NAME, requestOptionsOtherTenant);
    Assert.assertEquals(tenantKey2.getKey(), TenantKV.TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_.toString() + PLUGIN_NAME);
    Assert.assertEquals(tenantKey2.getValues().size(), 0);
    final AtomicReference<Payment> voidPaymentOtherTenant2Ref = new AtomicReference<Payment>();
    Awaitility.await().atMost(8, TimeUnit.SECONDS).pollInterval(Duration.TWO_SECONDS).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // The void should now go through
            try {
                final Payment voidPaymentOtherTenant2 = killBillClient.voidPayment(paymentOtherTenant.getPaymentId(), paymentOtherTenant.getPaymentExternalKey(), UUID.randomUUID().toString(), ImmutableList.<String>of(), ImmutableMap.<String, String>of(), requestOptionsOtherTenant);
                voidPaymentOtherTenant2Ref.set(voidPaymentOtherTenant2);
                return voidPaymentOtherTenant2 != null;
            } catch (final KillBillClientException e) {
                // Invalidation hasn't happened yet
                return false;
            }
        }
    });
    Assert.assertEquals(voidPaymentOtherTenant2Ref.get().getTransactions().get(0).getStatus(), TransactionStatus.SUCCESS.toString());
    Assert.assertEquals(voidPaymentOtherTenant2Ref.get().getTransactions().get(1).getStatus(), TransactionStatus.SUCCESS.toString());
}
Also used : Payment(org.killbill.billing.client.model.Payment) Tenant(org.killbill.billing.client.model.Tenant) RequestOptions(org.killbill.billing.client.RequestOptions) TenantKey(org.killbill.billing.client.model.TenantKey) KillBillClientException(org.killbill.billing.client.KillBillClientException) AtomicReference(java.util.concurrent.atomic.AtomicReference) KillBillClientException(org.killbill.billing.client.KillBillClientException) Test(org.testng.annotations.Test)

Example 2 with KillBillClientException

use of org.killbill.billing.client.KillBillClientException in project killbill by killbill.

the class TestUsage method testRecordUsageTrackingIdExists.

@Test(groups = "slow", description = "Test tracking ID already exists")
public void testRecordUsageTrackingIdExists() throws Exception {
    final Account accountJson = createAccountWithDefaultPaymentMethod();
    final Subscription base = new Subscription();
    base.setAccountId(accountJson.getAccountId());
    base.setProductName("Pistol");
    base.setProductCategory(ProductCategory.BASE);
    base.setBillingPeriod(BillingPeriod.MONTHLY);
    base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Subscription addOn = new Subscription();
    addOn.setAccountId(accountJson.getAccountId());
    addOn.setProductName("Bullets");
    addOn.setProductCategory(ProductCategory.ADD_ON);
    addOn.setBillingPeriod(BillingPeriod.NO_BILLING_PERIOD);
    addOn.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Bundle bundle = killBillClient.createSubscriptionWithAddOns(ImmutableList.<Subscription>of(base, addOn), null, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
    final UUID addOnSubscriptionId = Iterables.<Subscription>find(bundle.getSubscriptions(), new Predicate<Subscription>() {

        @Override
        public boolean apply(final Subscription input) {
            return ProductCategory.ADD_ON.equals(input.getProductCategory());
        }
    }).getSubscriptionId();
    final UsageRecord usageRecord1 = new UsageRecord();
    usageRecord1.setAmount(10L);
    usageRecord1.setRecordDate(clock.getUTCToday().minusDays(1));
    final UnitUsageRecord unitUsageRecord = new UnitUsageRecord();
    unitUsageRecord.setUnitType("bullets");
    unitUsageRecord.setUsageRecords(ImmutableList.<UsageRecord>of(usageRecord1));
    final SubscriptionUsageRecord usage = new SubscriptionUsageRecord();
    usage.setSubscriptionId(addOnSubscriptionId);
    usage.setTrackingId(UUID.randomUUID().toString());
    usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
    killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
    try {
        killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
        Assert.fail();
    } catch (final KillBillClientException e) {
        Assert.assertEquals(e.getBillingException().getCode(), (Integer) ErrorCode.USAGE_RECORD_TRACKING_ID_ALREADY_EXISTS.getCode());
    }
}
Also used : Account(org.killbill.billing.client.model.Account) Bundle(org.killbill.billing.client.model.Bundle) UnitUsageRecord(org.killbill.billing.client.model.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.client.model.SubscriptionUsageRecord) UsageRecord(org.killbill.billing.client.model.UsageRecord) UnitUsageRecord(org.killbill.billing.client.model.UnitUsageRecord) KillBillClientException(org.killbill.billing.client.KillBillClientException) Subscription(org.killbill.billing.client.model.Subscription) UUID(java.util.UUID) SubscriptionUsageRecord(org.killbill.billing.client.model.SubscriptionUsageRecord) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Example 3 with KillBillClientException

use of org.killbill.billing.client.KillBillClientException in project killbill by killbill.

the class TestPayment method testAuthorizeCompletionUsingPaymentIdAndTransactionExternalKey.

@Test(groups = "slow")
public void testAuthorizeCompletionUsingPaymentIdAndTransactionExternalKey() throws Exception {
    final Account account = createAccountWithDefaultPaymentMethod();
    final UUID paymentMethodId = account.getPaymentMethodId();
    final BigDecimal amount = BigDecimal.TEN;
    final String pending = PaymentPluginStatus.PENDING.toString();
    final ImmutableMap<String, String> pendingPluginProperties = ImmutableMap.<String, String>of(MockPaymentProviderPlugin.PLUGIN_PROPERTY_PAYMENT_PLUGIN_STATUS_OVERRIDE, pending);
    final ImmutableMap<String, String> pluginProperties = ImmutableMap.of();
    TransactionType transactionType = TransactionType.AUTHORIZE;
    final String paymentExternalKey = UUID.randomUUID().toString();
    final String authTransactionExternalKey = UUID.randomUUID().toString();
    final Payment initialPayment = createVerifyTransaction(account, paymentMethodId, paymentExternalKey, authTransactionExternalKey, transactionType, pending, amount, BigDecimal.ZERO, pendingPluginProperties, 1);
    final PaymentTransaction completeTransactionByPaymentIdAndInvalidTransactionExternalKey = new PaymentTransaction();
    completeTransactionByPaymentIdAndInvalidTransactionExternalKey.setPaymentId(initialPayment.getPaymentId());
    completeTransactionByPaymentIdAndInvalidTransactionExternalKey.setTransactionExternalKey("bozo");
    try {
        killBillClient.completePayment(completeTransactionByPaymentIdAndInvalidTransactionExternalKey, pluginProperties, requestOptions);
        fail("Payment completion should fail when invalid transaction externalKey has been provided");
    } catch (final KillBillClientException expected) {
    }
    final PaymentTransaction completeTransactionByPaymentIdAndTransactionExternalKey = new PaymentTransaction();
    completeTransactionByPaymentIdAndTransactionExternalKey.setPaymentId(initialPayment.getPaymentId());
    completeTransactionByPaymentIdAndTransactionExternalKey.setTransactionExternalKey(authTransactionExternalKey);
    final Payment completedPaymentByPaymentId = killBillClient.completePayment(completeTransactionByPaymentIdAndTransactionExternalKey, pluginProperties, requestOptions);
    verifyPayment(account, paymentMethodId, completedPaymentByPaymentId, paymentExternalKey, authTransactionExternalKey, transactionType.toString(), TransactionStatus.SUCCESS.name(), amount, amount, BigDecimal.ZERO, BigDecimal.ZERO, 1, 1);
}
Also used : PaymentTransaction(org.killbill.billing.client.model.PaymentTransaction) ComboPaymentTransaction(org.killbill.billing.client.model.ComboPaymentTransaction) Account(org.killbill.billing.client.model.Account) TransactionType(org.killbill.billing.payment.api.TransactionType) Payment(org.killbill.billing.client.model.Payment) KillBillClientException(org.killbill.billing.client.KillBillClientException) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with KillBillClientException

use of org.killbill.billing.client.KillBillClientException in project killbill by killbill.

the class TestPayment method testComboAuthorizationControlPluginException.

@Test(groups = "slow")
public void testComboAuthorizationControlPluginException() throws Exception {
    final Account accountJson = getAccount();
    accountJson.setAccountId(null);
    final String paymentExternalKey = UUID.randomUUID().toString();
    final ComboPaymentTransaction comboPaymentTransaction = createComboPaymentTransaction(accountJson, paymentExternalKey);
    mockPaymentControlProviderPlugin.throwsException(new IllegalStateException());
    try {
        killBillClient.createPayment(comboPaymentTransaction, Arrays.asList(MockPaymentControlProviderPlugin.PLUGIN_NAME), ImmutableMap.<String, String>of(), requestOptions);
        fail();
    } catch (KillBillClientException e) {
        assertEquals(e.getResponse().getStatusCode(), 500);
    }
}
Also used : Account(org.killbill.billing.client.model.Account) ComboPaymentTransaction(org.killbill.billing.client.model.ComboPaymentTransaction) KillBillClientException(org.killbill.billing.client.KillBillClientException) Test(org.testng.annotations.Test)

Example 5 with KillBillClientException

use of org.killbill.billing.client.KillBillClientException in project killbill by killbill.

the class TestPayment method testWithTimeoutPayment.

@Test(groups = "slow")
public void testWithTimeoutPayment() throws Exception {
    final Account account = createAccountWithDefaultPaymentMethod();
    mockPaymentProviderPlugin.makePluginWaitSomeMilliseconds(10000);
    final PaymentTransaction authTransaction = new PaymentTransaction();
    authTransaction.setAmount(BigDecimal.ONE);
    authTransaction.setCurrency(account.getCurrency());
    authTransaction.setTransactionType(TransactionType.AUTHORIZE.name());
    try {
        killBillClient.createPayment(account.getAccountId(), account.getPaymentMethodId(), authTransaction, ImmutableMap.<String, String>of(), requestOptions);
        fail();
    } catch (KillBillClientException e) {
        assertEquals(504, e.getResponse().getStatusCode());
    }
}
Also used : PaymentTransaction(org.killbill.billing.client.model.PaymentTransaction) ComboPaymentTransaction(org.killbill.billing.client.model.ComboPaymentTransaction) Account(org.killbill.billing.client.model.Account) KillBillClientException(org.killbill.billing.client.KillBillClientException) Test(org.testng.annotations.Test)

Aggregations

KillBillClientException (org.killbill.billing.client.KillBillClientException)21 Test (org.testng.annotations.Test)20 Account (org.killbill.billing.client.model.Account)13 ComboPaymentTransaction (org.killbill.billing.client.model.ComboPaymentTransaction)7 PaymentTransaction (org.killbill.billing.client.model.PaymentTransaction)6 UUID (java.util.UUID)5 Payment (org.killbill.billing.client.model.Payment)5 BigDecimal (java.math.BigDecimal)3 InvoicePaymentTransaction (org.killbill.billing.client.model.InvoicePaymentTransaction)3 TransactionType (org.killbill.billing.payment.api.TransactionType)3 Response (com.ning.http.client.Response)2 RequestOptions (org.killbill.billing.client.RequestOptions)2 InvoicePayment (org.killbill.billing.client.model.InvoicePayment)2 RoleDefinition (org.killbill.billing.client.model.RoleDefinition)2 Tenant (org.killbill.billing.client.model.Tenant)2 UserRoles (org.killbill.billing.client.model.UserRoles)2 Predicate (com.google.common.base.Predicate)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Bundle (org.killbill.billing.client.model.Bundle)1