Ejemplo n.º 1
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;
  }