use of com.paypal.base.rest.PayPalRESTException 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.PayPalRESTException 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();
}
}
Aggregations