Skip to content

Commit

Permalink
do not cancel reservation if refund fails
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Apr 3, 2020
1 parent 7d4cac1 commit caa3a32
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ public Result<TransactionAndPaymentInfo> getPaymentInfo(@PathVariable("eventName
public Result<Boolean> removeReservation(@PathVariable("eventName") String eventName, @PathVariable("reservationId") String reservationId, @RequestParam("refund") boolean refund,
@RequestParam(value = "notify", defaultValue = "false") boolean notify,
Principal principal) {
adminReservationManager.removeReservation(eventName, reservationId, refund, notify, principal.getName());
return Result.success(true);
return adminReservationManager.removeReservation(eventName, reservationId, refund, notify, principal.getName());
}

@PostMapping("/event/{eventName}/{reservationId}/credit")
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/alfio/manager/AdminReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,12 @@ public Result<TransactionAndPaymentInfo> getPaymentInfo(String eventName, String
}

@Transactional
public void removeReservation(String eventName, String reservationId, boolean refund, boolean notify, String username) {
removeReservation(eventName, reservationId, refund, notify, username, true)
.ifSuccess(pair -> markAsCancelled(pair.getRight(), username, pair.getLeft().getId()));
public Result<Boolean> removeReservation(String eventName, String reservationId, boolean refund, boolean notify, String username) {
return removeReservation(eventName, reservationId, refund, notify, username, true)
.map(pair -> {
markAsCancelled(pair.getRight(), username, pair.getLeft().getId());
return true;
});
}

@Transactional
Expand All @@ -666,22 +669,26 @@ public void creditReservation(String eventName, String reservationId, boolean re
}

private Result<Pair<Event, TicketReservation>> removeReservation(String eventName, String reservationId, boolean refund, boolean notify, String username, boolean removeReservation) {
return loadReservation(eventName, reservationId, username).map(res -> {
return loadReservation(eventName, reservationId, username).flatMap(res -> {
Event e = res.getRight();
TicketReservation reservation = res.getLeft();
List<Ticket> tickets = res.getMiddle();

if(refund && reservation.getPaymentMethod() != null && reservation.getPaymentMethod().isSupportRefund()) {
//fully refund
boolean refundResult = paymentManager.refund(reservation, e, null, username);
if(!refundResult) {
return Result.error(ErrorCode.custom("refund.failed", "Cannot perform refund"));
}
}

specialPriceRepository.resetToFreeAndCleanupForReservation(List.of(reservationId));

removeTicketsFromReservation(reservation, e, tickets.stream().map(Ticket::getId).collect(toList()), notify, username, removeReservation, false);

additionalServiceItemRepository.updateItemsStatusWithReservationUUID(reservation.getId(), AdditionalServiceItem.AdditionalServiceItemStatus.CANCELLED);

if(refund && reservation.getPaymentMethod() != null && reservation.getPaymentMethod().isSupportRefund()) {
//fully refund
paymentManager.refund(reservation, e, null, username);
}
return Pair.of(e, reservation);
return Result.success(Pair.of(e, reservation));
});
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/alfio/manager/PaymentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,21 @@ public boolean refund(TicketReservation reservation, Event event, Integer amount
.map(paymentProvider -> ((RefundRequest)paymentProvider).refund(transaction, event, amount))
.orElse(false);

Map<String, Object> changes = Map.of(
"refund", amount != null ? amount.toString() : "full",
"paymentMethod", reservation.getPaymentMethod().toString()
);
if(res) {
Map<String, Object> changes = new HashMap<>();
changes.put("refund", amount != null ? amount.toString() : "full");
changes.put("paymentMethod", reservation.getPaymentMethod().toString());
auditingRepository.insert(reservation.getId(), userRepository.findIdByUserName(username).orElse(null),
event.getId(),
Audit.EventType.REFUND, new Date(), Audit.EntityType.RESERVATION, reservation.getId(),
Collections.singletonList(changes));
extensionManager.handleRefund(event, reservation, getInfo(reservation, event));
} else {
auditingRepository.insert(reservation.getId(), userRepository.findIdByUserName(username).orElse(null),
event.getId(),
Audit.EventType.REFUND_ATTEMPT_FAILED, new Date(), Audit.EntityType.RESERVATION, reservation.getId(),
Collections.singletonList(changes));
}

return res;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/alfio/manager/payment/BaseStripeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class BaseStripeManager {

static final String STRIPE_MANAGER_TYPE_KEY = "stripeManagerType";
static final String SUCCEEDED = "succeeded";
static final String PENDING = "pending";
private final ConfigurationManager configurationManager;
private final ConfigurationRepository configurationRepository;
private final TicketRepository ticketRepository;
Expand Down Expand Up @@ -260,8 +261,9 @@ boolean refund(Transaction transaction, Event event, Integer amountToRefund) {
if(requestOptionsOptional.isPresent()) {
RequestOptions options = requestOptionsOptional.get();
Refund r = Refund.create(params, options);
if(SUCCEEDED.equals(r.getStatus())) {
log.info("Stripe: refund for payment {} executed with success for amount: {}", chargeId, amountOrFull);
boolean pending = PENDING.equals(r.getStatus());
if(SUCCEEDED.equals(r.getStatus()) || pending) {
log.info("Stripe: refund for payment {} {} for amount: {}", chargeId, pending ? "registered": "executed with success", amountOrFull);
return true;
} else {
log.warn("Stripe: was not able to refund payment with id {}, returned status is not 'succeded' but {}", chargeId, r.getStatus());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/alfio/model/Audit.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum EventType {
PAYMENT_CONFIRMED,
PAYMENT_ALREADY_CONFIRMED,
REFUND,
REFUND_ATTEMPT_FAILED,
CHECK_IN,
MANUAL_CHECK_IN,
REVERT_CHECK_IN,
Expand All @@ -71,6 +72,7 @@ public enum EventType {
AUTOMATIC_PAYMENT_CONFIRMATION,
AUTOMATIC_PAYMENT_CONFIRMATION_FAILED,
DYNAMIC_DISCOUNT_CODE_CREATED

}

private final String reservationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

angular.module('adminApplication').component('reservationCancel', {
controller: ['AdminReservationService', 'EventService', ReservationCancelCtrl],
controller: ['AdminReservationService', 'EventService', 'NotificationHandler', ReservationCancelCtrl],
templateUrl: '../resources/js/admin/feature/reservation-cancel/reservation-cancel.html',
bindings: {
event: '<',
Expand All @@ -14,7 +14,7 @@
});


function ReservationCancelCtrl(AdminReservationService, EventService) {
function ReservationCancelCtrl(AdminReservationService, EventService, NotificationHandler) {
var ctrl = this;

ctrl.confirmRemove = confirmRemove;
Expand All @@ -32,8 +32,12 @@

function confirmRemove() {
ctrl.submitted = true;
return EventService.cancelReservation(ctrl.event.shortName, ctrl.reservationId, ctrl.refund, ctrl.notify, ctrl.credit).then(function() {
ctrl.onSuccess();
return EventService.cancelReservation(ctrl.event.shortName, ctrl.reservationId, ctrl.refund, ctrl.notify, ctrl.credit).then(function(response) {
if(response.data.success) {
ctrl.onSuccess();
} else {
NotificationHandler.showError(response.data.firstErrorOrNull.description);
}
}).finally(function() {
ctrl.submitted = false;
});
Expand Down

0 comments on commit caa3a32

Please sign in to comment.