/
AdminPaymentsApiController.java
149 lines (138 loc) · 7.79 KB
/
AdminPaymentsApiController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* 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.api.admin;
import alfio.controller.api.support.PageAndContent;
import alfio.manager.AccessService;
import alfio.manager.PaymentManager;
import alfio.manager.PurchaseContextManager;
import alfio.manager.PurchaseContextSearchManager;
import alfio.manager.system.ConfigurationManager;
import alfio.model.PurchaseContext;
import alfio.model.ReservationPaymentDetail;
import alfio.model.modification.TransactionMetadataModification;
import alfio.model.system.ConfigurationKeys;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import static alfio.util.ExportUtils.exportExcel;
import static java.util.Objects.requireNonNullElse;
import static org.apache.commons.lang3.StringUtils.trimToNull;
@RestController
@RequestMapping("/admin/api/payments")
public class AdminPaymentsApiController {
private static final String[] EXPORT_COLUMNS = new String[] {
"ID",
"Name",
"Email",
"Type",
"Amount",
"Currency",
"Payment Date/Time",
"Notes"
};
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final PurchaseContextSearchManager purchaseContextSearchManager;
private final PurchaseContextManager purchaseContextManager;
private final PaymentManager paymentManager;
private final ConfigurationManager configurationManager;
private final AccessService accessService;
public AdminPaymentsApiController(PurchaseContextSearchManager purchaseContextSearchManager,
PurchaseContextManager purchaseContextManager,
PaymentManager paymentManager,
ConfigurationManager configurationManager,
AccessService accessService) {
this.purchaseContextSearchManager = purchaseContextSearchManager;
this.purchaseContextManager = purchaseContextManager;
this.paymentManager = paymentManager;
this.configurationManager = configurationManager;
this.accessService = accessService;
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/list")
PageAndContent<List<ReservationPaymentDetail>> getPaymentsForPurchaseContext(@PathVariable PurchaseContext.PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "search", required = false) String search,
Principal principal) {
return purchaseContextManager.findBy(purchaseContextType, publicIdentifier)
.map(purchaseContext -> {
accessService.checkOrganizationOwnership(principal, purchaseContext.getOrganizationId());
var res = purchaseContextSearchManager.findAllPaymentsFor(purchaseContext, page, search);
return new PageAndContent<>(res.getLeft(), res.getRight());
}).orElseGet(() -> new PageAndContent<>(List.of(), 0));
}
@PutMapping("/{purchaseContextType}/{publicIdentifier}/reservation/{reservationId}")
ResponseEntity<String> updateTransactionData(@PathVariable PurchaseContext.PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@RequestBody TransactionMetadataModification transactionMetadataModification,
Principal principal) {
try {
return ResponseEntity.of(purchaseContextManager.findBy(purchaseContextType, publicIdentifier)
.map(purchaseContext -> {
accessService.checkOrganizationOwnership(principal, purchaseContext.getOrganizationId());
var timestampModification = transactionMetadataModification.getTimestamp();
var timestamp = timestampModification != null ? timestampModification.toZonedDateTime(purchaseContext.getZoneId()) : null;
paymentManager.updateTransactionDetails(reservationId, transactionMetadataModification.getNotes(), timestamp, principal);
return "OK";
}));
} catch (IllegalArgumentException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/download")
void exportPayments(@PathVariable PurchaseContext.PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@RequestParam(value = "search", required = false) String search,
Principal principal,
HttpServletResponse response) throws IOException {
var purchaseContextOptional = purchaseContextManager.findBy(purchaseContextType, publicIdentifier);
if (purchaseContextOptional.isPresent()) {
var purchaseContext = purchaseContextOptional.get();
accessService.checkOrganizationOwnership(principal, purchaseContext.getOrganizationId());
boolean useInvoiceNumber = configurationManager.getFor(ConfigurationKeys.USE_INVOICE_NUMBER_AS_ID, purchaseContext.getConfigurationLevel())
.getValueAsBooleanOrDefault();
var data = purchaseContextSearchManager.findAllPaymentsForExport(purchaseContext, search)
.stream()
.map(d -> new String[] {
useInvoiceNumber ? requireNonNullElse(trimToNull(d.getInvoiceNumber()), "N/A") : d.getId(),
d.getFirstName() + " " + d.getLastName(),
d.getEmail(),
d.getPaymentMethod(),
d.getPaidAmount(),
d.getCurrencyCode(),
formatTimestamp(purchaseContext, d.getTransactionTimestamp()),
d.getTransactionNotes()
});
exportExcel(purchaseContext.getDisplayName()+" payments.xlsx", "Payments", EXPORT_COLUMNS, data, response);
} else {
response.setContentType("text/plain");
response.setStatus(HttpStatus.PRECONDITION_REQUIRED.value());
response.getWriter().write("No payments found");
}
}
private static String formatTimestamp(PurchaseContext purchaseContext, String timestamp) {
return ZonedDateTime.parse(timestamp)
.withZoneSameInstant(purchaseContext.getZoneId())
.format(DATE_TIME_FORMATTER);
}
}