コード例 #1
0
        // RegisterCore is the actual implementation of the Register method
        // This separation serves as test hook to validate the Uri
        // against the code returned by the service
        internal static PaymentRequestResponse RegisterCore(Credentials credentials, PaymentRequest payment)
        {
            UriBuilder uriBuilder = new UriBuilder(PagSeguroConfiguration.PaymentUri);

            uriBuilder.Query = ServiceHelper.EncodeCredentialsAsQueryString(credentials);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);

            request.Method      = ServiceHelper.PostMethod;
            request.ContentType = ServiceHelper.XmlEncoded;
            request.Timeout     = PagSeguroConfiguration.RequestTimeout;

            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - begin", payment));

            using (Stream requestStream = request.GetRequestStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                using (XmlWriter writer = XmlWriter.Create(requestStream, settings))
                {
                    PaymentRequestSerializer.Write(writer, payment);
                    writer.Close();
                    requestStream.Close();

                    try
                    {
                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {
                            if (response.StatusCode == HttpStatusCode.OK)
                            {
                                using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                                {
                                    PaymentRequestResponse paymentResponse = new PaymentRequestResponse(PagSeguroConfiguration.PaymentRedirectUri);
                                    PaymentRequestResponseSerializer.Read(reader, paymentResponse);
                                    PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - end {1}", payment, paymentResponse.PaymentRedirectUri));
                                    return(paymentResponse);
                                }
                            }
                            else
                            {
                                PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException(response);
                                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse));
                                throw pse;
                            }
                        }
                    }
                    catch (WebException exception)
                    {
                        PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                        PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse));
                        throw pse;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Common implmentation of all SearchByDate methods
        /// </summary>
        /// <param name="credentials">PagSeguro credentials. Required.</param>
        /// <param name="initialDate"></param>
        /// <param name="finalDate">End of date range. Use DateTime.MaxValue to search without an upper boundary.</param>
        /// <param name="pageNumber">Page number, starting with 1. If passed as 0, it will call the web service to get the default page, also page number 1.</param>
        /// <param name="resultsPerPage">Results per page, optional.</param>
        /// <returns></returns>
        private static TransactionSearchResult SearchByDateCore(Credentials credentials, DateTime initialDate, DateTime finalDate, int pageNumber, int resultsPerPage)
        {
            UriBuilder uriBuilder = new UriBuilder(PagSeguroConfiguration.SearchUri);

            QueryStringBuilder query = new QueryStringBuilder(ServiceHelper.EncodeCredentialsAsQueryString(credentials));

            query.Append(InitialDateParameterName, initialDate);

            if (finalDate < DateTime.MaxValue)
            {
                query.Append(FinalDateParameterName, finalDate);
            }

            if (pageNumber > 0)
            {
                query.Append(PageNumberParameterName, pageNumber);
            }

            if (resultsPerPage > 0)
            {
                query.Append(MaxPageResultsParameterName, resultsPerPage);
            }

            uriBuilder.Query = query.ToString();

            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByDate(initialDate={0}, finalDate={1}) - begin", initialDate, finalDate));

            WebRequest request = WebRequest.Create(uriBuilder.Uri);

            request.Method  = ServiceHelper.GetMethod;
            request.Timeout = PagSeguroConfiguration.RequestTimeout;

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                    {
                        TransactionSearchResult result = new TransactionSearchResult();
                        TransactionSearchResultSerializer.Read(reader, result);
                        PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByDate(initialDate={0}, finalDate={1}) - end {2}", initialDate, finalDate, result));
                        return(result);
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByDate(initialDate={0}, finalDate={1}) - error {2}", initialDate, finalDate, pse));
                throw pse;
            }
        }
コード例 #3
0
        /// <summary>
        /// Returns a transaction from a notification code
        /// </summary>
        /// <param name="credentials">PagSeguro credentials</param>
        /// <param name="notificationCode">Transaction notification code</param>
        /// <returns><c cref="T:Uol.PagSeguro.Transaction">Transaction</c></returns>
        public static Transaction CheckTransaction(Credentials credentials, string notificationCode)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }
            if (String.IsNullOrEmpty(notificationCode))
            {
                throw new ArgumentNullException("notificationCode");
            }

            UriBuilder    uriBuilder  = new UriBuilder(PagSeguroConfiguration.NotificationUri);
            StringBuilder pathBuilder = new StringBuilder(uriBuilder.Path);

            pathBuilder.Append('/');
            pathBuilder.Append(WebUtility.UrlEncode(notificationCode));
            pathBuilder.Append('/');
            uriBuilder.Path  = pathBuilder.ToString();
            uriBuilder.Query = ServiceHelper.EncodeCredentialsAsQueryString(credentials);

            WebRequest request = WebRequest.Create(uriBuilder.Uri);

            request.Method  = ServiceHelper.GetMethod;
            request.Timeout = PagSeguroConfiguration.RequestTimeout;

            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "NotificationService.CheckTransaction(notificationCode={0}) - begin", notificationCode));

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                    {
                        Transaction transaction = new Transaction();
                        TransactionSerializer.Read(reader, transaction);

                        PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "NotificationService.CheckTransaction(notificationCode={0}) - end {1}", notificationCode, transaction));
                        return(transaction);
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(
                    String.Format(CultureInfo.InvariantCulture, "NotificationService.CheckTransaction(notificationCode={0}) - error {1}", notificationCode, pse));
                throw pse;
            }
        }
コード例 #4
0
        public static TransactionSearchResult SearchByReference(Credentials credentials, string referenceCode)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }
            if (referenceCode == null)
            {
                throw new ArgumentNullException("referenceCode");
            }

            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByReference(referenceCode={0}) - begin", referenceCode));

            UriBuilder    uriBuilder  = new UriBuilder(PagSeguroConfiguration.SearchUri);
            StringBuilder pathBuilder = new StringBuilder(uriBuilder.Path);

            uriBuilder.Path  = pathBuilder.ToString();
            uriBuilder.Query = $"{ServiceHelper.EncodeCredentialsAsQueryString(credentials)}&reference={HttpUtility.UrlEncode(referenceCode)}";

            WebRequest request = WebRequest.Create(uriBuilder.Uri);

            request.Method  = ServiceHelper.GetMethod;
            request.Timeout = PagSeguroConfiguration.RequestTimeout;

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                    {
                        TransactionSearchResult result = new TransactionSearchResult();
                        TransactionSearchResultSerializer.Read(reader, result);
                        PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByReference(referenceCode={0}) - end", referenceCode));
                        return(result);
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByReference(referenceCode={0}) - error {1}", referenceCode, pse));
                throw pse;
            }
        }