Search in sources :

Example 41 with Currency

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

the class PaymentResource method captureAuthorizationInternal.

private Response captureAuthorizationInternal(final PaymentTransactionJson json, @Nullable final UUID paymentId, final List<String> paymentControlPluginNames, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final UriInfo uriInfo, final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "PaymentTransactionJson body should be specified");
    verifyNonNullOrEmpty(json.getAmount(), "PaymentTransactionJson amount needs to be set");
    final Iterable<PluginProperty> pluginPropertiesFromBody = extractPluginProperties(json.getProperties());
    final Iterable<PluginProperty> pluginPropertiesFromQuery = extractPluginProperties(pluginPropertiesString);
    final Iterable<PluginProperty> pluginProperties = Iterables.concat(pluginPropertiesFromQuery, pluginPropertiesFromBody);
    final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request);
    final Payment initialPayment = getPaymentByIdOrKey(paymentId, json.getPaymentExternalKey(), pluginProperties, callContext);
    final Account account = accountUserApi.getAccountById(initialPayment.getAccountId(), callContext);
    final Currency currency = json.getCurrency() == null ? account.getCurrency() : json.getCurrency();
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment payment = paymentApi.createCaptureWithPaymentControl(account, initialPayment.getId(), json.getAmount(), currency, json.getEffectiveDate(), json.getTransactionExternalKey(), pluginProperties, paymentOptions, callContext);
    return createPaymentResponse(uriInfo, payment, TransactionType.CAPTURE, json.getTransactionExternalKey(), request);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) Currency(org.killbill.billing.catalog.api.Currency) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext)

Example 42 with Currency

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

the class PaymentResource method createComboPayment.

@TimedResource
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("/" + COMBO)
@ApiOperation(value = "Combo api to create a new payment transaction on a existing (or not) account ", response = PaymentJson.class)
@ApiResponses(value = { @ApiResponse(code = 201, message = "Payment transaction created successfully"), @ApiResponse(code = 400, message = "Invalid data for Account or PaymentMethod"), @ApiResponse(code = 402, message = "Transaction declined by gateway"), @ApiResponse(code = 422, message = "Payment is aborted by a control plugin"), @ApiResponse(code = 502, message = "Failed to submit payment transaction"), @ApiResponse(code = 503, message = "Payment in unknown status, failed to receive gateway response"), @ApiResponse(code = 504, message = "Payment operation timeout") })
public Response createComboPayment(@MetricTag(tag = "type", property = "transactionType") final ComboPaymentTransactionJson json, @QueryParam(QUERY_PAYMENT_CONTROL_PLUGIN_NAME) final List<String> paymentControlPluginNames, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final UriInfo uriInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "ComboPaymentTransactionJson body should be specified");
    final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request);
    final Account account = getOrCreateAccount(json.getAccount(), callContext);
    final Iterable<PluginProperty> paymentMethodPluginProperties = extractPluginProperties(json.getPaymentMethodPluginProperties());
    final UUID paymentMethodId = getOrCreatePaymentMethod(account, json.getPaymentMethod(), paymentMethodPluginProperties, callContext);
    final PaymentTransactionJson paymentTransactionJson = json.getTransaction();
    final TransactionType transactionType = paymentTransactionJson.getTransactionType();
    final PaymentOptions paymentOptions = createControlPluginApiPaymentOptions(paymentControlPluginNames);
    final Payment result;
    final Iterable<PluginProperty> transactionPluginProperties = extractPluginProperties(json.getTransactionPluginProperties());
    final Currency currency = paymentTransactionJson.getCurrency() == null ? account.getCurrency() : paymentTransactionJson.getCurrency();
    // If we need to specify a paymentId (e.g 3DS authorization, we can use regular API, no need for combo call)
    final UUID paymentId = null;
    switch(transactionType) {
        case AUTHORIZE:
            result = paymentApi.createAuthorizationWithPaymentControl(account, paymentMethodId, paymentId, paymentTransactionJson.getAmount(), currency, paymentTransactionJson.getEffectiveDate(), paymentTransactionJson.getPaymentExternalKey(), paymentTransactionJson.getTransactionExternalKey(), transactionPluginProperties, paymentOptions, callContext);
            break;
        case PURCHASE:
            result = paymentApi.createPurchaseWithPaymentControl(account, paymentMethodId, paymentId, paymentTransactionJson.getAmount(), currency, paymentTransactionJson.getEffectiveDate(), paymentTransactionJson.getPaymentExternalKey(), paymentTransactionJson.getTransactionExternalKey(), transactionPluginProperties, paymentOptions, callContext);
            break;
        case CREDIT:
            result = paymentApi.createCreditWithPaymentControl(account, paymentMethodId, paymentId, paymentTransactionJson.getAmount(), currency, paymentTransactionJson.getEffectiveDate(), paymentTransactionJson.getPaymentExternalKey(), paymentTransactionJson.getTransactionExternalKey(), transactionPluginProperties, 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, paymentTransactionJson.getTransactionExternalKey(), request);
}
Also used : Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) TransactionType(org.killbill.billing.payment.api.TransactionType) Payment(org.killbill.billing.payment.api.Payment) ComboPaymentTransactionJson(org.killbill.billing.jaxrs.json.ComboPaymentTransactionJson) PaymentTransactionJson(org.killbill.billing.jaxrs.json.PaymentTransactionJson) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) PaymentOptions(org.killbill.billing.payment.api.PaymentOptions) CallContext(org.killbill.billing.util.callcontext.CallContext) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 43 with Currency

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

the class SubscriptionResourceHelpers method buildUsagePrices.

private static void buildUsagePrices(final Currency currency, final PhasePriceJson input, final Collection<UsagePriceOverride> usagePrices) {
    for (final UsagePriceJson usageOverrideJson : input.getUsagePrices()) {
        final List<TierPriceOverride> tierPriceOverrides = new LinkedList<TierPriceOverride>();
        for (final TierPriceJson tierPriceJson : usageOverrideJson.getTierPrices()) {
            final List<TieredBlockPriceOverride> blockPriceOverrides = new LinkedList<TieredBlockPriceOverride>();
            for (final BlockPriceJson block : tierPriceJson.getBlockPrices()) {
                blockPriceOverrides.add(new TieredBlockPriceOverride() {

                    @Override
                    public String getUnitName() {
                        return block.getUnitName();
                    }

                    @Override
                    public Double getSize() {
                        return block.getSize();
                    }

                    @Override
                    public BigDecimal getPrice() {
                        return block.getPrice();
                    }

                    @Override
                    public Currency getCurrency() {
                        return currency;
                    }

                    @Override
                    public Double getMax() {
                        return block.getMax();
                    }
                });
            }
            tierPriceOverrides.add(new TierPriceOverride() {

                @Override
                public List<TieredBlockPriceOverride> getTieredBlockPriceOverrides() {
                    return blockPriceOverrides;
                }
            });
        }
        usagePrices.add(new UsagePriceOverride() {

            @Override
            public String getName() {
                return usageOverrideJson.getUsageName();
            }

            @Override
            public UsageType getUsageType() {
                return usageOverrideJson.getUsageType();
            }

            @Override
            public List<TierPriceOverride> getTierPriceOverrides() {
                return tierPriceOverrides;
            }
        });
    }
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) TierPriceJson(org.killbill.billing.jaxrs.json.TierPriceJson) UsageType(org.killbill.billing.catalog.api.UsageType) LinkedList(java.util.LinkedList) BigDecimal(java.math.BigDecimal) UsagePriceJson(org.killbill.billing.jaxrs.json.UsagePriceJson) Currency(org.killbill.billing.catalog.api.Currency) List(java.util.List) LinkedList(java.util.LinkedList) BlockPriceJson(org.killbill.billing.jaxrs.json.BlockPriceJson) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride)

Example 44 with Currency

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

the class SubscriptionResourceHelpers method buildPlanPhasePriceOverride.

private static PlanPhasePriceOverride buildPlanPhasePriceOverride(final PlanSpecifier spec, final Currency currency, final PhasePriceJson input, final List<UsagePriceOverride> usagePrices) {
    if (input.getPhaseName() != null) {
        return new PlanPhasePriceOverride() {

            @Override
            public String getPhaseName() {
                return input.getPhaseName();
            }

            @Override
            public PlanPhaseSpecifier getPlanPhaseSpecifier() {
                return null;
            }

            @Override
            public Currency getCurrency() {
                return currency;
            }

            @Override
            public BigDecimal getFixedPrice() {
                return input.getFixedPrice();
            }

            @Override
            public BigDecimal getRecurringPrice() {
                return input.getRecurringPrice();
            }

            @Override
            public List<UsagePriceOverride> getUsagePriceOverrides() {
                return usagePrices;
            }
        };
    }
    final PhaseType phaseType = input.getPhaseType() != null ? PhaseType.valueOf(input.getPhaseType()) : null;
    final PlanPhaseSpecifier planPhaseSpecifier = spec.getPlanName() != null ? new PlanPhaseSpecifier(spec.getPlanName(), phaseType) : new PlanPhaseSpecifier(spec.getProductName(), spec.getBillingPeriod(), spec.getPriceListName(), phaseType);
    final Currency resolvedCurrency = input.getFixedPrice() != null || input.getRecurringPrice() != null ? currency : null;
    return new PlanPhasePriceOverride() {

        @Override
        public String getPhaseName() {
            return null;
        }

        @Override
        public PlanPhaseSpecifier getPlanPhaseSpecifier() {
            return planPhaseSpecifier;
        }

        @Override
        public Currency getCurrency() {
            return resolvedCurrency;
        }

        @Override
        public BigDecimal getFixedPrice() {
            return input.getFixedPrice();
        }

        @Override
        public BigDecimal getRecurringPrice() {
            return input.getRecurringPrice();
        }

        @Override
        public List<UsagePriceOverride> getUsagePriceOverrides() {
            return usagePrices;
        }
    };
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Currency(org.killbill.billing.catalog.api.Currency) PhaseType(org.killbill.billing.catalog.api.PhaseType) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 45 with Currency

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

the class TestInvoiceJsonWithBundleKeys method createCreditJson.

private InvoiceItemJson createCreditJson() {
    final BigDecimal creditAmount = BigDecimal.TEN;
    final UUID creditId = UUID.randomUUID();
    final Currency currency = Currency.USD;
    final UUID invoiceId = UUID.randomUUID();
    final LocalDate effectiveDate = clock.getUTCToday();
    final UUID accountId = UUID.randomUUID();
    return new InvoiceItemJson(creditId, invoiceId, null, accountId, null, null, null, null, null, null, null, null, null, null, null, InvoiceItemType.CBA_ADJ, null, effectiveDate, effectiveDate, creditAmount, null, currency, null, null, null, null, null);
}
Also used : Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

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