Skip to content

Commit

Permalink
better expose the ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS #657
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jul 21, 2019
1 parent ac5f4f0 commit be33756
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static class InvoicingConfiguration {
@Getter
public static class CaptchaConfiguration {
private final boolean captchaForTicketSelection;
private final boolean captchaForOfflinePaymentEnabled;
private final boolean captchaForOfflinePaymentAndFree;
private final String recaptchaApiKey;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public ResponseEntity<EventWithAdditionalInfo> getEvent(@PathVariable("eventName
recaptchaApiKey = configurationsValues.get(RECAPTCHA_API_KEY).getValueOrDefault(null);
}
//
boolean captchaForOfflinePaymentEnabled = configurationManager.isRecaptchaForOfflinePaymentEnabled(event);
var captchaConf = new EventWithAdditionalInfo.CaptchaConfiguration(captchaForTicketSelection, captchaForOfflinePaymentEnabled, recaptchaApiKey);
boolean captchaForOfflinePaymentAndFreeEnabled = configurationManager.isRecaptchaForOfflinePaymentAndFreeEnabled(event);
var captchaConf = new EventWithAdditionalInfo.CaptchaConfiguration(captchaForTicketSelection, captchaForOfflinePaymentAndFreeEnabled, recaptchaApiKey);


//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public ResponseEntity<ValidatedResponse<ReservationPaymentResult>> confirmOvervi
return buildReservationPaymentStatus(bindingResult);
}

if(isCaptchaInvalid(reservationCost.getPriceWithVAT(), paymentForm.getPaymentMethod(), request, event)) {
if(isCaptchaInvalid(reservationCost.getPriceWithVAT(), paymentForm.getPaymentMethod(), paymentForm.getCaptcha(), request, event)) {
log.debug("captcha validation failed.");
bindingResult.reject(ErrorsCode.STEP_2_CAPTCHA_VALIDATION_FAILED);
}
Expand Down Expand Up @@ -678,9 +678,9 @@ private static PriceContainer.VatStatus determineVatStatus(PriceContainer.VatSta
}


private boolean isCaptchaInvalid(int cost, PaymentProxy paymentMethod, HttpServletRequest request, EventAndOrganizationId event) {
private boolean isCaptchaInvalid(int cost, PaymentProxy paymentMethod, String recaptchaResponse, HttpServletRequest request, EventAndOrganizationId event) {
return (cost == 0 || paymentMethod == PaymentProxy.OFFLINE || paymentMethod == PaymentProxy.ON_SITE)
&& configurationManager.isRecaptchaForOfflinePaymentEnabled(event)
&& !recaptchaService.checkRecaptcha(null, request);
&& configurationManager.isRecaptchaForOfflinePaymentAndFreeEnabled(event)
&& !recaptchaService.checkRecaptcha(recaptchaResponse, request);
}
}
1 change: 1 addition & 0 deletions src/main/java/alfio/controller/form/PaymentForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class PaymentForm implements Serializable {
private Boolean termAndConditionsAccepted;
private Boolean privacyPolicyAccepted;
private String hmac;
private String captcha;


public void validate(BindingResult bindingResult, Event event, TotalPrice reservationCost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public PaymentResult doPayment(PaymentSpecification spec) {
}
Map<String, Object> model = new HashMap<>();
model.put("delayForOfflinePayment", Math.max(1, delay.orElse( 0 )));
boolean recaptchaEnabled = configurationManager.isRecaptchaForOfflinePaymentEnabled(event);
boolean recaptchaEnabled = configurationManager.isRecaptchaForOfflinePaymentAndFreeEnabled(event);
model.put("captchaRequestedForOffline", recaptchaEnabled);
if(recaptchaEnabled) {
model.put("recaptchaApiKey", configurationManager.getStringConfigValue(Configuration.getSystemConfiguration(RECAPTCHA_API_KEY), null));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/alfio/manager/payment/OnSiteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@

import alfio.manager.support.PaymentResult;
import alfio.manager.system.ConfigurationManager;
import alfio.model.system.Configuration;
import alfio.model.transaction.*;
import alfio.repository.TransactionRepository;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

import static alfio.manager.TicketReservationManager.NOT_YET_PAID_TRANSACTION_ID;
import static alfio.model.system.ConfigurationKeys.ON_SITE_ENABLED;
import static alfio.model.system.ConfigurationKeys.RECAPTCHA_API_KEY;

@Component
@Log4j2
Expand All @@ -53,4 +58,14 @@ public PaymentResult doPayment(PaymentSpecification spec) {
return PaymentResult.successful(NOT_YET_PAID_TRANSACTION_ID);
}

@Override
public Map<String, ?> getModelOptions(PaymentContext context) {
Map<String, Object> model = new HashMap<>();
boolean recaptchaEnabled = configurationManager.isRecaptchaForOfflinePaymentAndFreeEnabled(context.getEvent());
model.put("captchaRequestedForOffline", recaptchaEnabled);
if(recaptchaEnabled) {
model.put("recaptchaApiKey", configurationManager.getStringConfigValue(Configuration.getSystemConfiguration(RECAPTCHA_API_KEY), null));
}
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public boolean hasAllConfigurationsForInvoice(EventAndOrganizationId event) {
}


public boolean isRecaptchaForOfflinePaymentEnabled(EventAndOrganizationId event) {
public boolean isRecaptchaForOfflinePaymentAndFreeEnabled(EventAndOrganizationId event) {
var conf = getFor(event, Set.of(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS, RECAPTCHA_API_KEY));
return conf.get(ENABLE_CAPTCHA_FOR_OFFLINE_PAYMENTS).getValueAsBooleanOrDefault(false) &&
conf.get(RECAPTCHA_API_KEY).getValueOrDefault(null) != null;
Expand Down

0 comments on commit be33756

Please sign in to comment.