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();
}
}
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);
}
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);
}
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);
}
Aggregations