Skip to content

Commit

Permalink
#464 make the free flow work: still WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jun 26, 2018
1 parent a8c0fe8 commit 589b7a0
Show file tree
Hide file tree
Showing 5 changed files with 517 additions and 21 deletions.
101 changes: 83 additions & 18 deletions src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,45 @@ public String validateToOverview(@PathVariable("eventName") String eventName, @P

Event event = eventOptional.get();


final TotalPrice reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
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);
}

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

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

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

//FIXME persist data
//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);
//

// FIXME validate!
//paymentForm.validate(bindingResult, event, ticketFieldRepository.findAdditionalFieldsForEvent(event.getId()), companyVatChecked);

if(bindingResult.hasErrors()) {
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return "redirect:/event/" + eventName + "/reservation/" + reservationId + "/book";
}
ticketReservationRepository.updateValidationStatus(reservationId, true);


//FIXME implement validation
return "redirect:/event/" + eventName + "/reservation/" + reservationId + "/overview";
}

Expand All @@ -300,7 +334,29 @@ public String showOverview(@PathVariable("eventName") String eventName, @PathVar
return eventRepository.findOptionalByShortName(eventName)
.map(event -> ticketReservationManager.findById(reservationId)
.map(reservation -> {
if (reservation.getStatus() != TicketReservationStatus.PENDING) {
return redirectReservation(Optional.of(reservation), eventName, reservationId);
}
TicketReservationAdditionalInfo additionalInfo = ticketReservationRepository.getAdditionalInfo(reservationId);
if (!additionalInfo.hasBeenValidated()) {
return "redirect:/event/" + eventName + "/reservation/" + reservationId + "/book";
}


OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, event, locale);

addDelayForOffline(model, event);


List<PaymentProxy> activePaymentMethods = paymentManager.getPaymentMethods(event.getOrganizationId())
.stream()
.filter(p -> TicketReservationManager.isValidPaymentMethod(p, event, configurationManager))
.map(PaymentManager.PaymentMethod::getPaymentProxy)
.collect(toList());

model.addAttribute("multiplePaymentMethods" , activePaymentMethods.size() > 1 )
.addAttribute("activePaymentMethods", activePaymentMethods);

model.addAttribute("orderSummary", orderSummary)
.addAttribute("reservationId", reservationId)
.addAttribute("reservation", reservation)
Expand Down Expand Up @@ -493,29 +549,27 @@ public String handleReservation(@PathVariable("eventName") String eventName,
}

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(!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()) {
ticketReservationRepository.updateTicketReservation(reservationId, ticketReservation.getStatus().name(), paymentForm.getEmail(),
paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), locale.getLanguage(), paymentForm.getBillingAddress(), null, null, paymentForm.getCustomerReference());
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, true);
SessionUtil.addToFlash(bindingResult, redirectAttributes);
return redirectReservation(optionalReservation, eventName, reservationId);
return redirectReservation(Optional.of(ticketReservation), eventName, reservationId);
}
}

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

//handle paypal redirect!
boolean invoiceRequested = reservationCost.getPriceWithVAT() > 0 && ticketReservation.isInvoiceRequested();
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(), paymentForm.getCustomerReference(), locale, paymentForm.isPostponeAssignment(),
paymentForm.isInvoiceRequested());
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
return "redirect:" + checkoutUrl;
} catch (Exception e) {
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
Expand All @@ -534,7 +588,7 @@ public String handleReservation(@PathVariable("eventName") String eventName,
paymentForm.getVatCountryCode(),
paymentForm.getVatNr(),
optionalReservation.get().getVatStatus());
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, true, false);
return "redirect:" + checkoutUrl;
} catch (Exception e) {
bindingResult.reject(ErrorsCode.STEP_2_PAYMENT_REQUEST_CREATION);
Expand All @@ -544,10 +598,10 @@ public String handleReservation(@PathVariable("eventName") String eventName,
}
//

final PaymentResult status = ticketReservationManager.confirm(paymentForm.getToken(), paymentForm.getPaypalPayerID(), event, reservationId, paymentForm.getEmail(),
customerName, locale, paymentForm.getBillingAddress(), paymentForm.getCustomerReference(), reservationCost, SessionUtil.retrieveSpecialPriceSessionId(request),
Optional.ofNullable(paymentForm.getPaymentMethod()), paymentForm.isInvoiceRequested(), paymentForm.getVatCountryCode(),
paymentForm.getVatNr(), optionalReservation.get().getVatStatus(), paymentForm.getTermAndConditionsAccepted(), Optional.ofNullable(paymentForm.getPrivacyPolicyAccepted()).orElse(false));
final PaymentResult status = ticketReservationManager.confirm(paymentForm.getToken(), paymentForm.getPaypalPayerID(), event, reservationId, ticketReservation.getEmail(),
customerName, locale, ticketReservation.getBillingAddress(), ticketReservation.getCustomerReference(), reservationCost, SessionUtil.retrieveSpecialPriceSessionId(request),
Optional.ofNullable(paymentForm.getPaymentMethod()), invoiceRequested, ticketReservation.getVatCountryCode(),
ticketReservation.getVatNr(), ticketReservation.getVatStatus(), paymentForm.getTermAndConditionsAccepted(), Optional.ofNullable(paymentForm.getPrivacyPolicyAccepted()).orElse(false));

if(!status.isSuccessful()) {
String errorMessageCode = status.getErrorCode().get();
Expand All @@ -564,7 +618,7 @@ public String handleReservation(@PathVariable("eventName") String eventName,
//

if(paymentForm.getPaymentMethod() != PaymentProxy.PAYPAL) {
assignTickets(eventName, reservationId, paymentForm, bindingResult, request, paymentForm.getPaymentMethod() == PaymentProxy.OFFLINE, false);
//assignTickets(eventName, reservationId, paymentForm, bindingResult, request, paymentForm.getPaymentMethod() == PaymentProxy.OFFLINE, false);
}

return "redirect:/event/" + eventName + "/reservation/" + reservationId + "/success";
Expand Down Expand Up @@ -660,4 +714,15 @@ private Optional<String> checkReservation(PaymentForm paymentForm, String eventN
}
return Optional.empty();
}

private void addDelayForOffline(Model model, Event event) {
try {
model.addAttribute("delayForOfflinePayment", Math.max(1, TicketReservationManager.getOfflinePaymentWaitingPeriod(event, configurationManager)));
} catch (TicketReservationManager.OfflinePaymentException e) {
if(event.getAllowedPaymentProxies().contains(PaymentProxy.OFFLINE)) {
log.error("Already started event {} has been found with OFFLINE payment enabled" , event.getDisplayName() , e);
}
model.addAttribute("delayForOfflinePayment", 0);
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/alfio/i18n/public.properties
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,5 @@ reservation.add-company-billing-details=Company registration (a {0} number is re
error.STEP_2_EMPTY_BILLING_ADDRESS=Billing address is mandatory
common.customer-reference=Purchase Order (PO) / Internal Reference
error.STEP_2_INVALID_VAT=The VAT Number is not valid. Please check the input parameter.
error.vatVIESDown=An error occurred contacting the EU VIES vat checker. Please try again.
error.vatVIESDown=An error occurred contacting the EU VIES vat checker. Please try again.
common.back=
Loading

0 comments on commit 589b7a0

Please sign in to comment.