Ejemplo n.º 1
0
        public static List <T> Parse <T, TChildren>(WeChatPayDictionary dictionary) where T : new() where TChildren : new()
        {
            var list       = new List <T>();
            var properties = typeof(T).GetProperties();
            var keyfirst   = properties[0];
            var count      = dictionary.Keys.Where(p => Regex.IsMatch(p, $@"{GetKeyName(keyfirst)}_\d")).Count();

            for (var i = 0; i < count; i++)
            {
                var item = new T();
                foreach (var field in properties)
                {
                    if (field.PropertyType == typeof(List <TChildren>))
                    {
                        var sublist       = new List <TChildren>();
                        var subProperties = typeof(TChildren).GetProperties();
                        var subFirstkey   = subProperties[0];
                        var SubFirstName  = GetKeyName(subFirstkey);
                        var subCount      = dictionary.Keys.Where(p => Regex.IsMatch(p, $@"{SubFirstName}_{i}_\d")).Count();
                        for (var j = 0; j < subCount; j++)
                        {
                            var subItem = new TChildren();
                            foreach (var subfield in subProperties)
                            {
                                var name  = $"{GetKeyName(subfield)}_{i}_{j}";
                                var value = dictionary.GetValue(name);
                                if (!string.IsNullOrEmpty(value))
                                {
                                    subfield.SetValue(item, Convert.ChangeType(value, subfield.PropertyType));
                                }
                            }
                            sublist.Add(subItem);
                        }
                        field.SetValue(item, sublist);
                    }
                    else
                    {
                        var name  = $"{GetKeyName(field)}_{i}";
                        var value = dictionary.GetValue(name);
                        if (!string.IsNullOrEmpty(value))
                        {
                            field.SetValue(item, Convert.ChangeType(value, field.PropertyType));
                        }
                    }
                }
                list.Add(item);
            }

            return(list);
        }
        public List <T> Parse <T, TChildren>(WeChatPayDictionary dic, int index = -1)
        {
            var flag = true;
            var list = new List <T>();
            var i    = 0;

            while (flag)
            {
                var type            = typeof(T);
                var obj             = Activator.CreateInstance(type);
                var properties      = type.GetProperties();
                var isFirstProperty = true;

                foreach (var item in properties)
                {
                    if (item.PropertyType == typeof(List <TChildren>))
                    {
                        var chidrenList = Parse <TChildren, object>(dic, i);
                        item.SetValue(obj, chidrenList);
                        continue;
                    }

                    var renameAttribute = item.GetCustomAttributes(typeof(XmlElementAttribute), true);
                    if (renameAttribute.Length > 0)
                    {
                        var key = ((XmlElementAttribute)renameAttribute[0]).ElementName;

                        if (index > -1)
                        {
                            key += $"_{index}";
                        }
                        key += $"_{i}";

                        var value = dic.GetValue(key);
                        if (value == null)
                        {
                            if (isFirstProperty)
                            {
                                flag = false;
                                break;
                            }
                            continue;
                        }

                        isFirstProperty = false;
                        item.SetValue(obj, Convert.ChangeType(value, item.PropertyType));
                    }
                }

                if (!flag)
                {
                    return(list);
                }

                list.Add((T)obj);
                i++;
            }

            return(list);
        }
Ejemplo n.º 3
0
        public static List <T> Parse <T, TChildren>(WeChatPayDictionary dictionary, int index) where T : new() where TChildren : new()
        {
            var flag = true;
            var list = new List <T>();
            var i    = 0;

            while (flag)
            {
                var type            = typeof(T);
                var obj             = new T();
                var properties      = type.GetProperties();
                var isFirstProperty = true;

                foreach (var item in properties)
                {
                    if (item.PropertyType == typeof(List <TChildren>))
                    {
                        var chidrenList = Parse <TChildren, object>(dictionary, i);
                        item.SetValue(obj, chidrenList);
                        continue;
                    }

                    var key = GetKeyName(item);
                    if (index > -1)
                    {
                        key += $"_{index}";
                    }
                    key += $"_{i}";

                    var value = dictionary.GetValue(key);
                    if (value == null)
                    {
                        if (isFirstProperty)
                        {
                            flag = false;
                            break;
                        }
                        continue;
                    }

                    isFirstProperty = false;
                    item.SetValue(obj, Convert.ChangeType(value, item.PropertyType));
                }

                if (!flag)
                {
                    return(list);
                }

                list.Add(obj);
                i++;
            }

            return(list);
        }
Ejemplo n.º 4
0
        public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams)
        {
            if (string.IsNullOrEmpty(options.RsaPublicKey))
            {
                throw new WeChatPayException("WeChatPayPayBankRequest: RsaPublicKey is null!");
            }

            sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr());
            sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId);

            var no = OaepSHA1WithRSA.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_bank_no), options.RsaPublicKey);

            sortedTxtParams.SetValue(WeChatPayConsts.enc_bank_no, no);

            var name = OaepSHA1WithRSA.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_true_name), options.RsaPublicKey);

            sortedTxtParams.SetValue(WeChatPayConsts.enc_true_name, name);

            sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType));
        }
Ejemplo n.º 5
0
        public void PrimaryHandler(WeChatPayDictionary sortedTxtParams, WeChatPayOptions options)
        {
            sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr());
            sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId);

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(WeChatPayConsts.brand_mch_id)))
            {
                sortedTxtParams.Add(WeChatPayConsts.sub_mch_id, options.SubMchId);
            }

            sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.APIKey, signType));
        }
Ejemplo n.º 6
0
        public List <T> Parse <T>(WeChatPayDictionary dictionary) where T : new()
        {
            var list       = new List <T>();
            var properties = typeof(T).GetProperties();
            var keyfirst   = properties[0];
            var count      = dictionary.Keys.Where(p => Regex.IsMatch(p, $@"{GetKeyName(keyfirst)}_\d")).Count();

            for (var i = 0; i < count; i++)
            {
                var item = new T();
                foreach (var field in properties)
                {
                    var name = $"{GetKeyName(field)}_{i}";
                    field.SetValue(item, Convert.ChangeType(dictionary.GetValue(name), field.PropertyType));
                }
                list.Add(item);
            }
            return(list);
        }
Ejemplo n.º 7
0
 private static string BuildSignatureSourceData(WeChatPayDictionary sortedTxtParams)
 {
     return($"{sortedTxtParams.GetValue(WeChatPayConsts.appId)}\n{sortedTxtParams.GetValue(WeChatPayConsts.timeStamp)}\n{sortedTxtParams.GetValue(WeChatPayConsts.nonceStr)}\n{sortedTxtParams.GetValue(WeChatPayConsts.package)}\n");
 }