/// <summary>
        /// CancelPreApproval
        /// </summary>
        /// <param name="credentials">PagSeguro credentials</param>
        /// <param name="preApprovalCode">PreApproval code</param>
        /// <returns>The PreApprovalRequestResponse wich contains the response</returns>
        public static bool CancelPreApproval(Credentials credentials, string preApprovalCode)
        {
            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - begin", preApprovalCode));

            try
            {
                using (HttpWebResponse response = HttpURLConnectionUtil.GetHttpGetConnection(BuildCancelUrl(credentials, preApprovalCode)))
                {

                    if (HttpStatusCode.OK.Equals(response.StatusCode))
                    {
                        using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                        {
                            PreApprovalRequestResponse paymentResponse = new PreApprovalRequestResponse(PagSeguroConfiguration.PreApprovalCancelUri);
                            PreApprovalSerializer.Read(reader, paymentResponse);
                            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - end {1}", preApprovalCode, paymentResponse.Status));
                            return paymentResponse.Status.Equals("OK", StringComparison.CurrentCultureIgnoreCase);
                        }
                    }
                    else
                    {
                        PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(response);
                        PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - error {1}", preApprovalCode, pse));
                        throw pse;
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - error {1}", preApprovalCode, pse));
                throw pse;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="preApprovalResponse"></param>
        internal static void Read(XmlReader reader, PreApprovalRequestResponse preApprovalResponse)
        {

            if (reader.IsEmptyElement)
            {
                XMLParserUtils.SkipNode(reader);
                return;
            }

            string rootElement = reader.Name;
            reader.ReadStartElement();
            reader.MoveToContent();

            while (!reader.EOF)
            {
                if (XMLParserUtils.IsEndElement(reader, rootElement))
                {
                    XMLParserUtils.SkipNode(reader);
                    break;
                }

                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case PreApprovalSerializer.Date:
                            preApprovalResponse.RegistrationDate = reader.ReadElementContentAsDateTime();
                            break;
                        case PreApprovalSerializer.Code:
                            preApprovalResponse.Code = reader.ReadElementContentAsString();
                            break;
                        case PreApprovalSerializer.Status:
                            preApprovalResponse.Status = reader.ReadElementContentAsString();
                            break;
                        default:
                            XMLParserUtils.SkipElement(reader);
                            break;
                    }
                }
                else
                {
                    XMLParserUtils.SkipNode(reader);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// createCheckoutRequest 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
        /// </summary>
        /// <param name="credentials">PagSeguro credentials</param>
        /// <param name="preApproval">PreApproval request information</param>
        /// <returns>The Uri to where the user needs to be redirected to in order to complete the payment process</returns>
        public static Uri CreateCheckoutRequest(Credentials credentials, PreApprovalRequest preApproval)
        {

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

            try
            {
                using (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                    PagSeguroConfiguration.PreApprovalUri.AbsoluteUri, BuildCheckoutUrl(credentials, preApproval)))
                {

                    if (HttpStatusCode.OK.Equals(response.StatusCode))
                    {
                        using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                        {
                            PreApprovalRequestResponse preApprovalResponse = new PreApprovalRequestResponse(PagSeguroConfiguration.PreApprovalRedirectUri);
                            PreApprovalSerializer.Read(reader, preApprovalResponse);
                            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.Register({0}) - end {1}", preApproval, preApprovalResponse.PreApprovalRedirectUri));
                            return preApprovalResponse.PreApprovalRedirectUri;
                        }
                    }
                    else
                    {
                        PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(response);
                        PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.Register({0}) - error {1}", preApproval, pse));
                        throw pse;
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.Register({0}) - error {1}", preApproval, pse));
                throw pse;
            }
        }