use of com.paypal.base.rest.APIContext in project alf.io by alfio-event.
the class PaypalManager method refund.
public boolean refund(alfio.model.transaction.Transaction transaction, Event event, Optional<Integer> amount) {
String captureId = transaction.getTransactionId();
try {
APIContext apiContext = getApiContext(event);
String amountOrFull = amount.map(MonetaryUtil::formatCents).orElse("full");
log.info("Paypal: trying to do a refund for payment {} with amount: {}", captureId, amountOrFull);
Capture capture = Capture.get(apiContext, captureId);
RefundRequest refundRequest = new RefundRequest();
amount.ifPresent(a -> refundRequest.setAmount(new Amount(capture.getAmount().getCurrency(), formatCents(a))));
DetailedRefund res = capture.refund(apiContext, refundRequest);
log.info("Paypal: refund for payment {} executed with success for amount: {}", captureId, amountOrFull);
return true;
} catch (PayPalRESTException ex) {
log.warn("Paypal: was not able to refund payment with id " + captureId, ex);
return false;
}
}
use of com.paypal.base.rest.APIContext in project alf.io by alfio-event.
the class PaypalManager method getInfo.
Optional<PaymentInformation> getInfo(String paymentId, String transactionId, Event event, Supplier<String> platformFeeSupplier) {
try {
String refund = null;
APIContext apiContext = getApiContext(event);
// check for backward compatibility reason...
if (paymentId != null) {
// navigate in all refund objects and sum their amount
refund = Payment.get(apiContext, paymentId).getTransactions().stream().map(Transaction::getRelatedResources).flatMap(List::stream).filter(f -> f.getRefund() != null).map(RelatedResources::getRefund).map(Refund::getAmount).map(Amount::getTotal).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).toPlainString();
//
}
Capture c = Capture.get(apiContext, transactionId);
String gatewayFee = Optional.ofNullable(c.getTransactionFee()).map(Currency::getValue).map(BigDecimal::new).map(MonetaryUtil::unitToCents).map(String::valueOf).orElse(null);
return Optional.of(new PaymentInformation(c.getAmount().getTotal(), refund, gatewayFee, platformFeeSupplier.get()));
} catch (PayPalRESTException ex) {
log.warn("Paypal: error while fetching information for payment id " + transactionId, ex);
return Optional.empty();
}
}
use of com.paypal.base.rest.APIContext in project alf.io by alfio-event.
the class PaypalManager method createCheckoutRequest.
public String createCheckoutRequest(Event event, String reservationId, OrderSummary orderSummary, CustomerName customerName, String email, String billingAddress, Locale locale, boolean postponeAssignment, boolean invoiceRequested) throws Exception {
APIContext apiContext = getApiContext(event);
Optional<String> experienceProfileId = getOrCreateWebProfile(event, locale, apiContext);
List<Transaction> transactions = buildPaymentDetails(event, orderSummary, reservationId, locale);
String eventName = event.getShortName();
Payer payer = new Payer();
payer.setPaymentMethod("paypal");
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setTransactions(transactions);
RedirectUrls redirectUrls = new RedirectUrls();
String baseUrl = StringUtils.removeEnd(configurationManager.getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), ConfigurationKeys.BASE_URL)), "/");
String bookUrl = baseUrl + "/event/" + eventName + "/reservation/" + reservationId + "/book";
UriComponentsBuilder bookUrlBuilder = UriComponentsBuilder.fromUriString(bookUrl).queryParam("fullName", customerName.getFullName()).queryParam("firstName", customerName.getFirstName()).queryParam("lastName", customerName.getLastName()).queryParam("email", email).queryParam("billingAddress", billingAddress).queryParam("postponeAssignment", postponeAssignment).queryParam("invoiceRequested", invoiceRequested).queryParam("hmac", computeHMAC(customerName, email, billingAddress, event));
String finalUrl = bookUrlBuilder.toUriString();
redirectUrls.setCancelUrl(finalUrl + "&paypal-cancel=true");
redirectUrls.setReturnUrl(finalUrl + "&paypal-success=true");
payment.setRedirectUrls(redirectUrls);
experienceProfileId.ifPresent(payment::setExperienceProfileId);
Payment createdPayment = payment.create(apiContext);
TicketReservation reservation = ticketReservationRepository.findReservationById(reservationId);
// add 15 minutes of validity in case the paypal flow is slow
ticketReservationRepository.updateValidity(reservationId, DateUtils.addMinutes(reservation.getValidity(), 15));
if (!"created".equals(createdPayment.getState())) {
throw new Exception(createdPayment.getFailureReason());
}
// extract url for approval
return createdPayment.getLinks().stream().filter(l -> "approval_url".equals(l.getRel())).findFirst().map(Links::getHref).orElseThrow(IllegalStateException::new);
}
Aggregations