/// <summary> /// Finds a authorization with a matching authorization code /// </summary> /// <param name="credentials">PagSeguro credentials. Required.</param> /// <param name="code">Authorization code. Required</param> /// <returns>Authorization Summary</returns> public static AuthorizationSummary SearchByCode(Credentials credentials, string code) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "AuthorizationSearchService.SearchByCode({0}) - begin", code)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildSearchUrlByCode(credentials, code))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var authorization = new AuthorizationSummary(); AuthorizationSummarySerializer.Read(reader, authorization); return(authorization); } } } catch (WebException exception) { throw exception; } catch (PagSeguroServiceException pse) { throw pse; } }
/// <summary> /// Finds a authorization with a matching date interval /// </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> public static AuthorizationSearchResult SearchByDate(Credentials credentials, DateTime initialDate, DateTime finalDate, int?pageNumber = null, int?resultsPerPage = null) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "AuthorizationSearchService.SearchByDate(initialDate={0} - finalDate={1}) - begin", initialDate, finalDate)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildSearchUrlByDate(credentials, initialDate, finalDate, pageNumber, resultsPerPage))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var authorization = new AuthorizationSearchResult(); AuthorizationSearchResultSerializer.Read(reader, authorization); return(authorization); } } } catch (WebException exception) { throw exception; } catch (PagSeguroServiceException pse) { throw pse; } }
/// <summary> /// Creates a new authorization request /// </summary> /// <param name="credentials">PagSeguro credentials. Required</param> /// <param name="authorizationRequest">PagSeguro AuthorizationRequest</param> /// <param name="onlyAuthorizationCode"></param> /// <returns></returns> public static string CreateAuthorizationRequest(Credentials credentials, AuthorizationRequest authorizationRequest, bool onlyAuthorizationCode) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "AuthorizationService.CreateAuthorizationRequest() - begin")); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetAuthorizarionRequestUri(credentials).AbsoluteUri, BuildAuthorizationRequestUrl(credentials, authorizationRequest))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var authorization = new AuthorizationResponse(); AuthorizationSerializer.Read(reader, authorization); return(onlyAuthorizationCode ? authorization.Code : BuildAuthorizationUrl(credentials, authorization.Code)); } } } catch (WebException pse) { throw pse; } catch (PagSeguroServiceException pse) { throw pse; } }
/// <summary> /// Returns a authorization from a notification code /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="notificationCode">Authorization notification code</param> /// <returns><c cref="T:Uol.PagSeguro.Transaction">Transaction</c></returns> public static AuthorizationSummary CheckAuthorization(Credentials credentials, string notificationCode) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "NotificationService.CheckAuthorization(notificationCode={0}) - begin", notificationCode)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildAuthorizationNotificationUrl(credentials, notificationCode))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var authorization = new AuthorizationSummary(); AuthorizationSummarySerializer.Read(reader, authorization); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "NotificationService.CheckAuthorization(notificationCode={0}) - end {1}", notificationCode, authorization)); return(authorization); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error( string.Format(CultureInfo.InvariantCulture, "NotificationService.CheckAuthorization(notificationCode={0}) - error {1}", notificationCode, pse)); throw pse; } }
/// <summary> /// Request a direct payment session /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="amount"></param> /// <param name="cardBrand"></param> /// <param name="maxInstallmentNoInterest"></param> /// <returns><c cref="T:Uol.PagSeguro.CancelRequestResponse">Result</c></returns> public static Installments GetInstallments(Credentials credentials, decimal amount, string cardBrand, int maxInstallmentNoInterest) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "InstallmentService.GetInstallments() - begin")); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection( BuildInstallmentUrl(credentials, amount, cardBrand, maxInstallmentNoInterest))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var result = new Installments(); InstallmentsSerializer.Read(reader, result); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "InstallmentService.Register({0}) - end", result.ToString())); return(result); } } } catch (ArgumentException exception) { var pse = new PagSeguroServiceException(exception.Message); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "InstallmentService.Register() - error {0}", exception.Message)); throw pse; } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "InstallmentService.Register() - error {0}", 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="payment">Payment 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, PaymentRequest payment) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - begin", payment)); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetPaymentUri(credentials).AbsoluteUri, BuildCheckoutUrl(credentials, payment))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var paymentResponse = new PaymentRequestResponse(PagSeguroUris.GetPaymentRedirectUri(credentials)); PaymentSerializer.Read(reader, paymentResponse); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - end {1}", payment, paymentResponse.PaymentRedirectUri)); return(paymentResponse.PaymentRedirectUri); } } var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException(response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse)); throw pse; } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, 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> /// Request a transaction cancellation from transaction code /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="transactionCode">Transaction Code</param> /// <returns><c cref="T:Uol.PagSeguro.CancelRequestResponse">Result</c></returns> public static RequestResponse RequestCancel(Credentials credentials, string transactionCode) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "CancelService.Register(transactionCode = {0}) - begin", transactionCode)); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetCancelUri(credentials).AbsoluteUri, BuildCancelUrl(credentials, transactionCode))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var cancel = new RequestResponse(); CancelSerializer.Read(reader, cancel); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "CancelService.createRequest({0}) - end", cancel.ToString())); return(cancel); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "CancelService.createRequest() - error {0}", pse)); throw pse; } }
/// <summary> /// Request a transaction refund from transaction code /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="transactionCode">Transaction Code</param> /// <param name="refundValue"></param> /// <returns><c cref="T:Uol.PagSeguro.CancelRequestResponse">Result</c></returns> public static RequestResponse RequestRefund(Credentials credentials, string transactionCode, decimal?refundValue = null) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "RefundService.Register(transactionCode = {0}) - begin", transactionCode)); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetRefundUri(credentials).AbsoluteUri, BuildRefundUrl(credentials, transactionCode, refundValue))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var refund = new RequestResponse(); RefundSerializer.Read(reader, refund); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "RefundService.Register({0}) - end", refund.ToString())); return(refund); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "RefundService.Register() - error {0}", pse)); throw pse; } }
/// <summary> /// Finds a pre-approval with a matching pre-approval reference /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="reference">PagSeguro Pre-Approval Reference</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> public static PreApprovalSearchResult SearchByReference(Credentials credentials, string reference, DateTime initialDate, DateTime finalDate, int?pageNumber = null, int?resultsPerPage = null) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalSearchService.SearchByReference(reference={0}) - begin", reference)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildSearchUrlByReference(credentials, reference, initialDate, finalDate, pageNumber, resultsPerPage))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var preApproval = new PreApprovalSearchResult(); PreApprovalSearchResultSerializer.Read(reader, preApproval); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalSearchService.SearchByReference(reference={0}) - end {1}", reference, preApproval)); return(preApproval); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalSearchService.SearchByReference(reference={0}) - error {1}", reference, pse)); throw pse; } }
/// <summary> /// Create a new transaction checkout /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="checkout"></param> /// <returns cref="T:Uol.PagSeguro.Transaction"><c>Transaction</c></returns> public static Transaction CreateCheckout(Credentials credentials, Checkout checkout) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "TransactionService.Register() - begin")); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetTransactionsUri(credentials).AbsoluteUri, BuildTransactionUrl(credentials, checkout))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var transaction = new Transaction(); TransactionSerializer.Read(reader, transaction); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "TransactionService.Register() - end {0}", transaction)); return(transaction); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response, exception); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "TransactionService.Register() - error {0}", pse)); throw pse; } }
/// <summary> /// Finds a transaction with a matching reference code /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="reference">Reference</param> /// <returns></returns> public static TransactionSearchResult SearchByReference(Credentials credentials, string reference) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByReference(reference={0}) - begin", reference)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildSearchUrlByReference(credentials, reference))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var result = new TransactionSearchResult(); TransactionSearchResultSerializer.Read(reader, result); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByReference(reference={0}) - end", reference)); return(result); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchByReference(reference={0}) - error {1}", reference, pse)); throw pse; } }
/// <summary> /// Finds abandoned transactions /// </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> public static TransactionSearchResult SearchAbandoned(Credentials credentials, DateTime initialDate, DateTime finalDate, int?pageNumber = null, int?resultsPerPage = null) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchAbandoned(initialDate={0}, finalDate={1}) - begin", initialDate, finalDate)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildSearchUrlAbandoned(credentials, initialDate, finalDate, pageNumber, resultsPerPage))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var result = new TransactionSearchResult(); TransactionSearchResultSerializer.Read(reader, result); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchAbandoned(initialDate={0}, finalDate={1}) - end {2}", initialDate, finalDate, result)); return(result); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "TransactionSearchService.SearchAbandoned(initialDate={0}, finalDate={1}) - error {2}", initialDate, finalDate, pse)); throw pse; } }
/// <summary> /// Request a direct payment session /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <returns><c cref="T:Uol.PagSeguro.CancelRequestResponse">Result</c></returns> public static Session CreateSession(Credentials credentials) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "SessionService.Register() - begin")); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetSessionUri(credentials).AbsoluteUri, BuildSessionUrl(credentials))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var result = new Session(); SessionSerializer.Read(reader, result); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "SessionService.Register({0}) - end", result.ToString())); return(result); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response, exception); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "SessionService.Register() - error {0}", pse)); throw pse; } }
/// <summary> /// Finds a pre-approval with a matching notification code /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="notificationCode">Notification code</param> /// <returns cref="T:Uol.PagSeguro.Transaction"><c>Transaction</c></returns> public static PreApprovalTransaction SearchByNofication(Credentials credentials, string notificationCode) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalSearchService.SearchByNotification(notificationCode={0}) - begin", notificationCode)); try { using (var response = HttpUrlConnectionUtil.GetHttpGetConnection(BuildSearchUrlByNotification(credentials, notificationCode))) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var preApproval = new PreApprovalTransaction(); PreApprovalTransactionSerializer.Read(reader, preApproval); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalSearchService.SearchByNotification(notificationCode={0}) - end {1}", notificationCode, preApproval)); return(preApproval); } } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalSearchService.SearchByNotification(notificationCode={0}) - error {1}", notificationCode, pse)); throw pse; } }