Skip to content

Commit

Permalink
#464 split PaymentForm
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jul 2, 2018
1 parent 9668331 commit 9343b0e
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 217 deletions.
91 changes: 30 additions & 61 deletions src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package alfio.controller;

import alfio.controller.api.support.TicketHelper;
import alfio.controller.form.ContactAndTicketsForm;
import alfio.controller.form.PaymentForm;
import alfio.controller.form.UpdateTicketOwnerForm;
import alfio.controller.support.SessionUtil;
Expand Down Expand Up @@ -85,9 +86,7 @@ public class ReservationController {
private final TicketHelper ticketHelper;
private final TicketFieldRepository ticketFieldRepository;
private final PaymentManager paymentManager;
private final TicketRepository ticketRepository;
private final EuVatChecker vatChecker;
private final MollieManager mollieManager;
private final RecaptchaService recaptchaService;
private final TicketReservationRepository ticketReservationRepository;

Expand Down Expand Up @@ -146,7 +145,7 @@ public String showBookingPage(@PathVariable("eventName") String eventName,

boolean invoiceAllowed = configurationManager.hasAllConfigurationsForInvoice(event) || vatChecker.isVatCheckingEnabledFor(event.getOrganizationId());
boolean onlyInvoice = invoiceAllowed && configurationManager.getBooleanConfigValue(partialConfig.apply(ConfigurationKeys.GENERATE_ONLY_INVOICE), false);
PaymentForm paymentForm = PaymentForm.fromExistingReservation(reservation);
ContactAndTicketsForm contactAndTicketsForm = ContactAndTicketsForm.fromExistingReservation(reservation);
model.addAttribute("multiplePaymentMethods" , activePaymentMethods.size() > 1 )
.addAttribute("orderSummary", orderSummary)
.addAttribute("reservationId", reservationId)
Expand All @@ -162,13 +161,13 @@ public String showBookingPage(@PathVariable("eventName") String eventName,
.addAttribute("euVatCheckingEnabled", vatChecker.isVatCheckingEnabledFor(event.getOrganizationId()))
.addAttribute("invoiceIsAllowed", invoiceAllowed)
.addAttribute("onlyInvoice", onlyInvoice)
.addAttribute("vatNrIsLinked", orderSummary.isVatExempt() || paymentForm.getHasVatCountryCode())
.addAttribute("vatNrIsLinked", orderSummary.isVatExempt() || contactAndTicketsForm.getHasVatCountryCode())
.addAttribute("attendeeAutocompleteEnabled", ticketsInReservation.size() == 1 && configurationManager.getBooleanConfigValue(partialConfig.apply(ENABLE_ATTENDEE_AUTOCOMPLETE), true))
.addAttribute("billingAddressLabel", invoiceAllowed ? "reservation-page.billing-address" : "reservation-page.receipt-address")
.addAttribute("customerReferenceEnabled", configurationManager.getBooleanConfigValue(partialConfig.apply(ENABLE_CUSTOMER_REFERENCE), false));

Map<String, Object> modelMap = model.asMap();
modelMap.putIfAbsent("paymentForm", paymentForm);
modelMap.putIfAbsent("paymentForm", contactAndTicketsForm);
modelMap.putIfAbsent("hasErrors", false);

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

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

Optional<Event> eventOptional = eventRepository.findOptionalByShortName(eventName);
Optional<String> redirectForFailure = checkReservation(paymentForm.isBackFromOverview(), paymentForm.shouldCancelReservation(), eventName, reservationId, request, eventOptional);
Optional<String> redirectForFailure = checkReservation(contactAndTicketsForm.isBackFromOverview(), contactAndTicketsForm.shouldCancelReservation(), eventName, reservationId, request, eventOptional);
if(redirectForFailure.isPresent()) { //ugly
return redirectForFailure.get();
}
Expand All @@ -263,32 +262,32 @@ public String validateToOverview(@PathVariable("eventName") String eventName, @P
Configuration.ConfigurationPathKey forceAssignmentKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.FORCE_TICKET_OWNER_ASSIGNMENT_AT_RESERVATION);
boolean forceAssignment = configurationManager.getBooleanConfigValue(forceAssignmentKey, false);
if(forceAssignment) {
paymentForm.setPostponeAssignment(false);
contactAndTicketsForm.setPostponeAssignment(false);
}

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

final boolean companyVatChecked = invoiceOnly ? paymentForm.isAddCompanyBillingDetails() : paymentForm.isInvoiceRequested();
final boolean companyVatChecked = invoiceOnly ? contactAndTicketsForm.isAddCompanyBillingDetails() : contactAndTicketsForm.isInvoiceRequested();

if(invoiceOnly && reservationCost.getPriceWithVAT() > 0) {
//override, that's why we save it
paymentForm.setInvoiceRequested(true);
contactAndTicketsForm.setInvoiceRequested(true);
}

CustomerName customerName = new CustomerName(paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), event, false);
CustomerName customerName = new CustomerName(contactAndTicketsForm.getFullName(), contactAndTicketsForm.getFirstName(), contactAndTicketsForm.getLastName(), event, false);

//persist data
ticketReservationManager.updateReservation(reservationId, customerName, paymentForm.getEmail(),
paymentForm.getBillingAddressCompany(), paymentForm.getBillingAddressLine1(), paymentForm.getBillingAddressLine2(),
paymentForm.getBillingAddressZip(), paymentForm.getBillingAddressCity(), paymentForm.getVatCountryCode(),
paymentForm.getCustomerReference(), paymentForm.getVatNr(), paymentForm.isInvoiceRequested(),
paymentForm.isAddCompanyBillingDetails(), false);
assignTickets(event.getShortName(), reservationId, paymentForm, bindingResult, request, true, true);
ticketReservationManager.updateReservation(reservationId, customerName, contactAndTicketsForm.getEmail(),
contactAndTicketsForm.getBillingAddressCompany(), contactAndTicketsForm.getBillingAddressLine1(), contactAndTicketsForm.getBillingAddressLine2(),
contactAndTicketsForm.getBillingAddressZip(), contactAndTicketsForm.getBillingAddressCity(), contactAndTicketsForm.getVatCountryCode(),
contactAndTicketsForm.getCustomerReference(), contactAndTicketsForm.getVatNr(), contactAndTicketsForm.isInvoiceRequested(),
contactAndTicketsForm.isAddCompanyBillingDetails(), false);
assignTickets(event.getShortName(), reservationId, contactAndTicketsForm, bindingResult, request, true, true);
//

//
paymentForm.validate(bindingResult, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()), new SameCountryValidator(vatChecker, event.getOrganizationId(), event.getId(), reservationId));
contactAndTicketsForm.validate(bindingResult, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()), new SameCountryValidator(vatChecker, event.getOrganizationId(), event.getId(), reservationId));
//

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

@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) {
@PathVariable("reservationId") String reservationId, PaymentForm paymentForm, BindingResult bindingResult,
Model model, HttpServletRequest request, Locale locale, RedirectAttributes redirectAttributes) {

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

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

Configuration.ConfigurationPathKey forceAssignmentKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.FORCE_TICKET_OWNER_ASSIGNMENT_AT_RESERVATION);
boolean forceAssignment = configurationManager.getBooleanConfigValue(forceAssignmentKey, false);
if(forceAssignment) {
paymentForm.setPostponeAssignment(false);
paymentForm.validate(bindingResult, event, reservationCost);
if (bindingResult.hasErrors()) {
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(optionalReservation, eventName, reservationId);
}

Configuration.ConfigurationPathKey invoiceOnlyKey = Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.GENERATE_ONLY_INVOICE);
boolean invoiceOnly = configurationManager.getBooleanConfigValue(invoiceOnlyKey, false);
if(invoiceOnly && reservationCost.getPriceWithVAT() > 0) {
paymentForm.setInvoiceRequested(true);
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()), new SameCountryValidator(vatChecker, event.getOrganizationId(), event.getId(), reservationId));
if (bindingResult.hasErrors()) {
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(Optional.of(ticketReservation), eventName, reservationId);
Expand All @@ -561,28 +551,7 @@ public String handleReservation(@PathVariable("eventName") String eventName,
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);
try {
String checkoutUrl = paymentManager.createPayPalCheckoutRequest(event, reservationId, orderSummary, customerName,
paymentForm.getEmail(), paymentForm.getBillingAddress(), paymentForm.getCustomerReference(), locale, paymentForm.isPostponeAssignment(),
paymentForm.isInvoiceRequested());
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
return "redirect:" + checkoutUrl;
} catch (Exception e) {
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(optionalReservation, 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(),
optionalReservation.get().getVatStatus());
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
ticketReservation.getEmail(), ticketReservation.getBillingAddress(), ticketReservation.getCustomerReference(), locale);
return "redirect:" + checkoutUrl;
} catch (Exception e) {
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
Expand Down Expand Up @@ -620,9 +589,9 @@ private boolean isCaptchaInvalid(int cost, PaymentProxy paymentMethod, HttpServl
&& !recaptchaService.checkRecaptcha(request);
}

private void assignTickets(String eventName, String reservationId, PaymentForm paymentForm, BindingResult bindingResult, HttpServletRequest request, boolean preAssign, boolean skipValidation) {
if(!paymentForm.isPostponeAssignment()) {
paymentForm.getTickets().forEach((ticketId, owner) -> {
private void assignTickets(String eventName, String reservationId, ContactAndTicketsForm contactAndTicketsForm, BindingResult bindingResult, HttpServletRequest request, boolean preAssign, boolean skipValidation) {
if(!contactAndTicketsForm.isPostponeAssignment()) {
contactAndTicketsForm.getTickets().forEach((ticketId, owner) -> {
if (preAssign) {
Optional<Errors> bindingResultOptional = skipValidation ? Optional.empty() : Optional.of(bindingResult);
ticketHelper.preAssignTicket(eventName, reservationId, ticketId, owner, bindingResultOptional, request, (tr) -> {
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/alfio/controller/api/ReservationApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package alfio.controller.api;

import alfio.controller.api.support.TicketHelper;
import alfio.controller.form.PaymentForm;
import alfio.controller.form.ContactAndTicketsForm;
import alfio.controller.form.UpdateTicketOwnerForm;
import alfio.manager.EuVatChecker;
import alfio.manager.TicketReservationManager;
Expand Down Expand Up @@ -128,18 +128,18 @@ public ResponseEntity<Boolean> resetVat(@PathVariable("eventName") String eventN
@Transactional
public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String eventName,
@PathVariable("reservationId") String reservationId,
PaymentForm paymentForm,
ContactAndTicketsForm contactAndTicketsForm,
Locale locale,
HttpServletRequest request) {

String country = paymentForm.getVatCountryCode();
/*String country = contactAndTicketsForm.getVatCountryCode();
Optional<Triple<Event, TicketReservation, VatDetail>> vatDetail;
try {
vatDetail = eventRepository.findOptionalByShortName(eventName)
.flatMap(e -> ticketReservationRepository.findOptionalReservationById(reservationId).map(r -> Pair.of(e, r)))
.filter(e -> EnumSet.of(INCLUDED, NOT_INCLUDED).contains(e.getKey().getVatStatus()))
.filter(e -> vatChecker.isVatCheckingEnabledFor(e.getKey().getOrganizationId()))
.flatMap(e -> vatChecker.checkVat(paymentForm.getVatNr(), country, e.getKey().getOrganizationId()).map(vd -> Triple.of(e.getLeft(), e.getRight(), vd)));
.flatMap(e -> vatChecker.checkVat(contactAndTicketsForm.getVatNr(), country, e.getKey().getOrganizationId()).map(vd -> Triple.of(e.getLeft(), e.getRight(), vd)));
} catch (IllegalStateException e) {
return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE);
}
Expand All @@ -151,13 +151,13 @@ public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String
VatDetail vd = t.getRight();
String billingAddress = vd.getName() + "\n" + vd.getAddress();
PriceContainer.VatStatus vatStatus = determineVatStatus(t.getLeft().getVatStatus(), t.getRight().isVatExempt());
ticketReservationRepository.updateBillingData(vatStatus, vd.getVatNr(), country, paymentForm.isInvoiceRequested(), reservationId);
ticketReservationRepository.updateBillingData(vatStatus, vd.getVatNr(), country, contactAndTicketsForm.isInvoiceRequested(), reservationId);
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, t.getLeft(), Locale.forLanguageTag(t.getMiddle().getUserLanguage()));
ticketReservationRepository.addReservationInvoiceOrReceiptModel(reservationId, Json.toJson(orderSummary));
ticketReservationRepository.updateTicketReservation(reservationId, t.getMiddle().getStatus().name(), paymentForm.getEmail(),
paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), locale.getLanguage(), billingAddress, null,
Optional.ofNullable(paymentForm.getPaymentMethod()).map(PaymentProxy::name).orElse(null), paymentForm.getCustomerReference());
paymentForm.getTickets().forEach((ticketId, owner) -> {
ticketReservationRepository.updateTicketReservation(reservationId, t.getMiddle().getStatus().name(), contactAndTicketsForm.getEmail(),
contactAndTicketsForm.getFullName(), contactAndTicketsForm.getFirstName(), contactAndTicketsForm.getLastName(), locale.getLanguage(), billingAddress, null,
Optional.ofNullable(contactAndTicketsForm.getPaymentMethod()).map(PaymentProxy::name).orElse(null), contactAndTicketsForm.getCustomerReference());
contactAndTicketsForm.getTickets().forEach((ticketId, owner) -> {
if(isNotEmpty(owner.getEmail()) && ((isNotEmpty(owner.getFirstName()) && isNotEmpty(owner.getLastName())) || isNotEmpty(owner.getFullName()))) {
ticketHelper.preAssignTicket(eventName, reservationId, ticketId, owner, Optional.empty(), request, (tr) -> {}, Optional.empty());
}
Expand All @@ -173,7 +173,8 @@ public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String
return new ResponseEntity<VatDetail>(HttpStatus.BAD_REQUEST);
}
})
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));*/
return null;
}

private static PriceContainer.VatStatus determineVatStatus(PriceContainer.VatStatus current, boolean isVatExempt) {
Expand Down
Loading

0 comments on commit 9343b0e

Please sign in to comment.