use of alfio.manager.support.PaymentResult in project alf.io by alfio-event.
the class ReservationController method handleReservation.
@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}", method = RequestMethod.POST)
public String handleReservation(@PathVariable("eventName") String eventName, @PathVariable("reservationId") String reservationId, PaymentForm paymentForm, BindingResult bindingResult, Model model, HttpServletRequest request, Locale locale, RedirectAttributes redirectAttributes) {
Optional<Event> eventOptional = eventRepository.findOptionalByShortName(eventName);
if (!eventOptional.isPresent()) {
return "redirect:/";
}
Event event = eventOptional.get();
Optional<TicketReservation> ticketReservation = ticketReservationManager.findById(reservationId);
if (!ticketReservation.isPresent()) {
return redirectReservation(ticketReservation, eventName, reservationId);
}
if (paymentForm.shouldCancelReservation()) {
ticketReservationManager.cancelPendingReservation(reservationId, false);
SessionUtil.removeSpecialPriceData(request);
return "redirect:/event/" + eventName + "/";
}
if (!ticketReservation.get().getValidity().after(new Date())) {
bindingResult.reject(ErrorsCode.STEP_2_ORDER_EXPIRED);
}
final TotalPrice reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
if (isCaptchaInvalid(reservationCost.getPriceWithVAT(), paymentForm.getPaymentMethod(), request, event)) {
log.debug("captcha validation failed.");
bindingResult.reject(ErrorsCode.STEP_2_CAPTCHA_VALIDATION_FAILED);
}
if (paymentForm.getPaymentMethod() != PaymentProxy.PAYPAL || !paymentForm.hasPaypalTokens()) {
if (!paymentForm.isPostponeAssignment() && !ticketRepository.checkTicketUUIDs(reservationId, paymentForm.getTickets().keySet())) {
bindingResult.reject(ErrorsCode.STEP_2_MISSING_ATTENDEE_DATA);
}
paymentForm.validate(bindingResult, reservationCost, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()));
if (bindingResult.hasErrors()) {
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(ticketReservation, eventName, reservationId);
}
}
CustomerName customerName = new CustomerName(paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), event);
// handle paypal redirect!
if (paymentForm.getPaymentMethod() == PaymentProxy.PAYPAL && !paymentForm.hasPaypalTokens()) {
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);
try {
String checkoutUrl = paymentManager.createPayPalCheckoutRequest(event, reservationId, orderSummary, customerName, paymentForm.getEmail(), paymentForm.getBillingAddress(), locale, paymentForm.isPostponeAssignment(), paymentForm.isInvoiceRequested());
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true);
return "redirect:" + checkoutUrl;
} catch (Exception e) {
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(ticketReservation, eventName, reservationId);
}
}
// handle mollie redirect
if (paymentForm.getPaymentMethod() == PaymentProxy.MOLLIE) {
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);
try {
String checkoutUrl = mollieManager.createCheckoutRequest(event, reservationId, orderSummary, customerName, paymentForm.getEmail(), paymentForm.getBillingAddress(), locale, paymentForm.isInvoiceRequested(), paymentForm.getVatCountryCode(), paymentForm.getVatNr(), ticketReservation.get().getVatStatus());
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true);
return "redirect:" + checkoutUrl;
} catch (Exception e) {
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(ticketReservation, eventName, reservationId);
}
}
//
final PaymentResult status = ticketReservationManager.confirm(paymentForm.getToken(), paymentForm.getPaypalPayerID(), event, reservationId, paymentForm.getEmail(), customerName, locale, paymentForm.getBillingAddress(), reservationCost, SessionUtil.retrieveSpecialPriceSessionId(request), Optional.ofNullable(paymentForm.getPaymentMethod()), paymentForm.isInvoiceRequested(), paymentForm.getVatCountryCode(), paymentForm.getVatNr(), ticketReservation.get().getVatStatus());
if (!status.isSuccessful()) {
String errorMessageCode = status.getErrorCode().get();
MessageSourceResolvable message = new DefaultMessageSourceResolvable(new String[] { errorMessageCode, StripeManager.STRIPE_UNEXPECTED });
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_PROCESSING_ERROR, new Object[] { messageSource.getMessage(message, locale) }, null);
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(ticketReservation, eventName, reservationId);
}
//
TicketReservation reservation = ticketReservationManager.findById(reservationId).orElseThrow(IllegalStateException::new);
sendReservationCompleteEmail(request, event, reservation);
sendReservationCompleteEmailToOrganizer(request, event, reservation);
if (paymentForm.getPaymentMethod() != PaymentProxy.PAYPAL) {
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, paymentForm.getPaymentMethod() == PaymentProxy.OFFLINE);
}
return "redirect:/event/" + eventName + "/reservation/" + reservationId + "/success";
}
use of alfio.manager.support.PaymentResult in project alf.io by alfio-event.
the class TicketReservationManager method confirm.
public PaymentResult confirm(String gatewayToken, String payerId, Event event, String reservationId, String email, CustomerName customerName, Locale userLanguage, String billingAddress, TotalPrice reservationCost, Optional<String> specialPriceSessionId, Optional<PaymentProxy> method, boolean invoiceRequested, String vatCountryCode, String vatNr, PriceContainer.VatStatus vatStatus) {
PaymentProxy paymentProxy = evaluatePaymentProxy(method, reservationCost);
if (!initPaymentProcess(reservationCost, paymentProxy, reservationId, email, customerName, userLanguage, billingAddress)) {
return PaymentResult.unsuccessful("error.STEP2_UNABLE_TO_TRANSITION");
}
try {
PaymentResult paymentResult;
ticketReservationRepository.lockReservationForUpdate(reservationId);
if (reservationCost.getPriceWithVAT() > 0) {
if (invoiceRequested && configurationManager.hasAllConfigurationsForInvoice(event)) {
int invoiceSequence = invoiceSequencesRepository.lockReservationForUpdate(event.getOrganizationId());
invoiceSequencesRepository.incrementSequenceFor(event.getOrganizationId());
String pattern = configurationManager.getStringConfigValue(Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.INVOICE_NUMBER_PATTERN), "%d");
ticketReservationRepository.setInvoiceNumber(reservationId, String.format(pattern, invoiceSequence));
}
ticketReservationRepository.updateBillingData(vatStatus, vatNr, vatCountryCode, invoiceRequested, reservationId);
//
extensionManager.handleInvoiceGeneration(event, reservationId, email, customerName, userLanguage, billingAddress, reservationCost, invoiceRequested, vatCountryCode, vatNr, vatStatus).ifPresent(invoiceGeneration -> {
if (invoiceGeneration.getInvoiceNumber() != null) {
ticketReservationRepository.setInvoiceNumber(reservationId, invoiceGeneration.getInvoiceNumber());
}
});
//
switch(paymentProxy) {
case STRIPE:
paymentResult = paymentManager.processStripePayment(reservationId, gatewayToken, reservationCost.getPriceWithVAT(), event, email, customerName, billingAddress);
if (!paymentResult.isSuccessful()) {
reTransitionToPending(reservationId);
return paymentResult;
}
break;
case PAYPAL:
paymentResult = paymentManager.processPayPalPayment(reservationId, gatewayToken, payerId, reservationCost.getPriceWithVAT(), event);
if (!paymentResult.isSuccessful()) {
reTransitionToPending(reservationId);
return paymentResult;
}
break;
case OFFLINE:
transitionToOfflinePayment(event, reservationId, email, customerName, billingAddress);
paymentResult = PaymentResult.successful(NOT_YET_PAID_TRANSACTION_ID);
break;
case ON_SITE:
paymentResult = PaymentResult.successful(NOT_YET_PAID_TRANSACTION_ID);
break;
default:
throw new IllegalArgumentException("Payment proxy " + paymentProxy + " not recognized");
}
} else {
paymentResult = PaymentResult.successful(NOT_YET_PAID_TRANSACTION_ID);
}
completeReservation(event.getId(), reservationId, email, customerName, userLanguage, billingAddress, specialPriceSessionId, paymentProxy);
return paymentResult;
} catch (Exception ex) {
// it is guaranteed that in this case we're dealing with "local" error (e.g. database failure),
// thus it is safer to not rollback the reservation status
log.error("unexpected error during payment confirmation", ex);
return PaymentResult.unsuccessful("error.STEP2_STRIPE_unexpected");
}
}
use of alfio.manager.support.PaymentResult in project alf.io by alfio-event.
the class TicketReservationManagerIntegrationTest method testTicketSelection.
@Test
void testTicketSelection() {
List<TicketCategoryModification> categories = List.of(new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, AVAILABLE_SEATS, new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()), new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10, new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", true, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()));
Pair<Event, String> eventAndUsername = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
Event event = eventAndUsername.getKey();
TicketCategory bounded = ticketCategoryRepository.findAllTicketCategories(event.getId()).stream().filter(TicketCategory::isBounded).findFirst().orElseThrow(IllegalStateException::new);
TicketCategory unbounded = ticketCategoryRepository.findAllTicketCategories(event.getId()).stream().filter(t -> !t.isBounded()).findFirst().orElseThrow(IllegalStateException::new);
assertEquals(0, eventStatisticsManager.loadModifiedTickets(event.getId(), bounded.getId(), 0, null).size());
assertEquals(Integer.valueOf(0), eventStatisticsManager.countModifiedTicket(event.getId(), bounded.getId(), null));
assertEquals(0, eventStatisticsManager.loadModifiedTickets(event.getId(), unbounded.getId(), 0, null).size());
TicketReservationModification tr = new TicketReservationModification();
tr.setQuantity(10);
tr.setTicketCategoryId(bounded.getId());
TicketReservationModification tr2 = new TicketReservationModification();
tr2.setQuantity(9);
tr2.setTicketCategoryId(unbounded.getId());
TicketReservationWithOptionalCodeModification mod = new TicketReservationWithOptionalCodeModification(tr, Optional.empty());
TicketReservationWithOptionalCodeModification mod2 = new TicketReservationWithOptionalCodeModification(tr2, Optional.empty());
String reservationId = ticketReservationManager.createTicketReservation(event, List.of(mod, mod2), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Locale.ENGLISH, false, null);
List<TicketReservation> reservations = purchaseContextSearchManager.findAllReservationsFor(event, 0, null, null).getKey();
assertEquals(1, reservations.size());
assertEquals(reservationId, reservations.get(0).getId());
List<Ticket> pendingTickets = ticketRepository.findPendingTicketsInCategories(List.of(bounded.getId(), unbounded.getId()));
assertEquals(19, pendingTickets.size());
pendingTickets.forEach(t -> assertEquals(1000, t.getFinalPriceCts()));
List<Ticket> tickets = ticketRepository.findFreeByEventId(event.getId());
assertEquals(1, tickets.size());
assertTrue(tickets.stream().allMatch(t -> t.getCategoryId() == null));
Pair<TotalPrice, Optional<PromoCodeDiscount>> priceAndDiscount = ticketReservationManager.totalReservationCostWithVAT(reservationId);
TotalPrice totalPrice = priceAndDiscount.getLeft();
assertTrue(priceAndDiscount.getRight().isEmpty());
assertEquals(0, ticketReservationManager.getPendingPayments(event.getShortName()).size());
PaymentSpecification specification = new PaymentSpecification(reservationId, null, totalPrice.getPriceWithVAT(), event, "email@example.com", new CustomerName("full name", "full", "name", event.mustUseFirstAndLastName()), "billing address", null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
PaymentResult confirm = ticketReservationManager.performPayment(specification, totalPrice, PaymentProxy.OFFLINE, PaymentMethod.BANK_TRANSFER, null);
assertTrue(confirm.isSuccessful());
assertEquals(TicketReservation.TicketReservationStatus.OFFLINE_PAYMENT, ticketReservationManager.findById(reservationId).get().getStatus());
assertEquals(1, ticketReservationManager.getPendingPayments(event.getShortName()).size());
var from = ZonedDateTime.now(Clock.systemUTC()).minusDays(1).with(d -> d.with(ChronoField.HOUR_OF_DAY, 0));
var to = ZonedDateTime.now(Clock.systemUTC()).plusDays(1).with(ChronoField.HOUR_OF_DAY, 23);
// -> no reservations
assertTrue(ticketReservationRepository.getSoldStatistic(event.getId(), from, to, "day").stream().allMatch(tds -> tds.getCount() == 0L));
ticketReservationManager.validateAndConfirmOfflinePayment(reservationId, event, new BigDecimal("190.00"), eventAndUsername.getValue());
var soldStatisticsList = ticketReservationRepository.getSoldStatistic(event.getId(), from, to, "day");
assertEquals(3, soldStatisticsList.size());
assertEquals(LocalDate.now(ClockProvider.clock()).toString(), soldStatisticsList.get(1).getDate());
// -> 19 tickets reserved
assertEquals(19L, soldStatisticsList.get(1).getCount());
assertEquals(19L, soldStatisticsList.stream().mapToLong(TicketsByDateStatistic::getCount).sum());
assertEquals(10, eventStatisticsManager.loadModifiedTickets(event.getId(), bounded.getId(), 0, null).size());
assertEquals(Integer.valueOf(10), eventStatisticsManager.countModifiedTicket(event.getId(), bounded.getId(), null));
assertEquals(9, eventStatisticsManager.loadModifiedTickets(event.getId(), unbounded.getId(), 0, null).size());
assertEquals(Integer.valueOf(9), eventStatisticsManager.countModifiedTicket(event.getId(), unbounded.getId(), null));
assertEquals(TicketReservation.TicketReservationStatus.COMPLETE, ticketReservationManager.findById(reservationId).get().getStatus());
// -------------------
TicketReservationModification trForDelete = new TicketReservationModification();
trForDelete.setQuantity(1);
trForDelete.setTicketCategoryId(unbounded.getId());
TicketReservationWithOptionalCodeModification modForDelete = new TicketReservationWithOptionalCodeModification(trForDelete, Optional.empty());
String reservationId2 = ticketReservationManager.createTicketReservation(event, Collections.singletonList(modForDelete), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Locale.ENGLISH, false, null);
PaymentSpecification specification2 = new PaymentSpecification(reservationId2, null, totalPrice.getPriceWithVAT(), event, "email@example.com", new CustomerName("full name", "full", "name", event.mustUseFirstAndLastName()), "billing address", null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
PaymentResult confirm2 = ticketReservationManager.performPayment(specification2, totalPrice, PaymentProxy.OFFLINE, PaymentMethod.BANK_TRANSFER, null);
assertTrue(confirm2.isSuccessful());
ticketReservationManager.deleteOfflinePayment(event, reservationId2, false, false, null);
assertFalse(ticketReservationManager.findById(reservationId2).isPresent());
}
use of alfio.manager.support.PaymentResult in project alf.io by alfio-event.
the class TicketReservationManagerIntegrationTest method testDeletePendingPaymentUnboundedCategory.
@Test
void testDeletePendingPaymentUnboundedCategory() {
List<TicketCategoryModification> categories = Collections.singletonList(new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, AVAILABLE_SEATS, new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()));
Event event = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository).getKey();
TicketCategory unbounded = ticketCategoryRepository.findAllTicketCategories(event.getId()).get(0);
TicketReservationModification tr = new TicketReservationModification();
tr.setQuantity(AVAILABLE_SEATS / 2 + 1);
tr.setTicketCategoryId(unbounded.getId());
TicketReservationWithOptionalCodeModification mod = new TicketReservationWithOptionalCodeModification(tr, Optional.empty());
String reservationId = ticketReservationManager.createTicketReservation(event, Collections.singletonList(mod), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Locale.ENGLISH, false, null);
Pair<TotalPrice, Optional<PromoCodeDiscount>> priceAndDiscount = ticketReservationManager.totalReservationCostWithVAT(reservationId);
TotalPrice reservationCost = priceAndDiscount.getLeft();
assertTrue(priceAndDiscount.getRight().isEmpty());
PaymentSpecification specification = new PaymentSpecification(reservationId, null, reservationCost.getPriceWithVAT(), event, "email@example.com", new CustomerName("full name", "full", "name", event.mustUseFirstAndLastName()), "billing address", null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
PaymentResult result = ticketReservationManager.performPayment(specification, reservationCost, PaymentProxy.OFFLINE, PaymentMethod.BANK_TRANSFER, null);
assertTrue(result.isSuccessful());
ticketReservationManager.deleteOfflinePayment(event, reservationId, false, false, null);
waitingQueueManager.distributeSeats(event);
mod = new TicketReservationWithOptionalCodeModification(tr, Optional.empty());
reservationId = ticketReservationManager.createTicketReservation(event, Collections.singletonList(mod), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Locale.ENGLISH, false, null);
priceAndDiscount = ticketReservationManager.totalReservationCostWithVAT(reservationId);
reservationCost = priceAndDiscount.getLeft();
assertTrue(priceAndDiscount.getRight().isEmpty());
PaymentSpecification specification2 = new PaymentSpecification(reservationId, null, reservationCost.getPriceWithVAT(), event, "email@example.com", new CustomerName("full name", "full", "name", event.mustUseFirstAndLastName()), "billing address", null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
result = ticketReservationManager.performPayment(specification2, reservationCost, PaymentProxy.OFFLINE, PaymentMethod.BANK_TRANSFER, null);
assertTrue(result.isSuccessful());
}
use of alfio.manager.support.PaymentResult in project alf.io by alfio-event.
the class TicketReservationManagerIntegrationTest method testCleanupOfflineExpiredReservations.
@Test
void testCleanupOfflineExpiredReservations() {
List<TicketCategoryModification> categories = List.of(new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10, new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", true, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()));
Pair<Event, String> eventAndUsername = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
Event event = eventAndUsername.getKey();
TicketCategory bounded = ticketCategoryRepository.findAllTicketCategories(event.getId()).stream().filter(TicketCategory::isBounded).findFirst().orElseThrow(IllegalStateException::new);
TicketReservationModification tr = new TicketReservationModification();
tr.setQuantity(10);
tr.setTicketCategoryId(bounded.getId());
TicketReservationWithOptionalCodeModification mod = new TicketReservationWithOptionalCodeModification(tr, Optional.empty());
Date past = DateUtils.addDays(new Date(), -2);
Date now = new Date();
String reservationId = ticketReservationManager.createTicketReservation(event, List.of(mod), Collections.emptyList(), past, Optional.empty(), Locale.ENGLISH, false, null);
final Supplier<List<String>> idsOfflinePayment = () -> jdbcTemplate.queryForList("select id from tickets_reservation where validity < :date and status = 'OFFLINE_PAYMENT'", Collections.singletonMap("date", now), String.class);
assertTrue(idsOfflinePayment.get().isEmpty());
Pair<TotalPrice, Optional<PromoCodeDiscount>> priceAndDiscount = ticketReservationManager.totalReservationCostWithVAT(reservationId);
TotalPrice reservationCost = priceAndDiscount.getLeft();
assertTrue(priceAndDiscount.getRight().isEmpty());
PaymentSpecification specification = new PaymentSpecification(reservationId, null, reservationCost.getPriceWithVAT(), event, "email@example.com", new CustomerName("full name", "full", "name", event.mustUseFirstAndLastName()), "billing address", null, Locale.ENGLISH, true, false, null, "IT", "123456", PriceContainer.VatStatus.INCLUDED, true, false);
PaymentResult result = ticketReservationManager.performPayment(specification, reservationCost, PaymentProxy.OFFLINE, PaymentMethod.BANK_TRANSFER, null);
assertTrue(result.isSuccessful());
//
assertEquals(1, jdbcTemplate.update("update tickets_reservation set validity = :date where id = :id", Map.of("date", past, "id", reservationId)));
//
List<String> idsOffline = idsOfflinePayment.get();
assertEquals(1, idsOffline.size());
assertEquals(reservationId, idsOffline.get(0));
ticketReservationManager.cleanupExpiredOfflineReservations(now);
assertFalse(idsOfflinePayment.get().isEmpty());
configurationRepository.insert(AUTOMATIC_REMOVAL_EXPIRED_OFFLINE_PAYMENT.name(), "true", "");
ticketReservationManager.cleanupExpiredOfflineReservations(now);
assertTrue(idsOfflinePayment.get().isEmpty());
}
Aggregations