Search in sources :

Example 31 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class TestBlockingCalculator method createRealEvent.

protected BillingEvent createRealEvent(final DateTime effectiveDate, final SubscriptionBase subscription, final SubscriptionBaseTransitionType type) {
    try {
        final Integer billCycleDay = 1;
        final Plan plan = new MockPlan();
        final Currency currency = Currency.USD;
        final String description = "";
        final BillingPeriod billingPeriod = BillingPeriod.MONTHLY;
        final Long totalOrdering = 0L;
        final DateTimeZone tz = DateTimeZone.UTC;
        final PlanPhase planPhase = Mockito.mock(PlanPhase.class);
        final InternationalPrice recurringPrice = Mockito.mock(InternationalPrice.class);
        Mockito.when(recurringPrice.getPrice(Mockito.<Currency>any())).thenReturn(BigDecimal.TEN);
        final Recurring recurring = Mockito.mock(Recurring.class);
        Mockito.when(recurring.getRecurringPrice()).thenReturn(recurringPrice);
        Mockito.when(planPhase.getRecurring()).thenReturn(recurring);
        Mockito.when(planPhase.getUsages()).thenReturn(new DefaultUsage[0]);
        final BigDecimal fixedPrice = BigDecimal.TEN;
        return new DefaultBillingEvent(subscription, effectiveDate, true, plan, planPhase, fixedPrice, currency, billingPeriod, billCycleDay, description, totalOrdering, type, tz, null, false);
    } catch (final CatalogApiException e) {
        Assert.fail("", e);
    }
    throw new IllegalStateException();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) InternationalPrice(org.killbill.billing.catalog.api.InternationalPrice) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) DateTimeZone(org.joda.time.DateTimeZone) BigDecimal(java.math.BigDecimal) Recurring(org.killbill.billing.catalog.api.Recurring) MockPlan(org.killbill.billing.catalog.MockPlan) Currency(org.killbill.billing.catalog.api.Currency) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase)

Example 32 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class TestCreditJson method testJson.

@Test(groups = "fast")
public void testJson() throws Exception {
    final BigDecimal creditAmount = BigDecimal.TEN;
    final Currency currency = Currency.AED;
    final String invoiceId = UUID.randomUUID().toString();
    final String invoiceNumber = UUID.randomUUID().toString();
    final LocalDate effectiveDate = clock.getUTCToday();
    final String accountId = UUID.randomUUID().toString();
    final List<AuditLogJson> auditLogs = createAuditLogsJson(clock.getUTCNow());
    final CreditJson creditJson = new CreditJson(creditAmount, currency.name(), invoiceId, invoiceNumber, effectiveDate, accountId, null, auditLogs);
    Assert.assertEquals(creditJson.getEffectiveDate(), effectiveDate);
    Assert.assertEquals(creditJson.getCreditAmount(), creditAmount);
    Assert.assertEquals(creditJson.getInvoiceId(), invoiceId);
    Assert.assertEquals(creditJson.getInvoiceNumber(), invoiceNumber);
    Assert.assertEquals(creditJson.getAccountId(), accountId);
    final String asJson = mapper.writeValueAsString(creditJson);
    final CreditJson fromJson = mapper.readValue(asJson, CreditJson.class);
    Assert.assertEquals(fromJson, creditJson);
}
Also used : Currency(org.killbill.billing.catalog.api.Currency) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 33 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class AccountResource method processPayment.

private Response processPayment(final PaymentTransactionJson json, final Account account, final String paymentMethodIdStr, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final UriInfo uriInfo, final CallContext callContext, final HttpServletRequest request) throws PaymentApiException {
    verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified");
    verifyNonNullOrEmpty(json.getTransactionType(), "PaymentTransactionJson transactionType needs to be set", json.getAmount(), "PaymentTransactionJson amount needs to be set");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final Currency currency = json.getCurrency() == null ? account.getCurrency() : Currency.valueOf(json.getCurrency());
    final UUID paymentId = json.getPaymentId() == null ? null : UUID.fromString(json.getPaymentId());
    //
    // If paymentId was specified, it means we are attempting a payment completion. The preferred way is to use the PaymentResource
    // (PUT /1.0/kb/payments/{paymentId}/completeTransaction), but for backward compatibility we still allow the call to proceed
    // as long as the request/existing state is healthy (i.e there is a matching PENDING transaction)
    //
    final UUID paymentMethodId;
    if (paymentId != null) {
        final Payment initialPayment = paymentApi.getPayment(paymentId, false, false, pluginProperties, callContext);
        final PaymentTransaction pendingOrSuccessTransaction = lookupPendingOrSuccessTransaction(initialPayment, json != null ? json.getTransactionId() : null, json != null ? json.getTransactionExternalKey() : null, json != null ? json.getTransactionType() : null);
        // If transaction was already completed, return early (See #626)
        if (pendingOrSuccessTransaction.getTransactionStatus() == TransactionStatus.SUCCESS) {
            return uriBuilder.buildResponse(uriInfo, PaymentResource.class, "getPayment", pendingOrSuccessTransaction.getPaymentId(), request);
        }
        paymentMethodId = initialPayment.getPaymentMethodId();
    } else {
        paymentMethodId = paymentMethodIdStr == null ? account.getPaymentMethodId() : UUID.fromString(paymentMethodIdStr);
    }
    validatePaymentMethodForAccount(account.getId(), paymentMethodId, callContext);
    final TransactionType transactionType = TransactionType.valueOf(json.getTransactionType());
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment result;
    switch(transactionType) {
        case AUTHORIZE:
            result = paymentApi.createAuthorizationWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case PURCHASE:
            result = paymentApi.createPurchaseWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        case CREDIT:
            result = paymentApi.createCreditWithPaymentControl(account, paymentMethodId, paymentId, json.getAmount(), currency, json.getPaymentExternalKey(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
            break;
        default:
            return Response.status(Status.PRECONDITION_FAILED).entity("TransactionType " + transactionType + " is not allowed for an account").build();
    }
    return createPaymentResponse(uriInfo, result, transactionType, json.getTransactionExternalKey(), request);
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) TransactionType(org.killbill.billing.payment.api.TransactionType) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions)

Example 34 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class TestInvoiceModuleNoDB method installCurrencyConversionApi.

private void installCurrencyConversionApi() {
    final CurrencyConversionApi currencyConversionApi = Mockito.mock(CurrencyConversionApi.class);
    final CurrencyConversion currencyConversion = Mockito.mock(CurrencyConversion.class);
    final Set<Rate> rates = new HashSet<Rate>();
    rates.add(new Rate() {

        @Override
        public Currency getBaseCurrency() {
            return Currency.USD;
        }

        @Override
        public Currency getCurrency() {
            return Currency.BRL;
        }

        @Override
        public BigDecimal getValue() {
            return new BigDecimal("0.4234");
        }

        @Override
        public DateTime getConversionDate() {
            return new DateTime(DateTimeZone.UTC);
        }
    });
    Mockito.when(currencyConversion.getRates()).thenReturn(rates);
    try {
        Mockito.when(currencyConversionApi.getCurrencyConversion(Mockito.<Currency>any(), Mockito.<DateTime>any())).thenReturn(currencyConversion);
    } catch (CurrencyConversionException e) {
        throw new RuntimeException(e);
    }
    bind(CurrencyConversionApi.class).toInstance(currencyConversionApi);
}
Also used : Rate(org.killbill.billing.currency.api.Rate) CurrencyConversionApi(org.killbill.billing.currency.api.CurrencyConversionApi) Currency(org.killbill.billing.catalog.api.Currency) CurrencyConversion(org.killbill.billing.currency.api.CurrencyConversion) CurrencyConversionException(org.killbill.billing.currency.api.CurrencyConversionException) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet)

Example 35 with Currency

use of org.killbill.billing.catalog.api.Currency in project killbill by killbill.

the class TestExternalChargeInvoiceItem method testEquals.

@Test(groups = "fast")
public void testEquals() throws Exception {
    final UUID id = UUID.randomUUID();
    final UUID invoiceId = UUID.randomUUID();
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final String description = UUID.randomUUID().toString();
    final LocalDate effectiveDate = clock.getUTCToday();
    final BigDecimal amount = BigDecimal.TEN;
    final Currency currency = Currency.GBP;
    final ExternalChargeInvoiceItem item = new ExternalChargeInvoiceItem(id, invoiceId, accountId, bundleId, description, effectiveDate, effectiveDate, amount, currency, null);
    Assert.assertEquals(item.getAccountId(), accountId);
    Assert.assertEquals(item.getAmount().compareTo(amount), 0);
    Assert.assertEquals(item.getBundleId(), bundleId);
    Assert.assertEquals(item.getCurrency(), currency);
    Assert.assertEquals(item.getInvoiceItemType(), InvoiceItemType.EXTERNAL_CHARGE);
    Assert.assertEquals(item.getDescription(), description);
    Assert.assertEquals(item.getStartDate().compareTo(effectiveDate), 0);
    Assert.assertEquals(item.getEndDate().compareTo(effectiveDate), 0);
    Assert.assertNull(item.getPlanName());
    Assert.assertNull(item.getLinkedItemId());
    Assert.assertNull(item.getPhaseName());
    Assert.assertNull(item.getRate());
    Assert.assertNull(item.getSubscriptionId());
    Assert.assertNull(item.getItemDetails());
    Assert.assertEquals(item, item);
    final ExternalChargeInvoiceItem otherItem = new ExternalChargeInvoiceItem(id, invoiceId, UUID.randomUUID(), bundleId, description, effectiveDate, effectiveDate, amount, currency, null);
    Assert.assertNotEquals(otherItem, item);
    // Check comparison (done by start date)
    final ExternalChargeInvoiceItem itemBefore = new ExternalChargeInvoiceItem(id, invoiceId, accountId, bundleId, description, effectiveDate.minusDays(1), effectiveDate.minusDays(1), amount, currency, null);
    Assert.assertFalse(itemBefore.matches(item));
    final ExternalChargeInvoiceItem itemAfter = new ExternalChargeInvoiceItem(id, invoiceId, accountId, bundleId, description, effectiveDate.plusDays(1), effectiveDate.minusDays(1), amount, currency, null);
    Assert.assertFalse(itemAfter.matches(item));
}
Also used : Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

Currency (org.killbill.billing.catalog.api.Currency)65 BigDecimal (java.math.BigDecimal)36 UUID (java.util.UUID)31 DateTime (org.joda.time.DateTime)21 Test (org.testng.annotations.Test)20 LocalDate (org.joda.time.LocalDate)18 PluginProperty (org.killbill.billing.payment.api.PluginProperty)16 Invoice (org.killbill.billing.invoice.api.Invoice)15 MockPlan (org.killbill.billing.catalog.MockPlan)13 Plan (org.killbill.billing.catalog.api.Plan)13 Account (org.killbill.billing.account.api.Account)12 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)12 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)12 PaymentOptions (org.killbill.billing.payment.api.PaymentOptions)12 CallContext (org.killbill.billing.util.callcontext.CallContext)12 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)11 Payment (org.killbill.billing.payment.api.Payment)11 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)11 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)9 BillingEventSet (org.killbill.billing.junction.BillingEventSet)9