Ejemplo n.º 1
0
  /**
   * Performs some static checks and loads the transfers to be chargedback by the bulk reverse
   * operation.
   *
   * @param ids: Could be trace numbers or transfer ids.
   * @param loader: The object responsible for loading the transfer by trace number or transferId.
   */
  private <V> List<ChargebackResult> doBulkChargeback(
      final List<V> ids, final TransferLoader<V> loader) {
    final List<Transfer> transfers = new LinkedList<Transfer>();
    final List<AccountDTO> allAccounts = new LinkedList<AccountDTO>();
    final Member member = WebServiceContext.getMember();
    final List<ChargebackResult> result = new LinkedList<ChargebackResult>();
    boolean failed = false;

    for (final V id : ids) {
      Transfer transfer = null;
      try {
        transfer = loader.load(id);
        transfers.add(transfer);
        if (member != null && !transfer.getToOwner().equals(member)) {
          throw new EntityNotFoundException();
        }
        result.add(new ChargebackResult(ChargebackStatus.NOT_PERFORMED, null, null));
        allAccounts.add(new AccountDTO(transfer.getFrom()));
        allAccounts.add(new AccountDTO(transfer.getTo()));
      } catch (final EntityNotFoundException e) {
        failed = true;
        result.add(new ChargebackResult(ChargebackStatus.TRANSFER_NOT_FOUND, null, null));
        webServiceHelper.error(
            new Exception(
                "Bulk status [Id=" + id + "]: " + ChargebackStatus.TRANSFER_NOT_FOUND, e));
      }
    }

    if (failed) {
      return result;
    }

    return bulkReverse(transfers, allAccounts);
  }
Ejemplo n.º 2
0
 @Override
 protected void appendVariableValues(
     final Map<String, Object> variables, final LocalSettings localSettings) {
   variables.put("grant_date", localSettings.getDateConverter().toString(transfer.getDate()));
   variables.put(
       "amount",
       localSettings
           .getUnitsConverter(transfer.getTo().getType().getCurrency().getPattern())
           .toString(transfer.getAmount()));
 }
 private int getTransferNumber(final Transfer transfer) {
   final ScheduledPayment scheduledPayment = transfer.getScheduledPayment();
   int transferNumber = 0;
   for (final Transfer currentTransfer : scheduledPayment.getTransfers()) {
     transferNumber++;
     if (currentTransfer.equals(transfer)) {
       break;
     }
   }
   return transferNumber;
 }
Ejemplo n.º 4
0
 public Status getStatus() {
   if (transfer.getStatus() == Payment.Status.PENDING) {
     return Status.PENDING_AUTHORIZATION;
   }
   if (transfer.getStatus() == Payment.Status.DENIED) {
     return Status.AUTHORIZATION_DENIED;
   }
   for (final LoanPayment payment : getPayments()) {
     if (payment.getStatus().isOpen()) {
       return Status.OPEN;
     }
   }
   return Status.CLOSED;
 }
Ejemplo n.º 5
0
 public Member getMember() {
   try {
     return (Member) transfer.getTo().getOwner();
   } catch (final NullPointerException e) {
     return null;
   }
 }
Ejemplo n.º 6
0
  private ChargebackResult doChargeback(final Transfer transfer) {
    ChargebackStatus status = null;
    Transfer chargebackTransfer = null;

    // Check if the transfer can be charged back
    if (!paymentService.canChargeback(transfer, false)) {
      if (transfer.getChargedBackBy() != null) {
        chargebackTransfer = transfer.getChargedBackBy();
        status = ChargebackStatus.TRANSFER_ALREADY_CHARGEDBACK;
      } else {
        if (transfer.getStatus() == Payment.Status.PENDING) {
          final TransferAuthorizationDTO transferAuthorizationDto = new TransferAuthorizationDTO();
          transferAuthorizationDto.setTransfer(transfer);
          transferAuthorizationDto.setShowToMember(false);
          chargebackTransfer =
              transferAuthorizationService.cancelFromMemberAsReceiver(transferAuthorizationDto);
          status = ChargebackStatus.SUCCESS;
        } else {
          status = ChargebackStatus.TRANSFER_CANNOT_BE_CHARGEDBACK;
        }
      }
    }

    // Do the chargeback
    if (status == null) {
      chargebackTransfer =
          paymentService.chargeback(transfer, WebServiceContext.getClient().getId());
      status = ChargebackStatus.SUCCESS;
    }

    if (!status.isSuccessful()) {
      webServiceHelper.error("Chargeback result: " + status);
    }

    final Member member = WebServiceContext.getMember();
    // Build the result
    if (status == ChargebackStatus.SUCCESS
        || status == ChargebackStatus.TRANSFER_ALREADY_CHARGEDBACK) {
      final AccountOwner owner = member == null ? transfer.getToOwner() : member;
      final AccountHistoryTransferVO originalVO = accountHelper.toVO(owner, transfer, null);
      final AccountHistoryTransferVO chargebackVO =
          accountHelper.toVO(owner, chargebackTransfer, null);
      return new ChargebackResult(status, originalVO, chargebackVO);
    } else {
      return new ChargebackResult(status, null, null);
    }
  }
Ejemplo n.º 7
0
  private <V> ChargebackResult reverse(final V transferId, final TransferLoader<V> loader) {
    Exception errorException = null;
    ChargebackStatus status = null;
    Transfer transfer = null;

    try {
      transfer = loader.load(transferId);
      // Ensure the member is the one who received the payment
      final Member member = WebServiceContext.getMember();
      if (member != null && !transfer.getToOwner().equals(member)) {
        throw new EntityNotFoundException();
      } else {
        final Collection<TransferType> possibleTypes =
            fetchService
                .fetch(
                    WebServiceContext.getClient(),
                    ServiceClient.Relationships.CHARGEBACK_PAYMENT_TYPES)
                .getChargebackPaymentTypes();
        if (!possibleTypes.contains(transfer.getType())) {
          throw new EntityNotFoundException();
        }
      }
    } catch (final EntityNotFoundException e) {
      errorException = e;
      status = ChargebackStatus.TRANSFER_NOT_FOUND;
    }

    if (status == null) {
      try {
        return doChargeback(transfer);
      } catch (final Exception e) {
        webServiceHelper.error(e);
        return new ChargebackResult(ChargebackStatus.TRANSFER_CANNOT_BE_CHARGEDBACK, null, null);
      }
    } else {
      if (!status.isSuccessful()) {
        webServiceHelper.error(
            errorException != null
                ? errorException
                : new Exception("Chargeback status: " + status));
      }
      return new ChargebackResult(status, null, null);
    }
  }
  @Override
  protected ActionForward handleSubmit(final ActionContext context) throws Exception {
    Transfer transfer = resolveTransfer(context);

    // Validate the transaction password if needed
    if (shouldValidateTransactionPassword(context, transfer)) {
      final ScheduledPaymentForm form = context.getForm();
      context.checkTransactionPassword(form.getTransactionPassword());
    }

    // Perform the actual payment
    try {
      transfer = scheduledPaymentService.processTransfer(transfer);
    } catch (final CreditsException e) {
      return context.sendError(actionHelper.resolveErrorKey(e), actionHelper.resolveParameters(e));
    } catch (final UnexpectedEntityException e) {
      return context.sendError("payment.error.invalidTransferType");
    }
    return ActionHelper.redirectWithParam(
        context.getRequest(),
        context.getSuccessForward(),
        "paymentId",
        transfer.getScheduledPayment().getId());
  }
  @Override
  protected void prepareForm(final ActionContext context) throws Exception {
    final Transfer transfer = resolveTransfer(context);

    // Check for transaction password
    final HttpServletRequest request = context.getRequest();
    final boolean requestTransactionPassword = shouldValidateTransactionPassword(context, transfer);
    if (requestTransactionPassword) {
      context.validateTransactionPassword();
    }
    request.setAttribute("requestTransactionPassword", requestTransactionPassword);
    request.setAttribute(
        "wouldRequireAuthorization", paymentService.wouldRequireAuthorization(transfer));

    // Transfer number and number of transfers
    final int transferNumber = getTransferNumber(transfer);
    final int numberOfTransfers = getNumberOfTransfer(transfer);
    request.setAttribute("transferNumber", transferNumber);
    request.setAttribute("numberOfTransfers", numberOfTransfers);

    // Fetch related data
    final AccountOwner from = transfer.getFromOwner();
    final AccountOwner to = transfer.getToOwner();
    final TransferType transferType =
        transferTypeService.load(
            transfer.getType().getId(),
            RelationshipHelper.nested(
                TransferType.Relationships.FROM, AccountType.Relationships.CURRENCY),
            TransferType.Relationships.TO);
    final BigDecimal amount = transfer.getAmount();
    if (from instanceof Member) {
      request.setAttribute("fromMember", from);
    }
    if (to instanceof Member) {
      request.setAttribute("toMember", to);
    }
    transfer.setType(transferType);
    request.setAttribute("unitsPattern", transferType.getFrom().getCurrency().getPattern());

    // Store the transaction fees
    final TransactionFeePreviewDTO preview =
        transactionFeeService.preview(from, to, transferType, amount);
    request.setAttribute("finalAmount", preview.getFinalAmount());
    request.setAttribute("fees", preview.getFees());
    request.setAttribute("transfer", transfer);
  }
Ejemplo n.º 10
0
 public TransferType getTransferType() {
   return transfer == null ? null : transfer.getType();
 }
Ejemplo n.º 11
0
 public Calendar getGrantDate() {
   return transfer == null ? null : transfer.getProcessDate();
 }
Ejemplo n.º 12
0
 public BigDecimal getAmount() {
   return transfer == null ? null : transfer.getAmount();
 }
Ejemplo n.º 13
0
  public PaymentResult confirmPayment(final ConfirmPaymentParameters params) {
    Exception errorException = null;
    AccountStatus fromMemberStatus = null;
    AccountStatus toMemberStatus = null;
    Member fromMember = null;
    Member toMember = null;

    // It's nonsense to use this if restricted to a member
    if (WebServiceContext.getMember() != null) {
      throw new PermissionDeniedException();
    }
    final Channel channel = WebServiceContext.getChannel();
    final String channelName = channel == null ? null : channel.getInternalName();

    PaymentStatus status = null;
    AccountHistoryTransferVO transferVO = null;

    // Get the ticket
    PaymentRequestTicket ticket = null;
    try {
      // Check that the ticket is valid
      final Ticket t = ticketService.load(params.getTicket());
      fromMember = t.getFrom();
      toMember = t.getTo();

      if (!(t instanceof PaymentRequestTicket) || t.getStatus() != Ticket.Status.PENDING) {
        throw new Exception(
            "Invalid ticket and/or status: "
                + t.getClass().getName()
                + ", status: "
                + t.getStatus());
      }
      // Check that the channel is the expected one
      ticket = (PaymentRequestTicket) t;
      if (!ticket.getToChannel().getInternalName().equals(channelName)) {
        throw new Exception(
            "The ticket's destination channel is not the expected one (expected="
                + channelName
                + "): "
                + ticket.getToChannel().getInternalName());
      }
    } catch (final Exception e) {
      errorException = e;
      status = PaymentStatus.INVALID_PARAMETERS;
    }

    // Validate the Channel and credentials
    Member member = null;
    if (status == null) {
      member = ticket.getFrom();
      if (!accessService.isChannelEnabledForMember(channelName, member)) {
        status = PaymentStatus.INVALID_CHANNEL;
      }
      if (status == null && WebServiceContext.getClient().isCredentialsRequired()) {
        try {
          checkCredentials(member, channel, params.getCredentials());
        } catch (final InvalidCredentialsException e) {
          status = PaymentStatus.INVALID_CREDENTIALS;
        } catch (final BlockedCredentialsException e) {
          status = PaymentStatus.BLOCKED_CREDENTIALS;
        }
      }
    }
    // Confirm the payment
    if (status == null) {
      try {
        final Transfer transfer = (Transfer) paymentService.confirmPayment(ticket.getTicket());
        transferVO = accountHelper.toVO(member, transfer, null);
        status = paymentHelper.toStatus(transfer);

        if (WebServiceContext.getClient()
            .getPermissions()
            .contains(ServiceOperation.ACCOUNT_DETAILS)) {
          if (WebServiceContext.getMember() == null) {
            fromMemberStatus =
                accountService.getStatus(
                    new GetTransactionsDTO(fromMember, transfer.getFrom().getType()));
            toMemberStatus =
                accountService.getStatus(
                    new GetTransactionsDTO(toMember, transfer.getTo().getType()));
          } else if (WebServiceContext.getMember().equals(fromMember)) {
            fromMemberStatus =
                accountService.getStatus(
                    new GetTransactionsDTO(fromMember, transfer.getFrom().getType()));
          } else {
            toMemberStatus =
                accountService.getStatus(
                    new GetTransactionsDTO(toMember, transfer.getTo().getType()));
          }
        }
      } catch (final Exception e) {
        errorException = e;
        status = paymentHelper.toStatus(e);
      }
    }

    if (!status.isSuccessful()) {
      webServiceHelper.error(
          errorException != null
              ? errorException
              : new Exception("Confirm payment status: " + status));
    }
    // Build the result
    return new PaymentResult(
        status,
        transferVO,
        accountHelper.toVO(fromMemberStatus),
        accountHelper.toVO(toMemberStatus));
  }
 private boolean shouldValidateTransactionPassword(
     final ActionContext context, final Transfer transfer) {
   final TransferType transferType =
       transferTypeService.load(transfer.getType().getId(), TransferType.Relationships.FROM);
   return context.isTransactionPasswordEnabled(transferType.getFrom());
 }
 private int getNumberOfTransfer(final Transfer transfer) {
   final ScheduledPayment scheduledPayment = transfer.getScheduledPayment();
   return scheduledPayment.getTransfers().size();
 }