/**
   * Finds a transaction with a matching transaction code
   *
   * @param credentials
   * @param transactionCode
   * @return a transaction in PagSeguro
   * @see Transaction
   * @throws PagSeguroServiceException
   */
  public static Transaction searchByCode(Credentials credentials, String transactionCode)
      throws PagSeguroServiceException {

    log.info(
        "TransactionSearchService.SearchByCode(transactionCode=" + transactionCode + ") - begin");

    if (transactionCode == null || transactionCode.trim().equals("")) {
      throw new IllegalArgumentException("transaction code can not be null");
    }

    // calling transaction search web service
    HttpURLConnection connection =
        HttpsURLConnectionUtil.getHttpsGetConnection(
            buildURLByCode(credentials, transactionCode), CONTENT_TYPE);

    try {
      // parsing transaction
      Transaction transaction = TransactionParser.readTransaction(connection.getInputStream());
      log.info(
          "TransactionSearchService.SearchByCode(transactionCode="
              + transactionCode
              + ") - end - "
              + transaction.toString());
      return transaction;
    } catch (Exception e) {
      log.error(
          "TransactionSearchService.SearchByCode(transactionCode=" + transactionCode + ") - error",
          e);
      throw new RuntimeException(e);
    } finally {
      connection.disconnect();
    }
  }
Пример #2
0
  /**
   * Requests a payment
   *
   * @param paymentRequest
   * @return The Uri to where the user needs to be redirected to in order to complete the payment
   *     process
   * @throws PagSeguroServiceException
   */
  public static URL doPayment(Credentials credentials, PaymentRequest paymentRequest)
      throws PagSeguroServiceException {

    log.info("PaymentService.Register(" + paymentRequest + ") - begin");
    String xml = PaymentParser.writePaymentXml(paymentRequest);

    // calling payment web service
    HttpURLConnection connection =
        HttpsURLConnectionUtil.getHttpsPostConnection(
            buildUrl(credentials, paymentRequest), URL_CONTENT_TYPE + "; charset=" + CHARSET, xml);

    try {
      // parsing web service response
      URL paymentURL =
          new URL(URL_REDIR + PaymentParser.readPaymentReturnXml(connection.getInputStream()));

      log.info("PaymentService.Register(" + paymentRequest + ") - end - " + paymentURL);
      return paymentURL;

    } catch (Exception e) {
      log.error("PaymentService.Register(" + paymentRequest + ") - error ", e);
      throw new RuntimeException(e);
    } finally {
      connection.disconnect();
    }
  }
  /**
   * Search transactions associated with this set of credentials within a date range
   *
   * @param credentials
   * @param initialDate
   * @param finalDate
   * @param page
   * @param maxPageResults
   * @return a object of <b>TransactionSearchResult</b>
   * @see TransactionSearchResult
   * @throws PagSeguroServiceException
   */
  public static TransactionSearchResult searchByDate(
      Credentials credentials,
      Date initialDate,
      Date finalDate,
      Integer page,
      Integer maxPageResults)
      throws PagSeguroServiceException {

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    String initialDateString = initialDate != null ? sdf.format(initialDate) : null;
    String finalDateString = finalDate != null ? sdf.format(finalDate) : null;

    log.info(
        "TransactionSearchService.SearchByDate(initialDate="
            + initialDateString
            + ", finalDate="
            + finalDateString
            + ") - begin");

    // call transaction search web service
    HttpURLConnection connection =
        HttpsURLConnectionUtil.getHttpsGetConnection(
            buildURLByDate(credentials, initialDate, finalDate, page, maxPageResults),
            CONTENT_TYPE);

    TransactionSearchResult search = new TransactionSearchResult();
    try {
      // parsing PagSeguro response
      TransactionSearchResultXMLHandler.getHandler(connection.getInputStream(), search);

      log.info(
          "TransactionSearchService.SearchByDate(initialDate="
              + initialDateString
              + ", finalDate="
              + finalDateString
              + ") - end - "
              + search);

      return search;

    } catch (Exception e) {
      log.error(
          "TransactionSearchService.SearchByDate(initialDate="
              + initialDateString
              + ", finalDate="
              + finalDateString
              + ") - error "
              + search,
          e);
      throw new RuntimeException(e);
    } finally {
      connection.disconnect();
    }
  }