Example #1
0
  public boolean expireTicket(final String ticketStr) {
    try {
      final PaymentRequestTicket ticket =
          (PaymentRequestTicket)
              ticketService.load(ticketStr, PaymentRequestTicket.Relationships.FROM_CHANNEL);
      // Check the member restriction
      final Member restricted = WebServiceContext.getMember();
      if (restricted != null && !restricted.equals(ticket.getTo())) {
        throw new Exception();
      }

      // Check the from channel
      final Channel resolvedChannel = WebServiceContext.getChannel();
      final Channel fromChannel = ticket.getFromChannel();
      final Channel toChannel = ticket.getToChannel();
      if ((fromChannel == null || !fromChannel.equals(resolvedChannel))
          && (toChannel == null || !toChannel.equals(resolvedChannel))) {
        throw new Exception();
      }

      // Check by status
      if (ticket.getStatus() == Ticket.Status.PENDING) {
        ticketService.expirePaymentRequestTicket(ticket);
        return true;
      }
    } catch (final Exception e) {
      webServiceHelper.error(e);
      // Ignore exceptions
    }
    return false;
  }
Example #2
0
  public RequestPaymentResult requestPaymentConfirmation(final RequestPaymentParameters params) {
    Exception errorException = null;
    PaymentRequestStatus status = null;
    // Get the to member
    Member toMember = null;
    final Member restricted = WebServiceContext.getMember();
    if (restricted != null) {
      // When restricted to a member, he is always the to
      toMember = restricted;
    } else {
      try {
        toMember = paymentHelper.resolveToMember(params);
      } catch (final EntityNotFoundException e) {
        status = PaymentRequestStatus.TO_NOT_FOUND;
      }
      // When not restricted to a member, check the channel access of the payment receiver
      if (status == null && !memberHelper.isChannelEnabledForMember(toMember)) {
        status = PaymentRequestStatus.TO_INVALID_CHANNEL;
      }
    }
    // Get the from member
    Member fromMember = null;
    if (status == null) {
      try {
        fromMember = paymentHelper.resolveFromMember(params);
      } catch (final EntityNotFoundException e) {
        status = PaymentRequestStatus.FROM_NOT_FOUND;
      }
    }

    // Generate the ticket if no error so far
    PaymentRequestTicket ticket = null;
    if (status == null) {
      try {
        ticket = paymentHelper.toTicket(params, null);
        ticket.setFrom(fromMember);
        ticket.setTo(toMember);
        ticket = ticketService.generate(ticket);
        status = PaymentRequestStatus.REQUEST_RECEIVED;
      } catch (final InvalidChannelException e) {
        status = PaymentRequestStatus.FROM_INVALID_CHANNEL;
      } catch (final Exception e) {
        errorException = e;
        final PaymentStatus paymentStatus = paymentHelper.toStatus(e);
        try {
          // Probably it's a payment status also present on payment request status
          status = PaymentRequestStatus.valueOf(paymentStatus.name());
        } catch (final Exception e1) {
          e1.initCause(e);
          errorException = e1;
          status = PaymentRequestStatus.UNKNOWN_ERROR;
        }
      }
    }

    if (!status.isSuccessful()) {
      webServiceHelper.error(
          errorException != null
              ? errorException
              : new Exception("Request payment confirmation status: " + status));
    }

    // Build a result
    final RequestPaymentResult result = new RequestPaymentResult();
    result.setStatus(status);
    if (ticket != null) {
      result.setTicket(ticket.getTicket());
    }
    return result;
  }
Example #3
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));
  }