Search in sources :

Example 1 with MonetaryUtil

use of alfio.util.MonetaryUtil in project alf.io by alfio-event.

the class PaypalManager method getInfo.

Optional<PaymentInformation> getInfo(String paymentId, String transactionId, Event event, Supplier<String> platformFeeSupplier) {
    try {
        String refund = null;
        APIContext apiContext = getApiContext(event);
        // check for backward compatibility  reason...
        if (paymentId != null) {
            // navigate in all refund objects and sum their amount
            refund = Payment.get(apiContext, paymentId).getTransactions().stream().map(Transaction::getRelatedResources).flatMap(List::stream).filter(f -> f.getRefund() != null).map(RelatedResources::getRefund).map(Refund::getAmount).map(Amount::getTotal).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).toPlainString();
        // 
        }
        Capture c = Capture.get(apiContext, transactionId);
        String gatewayFee = Optional.ofNullable(c.getTransactionFee()).map(Currency::getValue).map(BigDecimal::new).map(MonetaryUtil::unitToCents).map(String::valueOf).orElse(null);
        return Optional.of(new PaymentInformation(c.getAmount().getTotal(), refund, gatewayFee, platformFeeSupplier.get()));
    } catch (PayPalRESTException ex) {
        log.warn("Paypal: error while fetching information for payment id " + transactionId, ex);
        return Optional.empty();
    }
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) java.util(java.util) MessageDigest(java.security.MessageDigest) TicketReservationRepository(alfio.repository.TicketReservationRepository) ConfigurationManager(alfio.manager.system.ConfigurationManager) Cache(com.github.benmanes.caffeine.cache.Cache) StringUtils(org.apache.commons.lang3.StringUtils) Supplier(java.util.function.Supplier) com.paypal.api.payments(com.paypal.api.payments) BigDecimal(java.math.BigDecimal) MonetaryUtil.formatCents(alfio.util.MonetaryUtil.formatCents) Pair(org.apache.commons.lang3.tuple.Pair) APIContext(com.paypal.base.rest.APIContext) HmacAlgorithms(org.apache.commons.codec.digest.HmacAlgorithms) MessageSource(org.springframework.context.MessageSource) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) TicketRepository(alfio.repository.TicketRepository) HmacUtils(org.apache.commons.codec.digest.HmacUtils) Currency(com.paypal.api.payments.Currency) FeeCalculator(alfio.manager.support.FeeCalculator) PayPalRESTException(com.paypal.base.rest.PayPalRESTException) StandardCharsets(java.nio.charset.StandardCharsets) DateUtils(org.apache.commons.lang3.time.DateUtils) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) MonetaryUtil(alfio.util.MonetaryUtil) alfio.model(alfio.model) Configuration(alfio.model.system.Configuration) Log4j2(lombok.extern.log4j.Log4j2) Event(alfio.model.Event) AllArgsConstructor(lombok.AllArgsConstructor) DigestUtils(org.apache.commons.codec.digest.DigestUtils) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Currency(com.paypal.api.payments.Currency) MonetaryUtil(alfio.util.MonetaryUtil) APIContext(com.paypal.base.rest.APIContext) PayPalRESTException(com.paypal.base.rest.PayPalRESTException) BigDecimal(java.math.BigDecimal)

Example 2 with MonetaryUtil

use of alfio.util.MonetaryUtil in project alf.io by alfio-event.

the class AdditionalServiceApiController method insert.

@RequestMapping(value = "/event/{eventId}/additional-services", method = RequestMethod.POST)
@Transactional
public ResponseEntity<EventModification.AdditionalService> insert(@PathVariable("eventId") int eventId, @RequestBody EventModification.AdditionalService additionalService, BindingResult bindingResult) {
    ValidationResult validationResult = Validator.validateAdditionalService(additionalService, bindingResult);
    Validate.isTrue(validationResult.isSuccess(), "validation failed");
    return eventRepository.findOptionalById(eventId).map(event -> {
        AffectedRowCountAndKey<Integer> result = additionalServiceRepository.insert(eventId, Optional.ofNullable(additionalService.getPrice()).map(MonetaryUtil::unitToCents).orElse(0), additionalService.isFixPrice(), additionalService.getOrdinal(), additionalService.getAvailableQuantity(), additionalService.getMaxQtyPerOrder(), additionalService.getInception().toZonedDateTime(event.getZoneId()), additionalService.getExpiration().toZonedDateTime(event.getZoneId()), additionalService.getVat(), additionalService.getVatType(), additionalService.getType(), additionalService.getSupplementPolicy());
        Validate.isTrue(result.getAffectedRowCount() == 1, "too many records updated");
        int id = result.getKey();
        Stream.concat(additionalService.getTitle().stream(), additionalService.getDescription().stream()).forEach(t -> additionalServiceTextRepository.insert(id, t.getLocale(), t.getType(), t.getValue()));
        return ResponseEntity.ok(EventModification.AdditionalService.from(additionalServiceRepository.getById(result.getKey(), eventId)).withText(additionalServiceTextRepository.findAllByAdditionalServiceId(result.getKey())).withZoneId(event.getZoneId()).build());
    }).orElseThrow(IllegalArgumentException::new);
}
Also used : PriceContainer(alfio.model.PriceContainer) ValidationResult(alfio.model.result.ValidationResult) AffectedRowCountAndKey(ch.digitalfondue.npjt.AffectedRowCountAndKey) Autowired(org.springframework.beans.factory.annotation.Autowired) BindingResult(org.springframework.validation.BindingResult) EventModification(alfio.model.modification.EventModification) BigDecimal(java.math.BigDecimal) AdditionalServiceRepository(alfio.repository.AdditionalServiceRepository) AdditionalServiceTextRepository(alfio.repository.AdditionalServiceTextRepository) EventRepository(alfio.repository.EventRepository) Validator(alfio.util.Validator) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) AdditionalService(alfio.model.AdditionalService) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) Stream(java.util.stream.Stream) MonetaryUtil(alfio.util.MonetaryUtil) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Event(alfio.model.Event) Collections(java.util.Collections) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) Transactional(org.springframework.transaction.annotation.Transactional) AffectedRowCountAndKey(ch.digitalfondue.npjt.AffectedRowCountAndKey) ValidationResult(alfio.model.result.ValidationResult) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with MonetaryUtil

use of alfio.util.MonetaryUtil in project alf.io by alfio-event.

the class EventManager method findAdditionalService.

private Integer findAdditionalService(Event event, EventModification.AdditionalService as) {
    ZoneId utc = ZoneId.of("UTC");
    int eventId = event.getId();
    String checksum = new AdditionalService(0, eventId, as.isFixPrice(), as.getOrdinal(), as.getAvailableQuantity(), as.getMaxQtyPerOrder(), as.getInception().toZonedDateTime(event.getZoneId()).withZoneSameInstant(utc), as.getExpiration().toZonedDateTime(event.getZoneId()).withZoneSameInstant(utc), as.getVat(), as.getVatType(), Optional.ofNullable(as.getPrice()).map(MonetaryUtil::unitToCents).orElse(0), as.getType(), as.getSupplementPolicy()).getChecksum();
    return additionalServiceRepository.loadAllForEvent(eventId).stream().filter(as1 -> as1.getChecksum().equals(checksum)).findFirst().map(AdditionalService::getId).orElse(null);
}
Also used : AdditionalField(alfio.model.modification.EventModification.AdditionalField) alfio.repository(alfio.repository) PaymentProxy(alfio.model.transaction.PaymentProxy) AffectedRowCountAndKey(ch.digitalfondue.npjt.AffectedRowCountAndKey) DiscountType(alfio.model.PromoCodeDiscount.DiscountType) PromoCodeDiscountWithFormattedTime(alfio.model.modification.PromoCodeDiscountWithFormattedTime) ZonedDateTime(java.time.ZonedDateTime) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) TicketStatus(alfio.model.Ticket.TicketStatus) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) BigDecimal(java.math.BigDecimal) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) Arrays.asList(java.util.Arrays.asList) Triple(org.apache.commons.lang3.tuple.Triple) OrganizationRepository(alfio.repository.user.OrganizationRepository) Organization(alfio.model.user.Organization) Predicate(java.util.function.Predicate) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Initializer(alfio.config.Initializer) Stream(java.util.stream.Stream) alfio.model(alfio.model) Environment(org.springframework.core.env.Environment) CollectionUtils(org.springframework.util.CollectionUtils) Configuration(alfio.model.system.Configuration) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) java.util(java.util) DateTimeModification.toZonedDateTime(alfio.model.modification.DateTimeModification.toZonedDateTime) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConfigurationManager(alfio.manager.system.ConfigurationManager) EventModification(alfio.model.modification.EventModification) Function(java.util.function.Function) TicketFieldDescriptionModification(alfio.model.modification.TicketFieldDescriptionModification) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Context(alfio.model.TicketFieldConfiguration.Context) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) Result(alfio.model.result.Result) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) Validate(org.apache.commons.lang3.Validate) ChronoUnit(java.time.temporal.ChronoUnit) CategoryEvaluator(alfio.manager.support.CategoryEvaluator) MonetaryUtil(alfio.util.MonetaryUtil) UserManager(alfio.manager.user.UserManager) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) EventUtil(alfio.util.EventUtil) Flyway(org.flywaydb.core.Flyway) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ZoneId(java.time.ZoneId)

Example 4 with MonetaryUtil

use of alfio.util.MonetaryUtil in project alf.io by alfio-event.

the class AdditionalServiceApiController method update.

@RequestMapping(value = "/event/{eventId}/additional-services/{additionalServiceId}", method = RequestMethod.PUT)
@Transactional
public ResponseEntity<EventModification.AdditionalService> update(@PathVariable("eventId") int eventId, @PathVariable("additionalServiceId") int additionalServiceId, @RequestBody EventModification.AdditionalService additionalService, BindingResult bindingResult) {
    ValidationResult validationResult = Validator.validateAdditionalService(additionalService, bindingResult);
    Validate.isTrue(validationResult.isSuccess(), "validation failed");
    Validate.isTrue(additionalServiceId == additionalService.getId(), "wrong input");
    return eventRepository.findOptionalById(eventId).map(event -> {
        int result = additionalServiceRepository.update(additionalServiceId, additionalService.isFixPrice(), additionalService.getOrdinal(), additionalService.getAvailableQuantity(), additionalService.getMaxQtyPerOrder(), additionalService.getInception().toZonedDateTime(event.getZoneId()), additionalService.getExpiration().toZonedDateTime(event.getZoneId()), additionalService.getVat(), additionalService.getVatType(), Optional.ofNullable(additionalService.getPrice()).map(MonetaryUtil::unitToCents).orElse(0));
        Validate.isTrue(result <= 1, "too many records updated");
        Stream.concat(additionalService.getTitle().stream(), additionalService.getDescription().stream()).forEach(t -> {
            if (t.getId() != null) {
                additionalServiceTextRepository.update(t.getId(), t.getLocale(), t.getType(), t.getValue());
            } else {
                additionalServiceTextRepository.insert(additionalService.getId(), t.getLocale(), t.getType(), t.getValue());
            }
        });
        return ResponseEntity.ok(additionalService);
    }).orElseThrow(IllegalArgumentException::new);
}
Also used : PriceContainer(alfio.model.PriceContainer) ValidationResult(alfio.model.result.ValidationResult) AffectedRowCountAndKey(ch.digitalfondue.npjt.AffectedRowCountAndKey) Autowired(org.springframework.beans.factory.annotation.Autowired) BindingResult(org.springframework.validation.BindingResult) EventModification(alfio.model.modification.EventModification) BigDecimal(java.math.BigDecimal) AdditionalServiceRepository(alfio.repository.AdditionalServiceRepository) AdditionalServiceTextRepository(alfio.repository.AdditionalServiceTextRepository) EventRepository(alfio.repository.EventRepository) Validator(alfio.util.Validator) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) AdditionalService(alfio.model.AdditionalService) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) Stream(java.util.stream.Stream) MonetaryUtil(alfio.util.MonetaryUtil) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Event(alfio.model.Event) Collections(java.util.Collections) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) Transactional(org.springframework.transaction.annotation.Transactional) ValidationResult(alfio.model.result.ValidationResult) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MonetaryUtil (alfio.util.MonetaryUtil)4 BigDecimal (java.math.BigDecimal)4 Log4j2 (lombok.extern.log4j.Log4j2)4 Event (alfio.model.Event)3 EventModification (alfio.model.modification.EventModification)3 OptionalWrapper.optionally (alfio.util.OptionalWrapper.optionally)3 AffectedRowCountAndKey (ch.digitalfondue.npjt.AffectedRowCountAndKey)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 ConfigurationManager (alfio.manager.system.ConfigurationManager)2 alfio.model (alfio.model)2 AdditionalService (alfio.model.AdditionalService)2 PriceContainer (alfio.model.PriceContainer)2 ValidationResult (alfio.model.result.ValidationResult)2 Configuration (alfio.model.system.Configuration)2 ConfigurationKeys (alfio.model.system.ConfigurationKeys)2 AdditionalServiceRepository (alfio.repository.AdditionalServiceRepository)2 AdditionalServiceTextRepository (alfio.repository.AdditionalServiceTextRepository)2 EventRepository (alfio.repository.EventRepository)2 Validate (org.apache.commons.lang3.Validate)2