/// <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 (var response = HttpURLConnectionUtil.GetHttpGetConnection(BuildCancelUrl(credentials, preApprovalCode))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (XmlReader reader = XmlReader.Create(response.Content.ReadAsStreamAsync().Result)) { 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(new System.Exception(response.StatusCode.ToString())); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - error {1}", preApprovalCode, pse)); throw pse; } } } catch (System.Exception exception) { PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(exception); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - error {1}", preApprovalCode, pse)); throw pse; } }
/// <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.CurrentConfig.PreApprovalUrl.AbsoluteUri, BuildCheckoutUrl(credentials, preApproval))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) { PreApprovalRequestResponse preApprovalResponse = new PreApprovalRequestResponse(PagSeguroConfiguration.CurrentConfig.PreApprovalRedirectUrl); 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; } }
/// <summary> /// CreatePreApproval 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 CreatePreApproval(Credentials credentials, PreApprovalRequest preApproval) { PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.Register({0}) - begin", preApproval)); try { using (var response = HttpURLConnectionUtil.GetHttpPostConnection( PagSeguroConfiguration.PreApprovalUri.AbsoluteUri, BuildPreApprovalUrl(credentials, preApproval))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (XmlReader reader = XmlReader.Create(response.Content.ReadAsStreamAsync().Result)) { 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(new System.Exception(response.StatusCode.ToString())); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.Register({0}) - error {1}", preApproval, pse)); throw pse; } } } catch (System.Exception exception) { PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(exception); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.Register({0}) - error {1}", preApproval, pse)); throw pse; } }
/// <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 (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildCancelUrl(credentials, preApprovalCode))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var paymentResponse = new PreApprovalRequestResponse(PagSeguroUris.GetPreApprovalCancelUri(credentials)); 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)); } } var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException(response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - error {1}", preApprovalCode, pse)); throw pse; } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.CancelPreApproval({0}) - error {1}", preApprovalCode, pse)); throw pse; } }
/// <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); } } }