Search in sources :

Example 1 with TenantKey

use of org.killbill.billing.client.model.TenantKey in project killbill by killbill.

the class TestPerTenantConfig method testFailedPaymentWithPerTenantRetryConfig.

@Test(groups = "slow")
public void testFailedPaymentWithPerTenantRetryConfig() throws Exception {
    // Create the tenant
    final String apiKeyTenant1 = "tenantSuperTuned";
    final String apiSecretTenant1 = "2367$$ffr79";
    loginTenant(apiKeyTenant1, apiSecretTenant1);
    final Tenant tenant1 = new Tenant();
    tenant1.setApiKey(apiKeyTenant1);
    tenant1.setApiSecret(apiSecretTenant1);
    killBillClient.createTenant(tenant1, createdBy, reason, comment);
    // Configure our plugin to fail
    mockPaymentProviderPlugin.makeAllInvoicesFailWithError(true);
    // Upload the config
    final ObjectMapper mapper = new ObjectMapper();
    final HashMap<String, String> perTenantProperties = new HashMap<String, String>();
    perTenantProperties.put("org.killbill.payment.retry.days", "1,1,1");
    final String perTenantConfig = mapper.writeValueAsString(perTenantProperties);
    final TenantKey tenantKey = killBillClient.postConfigurationPropertiesForTenant(perTenantConfig, requestOptions);
    final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();
    final Payments payments = killBillClient.getPaymentsForAccount(accountJson.getAccountId());
    Assert.assertEquals(payments.size(), 1);
    Assert.assertEquals(payments.get(0).getTransactions().size(), 1);
    //
    // Because we have specified a retry interval of one day we should see the new attempt after moving clock 1 day (and not 8 days which is default)
    //
    //
    // Now unregister special per tenant config and we the first retry occurs one day after (and still fails), it now sets a retry date of 8 days
    //
    killBillClient.unregisterConfigurationForTenant(requestOptions);
    // org.killbill.tenant.broadcast.rate has been set to 1s
    crappyWaitForLackOfProperSynchonization(2000);
    clock.addDays(1);
    Awaitility.await().atMost(4, TimeUnit.SECONDS).pollInterval(Duration.ONE_SECOND).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return killBillClient.getPaymentsForAccount(accountJson.getAccountId()).get(0).getTransactions().size() == 2;
        }
    });
    final Payments payments2 = killBillClient.getPaymentsForAccount(accountJson.getAccountId());
    Assert.assertEquals(payments2.size(), 1);
    Assert.assertEquals(payments2.get(0).getTransactions().size(), 2);
    Assert.assertEquals(payments2.get(0).getTransactions().get(0).getStatus(), TransactionStatus.PAYMENT_FAILURE.name());
    Assert.assertEquals(payments2.get(0).getTransactions().get(1).getStatus(), TransactionStatus.PAYMENT_FAILURE.name());
    clock.addDays(1);
    crappyWaitForLackOfProperSynchonization(3000);
    // No retry with default config
    final Payments payments3 = killBillClient.getPaymentsForAccount(accountJson.getAccountId());
    Assert.assertEquals(payments3.size(), 1);
    Assert.assertEquals(payments3.get(0).getTransactions().size(), 2);
    mockPaymentProviderPlugin.makeAllInvoicesFailWithError(false);
    clock.addDays(7);
    Awaitility.await().atMost(4, TimeUnit.SECONDS).pollInterval(Duration.ONE_SECOND).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return killBillClient.getPaymentsForAccount(accountJson.getAccountId()).get(0).getTransactions().size() == 3;
        }
    });
    final Payments payments4 = killBillClient.getPaymentsForAccount(accountJson.getAccountId());
    Assert.assertEquals(payments4.size(), 1);
    Assert.assertEquals(payments4.get(0).getTransactions().size(), 3);
    Assert.assertEquals(payments4.get(0).getTransactions().get(0).getStatus(), TransactionStatus.PAYMENT_FAILURE.name());
    Assert.assertEquals(payments4.get(0).getTransactions().get(1).getStatus(), TransactionStatus.PAYMENT_FAILURE.name());
    Assert.assertEquals(payments4.get(0).getTransactions().get(2).getStatus(), TransactionStatus.SUCCESS.name());
}
Also used : Account(org.killbill.billing.client.model.Account) Tenant(org.killbill.billing.client.model.Tenant) HashMap(java.util.HashMap) TenantKey(org.killbill.billing.client.model.TenantKey) Payments(org.killbill.billing.client.model.Payments) ObjectMapper(org.killbill.billing.util.jackson.ObjectMapper) Test(org.testng.annotations.Test)

Example 2 with TenantKey

use of org.killbill.billing.client.model.TenantKey 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 3 with TenantKey

use of org.killbill.billing.client.model.TenantKey in project killbill by killbill.

the class TestPushNotification method registerTenantForCallback.

private String registerTenantForCallback() throws KillBillClientException, InterruptedException {
    // Register tenant for callback
    final String callback = "http://127.0.0.1:" + SERVER_PORT + CALLBACK_ENDPOINT;
    final TenantKey result0 = killBillClient.registerCallbackNotificationForTenant(callback, requestOptions);
    Assert.assertTrue(waitForCallbacksToComplete());
    // expected true because is not an ACCOUNT_CREATION event
    Assert.assertTrue(callbackCompletedWithError);
    Assert.assertEquals(result0.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
    Assert.assertEquals(result0.getValues().size(), 1);
    Assert.assertEquals(result0.getValues().get(0), callback);
    // reset values
    resetCallbackStatusProperties();
    return callback;
}
Also used : TenantKey(org.killbill.billing.client.model.TenantKey)

Example 4 with TenantKey

use of org.killbill.billing.client.model.TenantKey in project killbill by killbill.

the class TestPushNotification method unregisterTenantForCallback.

private void unregisterTenantForCallback(final String callback) throws KillBillClientException {
    final TenantKey result = killBillClient.getCallbackNotificationForTenant(requestOptions);
    Assert.assertEquals(result.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
    Assert.assertEquals(result.getValues().size(), 1);
    Assert.assertEquals(result.getValues().get(0), callback);
    killBillClient.unregisterCallbackNotificationForTenant(requestOptions);
    final TenantKey result2 = killBillClient.getCallbackNotificationForTenant(requestOptions);
    Assert.assertEquals(result2.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
    Assert.assertEquals(result2.getValues().size(), 0);
}
Also used : TenantKey(org.killbill.billing.client.model.TenantKey)

Example 5 with TenantKey

use of org.killbill.billing.client.model.TenantKey in project killbill by killbill.

the class TestTenantKV method testPerTenantPluginConfig.

@Test(groups = "slow", description = "Upload and retrieve a per plugin config")
public void testPerTenantPluginConfig() throws Exception {
    final String pluginName = "PLUGIN_FOO";
    final String pluginPath = Resources.getResource("plugin.yml").getPath();
    final TenantKey tenantKey0 = killBillClient.registerPluginConfigurationForTenant(pluginName, pluginPath, createdBy, reason, comment);
    Assert.assertEquals(tenantKey0.getKey(), TenantKV.TenantKey.PLUGIN_CONFIG_.toString() + pluginName);
    final TenantKey tenantKey1 = killBillClient.getPluginConfigurationForTenant(pluginName);
    Assert.assertEquals(tenantKey1.getKey(), TenantKV.TenantKey.PLUGIN_CONFIG_.toString() + pluginName);
    Assert.assertEquals(tenantKey1.getValues().size(), 1);
    killBillClient.unregisterPluginConfigurationForTenant(pluginName, createdBy, reason, comment);
    final TenantKey tenantKey2 = killBillClient.getPluginConfigurationForTenant(pluginName);
    Assert.assertEquals(tenantKey2.getKey(), TenantKV.TenantKey.PLUGIN_CONFIG_.toString() + pluginName);
    Assert.assertEquals(tenantKey2.getValues().size(), 0);
}
Also used : TenantKey(org.killbill.billing.client.model.TenantKey) Test(org.testng.annotations.Test)

Aggregations

TenantKey (org.killbill.billing.client.model.TenantKey)5 Test (org.testng.annotations.Test)3 Tenant (org.killbill.billing.client.model.Tenant)2 HashMap (java.util.HashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 KillBillClientException (org.killbill.billing.client.KillBillClientException)1 RequestOptions (org.killbill.billing.client.RequestOptions)1 Account (org.killbill.billing.client.model.Account)1 Payment (org.killbill.billing.client.model.Payment)1 Payments (org.killbill.billing.client.model.Payments)1 ObjectMapper (org.killbill.billing.util.jackson.ObjectMapper)1