use of org.killbill.billing.client.model.Payment in project killbill by killbill.
the class TestInvoicePayment method testRetrievePayment.
@Test(groups = "slow")
public void testRetrievePayment() throws Exception {
final InvoicePayment paymentJson = setupScenarioWithPayment();
final Payment retrievedPaymentJson = killBillClient.getPayment(paymentJson.getPaymentId(), false);
Assert.assertTrue(retrievedPaymentJson.equals((Payment) paymentJson));
}
use of org.killbill.billing.client.model.Payment in project killbill by killbill.
the class TestInvoicePayment method testPartialRefundWithInvoiceItemAdjustment.
@Test(groups = "slow", description = "Can create a partial refund with invoice item adjustment")
public void testPartialRefundWithInvoiceItemAdjustment() throws Exception {
final InvoicePayment paymentJson = setupScenarioWithPayment();
// Get the individual items for the invoice
final Invoice invoice = killBillClient.getInvoice(paymentJson.getTargetInvoiceId(), true);
final InvoiceItem itemToAdjust = invoice.getItems().get(0);
// Issue a refund for a fraction of the amount
final BigDecimal refundAmount = getFractionOfAmount(itemToAdjust.getAmount());
final BigDecimal expectedInvoiceBalance = BigDecimal.ZERO;
// Post and verify the refund
final InvoicePaymentTransaction refund = new InvoicePaymentTransaction();
refund.setPaymentId(paymentJson.getPaymentId());
refund.setIsAdjusted(true);
final InvoiceItem adjustment = new InvoiceItem();
adjustment.setInvoiceItemId(itemToAdjust.getInvoiceItemId());
adjustment.setAmount(refundAmount);
refund.setAdjustments(ImmutableList.<InvoiceItem>of(adjustment));
final Payment paymentAfterRefundJson = killBillClient.createInvoicePaymentRefund(refund, createdBy, reason, comment);
verifyRefund(paymentJson, paymentAfterRefundJson, refundAmount);
// Verify the invoice balance
verifyInvoice(paymentJson, expectedInvoiceBalance);
}
use of org.killbill.billing.client.model.Payment in project killbill by killbill.
the class TestInvoicePayment method testPaymentsAndRefundsPagination.
@Test(groups = "slow", description = "Can paginate through all payments and refunds")
public void testPaymentsAndRefundsPagination() throws Exception {
InvoicePayment lastPayment = setupScenarioWithPayment();
for (int i = 0; i < 5; i++) {
final InvoicePaymentTransaction refund = new InvoicePaymentTransaction();
refund.setPaymentId(lastPayment.getPaymentId());
refund.setAmount(lastPayment.getPurchasedAmount());
killBillClient.createInvoicePaymentRefund(refund, createdBy, reason, comment);
final InvoicePayment invoicePayment = new InvoicePayment();
invoicePayment.setPurchasedAmount(lastPayment.getPurchasedAmount());
invoicePayment.setAccountId(lastPayment.getAccountId());
invoicePayment.setTargetInvoiceId(lastPayment.getTargetInvoiceId());
final InvoicePayment payment = killBillClient.createInvoicePayment(invoicePayment, false, createdBy, reason, comment);
lastPayment = payment;
}
final InvoicePayments allPayments = killBillClient.getInvoicePaymentsForAccount(lastPayment.getAccountId());
Assert.assertEquals(allPayments.size(), 6);
final List<PaymentTransaction> objRefundFromJson = getPaymentTransactions(allPayments, TransactionType.REFUND.toString());
Assert.assertEquals(objRefundFromJson.size(), 5);
Payments paymentsPage = killBillClient.getPayments(0L, 1L);
for (int i = 0; i < 6; i++) {
Assert.assertNotNull(paymentsPage);
Assert.assertEquals(paymentsPage.size(), 1);
Assert.assertTrue(paymentsPage.get(0).equals((Payment) allPayments.get(i)));
paymentsPage = paymentsPage.getNext();
}
Assert.assertNull(paymentsPage);
}
use of org.killbill.billing.client.model.Payment in project killbill by killbill.
the class TestInvoicePayment method verifyRefund.
private void verifyRefund(final InvoicePayment paymentJson, final Payment paymentAfterRefund, final BigDecimal refundAmount) throws KillBillClientException {
final List<PaymentTransaction> transactions = getPaymentTransactions(ImmutableList.of(paymentAfterRefund), TransactionType.REFUND.toString());
Assert.assertEquals(transactions.size(), 1);
final PaymentTransaction refund = transactions.get(0);
Assert.assertEquals(refund.getPaymentId(), paymentJson.getPaymentId());
Assert.assertEquals(refund.getAmount().setScale(2, RoundingMode.HALF_UP), refundAmount.setScale(2, RoundingMode.HALF_UP));
Assert.assertEquals(refund.getCurrency(), DEFAULT_CURRENCY);
Assert.assertEquals(refund.getStatus(), "SUCCESS");
Assert.assertEquals(refund.getEffectiveDate().getYear(), clock.getUTCNow().getYear());
Assert.assertEquals(refund.getEffectiveDate().getMonthOfYear(), clock.getUTCNow().getMonthOfYear());
Assert.assertEquals(refund.getEffectiveDate().getDayOfMonth(), clock.getUTCNow().getDayOfMonth());
// Verify the refund via the payment API
final Payment retrievedPaymentJson = killBillClient.getPayment(paymentJson.getPaymentId(), true);
Assert.assertEquals(retrievedPaymentJson.getPaymentId(), paymentJson.getPaymentId());
Assert.assertEquals(retrievedPaymentJson.getPurchasedAmount().setScale(2, RoundingMode.HALF_UP), paymentJson.getPurchasedAmount().setScale(2, RoundingMode.HALF_UP));
Assert.assertEquals(retrievedPaymentJson.getAccountId(), paymentJson.getAccountId());
Assert.assertEquals(retrievedPaymentJson.getCurrency(), paymentJson.getCurrency());
Assert.assertEquals(retrievedPaymentJson.getPaymentMethodId(), paymentJson.getPaymentMethodId());
}
use of org.killbill.billing.client.model.Payment 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());
}
Aggregations