Skip to content

Commit 9343b0e

Browse files
committed
#464 split PaymentForm
1 parent 9668331 commit 9343b0e

File tree

7 files changed

+241
-217
lines changed

7 files changed

+241
-217
lines changed

src/main/java/alfio/controller/ReservationController.java

+30-61
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package alfio.controller;
1818

1919
import alfio.controller.api.support.TicketHelper;
20+
import alfio.controller.form.ContactAndTicketsForm;
2021
import alfio.controller.form.PaymentForm;
2122
import alfio.controller.form.UpdateTicketOwnerForm;
2223
import alfio.controller.support.SessionUtil;
@@ -85,9 +86,7 @@ public class ReservationController {
8586
private final TicketHelper ticketHelper;
8687
private final TicketFieldRepository ticketFieldRepository;
8788
private final PaymentManager paymentManager;
88-
private final TicketRepository ticketRepository;
8989
private final EuVatChecker vatChecker;
90-
private final MollieManager mollieManager;
9190
private final RecaptchaService recaptchaService;
9291
private final TicketReservationRepository ticketReservationRepository;
9392

@@ -146,7 +145,7 @@ public String showBookingPage(@PathVariable("eventName") String eventName,
146145

147146
boolean invoiceAllowed = configurationManager.hasAllConfigurationsForInvoice(event) || vatChecker.isVatCheckingEnabledFor(event.getOrganizationId());
148147
boolean onlyInvoice = invoiceAllowed && configurationManager.getBooleanConfigValue(partialConfig.apply(ConfigurationKeys.GENERATE_ONLY_INVOICE), false);
149-
PaymentForm paymentForm = PaymentForm.fromExistingReservation(reservation);
148+
ContactAndTicketsForm contactAndTicketsForm = ContactAndTicketsForm.fromExistingReservation(reservation);
150149
model.addAttribute("multiplePaymentMethods" , activePaymentMethods.size() > 1 )
151150
.addAttribute("orderSummary", orderSummary)
152151
.addAttribute("reservationId", reservationId)
@@ -162,13 +161,13 @@ public String showBookingPage(@PathVariable("eventName") String eventName,
162161
.addAttribute("euVatCheckingEnabled", vatChecker.isVatCheckingEnabledFor(event.getOrganizationId()))
163162
.addAttribute("invoiceIsAllowed", invoiceAllowed)
164163
.addAttribute("onlyInvoice", onlyInvoice)
165-
.addAttribute("vatNrIsLinked", orderSummary.isVatExempt() || paymentForm.getHasVatCountryCode())
164+
.addAttribute("vatNrIsLinked", orderSummary.isVatExempt() || contactAndTicketsForm.getHasVatCountryCode())
166165
.addAttribute("attendeeAutocompleteEnabled", ticketsInReservation.size() == 1 && configurationManager.getBooleanConfigValue(partialConfig.apply(ENABLE_ATTENDEE_AUTOCOMPLETE), true))
167166
.addAttribute("billingAddressLabel", invoiceAllowed ? "reservation-page.billing-address" : "reservation-page.receipt-address")
168167
.addAttribute("customerReferenceEnabled", configurationManager.getBooleanConfigValue(partialConfig.apply(ENABLE_CUSTOMER_REFERENCE), false));
169168

170169
Map<String, Object> modelMap = model.asMap();
171-
modelMap.putIfAbsent("paymentForm", paymentForm);
170+
modelMap.putIfAbsent("paymentForm", contactAndTicketsForm);
172171
modelMap.putIfAbsent("hasErrors", false);
173172

174173
boolean hasPaidSupplement = ticketReservationManager.hasPaidSupplements(reservationId);
@@ -246,11 +245,11 @@ public String showConfirmationPage(@PathVariable("eventName") String eventName,
246245

247246
@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}/validate-to-overview", method = RequestMethod.POST)
248247
public String validateToOverview(@PathVariable("eventName") String eventName, @PathVariable("reservationId") String reservationId,
249-
PaymentForm paymentForm, BindingResult bindingResult,
248+
ContactAndTicketsForm contactAndTicketsForm, BindingResult bindingResult,
250249
Model model, HttpServletRequest request, Locale locale, RedirectAttributes redirectAttributes) {
251250

252251
Optional<Event> eventOptional = eventRepository.findOptionalByShortName(eventName);
253-
Optional<String> redirectForFailure = checkReservation(paymentForm.isBackFromOverview(), paymentForm.shouldCancelReservation(), eventName, reservationId, request, eventOptional);
252+
Optional<String> redirectForFailure = checkReservation(contactAndTicketsForm.isBackFromOverview(), contactAndTicketsForm.shouldCancelReservation(), eventName, reservationId, request, eventOptional);
254253
if(redirectForFailure.isPresent()) { //ugly
255254
return redirectForFailure.get();
256255
}
@@ -263,32 +262,32 @@ public String validateToOverview(@PathVariable("eventName") String eventName, @P
263262
Configuration.ConfigurationPathKey forceAssignmentKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.FORCE_TICKET_OWNER_ASSIGNMENT_AT_RESERVATION);
264263
boolean forceAssignment = configurationManager.getBooleanConfigValue(forceAssignmentKey, false);
265264
if(forceAssignment) {
266-
paymentForm.setPostponeAssignment(false);
265+
contactAndTicketsForm.setPostponeAssignment(false);
267266
}
268267

269268
Configuration.ConfigurationPathKey invoiceOnlyKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.GENERATE_ONLY_INVOICE);
270269
boolean invoiceOnly = configurationManager.getBooleanConfigValue(invoiceOnlyKey, false);
271270

272-
final boolean companyVatChecked = invoiceOnly ? paymentForm.isAddCompanyBillingDetails() : paymentForm.isInvoiceRequested();
271+
final boolean companyVatChecked = invoiceOnly ? contactAndTicketsForm.isAddCompanyBillingDetails() : contactAndTicketsForm.isInvoiceRequested();
273272

274273
if(invoiceOnly && reservationCost.getPriceWithVAT() > 0) {
275274
//override, that's why we save it
276-
paymentForm.setInvoiceRequested(true);
275+
contactAndTicketsForm.setInvoiceRequested(true);
277276
}
278277

279-
CustomerName customerName = new CustomerName(paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), event, false);
278+
CustomerName customerName = new CustomerName(contactAndTicketsForm.getFullName(), contactAndTicketsForm.getFirstName(), contactAndTicketsForm.getLastName(), event, false);
280279

281280
//persist data
282-
ticketReservationManager.updateReservation(reservationId, customerName, paymentForm.getEmail(),
283-
paymentForm.getBillingAddressCompany(), paymentForm.getBillingAddressLine1(), paymentForm.getBillingAddressLine2(),
284-
paymentForm.getBillingAddressZip(), paymentForm.getBillingAddressCity(), paymentForm.getVatCountryCode(),
285-
paymentForm.getCustomerReference(), paymentForm.getVatNr(), paymentForm.isInvoiceRequested(),
286-
paymentForm.isAddCompanyBillingDetails(), false);
287-
assignTickets(event.getShortName(), reservationId, paymentForm, bindingResult, request, true, true);
281+
ticketReservationManager.updateReservation(reservationId, customerName, contactAndTicketsForm.getEmail(),
282+
contactAndTicketsForm.getBillingAddressCompany(), contactAndTicketsForm.getBillingAddressLine1(), contactAndTicketsForm.getBillingAddressLine2(),
283+
contactAndTicketsForm.getBillingAddressZip(), contactAndTicketsForm.getBillingAddressCity(), contactAndTicketsForm.getVatCountryCode(),
284+
contactAndTicketsForm.getCustomerReference(), contactAndTicketsForm.getVatNr(), contactAndTicketsForm.isInvoiceRequested(),
285+
contactAndTicketsForm.isAddCompanyBillingDetails(), false);
286+
assignTickets(event.getShortName(), reservationId, contactAndTicketsForm, bindingResult, request, true, true);
288287
//
289288

290289
//
291-
paymentForm.validate(bindingResult, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()), new SameCountryValidator(vatChecker, event.getOrganizationId(), event.getId(), reservationId));
290+
contactAndTicketsForm.validate(bindingResult, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()), new SameCountryValidator(vatChecker, event.getOrganizationId(), event.getId(), reservationId));
292291
//
293292

294293
if(bindingResult.hasErrors()) {
@@ -499,8 +498,8 @@ private String redirectReservation(Optional<TicketReservation> ticketReservation
499498

500499
@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}", method = RequestMethod.POST)
501500
public String handleReservation(@PathVariable("eventName") String eventName,
502-
@PathVariable("reservationId") String reservationId, PaymentForm paymentForm, BindingResult bindingResult,
503-
Model model, HttpServletRequest request, Locale locale, RedirectAttributes redirectAttributes) {
501+
@PathVariable("reservationId") String reservationId, PaymentForm paymentForm, BindingResult bindingResult,
502+
Model model, HttpServletRequest request, Locale locale, RedirectAttributes redirectAttributes) {
504503

505504
Optional<Event> eventOptional = eventRepository.findOptionalByShortName(eventName);
506505
Optional<String> redirectForFailure = checkReservation(paymentForm.isBackFromOverview(), paymentForm.shouldCancelReservation(), eventName, reservationId, request, eventOptional);
@@ -525,28 +524,19 @@ public String handleReservation(@PathVariable("eventName") String eventName,
525524
final TicketReservation ticketReservation = optionalReservation.get();
526525

527526
final TotalPrice reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
528-
if(isCaptchaInvalid(reservationCost.getPriceWithVAT(), paymentForm.getPaymentMethod(), request, event)) {
529-
log.debug("captcha validation failed.");
530-
bindingResult.reject(ErrorsCode.STEP_2_CAPTCHA_VALIDATION_FAILED);
531-
}
532527

533-
Configuration.ConfigurationPathKey forceAssignmentKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.FORCE_TICKET_OWNER_ASSIGNMENT_AT_RESERVATION);
534-
boolean forceAssignment = configurationManager.getBooleanConfigValue(forceAssignmentKey, false);
535-
if(forceAssignment) {
536-
paymentForm.setPostponeAssignment(false);
528+
paymentForm.validate(bindingResult, event, reservationCost);
529+
if (bindingResult.hasErrors()) {
530+
SessionUtil.addToFlash(bindingResult, redirectAttributes);
531+
return redirectReservation(optionalReservation, eventName, reservationId);
537532
}
538533

539-
Configuration.ConfigurationPathKey invoiceOnlyKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.GENERATE_ONLY_INVOICE);
540-
boolean invoiceOnly = configurationManager.getBooleanConfigValue(invoiceOnlyKey, false);
541-
if(invoiceOnly && reservationCost.getPriceWithVAT() > 0) {
542-
paymentForm.setInvoiceRequested(true);
534+
if(isCaptchaInvalid(reservationCost.getPriceWithVAT(), paymentForm.getPaymentMethod(), request, event)) {
535+
log.debug("captcha validation failed.");
536+
bindingResult.reject(ErrorsCode.STEP_2_CAPTCHA_VALIDATION_FAILED);
543537
}
544538

545539
if(paymentForm.getPaymentMethod() != PaymentProxy.PAYPAL || !paymentForm.hasPaypalTokens()) {
546-
//if(!paymentForm.isPostponeAssignment() && !ticketRepository.checkTicketUUIDs(reservationId, paymentForm.getTickets().keySet())) {
547-
//bindingResult.reject(ErrorsCode.STEP_2_MISSING_ATTENDEE_DATA);
548-
//}
549-
//paymentForm.validate(bindingResult, reservationCost, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()), new SameCountryValidator(vatChecker, event.getOrganizationId(), event.getId(), reservationId));
550540
if (bindingResult.hasErrors()) {
551541
SessionUtil.addToFlash(bindingResult, redirectAttributes);
552542
return redirectReservation(Optional.of(ticketReservation), eventName, reservationId);
@@ -561,28 +551,7 @@ public String handleReservation(@PathVariable("eventName") String eventName,
561551
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);
562552
try {
563553
String checkoutUrl = paymentManager.createPayPalCheckoutRequest(event, reservationId, orderSummary, customerName,
564-
paymentForm.getEmail(), paymentForm.getBillingAddress(), paymentForm.getCustomerReference(), locale, paymentForm.isPostponeAssignment(),
565-
paymentForm.isInvoiceRequested());
566-
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
567-
return "redirect:" + checkoutUrl;
568-
} catch (Exception e) {
569-
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
570-
SessionUtil.addToFlash(bindingResult, redirectAttributes);
571-
return redirectReservation(optionalReservation, eventName, reservationId);
572-
}
573-
}
574-
575-
//handle mollie redirect
576-
if(paymentForm.getPaymentMethod() == PaymentProxy.MOLLIE) {
577-
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);
578-
try {
579-
String checkoutUrl = mollieManager.createCheckoutRequest(event, reservationId, orderSummary, customerName,
580-
paymentForm.getEmail(), paymentForm.getBillingAddress(), locale,
581-
paymentForm.isInvoiceRequested(),
582-
paymentForm.getVatCountryCode(),
583-
paymentForm.getVatNr(),
584-
optionalReservation.get().getVatStatus());
585-
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
554+
ticketReservation.getEmail(), ticketReservation.getBillingAddress(), ticketReservation.getCustomerReference(), locale);
586555
return "redirect:" + checkoutUrl;
587556
} catch (Exception e) {
588557
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
@@ -620,9 +589,9 @@ private boolean isCaptchaInvalid(int cost, PaymentProxy paymentMethod, HttpServl
620589
&& !recaptchaService.checkRecaptcha(request);
621590
}
622591

623-
private void assignTickets(String eventName, String reservationId, PaymentForm paymentForm, BindingResult bindingResult, HttpServletRequest request, boolean preAssign, boolean skipValidation) {
624-
if(!paymentForm.isPostponeAssignment()) {
625-
paymentForm.getTickets().forEach((ticketId, owner) -> {
592+
private void assignTickets(String eventName, String reservationId, ContactAndTicketsForm contactAndTicketsForm, BindingResult bindingResult, HttpServletRequest request, boolean preAssign, boolean skipValidation) {
593+
if(!contactAndTicketsForm.isPostponeAssignment()) {
594+
contactAndTicketsForm.getTickets().forEach((ticketId, owner) -> {
626595
if (preAssign) {
627596
Optional<Errors> bindingResultOptional = skipValidation ? Optional.empty() : Optional.of(bindingResult);
628597
ticketHelper.preAssignTicket(eventName, reservationId, ticketId, owner, bindingResultOptional, request, (tr) -> {

src/main/java/alfio/controller/api/ReservationApiController.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package alfio.controller.api;
1818

1919
import alfio.controller.api.support.TicketHelper;
20-
import alfio.controller.form.PaymentForm;
20+
import alfio.controller.form.ContactAndTicketsForm;
2121
import alfio.controller.form.UpdateTicketOwnerForm;
2222
import alfio.manager.EuVatChecker;
2323
import alfio.manager.TicketReservationManager;
@@ -128,18 +128,18 @@ public ResponseEntity<Boolean> resetVat(@PathVariable("eventName") String eventN
128128
@Transactional
129129
public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String eventName,
130130
@PathVariable("reservationId") String reservationId,
131-
PaymentForm paymentForm,
131+
ContactAndTicketsForm contactAndTicketsForm,
132132
Locale locale,
133133
HttpServletRequest request) {
134134

135-
String country = paymentForm.getVatCountryCode();
135+
/*String country = contactAndTicketsForm.getVatCountryCode();
136136
Optional<Triple<Event, TicketReservation, VatDetail>> vatDetail;
137137
try {
138138
vatDetail = eventRepository.findOptionalByShortName(eventName)
139139
.flatMap(e -> ticketReservationRepository.findOptionalReservationById(reservationId).map(r -> Pair.of(e, r)))
140140
.filter(e -> EnumSet.of(INCLUDED, NOT_INCLUDED).contains(e.getKey().getVatStatus()))
141141
.filter(e -> vatChecker.isVatCheckingEnabledFor(e.getKey().getOrganizationId()))
142-
.flatMap(e -> vatChecker.checkVat(paymentForm.getVatNr(), country, e.getKey().getOrganizationId()).map(vd -> Triple.of(e.getLeft(), e.getRight(), vd)));
142+
.flatMap(e -> vatChecker.checkVat(contactAndTicketsForm.getVatNr(), country, e.getKey().getOrganizationId()).map(vd -> Triple.of(e.getLeft(), e.getRight(), vd)));
143143
} catch (IllegalStateException e) {
144144
return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE);
145145
}
@@ -151,13 +151,13 @@ public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String
151151
VatDetail vd = t.getRight();
152152
String billingAddress = vd.getName() + "\n" + vd.getAddress();
153153
PriceContainer.VatStatus vatStatus = determineVatStatus(t.getLeft().getVatStatus(), t.getRight().isVatExempt());
154-
ticketReservationRepository.updateBillingData(vatStatus, vd.getVatNr(), country, paymentForm.isInvoiceRequested(), reservationId);
154+
ticketReservationRepository.updateBillingData(vatStatus, vd.getVatNr(), country, contactAndTicketsForm.isInvoiceRequested(), reservationId);
155155
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, t.getLeft(), Locale.forLanguageTag(t.getMiddle().getUserLanguage()));
156156
ticketReservationRepository.addReservationInvoiceOrReceiptModel(reservationId, Json.toJson(orderSummary));
157-
ticketReservationRepository.updateTicketReservation(reservationId, t.getMiddle().getStatus().name(), paymentForm.getEmail(),
158-
paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), locale.getLanguage(), billingAddress, null,
159-
Optional.ofNullable(paymentForm.getPaymentMethod()).map(PaymentProxy::name).orElse(null), paymentForm.getCustomerReference());
160-
paymentForm.getTickets().forEach((ticketId, owner) -> {
157+
ticketReservationRepository.updateTicketReservation(reservationId, t.getMiddle().getStatus().name(), contactAndTicketsForm.getEmail(),
158+
contactAndTicketsForm.getFullName(), contactAndTicketsForm.getFirstName(), contactAndTicketsForm.getLastName(), locale.getLanguage(), billingAddress, null,
159+
Optional.ofNullable(contactAndTicketsForm.getPaymentMethod()).map(PaymentProxy::name).orElse(null), contactAndTicketsForm.getCustomerReference());
160+
contactAndTicketsForm.getTickets().forEach((ticketId, owner) -> {
161161
if(isNotEmpty(owner.getEmail()) && ((isNotEmpty(owner.getFirstName()) && isNotEmpty(owner.getLastName())) || isNotEmpty(owner.getFullName()))) {
162162
ticketHelper.preAssignTicket(eventName, reservationId, ticketId, owner, Optional.empty(), request, (tr) -> {}, Optional.empty());
163163
}
@@ -173,7 +173,8 @@ public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String
173173
return new ResponseEntity<VatDetail>(HttpStatus.BAD_REQUEST);
174174
}
175175
})
176-
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
176+
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));*/
177+
return null;
177178
}
178179

179180
private static PriceContainer.VatStatus determineVatStatus(PriceContainer.VatStatus current, boolean isVatExempt) {

0 commit comments

Comments
 (0)