Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saferpay integration #944

Merged
merged 7 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.controller.payment;

import alfio.manager.TicketReservationManager;
import alfio.manager.payment.saferpay.PaymentPageInitializeRequestBuilder;
import alfio.repository.EventRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.util.UriComponentsBuilder;

@Controller
@RequiredArgsConstructor
public class SaferpayCallbackController {

private final TicketReservationManager ticketReservationManager;
private final EventRepository eventRepository;

@GetMapping(PaymentPageInitializeRequestBuilder.CANCEL_URL_TEMPLATE)
public String saferpayCancel(@PathVariable("eventName") String eventName,
@PathVariable("reservationId") String reservationId) {
var optionalEvent = eventRepository.findOptionalByShortName(eventName);
if(optionalEvent.isEmpty()) {
return "redirect:/";
}
var event = optionalEvent.get();
var optionalReservation = ticketReservationManager.findByIdForEvent(reservationId, event.getId());
if(optionalReservation.isEmpty()) {
return "redirect:/event/"+eventName;
}
var optionalResult = ticketReservationManager.forceTransactionCheck(event, optionalReservation.get());
if(optionalResult.isEmpty()) {
// there's no transaction available.
return "redirect:/event/"+eventName;
}
return "redirect:" + UriComponentsBuilder.fromPath(PaymentPageInitializeRequestBuilder.SUCCESS_URL_TEMPLATE)
.buildAndExpand(eventName, reservationId).toUriString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.controller.payment.api.saferpay;

import alfio.manager.TicketReservationManager;
import alfio.manager.payment.saferpay.PaymentPageInitializeRequestBuilder;
import alfio.model.transaction.PaymentContext;
import alfio.model.transaction.PaymentProxy;
import alfio.repository.EventRepository;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@AllArgsConstructor
public class SaferpayPaymentWebhookController {
private final TicketReservationManager ticketReservationManager;
private final EventRepository eventRepository;

@GetMapping(PaymentPageInitializeRequestBuilder.WEBHOOK_URL_TEMPLATE)
ResponseEntity<String> handleTransactionNotification(@PathVariable("eventShortName") String eventName,
@PathVariable("reservationId") String reservationId) {
return eventRepository.findOptionalByShortName(eventName)
.map(event -> {
var result = ticketReservationManager.processTransactionWebhook("", null, PaymentProxy.SAFERPAY,
Map.of("eventName", eventName, "reservationId", reservationId), new PaymentContext(event, reservationId));
if(result.isSuccessful()) {
return ResponseEntity.ok("OK");
} else if(result.isError()) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result.getReason());
}
return ResponseEntity.ok(result.getReason());
})
.orElseGet(() -> ResponseEntity.badRequest().body("NOK"));
}
}
5 changes: 5 additions & 0 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,10 @@ public int maxAmountOfTicketsForCategory(EventAndOrganizationId eventAndOrganiza
}
return configurationManager.getFor(MAX_AMOUNT_OF_TICKETS_BY_RESERVATION, ConfigurationLevel.ticketCategory(eventAndOrganizationId, ticketCategoryId)).getValueAsIntOrDefault(5);
}

public Optional<TicketReservation> findByIdForEvent(String reservationId, int eventId) {
return ticketReservationRepository.findOptionalReservationByIdAndEventId(reservationId, eventId);
}

public Optional<TicketReservation> findById(String reservationId) {
return ticketReservationRepository.findOptionalReservationById(reservationId);
Expand Down Expand Up @@ -2122,6 +2126,7 @@ public Optional<PaymentResult> forceTransactionCheck(Event event, TicketReservat
switch(paymentWebhookResult.getType()) {
case FAILED:
case REJECTED:
case CANCELLED:
return PaymentResult.failed(paymentWebhookResult.getReason());
case NOT_RELEVANT:
case ERROR:
Expand Down
Loading