Search in sources :

Example 1 with PayPalRESTException

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;
    }
}
Also used : APIContext(com.paypal.base.rest.APIContext) PayPalRESTException(com.paypal.base.rest.PayPalRESTException)

Example 2 with PayPalRESTException

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();
    }
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) java.util(java.util) MessageDigest(java.security.MessageDigest) TicketReservationRepository(alfio.repository.TicketReservationRepository) ConfigurationManager(alfio.manager.system.ConfigurationManager) Cache(com.github.benmanes.caffeine.cache.Cache) StringUtils(org.apache.commons.lang3.StringUtils) Supplier(java.util.function.Supplier) com.paypal.api.payments(com.paypal.api.payments) BigDecimal(java.math.BigDecimal) MonetaryUtil.formatCents(alfio.util.MonetaryUtil.formatCents) Pair(org.apache.commons.lang3.tuple.Pair) APIContext(com.paypal.base.rest.APIContext) HmacAlgorithms(org.apache.commons.codec.digest.HmacAlgorithms) MessageSource(org.springframework.context.MessageSource) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) TicketRepository(alfio.repository.TicketRepository) HmacUtils(org.apache.commons.codec.digest.HmacUtils) Currency(com.paypal.api.payments.Currency) FeeCalculator(alfio.manager.support.FeeCalculator) PayPalRESTException(com.paypal.base.rest.PayPalRESTException) StandardCharsets(java.nio.charset.StandardCharsets) DateUtils(org.apache.commons.lang3.time.DateUtils) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) MonetaryUtil(alfio.util.MonetaryUtil) alfio.model(alfio.model) Configuration(alfio.model.system.Configuration) Log4j2(lombok.extern.log4j.Log4j2) Event(alfio.model.Event) AllArgsConstructor(lombok.AllArgsConstructor) DigestUtils(org.apache.commons.codec.digest.DigestUtils) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Currency(com.paypal.api.payments.Currency) MonetaryUtil(alfio.util.MonetaryUtil) APIContext(com.paypal.base.rest.APIContext) PayPalRESTException(com.paypal.base.rest.PayPalRESTException) BigDecimal(java.math.BigDecimal)

Aggregations

APIContext (com.paypal.base.rest.APIContext)2 PayPalRESTException (com.paypal.base.rest.PayPalRESTException)2 FeeCalculator (alfio.manager.support.FeeCalculator)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 alfio.model (alfio.model)1 Event (alfio.model.Event)1 Configuration (alfio.model.system.Configuration)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 TicketRepository (alfio.repository.TicketRepository)1 TicketReservationRepository (alfio.repository.TicketReservationRepository)1 MonetaryUtil (alfio.util.MonetaryUtil)1 MonetaryUtil.formatCents (alfio.util.MonetaryUtil.formatCents)1 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 com.paypal.api.payments (com.paypal.api.payments)1 Currency (com.paypal.api.payments.Currency)1 BigDecimal (java.math.BigDecimal)1 StandardCharsets (java.nio.charset.StandardCharsets)1 MessageDigest (java.security.MessageDigest)1 java.util (java.util)1