Ejemplo n.º 1
0
        /// <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 (var response = HttpURLConnectionUtil.GetHttpPostConnection(
                           PagSeguroConfiguration.PreApprovalPaymentUri.AbsoluteUri, BuildChargeUrl(credentials, payment)))
                {
                    if (HttpStatusCode.OK.Equals(response.StatusCode))
                    {
                        using (XmlReader reader = XmlReader.Create(response.Content.ReadAsStreamAsync().Result))
                        {
                            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(new System.Exception(response.StatusCode.ToString()));
                        PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse));
                        throw pse;
                    }
                }
            }
            catch (System.Exception exception)
            {
                PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(exception);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse));
                throw pse;
            }
        }
Ejemplo n.º 2
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.º 3
0
        /// <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 (var response = HttpUrlConnectionUtil.GetHttpPostConnection(
                           PagSeguroUris.GetPreApprovalPaymentUri(credentials).AbsoluteUri, BuildChargeUrl(credentials, payment)))
                {
                    if (HttpStatusCode.OK.Equals(response.StatusCode))
                    {
                        using (var reader = XmlReader.Create(response.GetResponseStream()))
                        {
                            var chargeResponse = new PaymentRequestResponse(PagSeguroUris.GetPreApprovalPaymentUri(credentials));
                            PaymentSerializer.Read(reader, chargeResponse);
                            PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - end {1}", payment, chargeResponse.PaymentRedirectUri));
                            return(chargeResponse.TransactionCode);
                        }
                    }

                    var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException(response);
                    PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse));
                    throw pse;
                }
            }
            catch (WebException exception)
            {
                var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse));
                throw pse;
            }
        }