use of alfio.model.transaction.Transaction in project alf.io by alfio-event.
the class PaymentManager method refund.
public boolean refund(TicketReservation reservation, Event event, Optional<Integer> amount, String username) {
Transaction transaction = transactionRepository.loadByReservationId(reservation.getId());
boolean res;
switch(reservation.getPaymentMethod()) {
case PAYPAL:
res = paypalManager.refund(transaction, event, amount);
break;
case STRIPE:
res = stripeManager.refund(transaction, event, amount);
break;
default:
throw new IllegalStateException("Cannot refund ");
}
if (res) {
Map<String, Object> changes = new HashMap<>();
changes.put("refund", amount.map(Object::toString).orElse("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));
}
return res;
}
use of alfio.model.transaction.Transaction in project alf.io by alfio-event.
the class PaymentManager method getInfo.
TransactionAndPaymentInfo getInfo(TicketReservation reservation, Event event) {
Optional<TransactionAndPaymentInfo> maybeTransaction = transactionRepository.loadOptionalByReservationId(reservation.getId()).map(transaction -> {
switch(reservation.getPaymentMethod()) {
case PAYPAL:
return new TransactionAndPaymentInfo(reservation.getPaymentMethod(), transaction, paypalManager.getInfo(transaction, event).orElse(null));
case STRIPE:
return new TransactionAndPaymentInfo(reservation.getPaymentMethod(), transaction, stripeManager.getInfo(transaction, event).orElse(null));
default:
return new TransactionAndPaymentInfo(reservation.getPaymentMethod(), transaction, new PaymentInformation(reservation.getPaidAmount(), null, String.valueOf(transaction.getGatewayFee()), String.valueOf(transaction.getPlatformFee())));
}
});
maybeTransaction.ifPresent(info -> {
try {
Transaction transaction = info.getTransaction();
String transactionId = transaction.getTransactionId();
PaymentInformation paymentInformation = info.getPaymentInformation();
if (paymentInformation != null) {
transactionRepository.updateFees(transactionId, reservation.getId(), safeParseLong(paymentInformation.getPlatformFee()), safeParseLong(paymentInformation.getFee()));
}
} catch (Exception e) {
log.warn("cannot update fees", e);
}
});
return maybeTransaction.orElseGet(() -> new TransactionAndPaymentInfo(reservation.getPaymentMethod(), null, new PaymentInformation(reservation.getPaidAmount(), null, null, null)));
}
use of alfio.model.transaction.Transaction in project alf.io by alfio-event.
the class EventApiController method exportLines.
private Stream<String[]> exportLines(String eventName, Principal principal, List<String> fields, Map<Integer, TicketCategory> categoriesMap, ZoneId eventZoneId) {
var username = principal.getName();
var eInvoicingEnabled = configurationManager.isItalianEInvoicingEnabled(eventManager.getEventAndOrganizationId(eventName, username));
return eventManager.findAllConfirmedTicketsForCSV(eventName, username).stream().map(trs -> {
Ticket t = trs.getTicket();
var currencyCode = t.getCurrencyCode();
TicketReservation reservation = trs.getTicketReservation();
List<String> line = new ArrayList<>();
if (fields.contains("ID")) {
line.add(t.getUuid());
}
if (fields.contains("Category")) {
line.add(categoriesMap.get(t.getCategoryId()).getName());
}
if (fields.contains("Event")) {
line.add(eventName);
}
if (fields.contains("Status")) {
line.add(t.getStatus().toString());
}
if (fields.contains("OriginalPrice")) {
line.add(MonetaryUtil.centsToUnit(t.getSrcPriceCts(), currencyCode).toString());
}
if (fields.contains("PaidPrice")) {
line.add(MonetaryUtil.centsToUnit(t.getFinalPriceCts(), currencyCode).toString());
}
if (fields.contains("Discount")) {
line.add(MonetaryUtil.centsToUnit(t.getDiscountCts(), currencyCode).toString());
}
if (fields.contains("VAT")) {
line.add(MonetaryUtil.centsToUnit(t.getVatCts(), currencyCode).toString());
}
if (fields.contains("ReservationID")) {
line.add(t.getTicketsReservationId());
}
if (fields.contains("Full Name")) {
line.add(t.getFullName());
}
if (fields.contains("First Name")) {
line.add(t.getFirstName());
}
if (fields.contains("Last Name")) {
line.add(t.getLastName());
}
if (fields.contains("E-Mail")) {
line.add(t.getEmail());
}
if (fields.contains("Locked")) {
line.add(String.valueOf(t.getLockedAssignment()));
}
if (fields.contains("Language")) {
line.add(String.valueOf(t.getUserLanguage()));
}
if (fields.contains("Confirmation")) {
line.add(reservation.getConfirmationTimestamp().withZoneSameInstant(eventZoneId).toString());
}
if (fields.contains("Billing Address")) {
line.add(reservation.getBillingAddress());
}
if (fields.contains("Country Code")) {
line.add(reservation.getVatCountryCode());
}
boolean paymentIdRequested = fields.contains("Payment ID");
boolean paymentGatewayRequested = fields.contains(PAYMENT_METHOD);
if ((paymentIdRequested || paymentGatewayRequested)) {
Optional<Transaction> transaction = trs.getTransaction();
if (paymentIdRequested) {
line.add(defaultString(transaction.map(Transaction::getPaymentId).orElse(null), transaction.map(Transaction::getTransactionId).orElse("")));
}
if (paymentGatewayRequested) {
line.add(transaction.map(tr -> tr.getPaymentProxy().name()).orElse(""));
}
}
if (eInvoicingEnabled) {
var billingDetails = trs.getBillingDetails();
var optionalInvoicingData = Optional.ofNullable(billingDetails.getInvoicingAdditionalInfo()).map(TicketReservationInvoicingAdditionalInfo::getItalianEInvoicing);
if (fields.contains(FISCAL_CODE)) {
line.add(optionalInvoicingData.map(ItalianEInvoicing::getFiscalCode).orElse(""));
}
if (fields.contains(REFERENCE_TYPE)) {
line.add(optionalInvoicingData.map(ItalianEInvoicing::getReferenceTypeAsString).orElse(""));
}
if (fields.contains("Addressee Code")) {
line.add(optionalInvoicingData.map(ItalianEInvoicing::getAddresseeCode).orElse(""));
}
if (fields.contains("PEC")) {
line.add(optionalInvoicingData.map(ItalianEInvoicing::getPec).orElse(""));
}
}
// obviously not optimized
Map<String, String> additionalValues = ticketFieldRepository.findAllValuesForTicketId(t.getId());
Predicate<String> contains = FIXED_FIELDS::contains;
fields.stream().filter(contains.negate()).filter(f -> f.startsWith(CUSTOM_FIELDS_PREFIX)).forEachOrdered(field -> {
String customFieldName = field.substring(CUSTOM_FIELDS_PREFIX.length());
line.add(additionalValues.getOrDefault(customFieldName, "").replace("\"", ""));
});
return line.toArray(new String[0]);
});
}
use of alfio.model.transaction.Transaction in project alf.io by alfio-event.
the class BaseStripeManager method refund.
// https://stripe.com/docs/api#create_refund
boolean refund(Transaction transaction, PurchaseContext purchaseContext, Integer amountToRefund) {
Optional<Integer> amount = Optional.ofNullable(amountToRefund);
String chargeId = transaction.getTransactionId();
try {
String amountOrFull = amount.map(p -> MonetaryUtil.formatCents(p, transaction.getCurrency())).orElse("full");
log.info("Stripe: trying to do a refund for payment {} with amount: {}", chargeId, amountOrFull);
Map<String, Object> params = new HashMap<>();
params.put("charge", chargeId);
amount.ifPresent(a -> params.put("amount", a));
if (transaction.getPlatformFee() > 0 && isConnectEnabled(new PaymentContext(purchaseContext))) {
params.put("refund_application_fee", true);
}
Optional<RequestOptions> requestOptionsOptional = options(purchaseContext);
if (requestOptionsOptional.isPresent()) {
RequestOptions options = requestOptionsOptional.get();
Refund r = Refund.create(params, options);
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());
return false;
}
}
return false;
} catch (StripeException e) {
log.warn("Stripe: was not able to refund payment with id " + chargeId, e);
return false;
}
}
Aggregations