Ejemplo n.º 1
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="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 (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                           PagSeguroConfiguration.CurrentConfig.PaymentUrl.AbsoluteUri, BuildCheckoutUrl(credentials, payment)))
                {
                    if (HttpStatusCode.OK.Equals(response.StatusCode))
                    {
                        using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                        {
                            PaymentRequestResponse paymentResponse = new PaymentRequestResponse(PagSeguroConfiguration.CurrentConfig.PaymentRedirectUrl);
                            PaymentSerializer.Read(reader, paymentResponse);
                            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - end {1}", payment, paymentResponse.PaymentRedirectUri));
                            return(paymentResponse.PaymentRedirectUri);
                        }
                    }
                    else
                    {
                        PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(response);
                        PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse));
                        throw pse;
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse));
                throw pse;
            }
        }
Ejemplo n.º 2
0
        /// <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 (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                           PagSeguroConfiguration.AuthorizarionRequestUri.AbsoluteUri, buildAuthorizationRequestUrl(credentials, authorizationRequest)))
                {
                    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                    {
                        AuthorizationResponse authorization = new AuthorizationResponse();
                        AuthorizationSerializer.Read(reader, authorization);

                        if (onlyAuthorizationCode)
                        {
                            return(authorization.Code);
                        }
                        else
                        {
                            return(BuildAuthorizationURL(authorization.Code));
                        }
                    }
                }
            }
            catch (WebException pse)
            {
                throw pse;
            }
            catch (PagSeguroServiceException pse)
            {
                throw pse;
            }
        }
Ejemplo n.º 3
0
        /// <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>
        /// ChargePreApproval
        /// </summary>
        /// <param name="credentials">PagSeguro credentials</param>
        /// <param name="payment">PreApproval 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 string ChargePreApproval(Credentials credentials, PaymentRequest payment)
        {
            PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - begin", payment));

            try
            {
                using (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                           PagSeguroConfiguration.PreApprovalPaymentUri.AbsoluteUri, BuildChargeUrl(credentials, payment)))
                {
                    if (HttpStatusCode.OK.Equals(response.StatusCode))
                    {
                        using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                        {
                            PaymentRequestResponse chargeResponse = new PaymentRequestResponse(PagSeguroConfiguration.PreApprovalPaymentUri);
                            PaymentSerializer.Read(reader, chargeResponse);
                            PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - end {1}", payment, chargeResponse.PaymentRedirectUri));
                            return(chargeResponse.TransactionCode);
                        }
                    }
                    else
                    {
                        PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(response);
                        PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse));
                        throw pse;
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse));
                throw pse;
            }
        }
Ejemplo n.º 5
0
 /// <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 (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                    PagSeguroConfiguration.TransactionsUri.AbsoluteUri,
                    BuildTransactionUrl(credentials, checkout),
                    credentials.IsSandbox()
                    )
                )
         {
             using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
             {
                 Transaction transaction = new Transaction();
                 TransactionSerializer.Read(reader, transaction);
                 PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "TransactionService.Register() - end {0}", transaction));
                 return(transaction);
             }
         }
     }
     catch (WebException exception)
     {
         PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
         PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "TransactionService.Register() - 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>
        /// <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 (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                           PagSeguroConfiguration.RefundUri.AbsoluteUri, BuildRefundURL(credentials, transactionCode, refundValue)))
                {
                    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                    {
                        RequestResponse refund = new RequestResponse();
                        RefundSerializer.Read(reader, refund);
                        PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "RefundService.Register({0}) - end", refund.ToString()));
                        return(refund);
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "RefundService.Register() - error {0}", pse));
                throw pse;
            }
        }
Ejemplo n.º 7
0
 /// <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(
                    PagSeguroConfiguration.CancelUri.AbsoluteUri, BuildCancelURL(credentials, transactionCode)))
         {
             using (XmlReader reader = XmlReader.Create(response.Content.ReadAsStreamAsync().Result))
             {
                 RequestResponse cancel = new RequestResponse();
                 CancelSerializer.Read(reader, cancel);
                 PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "CancelService.createRequest({0}) - end", cancel.ToString()));
                 return(cancel);
             }
         }
     } catch (System.Exception exception) {
         PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(exception);
         PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "CancelService.createRequest() - error {0}", 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 (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection(
                    PagSeguroConfiguration.SessionUri.AbsoluteUri, BuildSessionURL(credentials)))
         {
             using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
             {
                 Session result = new Session();
                 SessionSerializer.Read(reader, result);
                 PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "SessionService.Register({0}) - end", result.ToString()));
                 return(result);
             }
         }
     }
     catch (WebException exception)
     {
         PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
         PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "SessionService.Register() - error {0}", pse));
         throw pse;
     }
 }