private static String BuildInstallmentURL(Credentials credentials, Decimal amount, String cardBrand)
        {
            QueryStringBuilder builder = new QueryStringBuilder("{url}?{credentials}&amount={amount}&cardBrand={cardBrand}");

            builder.ReplaceValue("{url}", PagSeguroConfiguration.InstallmentUri.AbsoluteUri);
            builder.ReplaceValue("{credentials}", new QueryStringBuilder().EncodeCredentialsAsQueryString(credentials).ToString());
            builder.ReplaceValue("{amount}", PagSeguroUtil.DecimalFormat(amount));
            builder.ReplaceValue("{cardBrand}", HttpUtility.UrlEncode(cardBrand.ToString()));

            return(builder.ToString());
        }
Ejemplo n.º 2
0
        private static string BuildInstallmentUrl(Credentials credentials, decimal amount, string cardBrand, int maxInstallmentNoInterest)
        {
            var builder = new QueryStringBuilder("{url}?{credentials}&amount={amount}&cardBrand={cardBrand}&maxInstallmentNoInterest={maxInstallmentNoInterest}");

            builder.ReplaceValue("{url}", PagSeguroUris.GetInstallmentUri(credentials).AbsoluteUri);
            builder.ReplaceValue("{credentials}", new QueryStringBuilder().EncodeCredentialsAsQueryString(credentials).ToString());
            builder.ReplaceValue("{amount}", PagSeguroUtil.DecimalFormat(amount));
            builder.ReplaceValue("{cardBrand}", HttpUtility.UrlEncode(cardBrand));
            builder.ReplaceValue("{maxInstallmentNoInterest}", HttpUtility.UrlEncode(maxInstallmentNoInterest.ToString()));

            return(builder.ToString());
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="credentials">PagSeguro credentials</param>
        /// <param name="transactionCode">Transaction Code</param>
        /// <param name="refundValue">Refund Value</param>
        /// <returns></returns>
        private static string BuildRefundURL(Credentials credentials, string transactionCode, decimal?refundValue)
        {
            QueryStringBuilder builder = new QueryStringBuilder();

            builder.EncodeCredentialsAsQueryString(credentials);
            builder.Append("transactionCode", transactionCode);
            if (refundValue.HasValue)
            {
                builder.Append("refundValue", PagSeguroUtil.DecimalFormat(refundValue.Value));
            }

            return(builder.ToString());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="checkout"></param>
        /// <returns></returns>
        public static IDictionary <string, string> GetData(Checkout checkout)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            // payment mode
            if (checkout.PaymentMode != null)
            {
                data["paymentMode"] = checkout.PaymentMode;
            }

            // receiver e-mail
            if (checkout.ReceiverEmail != null)
            {
                data["receiverEmail"] = checkout.ReceiverEmail;
            }

            // reference
            if (checkout.Reference != null)
            {
                data["reference"] = checkout.Reference;
            }

            // sender
            if (checkout.Sender != null)
            {
                if (checkout.Sender.Name != null)
                {
                    data["senderName"] = checkout.Sender.Name;
                }
                if (checkout.Sender.Email != null)
                {
                    data["senderEmail"] = checkout.Sender.Email;
                }
                if (checkout.Sender.Hash != null)
                {
                    data["senderHash"] = checkout.Sender.Hash;
                }

                // phone
                if (checkout.Sender.Phone != null)
                {
                    if (checkout.Sender.Phone.AreaCode != null)
                    {
                        data["senderAreaCode"] = checkout.Sender.Phone.AreaCode;
                    }
                    if (checkout.Sender.Phone.Number != null)
                    {
                        data["senderPhone"] = checkout.Sender.Phone.Number;
                    }
                }

                // documents
                if (checkout.Sender.Documents != null)
                {
                    var documents = checkout.Sender.Documents;
                    if (documents.Count == 1)
                    {
                        foreach (SenderDocument document in documents)
                        {
                            if (document != null)
                            {
                                if (document.Type.Equals("Cadastro de Pessoa Física"))
                                {
                                    data["senderCPF"] = document.Value;
                                }
                                else
                                {
                                    data["senderCNPJ"] = document.Value;
                                }
                            }
                        }
                    }
                }
            }

            // currency
            if (checkout.Currency != null)
            {
                data["currency"] = checkout.Currency;
            }

            // items
            if (checkout.Items.Count > 0)
            {
                var items = checkout.Items;
                int i     = 0;
                foreach (Item item in items)
                {
                    i++;

                    if (item.Id != null)
                    {
                        data["itemId" + i] = item.Id;
                    }
                    if (item.Description != null)
                    {
                        data["itemDescription" + i] = item.Description;
                    }
                    if (item.Quantity != null)
                    {
                        data["itemQuantity" + i] = item.Quantity.ToString();
                    }
                    if (item.Amount != null)
                    {
                        data["itemAmount" + i] = PagSeguroUtil.DecimalFormat(item.Amount);
                    }
                    if (item.Weight != null)
                    {
                        data["itemWeight" + i] = item.Weight.ToString();
                    }
                    if (item.ShippingCost != null)
                    {
                        data["itemShippingCost" + i] = PagSeguroUtil.DecimalFormat((decimal)item.ShippingCost);
                    }
                }
            }

            // extraAmount
            if (checkout.ExtraAmount != null)
            {
                data["extraAmount"] = PagSeguroUtil.DecimalFormat((decimal)checkout.ExtraAmount);
            }

            // shipping
            if (checkout.Shipping != null)
            {
                if (checkout.Shipping.ShippingType != null && checkout.Shipping.ShippingType.Value != null)
                {
                    data["shippingType"] = checkout.Shipping.ShippingType.Value.ToString();
                }

                if (checkout.Shipping.Cost != null)
                {
                    data["shippingCost"] = PagSeguroUtil.DecimalFormat((decimal)checkout.Shipping.Cost);
                }

                // address
                if (checkout.Shipping.Address != null)
                {
                    if (checkout.Shipping.Address.Street != null)
                    {
                        data["shippingAddressStreet"] = checkout.Shipping.Address.Street;
                    }
                    if (checkout.Shipping.Address.Number != null)
                    {
                        data["shippingAddressNumber"] = checkout.Shipping.Address.Number;
                    }
                    if (checkout.Shipping.Address.Complement != null)
                    {
                        data["shippingAddressComplement"] = checkout.Shipping.Address.Complement;
                    }
                    if (checkout.Shipping.Address.City != null)
                    {
                        data["shippingAddressCity"] = checkout.Shipping.Address.City;
                    }
                    if (checkout.Shipping.Address.State != null)
                    {
                        data["shippingAddressState"] = checkout.Shipping.Address.State;
                    }
                    if (checkout.Shipping.Address.District != null)
                    {
                        data["shippingAddressDistrict"] = checkout.Shipping.Address.District;
                    }
                    if (checkout.Shipping.Address.PostalCode != null)
                    {
                        data["shippingAddressPostalCode"] = checkout.Shipping.Address.PostalCode;
                    }
                    if (checkout.Shipping.Address.Country != null)
                    {
                        data["shippingAddressCountry"] = checkout.Shipping.Address.Country;
                    }
                }
            }

            // maxAge
            if (checkout.MaxAge != null)
            {
                data["maxAge"] = checkout.MaxAge.ToString();
            }
            // maxUses
            if (checkout.MaxUses != null)
            {
                data["maxUses"] = checkout.MaxUses.ToString();
            }

            // notificationURL
            if (checkout.NotificationURL != null)
            {
                data["notificationURL"] = checkout.NotificationURL;
            }

            // metadata
            if (checkout.MetaData.Items.Count > 0)
            {
                int i             = 0;
                var metaDataItems = checkout.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 (checkout.Parameter.Items.Count > 0)
            {
                var parameterItems = checkout.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;
                        }
                    }
                }
            }

            //Verify if exists the credit card checkout data
            if (checkout is CreditCardCheckout)
            {
                CreditCardCheckout creditcard = (CreditCardCheckout)checkout;
                // billing
                if (creditcard.Billing != null)
                {
                    // address
                    if (creditcard.Billing.Address != null)
                    {
                        if (creditcard.Billing.Address.Street != null)
                        {
                            data["billingAddressStreet"] = creditcard.Billing.Address.Street;
                        }
                        if (creditcard.Billing.Address.Number != null)
                        {
                            data["billingAddressNumber"] = creditcard.Billing.Address.Number;
                        }
                        if (creditcard.Billing.Address.Complement != null)
                        {
                            data["billingAddressComplement"] = creditcard.Billing.Address.Complement;
                        }
                        if (creditcard.Billing.Address.City != null)
                        {
                            data["billingAddressCity"] = creditcard.Billing.Address.City;
                        }
                        if (creditcard.Billing.Address.State != null)
                        {
                            data["billingAddressState"] = creditcard.Billing.Address.State;
                        }
                        if (creditcard.Billing.Address.District != null)
                        {
                            data["billingAddressDistrict"] = creditcard.Billing.Address.District;
                        }
                        if (creditcard.Billing.Address.PostalCode != null)
                        {
                            data["billingAddressPostalCode"] = creditcard.Billing.Address.PostalCode;
                        }
                        if (creditcard.Billing.Address.Country != null)
                        {
                            data["billingAddressCountry"] = creditcard.Billing.Address.Country;
                        }
                    }
                }

                // holder
                if (creditcard.Holder != null)
                {
                    //holder name
                    if (creditcard.Holder.Name != null)
                    {
                        data["creditCardHolderName"] = creditcard.Holder.Name;
                    }
                    //holder phone
                    if (creditcard.Holder.Phone != null)
                    {
                        if (creditcard.Holder.Phone.AreaCode != null)
                        {
                            data["creditCardHolderAreaCode"] = creditcard.Holder.Phone.AreaCode;
                        }
                        if (creditcard.Holder.Phone.Number != null)
                        {
                            data["creditCardHolderPhone"] = creditcard.Holder.Phone.Number;
                        }
                    }
                    //holder document
                    if (creditcard.Holder.Document != null)
                    {
                        if (creditcard.Holder.Document.Value != null)
                        {
                            data["creditCardHolderCPF"] = creditcard.Holder.Document.Value;
                        }
                    }
                    //holder birth date
                    if (creditcard.Holder.Birthdate != null)
                    {
                        data["creditCardHolderBirthDate"] = creditcard.Holder.Birthdate;
                    }
                }

                // token
                if (creditcard.Token != null)
                {
                    data["creditCardToken"] = creditcard.Token;
                }

                // installment
                if (creditcard.Installment != null)
                {
                    if (creditcard.Installment.Quantity > 0)
                    {
                        data["installmentQuantity"] = creditcard.Installment.Quantity.ToString();
                    }
                    if (creditcard.Installment.Value > 0)
                    {
                        data["installmentValue"] = PagSeguroUtil.DecimalFormat((decimal)creditcard.Installment.Value);
                    }
                    if (creditcard.Installment.NoInterestInstallmentQuantity > 0)
                    {
                        data["noInterestInstallmentQuantity"] = creditcard.Installment.NoInterestInstallmentQuantity.ToString();
                    }
                }

                // payment method
                if (creditcard.PaymentMethod != null)
                {
                    data["paymentMethod"] = creditcard.PaymentMethod;
                }
            }


            //Verify if exists the boleto checkout data
            if (checkout is BoletoCheckout)
            {
                BoletoCheckout boleto = (BoletoCheckout)checkout;

                // payment method
                if (boleto.PaymentMethod != null)
                {
                    data["paymentMethod"] = boleto.PaymentMethod;
                }
            }

            //Verify if exists the online debit checkout data
            if (checkout is OnlineDebitCheckout)
            {
                OnlineDebitCheckout onlineDebit = (OnlineDebitCheckout)checkout;

                // payment method
                if (onlineDebit.PaymentMethod != null)
                {
                    data["paymentMethod"] = onlineDebit.PaymentMethod;
                }

                // bank name
                if (onlineDebit.BankName != null)
                {
                    data["bankName"] = onlineDebit.BankName;
                }
            }

            return(data);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="payment"></param>
        /// <returns></returns>
        public static IDictionary <string, string> GetData(PaymentRequest payment)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            // reference
            if (payment.Reference != null)
            {
                data["reference"] = payment.Reference;
            }

            // sender
            if (payment.Sender != null)
            {
                if (payment.Sender.Name != null)
                {
                    data["senderName"] = payment.Sender.Name;
                }
                if (payment.Sender.Email != null)
                {
                    data["senderEmail"] = payment.Sender.Email;
                }

                // phone
                if (payment.Sender.Phone != null)
                {
                    if (payment.Sender.Phone.AreaCode != null)
                    {
                        data["senderAreaCode"] = payment.Sender.Phone.AreaCode;
                    }
                    if (payment.Sender.Phone.Number != null)
                    {
                        data["senderPhone"] = payment.Sender.Phone.Number;
                    }
                }

                // documents
                if (payment.Sender.Documents != null)
                {
                    var documents = payment.Sender.Documents;
                    if (documents.Count == 1)
                    {
                        foreach (SenderDocument document in documents)
                        {
                            if (document != null)
                            {
                                if (document.Type.Equals("Cadastro de Pessoa Física"))
                                {
                                    data["senderCPF"] = document.Value;
                                }
                                else
                                {
                                    data["senderCNPJ"] = document.Value;
                                }
                            }
                        }
                    }
                }
            }

            // currency
            if (payment.Currency != null)
            {
                data["currency"] = payment.Currency;
            }

            // items
            if (payment.Items.Count > 0)
            {
                var items = payment.Items;
                int i     = 0;
                foreach (Item item in items)
                {
                    i++;

                    if (item.Id != null)
                    {
                        data["itemId" + i] = item.Id;
                    }
                    if (item.Description != null)
                    {
                        data["itemDescription" + i] = item.Description;
                    }

                    data["itemQuantity" + i] = item.Quantity.ToString();
                    data["itemAmount" + i]   = PagSeguroUtil.DecimalFormat(item.Amount);

                    if (item.Weight != null)
                    {
                        data["itemWeight" + i] = item.Weight.ToString();
                    }
                    if (item.ShippingCost != null)
                    {
                        data["itemShippingCost" + i] = PagSeguroUtil.DecimalFormat((decimal)item.ShippingCost);
                    }
                }
            }

            //preApproval
            if (payment.PreApproval != null)
            {
                data["preApprovalCharge"]           = payment.PreApproval.Charge;
                data["preApprovalName"]             = payment.PreApproval.Name;
                data["preApprovalDetails"]          = payment.PreApproval.Details;
                data["preApprovalPeriod"]           = payment.PreApproval.Period;
                data["preApprovalFinalDate"]        = payment.PreApproval.FinalDate.ToString("yyyy-MM-dd") + "T01:00:00.45-03:00";
                data["preApprovalMaxTotalAmount"]   = payment.PreApproval.MaxTotalAmount.ToString("F").Replace(",", ".");
                data["preApprovalAmountPerPayment"] = payment.PreApproval.AmountPerPayment.ToString("F").Replace(",", ".");

                if (payment.PreApproval.Charge == Charge.Manual)
                {
                    data["preApprovalInitialDate"]          = payment.PreApproval.InitialDate.ToString("yyyy-MM-dd") + "T01:00:00.45-03:00";
                    data["preApprovalMaxAmountPerPeriod"]   = payment.PreApproval.MaxAmountPerPeriod.ToString("F").Replace(",", ".");
                    data["preApprovalMaxPaymentsPerPeriod"] = payment.PreApproval.MaxPaymentsPerPeriod.ToString();

                    if (payment.PreApproval.Period == Period.Yearly)
                    {
                        data["preApprovalDayOfYear"] = payment.PreApproval.DayOfYear.ToString();
                    }

                    if (payment.PreApproval.Period == Period.Monthly || payment.PreApproval.Period == Period.Bimonthly || payment.PreApproval.Period == Period.Trimonthly || payment.PreApproval.Period == Period.SemiAnnually)
                    {
                        data["preApprovalDayOfMonth"] = payment.PreApproval.DayOfMonth.ToString();
                    }

                    if (payment.PreApproval.Period == Period.Weekly)
                    {
                        data["preApprovalDayOfWeek"] = payment.PreApproval.DayOfWeek.ToString();
                    }
                }

                data["reviewUrl"] = payment.ReviewUri.ToString();
            }

            //preApproval payment
            if (payment.PreApprovalCode != null)
            {
                data["preApprovalCode"] = payment.PreApprovalCode;
            }

            // extraAmount
            if (payment.ExtraAmount != null)
            {
                data["extraAmount"] = PagSeguroUtil.DecimalFormat((decimal)payment.ExtraAmount);
            }

            // shipping
            if (payment.Shipping != null)
            {
                if (payment.Shipping.ShippingType != null && payment.Shipping.ShippingType.HasValue)
                {
                    data["shippingType"] = payment.Shipping.ShippingType.Value.ToString();
                }

                if (payment.Shipping.Cost != null)
                {
                    data["shippingCost"] = PagSeguroUtil.DecimalFormat((decimal)payment.Shipping.Cost);
                }

                // address
                if (payment.Shipping.Address != null)
                {
                    if (payment.Shipping.Address.Street != null)
                    {
                        data["shippingAddressStreet"] = payment.Shipping.Address.Street;
                    }
                    if (payment.Shipping.Address.Number != null)
                    {
                        data["shippingAddressNumber"] = payment.Shipping.Address.Number;
                    }
                    if (payment.Shipping.Address.Complement != null)
                    {
                        data["shippingAddressComplement"] = payment.Shipping.Address.Complement;
                    }
                    if (payment.Shipping.Address.City != null)
                    {
                        data["shippingAddressCity"] = payment.Shipping.Address.City;
                    }
                    if (payment.Shipping.Address.State != null)
                    {
                        data["shippingAddressState"] = payment.Shipping.Address.State;
                    }
                    if (payment.Shipping.Address.District != null)
                    {
                        data["shippingAddressDistrict"] = payment.Shipping.Address.District;
                    }
                    if (payment.Shipping.Address.PostalCode != null)
                    {
                        data["shippingAddressPostalCode"] = payment.Shipping.Address.PostalCode;
                    }
                    if (payment.Shipping.Address.Country != null)
                    {
                        data["shippingAddressCountry"] = payment.Shipping.Address.Country;
                    }
                }
            }

            // maxAge
            if (payment.MaxAge != null)
            {
                data["maxAge"] = payment.MaxAge.ToString();
            }
            // maxUses
            if (payment.MaxUses != null)
            {
                data["maxUses"] = payment.MaxUses.ToString();
            }

            // redirectURL
            if (payment.RedirectUri != null)
            {
                data["redirectURL"] = payment.RedirectUri.ToString();
            }

            // notificationURL
            if (payment.NotificationURL != null)
            {
                data["notificationURL"] = payment.NotificationURL;
            }

            // metadata
            if (payment.MetaData.Items.Count > 0)
            {
                int i             = 0;
                var metaDataItems = payment.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 (payment.Parameter.Items.Count > 0)
            {
                var parameterItems = payment.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;
                        }
                    }
                }
            }

            // paymentMethodConfig
            if (payment.PaymentMethodConfig.Items.Count > 0)
            {
                int i           = 0;
                var configItems = payment.PaymentMethodConfig.Items;
                foreach (PaymentMethodConfigItem item in configItems)
                {
                    if (!PagSeguroUtil.IsEmpty(item.Key) && !PagSeguroUtil.IsEmpty(item.Group))
                    {
                        i++;
                        data["paymentMethodGroup" + i]            = item.Group;
                        data["paymentMethodConfigKey" + i + "_1"] = item.Key;
                        if (item.Key.Equals(PaymentMethodConfigKeys.DiscountPercent))
                        {
                            data["paymentMethodConfigValue" + i + "_1"] = PagSeguroUtil.DecimalFormat(item.Value);
                        }
                        else
                        {
                            data["paymentMethodConfigValue" + i + "_1"] = PagSeguroUtil.DoubleToInt(item.Value);
                        }
                    }
                }
            }

            // paymentMethodConfig
            if (payment.AcceptedPaymentMethods.Items.Count > 0)
            {
                var acceptGroupList  = new List <string>();
                var acceptNameList   = new List <string>();
                var excludeGroupList = new List <string>();
                var excludeNameList  = new List <string>();
                var config           = payment.AcceptedPaymentMethods.Items;

                foreach (AcceptedPayments item in config)
                {
                    if (item.GetType() == typeof(AcceptPaymentMethod))
                    {
                        if (!acceptGroupList.Contains(item.Group))
                        {
                            acceptGroupList.Add(item.Group);
                        }
                        acceptNameList = item.Name;
                    }
                    if (item.GetType() == typeof(ExcludePaymentMethod))
                    {
                        if (!excludeGroupList.Contains(item.Group))
                        {
                            excludeGroupList.Add(item.Group);
                        }
                        excludeNameList = item.Name;
                    }
                }

                if (acceptGroupList.Count > 0 && acceptNameList.Count > 0)
                {
                    data["acceptPaymentMethodGroup"] = String.Join(",", acceptGroupList.ToArray());
                    data["acceptPaymentMethodName"]  = String.Join(",", acceptNameList.ToArray());
                }
                if (excludeGroupList.Count > 0 && excludeNameList.Count > 0)
                {
                    data["excludePaymentMethodGroup"] = String.Join(",", excludeGroupList.ToArray());
                    data["excludePaymentMethodName"]  = String.Join(",", excludeNameList.ToArray());
                }
            }

            return(data);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="checkout"></param>
        /// <returns></returns>
        public static IDictionary <string, string> GetData(Checkout checkout)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            // payment mode
            if (checkout.PaymentMode)
            {
                data["paymentMode"] = checkout.PaymentMode;
            }

            // receiver e-mail
            if (checkout.ReceiverEmail)
            {
                data["receiverEmail"] = checkout.ReceiverEmail;
            }

            // reference
            if (checkout.Reference)
            {
                data["reference"] = checkout.Reference;
            }

            // sender
            if (checkout.Sender)
            {
                if (checkout.Sender.Name)
                {
                    data["senderName"] = checkout.Sender.Name;
                }

                if (checkout.Sender.Email)
                {
                    data["senderEmail"] = checkout.Sender.Email;
                }

                if (checkout.Sender.Hash)
                {
                    data["senderHash"] = checkout.Sender.Hash;
                }

                // phone
                if (checkout.Sender.Phone)
                {
                    if (checkout.Sender.Phone.AreaCode)
                    {
                        data["senderAreaCode"] = checkout.Sender.Phone.AreaCode;
                    }

                    if (checkout.Sender.Phone.Number)
                    {
                        data["senderPhone"] = checkout.Sender.Phone.Number;
                    }
                }

                // documents
                if (checkout.Sender.Documents)
                {
                    var documents = checkout.Sender.Documents;
                    if (documents.Count == 1)
                    {
                        foreach (var document in documents)
                        {
                            if (document)
                            {
                                if (document.Type.Equals("Cadastro de Pessoa Física"))
                                {
                                    data["senderCPF"] = document.Value;
                                }
                                else
                                {
                                    data["senderCNPJ"] = document.Value;
                                }
                            }
                        }
                    }
                }
            }

            // currency
            if (checkout.Currency)
            {
                data["currency"] = checkout.Currency;
            }

            // items
            if (checkout.Items.Count > 0)
            {
                var i     = 0;
                var items = checkout.Items;
                foreach (var item in items)
                {
                    i++;

                    if (item.Id)
                    {
                        data["itemId" + i] = item.Id;
                    }

                    if (item.Description)
                    {
                        data["itemDescription" + i] = item.Description;
                    }

                    data["itemQuantity" + i] = item.Quantity.ToString();
                    data["itemAmount" + i]   = PagSeguroUtil.DecimalFormat(item.Amount);

                    if (item.Weight)
                    {
                        data["itemWeight" + i] = item.Weight.ToString();
                    }

                    if (item.ShippingCost)
                    {
                        data["itemShippingCost" + i] = PagSeguroUtil.DecimalFormat((decimal)item.ShippingCost);
                    }
                }
            }

            // extraAmount
            if (checkout.ExtraAmount)
            {
                data["extraAmount"] = PagSeguroUtil.DecimalFormat((decimal)checkout.ExtraAmount);
            }

            // shipping
            if (checkout.Shipping)
            {
                if (checkout.Shipping.ShippingType.HasValue)
                {
                    data["shippingType"] = checkout.Shipping.ShippingType.Value.ToString();
                }

                if (checkout.Shipping.Cost.HasValue)
                {
                    data["shippingCost"] = PagSeguroUtil.DecimalFormat(checkout.Shipping.Cost.Value);
                }

                // address
                if (checkout.Shipping.Address)
                {
                    if (checkout.Shipping.Address.Street)
                    {
                        data["shippingAddressStreet"] = checkout.Shipping.Address.Street;
                    }

                    if (checkout.Shipping.Address.Number)
                    {
                        data["shippingAddressNumber"] = checkout.Shipping.Address.Number;
                    }

                    if (checkout.Shipping.Address.Complement)
                    {
                        data["shippingAddressComplement"] = checkout.Shipping.Address.Complement;
                    }

                    if (checkout.Shipping.Address.City)
                    {
                        data["shippingAddressCity"] = checkout.Shipping.Address.City;
                    }

                    if (checkout.Shipping.Address.State)
                    {
                        data["shippingAddressState"] = checkout.Shipping.Address.State;
                    }

                    if (checkout.Shipping.Address.District)
                    {
                        data["shippingAddressDistrict"] = checkout.Shipping.Address.District;
                    }

                    if (checkout.Shipping.Address.PostalCode)
                    {
                        data["shippingAddressPostalCode"] = checkout.Shipping.Address.PostalCode;
                    }

                    if (checkout.Shipping.Address.Country)
                    {
                        data["shippingAddressCountry"] = checkout.Shipping.Address.Country;
                    }
                }
            }

            // maxAge
            if (checkout.MaxAge)
            {
                data["maxAge"] = checkout.MaxAge.ToString();
            }

            // maxUses
            if (checkout.MaxUses)
            {
                data["maxUses"] = checkout.MaxUses.ToString();
            }

            // notificationURL
            if (checkout.NotificationUrlnull)
            {
                data["notificationURL"] = checkout.NotificationUrl;
            }

            // metadata
            if (checkout.MetaData.Items.Count > 0)
            {
                var i             = 0;
                var metaDataItems = checkout.MetaData.Items;
                foreach (var item in metaDataItems)
                {
                    if (PagSeguroUtil.IsEmpty(item.Key) || PagSeguroUtil.IsEmpty(item.Value))
                    {
                        continue;
                    }

                    i++;
                    data["metadataItemKey" + i]   = item.Key;
                    data["metadataItemValue" + i] = item.Value;

                    if (item.Group)
                    {
                        data["metadataItemGroup" + i] = item.Group.ToString();
                    }
                }
            }

            // parameter
            if (checkout.Parameter.Items.Count > 0)
            {
                var parameterItems = checkout.Parameter.Items;
                foreach (var item in parameterItems)
                {
                    if (PagSeguroUtil.IsEmpty(item.Key) || PagSeguroUtil.IsEmpty(item.Value))
                    {
                        continue;
                    }

                    { if (item.Group)
                      {
                          data[item.Key + "" + item.Group] = item.Value;
                      }
                      else
                      {
                          data[item.Key] = item.Value;
                      } }
                }
            }
            switch (checkout)
            {
            //Verify if exists the credit card checkout data
            case CreditCardCheckout creditcard:
                // billing address
                if (creditcard.Billing?.Address)
                {
                    if (creditcard.Billing.Address.Street)
                    {
                        data["billingAddressStreet"] = creditcard.Billing.Address.Street;
                    }

                    if (creditcard.Billing.Address.Number)
                    {
                        data["billingAddressNumber"] = creditcard.Billing.Address.Number;
                    }

                    if (creditcard.Billing.Address.Complement)
                    {
                        data["billingAddressComplement"] = creditcard.Billing.Address.Complement;
                    }

                    if (creditcard.Billing.Address.City)
                    {
                        data["billingAddressCity"] = creditcard.Billing.Address.City;
                    }

                    if (creditcard.Billing.Address.State)
                    {
                        data["billingAddressState"] = creditcard.Billing.Address.State;
                    }

                    if (creditcard.Billing.Address.District)
                    {
                        data["billingAddressDistrict"] = creditcard.Billing.Address.District;
                    }

                    if (creditcard.Billing.Address.PostalCode)
                    {
                        data["billingAddressPostalCode"] = creditcard.Billing.Address.PostalCode;
                    }

                    if (creditcard.Billing.Address.Country)
                    {
                        data["billingAddressCountry"] = creditcard.Billing.Address.Country;
                    }
                }

                // holder
                if (creditcard.Holder)
                {
                    //holder name
                    if (creditcard.Holder.Name)
                    {
                        data["creditCardHolderName"] = creditcard.Holder.Name;
                    }

                    //holder phone
                    if (creditcard.Holder.Phone)
                    {
                        if (creditcard.Holder.Phone.AreaCode)
                        {
                            data["creditCardHolderAreaCode"] = creditcard.Holder.Phone.AreaCode;
                        }

                        if (creditcard.Holder.Phone.Number)
                        {
                            data["creditCardHolderPhone"] = creditcard.Holder.Phone.Number;
                        }
                    }

                    //holder document
                    if (creditcard.Holder.Document?.Value)
                    {
                        data["creditCardHolderCPF"] = creditcard.Holder.Document.Value;
                    }

                    //holder birth date
                    if (creditcard.Holder.Birthdate)
                    {
                        data["creditCardHolderBirthDate"] = creditcard.Holder.Birthdate;
                    }
                }

                // token
                if (creditcard.Token)
                {
                    data["creditCardToken"] = creditcard.Token;
                }

                // installment
                if (creditcard.Installment)
                {
                    if (creditcard.Installment.Quantity > 0)
                    {
                        data["installmentQuantity"] = creditcard.Installment.Quantity.ToString();
                    }

                    if (creditcard.Installment.Value > 0)
                    {
                        data["installmentValue"] = PagSeguroUtil.DecimalFormat(creditcard.Installment.Value);
                    }

                    if (creditcard.Installment.NoInterestInstallmentQuantity > 0)
                    {
                        data["noInterestInstallmentQuantity"] = creditcard.Installment.NoInterestInstallmentQuantity.ToString();
                    }
                }

                // payment method
                if (creditcard.PaymentMethod)
                {
                    data["paymentMethod"] = creditcard.PaymentMethod;
                }

                break;

            default:
                throw new InvalidOperationException("Unexpected value");

            //Verify if exists the boleto checkout data
            case BoletoCheckout _:
                var boleto = (BoletoCheckout)checkout;

                // payment method
                if (boleto.PaymentMethod)
                {
                    data["paymentMethod"] = boleto.PaymentMethod;
                }

                break;

            //Verify if exists the online debit checkout data
            case OnlineDebitCheckout _:
                var onlineDebit = (OnlineDebitCheckout)checkout;

                // payment method
                if (onlineDebit.PaymentMethod)
                {
                    data["paymentMethod"] = onlineDebit.PaymentMethod;
                }

                // bank name
                if (onlineDebit.BankName)
                {
                    data["bankName"] = onlineDebit.BankName;
                }

                break;
            }

            return(data);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="payment"></param>
        /// <returns></returns>
        public static IDictionary <string, string> GetData(PaymentRequest payment)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            // reference
            if (payment.Reference != null)
            {
                data["reference"] = payment.Reference;
            }

            // sender
            if (payment.Sender != null)
            {
                if (payment.Sender.Name != null)
                {
                    data["senderName"] = payment.Sender.Name;
                }
                if (payment.Sender.Email != null)
                {
                    data["senderEmail"] = payment.Sender.Email;
                }

                // phone
                if (payment.Sender.Phone != null)
                {
                    if (payment.Sender.Phone.AreaCode != null)
                    {
                        data["senderAreaCode"] = payment.Sender.Phone.AreaCode;
                    }
                    if (payment.Sender.Phone.Number != null)
                    {
                        data["senderPhone"] = payment.Sender.Phone.Number;
                    }
                }

                // documents
                if (payment.Sender.Documents != null)
                {
                    var documents = payment.Sender.Documents;
                    if (documents.Count == 1)
                    {
                        foreach (SenderDocument document in documents)
                        {
                            if (document != null)
                            {
                                data["senderCPF"] = document.Value;
                            }
                        }
                    }
                }
            }

            // currency
            if (payment.Currency != null)
            {
                data["currency"] = payment.Currency;
            }

            // items
            if (payment.Items.Count > 0)
            {
                var items = payment.Items;
                int i     = 0;
                foreach (Item item in items)
                {
                    i++;

                    if (item.Id != null)
                    {
                        data["itemId" + i] = item.Id;
                    }
                    if (item.Description != null)
                    {
                        data["itemDescription" + i] = item.Description;
                    }
                    if (item.Quantity != null)
                    {
                        data["itemQuantity" + i] = item.Quantity.ToString();
                    }
                    if (item.Amount != null)
                    {
                        data["itemAmount" + i] = PagSeguroUtil.DecimalFormat(item.Amount);
                    }
                    if (item.Weight != null)
                    {
                        data["itemWeight" + i] = item.Weight.ToString();
                    }
                    if (item.ShippingCost != null)
                    {
                        data["itemShippingCost" + i] = PagSeguroUtil.DecimalFormat((decimal)item.ShippingCost);
                    }
                }
            }

            // extraAmount
            if (payment.ExtraAmount != null)
            {
                data["extraAmount"] = PagSeguroUtil.DecimalFormat((decimal)payment.ExtraAmount);
            }

            // shipping
            if (payment.Shipping != null)
            {
                if (payment.Shipping.ShippingType != null && payment.Shipping.ShippingType.Value != null)
                {
                    data["shippingType"] = payment.Shipping.ShippingType.Value.ToString();
                }

                if (payment.Shipping.Cost != null)
                {
                    data["shippingCost"] = PagSeguroUtil.DecimalFormat((decimal)payment.Shipping.Cost);
                }

                // address
                if (payment.Shipping.Address != null)
                {
                    if (payment.Shipping.Address.Street != null)
                    {
                        data["shippingAddressStreet"] = payment.Shipping.Address.Street;
                    }
                    if (payment.Shipping.Address.Number != null)
                    {
                        data["shippingAddressNumber"] = payment.Shipping.Address.Number;
                    }
                    if (payment.Shipping.Address.Complement != null)
                    {
                        data["shippingAddressComplement"] = payment.Shipping.Address.Complement;
                    }
                    if (payment.Shipping.Address.City != null)
                    {
                        data["shippingAddressCity"] = payment.Shipping.Address.City;
                    }
                    if (payment.Shipping.Address.State != null)
                    {
                        data["shippingAddressState"] = payment.Shipping.Address.State;
                    }
                    if (payment.Shipping.Address.District != null)
                    {
                        data["shippingAddressDistrict"] = payment.Shipping.Address.District;
                    }
                    if (payment.Shipping.Address.PostalCode != null)
                    {
                        data["shippingAddressPostalCode"] = payment.Shipping.Address.PostalCode;
                    }
                    if (payment.Shipping.Address.Country != null)
                    {
                        data["shippingAddressCountry"] = payment.Shipping.Address.Country;
                    }
                }
            }

            // maxAge
            if (payment.MaxAge != null)
            {
                data["maxAge"] = payment.MaxAge.ToString();
            }
            // maxUses
            if (payment.MaxUses != null)
            {
                data["maxUses"] = payment.MaxUses.ToString();
            }

            // redirectURL
            if (payment.RedirectUri != null)
            {
                data["redirectURL"] = payment.RedirectUri.ToString();
            }

            // notificationURL
            if (payment.NotificationURL != null)
            {
                data["notificationURL"] = payment.NotificationURL;
            }

            // metadata
            if (payment.MetaData.Items.Count > 0)
            {
                int i             = 0;
                var metaDataItems = payment.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 (payment.Parameter.Items.Count > 0)
            {
                var parameterItems = payment.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);
        }