/// <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; } }
static void Main(string[] args) { bool isSandbox = false; EnvironmentConfiguration.ChangeEnvironment(isSandbox); // Instantiate a new preApproval request PreApprovalRequest preApproval = new PreApprovalRequest(); // Sets the currency preApproval.Currency = Currency.Brl; // Sets a reference code for this preApproval request, it is useful to identify this payment in future notifications. preApproval.Reference = "REF1234"; // Sets your customer information. preApproval.Sender = new Sender( "Joao Comprador", "*****@*****.**", new Phone("11", "56273440") ); // Sets the preApproval informations var now = DateTime.Now; preApproval.PreApproval = new PreApproval(); preApproval.PreApproval.Charge = Charge.Manual; preApproval.PreApproval.Name = "Seguro contra roubo do Notebook"; preApproval.PreApproval.AmountPerPayment = 100.00m; preApproval.PreApproval.MaxAmountPerPeriod = 100.00m; preApproval.PreApproval.MaxPaymentsPerPeriod = 5; preApproval.PreApproval.Details = string.Format("Todo dia {0} será cobrado o valor de {1} referente ao seguro contra roubo do Notebook.", now.Day, preApproval.PreApproval.AmountPerPayment.ToString("C2")); preApproval.PreApproval.Period = Period.Monthly; preApproval.PreApproval.DayOfMonth = now.Day; preApproval.PreApproval.InitialDate = now; preApproval.PreApproval.FinalDate = now.AddMonths(6); preApproval.PreApproval.MaxTotalAmount = 600.00m; // Sets the url used by PagSeguro for redirect user after ends checkout process preApproval.RedirectUri = new Uri("http://www.lojamodelo.com.br/retorno"); // Sets the url used for user review the signature or read the rules preApproval.ReviewUri = new Uri("http://www.lojamodelo.com.br/revisao"); SenderDocument senderCPF = new SenderDocument(Documents.GetDocumentByType("CPF"), "12345678909"); preApproval.Sender.Documents.Add(senderCPF); try { AccountCredentials credentials = PagSeguroConfiguration.Credentials(isSandbox); Uri preApprovalRedirectUri = preApproval.Register(credentials); Console.WriteLine("URL do pagamento : " + preApprovalRedirectUri); Console.ReadKey(); } catch (PagSeguroServiceException exception) { Console.WriteLine(exception.Message + "\n"); foreach (ServiceError element in exception.Errors) { Console.WriteLine(element + "\n"); } Console.ReadKey(); } }
static void Main(string[] args) { // TODO: Substitute the parameters below with your credentials //AccountCredentials credentials = new AccountCredentials("*****@*****.**", "your_token_here"); AccountCredentials credentials = PagSeguroConfiguration.Credentials; try { // Instantiate a new preApproval request PreApprovalRequest preApproval = new PreApprovalRequest(); // Sets the currency preApproval.Currency = Currency.Brl; // Sets a reference code for this preApproval request, it is useful to identify this payment in future notifications. preApproval.Reference = "REF1234"; // Sets your customer information. preApproval.Sender = new Sender( "Joao Comprador", "*****@*****.**", new Phone("11", "56273440") ); // Sets the preApproval informations var now = DateTime.Now; preApproval.PreApproval = new PreApproval(); preApproval.PreApproval.Charge = Charge.Auto; preApproval.PreApproval.Name = "Seguro contra roubo do Notebook"; preApproval.PreApproval.AmountPerPayment = 100.00m; preApproval.PreApproval.MaxAmountPerPeriod = 100.00m; preApproval.PreApproval.Details = string.Format("Todo dia {0} será cobrado o valor de {1} referente ao seguro contra roubo do Notebook.", now.Day, preApproval.PreApproval.AmountPerPayment.ToString("C2")); preApproval.PreApproval.Period = Period.Monthly; preApproval.PreApproval.DayOfMonth = now.Day; preApproval.PreApproval.InitialDate = now; preApproval.PreApproval.FinalDate = now.AddMonths(6); preApproval.PreApproval.MaxTotalAmount = 600.00m; // Sets the url used by PagSeguro for redirect user after ends checkout process preApproval.RedirectUri = new Uri("http://www.lojamodelo.com.br/retorno"); // Sets the url used for user review the signature or read the rules preApproval.ReviewUri = new Uri("http://www.lojamodelo.com.br/revisao"); SenderDocument senderCPF = new SenderDocument(Documents.GetDocumentByType("CPF"), "12345678909"); preApproval.Sender.Documents.Add(senderCPF); Uri preApprovalRedirectUri = preApproval.Register(credentials); Console.WriteLine("URL do pagamento : " + preApprovalRedirectUri); Console.ReadKey(); } catch (PagSeguroServiceException exception) { if (exception.StatusCode == HttpStatusCode.Unauthorized) { Console.WriteLine("Unauthorized: please verify if the credentials used in the web service call are correct.\n"); } Console.ReadKey(); } }
/// <summary> /// /// </summary> /// <param name="credentials"></param> /// <param name="preApproval"></param> /// <returns></returns> internal static string BuildCheckoutUrl(Credentials credentials, PreApprovalRequest preApproval) { QueryStringBuilder builder = new QueryStringBuilder(); IDictionary<string, string> data = PreApprovalParse.GetData(preApproval); builder. EncodeCredentialsAsQueryString(credentials); foreach (KeyValuePair<string, string> pair in data) { builder.Append(pair.Key, pair.Value); } return builder.ToString(); }
/// <summary> /// /// </summary> /// <param name="preApproval"></param> /// <returns></returns> public static IDictionary<string, string> GetData(PreApprovalRequest preApproval) { IDictionary<string, string> data = new Dictionary<string, string>(); // reference if (preApproval.Reference != null) { data["reference"] = preApproval.Reference; } // sender if (preApproval.Sender != null) { if (preApproval.Sender.Name != null) { data["senderName"] = preApproval.Sender.Name; } if (preApproval.Sender.Email != null) { data["senderEmail"] = preApproval.Sender.Email; } // phone if (preApproval.Sender.Phone != null) { if (preApproval.Sender.Phone.AreaCode != null) { data["senderAreaCode"] = preApproval.Sender.Phone.AreaCode; } if (preApproval.Sender.Phone.Number != null) { data["senderPhone"] = preApproval.Sender.Phone.Number; } } // documents if (preApproval.Sender.Documents != null) { var documents = preApproval.Sender.Documents; if (documents.Count == 1) { foreach (SenderDocument document in documents) { if (document != null) { data["senderCPF"] = document.Value; } } } } } data["preApprovalCharge"] = preApproval.PreApproval.Charge; data["preApprovalName"] = preApproval.PreApproval.Name; data["preApprovalDetails"] = preApproval.PreApproval.Details; data["preApprovalPeriod"] = preApproval.PreApproval.Period; data["preApprovalFinalDate"] = preApproval.PreApproval.FinalDate.ToString("yyyy-MM-dd") + "T01:00:00.45-03:00"; data["preApprovalMaxTotalAmount"] = preApproval.PreApproval.MaxTotalAmount.ToString("F").Replace(",", "."); data["preApprovalAmountPerPayment"] = preApproval.PreApproval.AmountPerPayment.ToString("F").Replace(",", "."); if (preApproval.PreApproval.Charge == Charge.Manual) { data["preApprovalInitialDate"] = preApproval.PreApproval.InitialDate.ToString("yyyy-MM-dd") + "T01:00:00.45-03:00"; data["preApprovalMaxAmountPerPeriod"] = preApproval.PreApproval.MaxAmountPerPeriod.ToString("F").Replace(",", "."); data["preApprovalMaxPaymentsPerPeriod"] = preApproval.PreApproval.MaxPaymentsPerPeriod.ToString(); if (preApproval.PreApproval.Period == Period.Yearly) data["preApprovalDayOfYear"] = preApproval.PreApproval.DayOfYear.ToString(); if (preApproval.PreApproval.Period == Period.Monthly || preApproval.PreApproval.Period == Period.Bimonthly || preApproval.PreApproval.Period == Period.Trimonthly || preApproval.PreApproval.Period == Period.SemiAnnually) data["preApprovalDayOfMonth"] = preApproval.PreApproval.DayOfMonth.ToString(); if (preApproval.PreApproval.Period == Period.Weekly) data["preApprovalDayOfWeek"] = preApproval.PreApproval.DayOfWeek.ToString(); } // currency if (preApproval.Currency != null) { data["currency"] = preApproval.Currency; } // redirectURL if (preApproval.RedirectUri != null) { data["redirectURL"] = preApproval.RedirectUri.ToString(); } // redirectURL if (preApproval.ReviewUri != null) { data["reviewUrl"] = preApproval.ReviewUri.ToString(); } // notificationURL if (preApproval.NotificationURL != null) { data["notificationURL"] = preApproval.NotificationURL; } // metadata if (preApproval.MetaData.Items.Count > 0) { int i = 0; var metaDataItems = preApproval.MetaData.Items; foreach (MetaDataItem item in metaDataItems) { if (!PagSeguroUtil.IsEmpty(item.Key) && !PagSeguroUtil.IsEmpty(item.Value)) { i++; data["metadataItemKey" + i] = item.Key; data["metadataItemValue" + i] = item.Value; if (item.Group != null) { data["metadataItemGroup" + i] = item.Group.ToString(); } } } } // parameter if (preApproval.Parameter.Items.Count > 0) { var parameterItems = preApproval.Parameter.Items; foreach (ParameterItem item in parameterItems) { if (!PagSeguroUtil.IsEmpty(item.Key) && !PagSeguroUtil.IsEmpty(item.Value)) { if (item.Group != null) { data[item.Key + "" + item.Group] = item.Value; } else { data[item.Key] = item.Value; } } } } return data; }