Ejemplo n.º 1
0
 private static bool IsConfigured(PaymentGatewayCollection gateways, string classId)
 {
     foreach (PaymentGateway gateway in gateways)
     {
         if (gateway.ClassId.Equals(classId))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        public static PaymentGatewayCollection LoadForStore(int maximumRows, int startRowIndex, string sortExpression)
        {
            int storeId = Token.Instance.StoreId;
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT");
            if (maximumRows > 0)
            {
                selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
            }
            selectQuery.Append(" " + PaymentGateway.GetColumnNames(string.Empty));
            selectQuery.Append(" FROM ac_PaymentGateways");
            selectQuery.Append(" WHERE StoreId = @storeId");
            if (!string.IsNullOrEmpty(sortExpression))
            {
                selectQuery.Append(" ORDER BY " + sortExpression);
            }
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, storeId);
            //EXECUTE THE COMMAND
            PaymentGatewayCollection results = new PaymentGatewayCollection();
            int thisIndex = 0;
            int rowCount  = 0;

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
                {
                    if (thisIndex >= startRowIndex)
                    {
                        PaymentGateway paymentGateway = new PaymentGateway();
                        PaymentGateway.LoadDataReader(paymentGateway, dr);
                        results.Add(paymentGateway);
                        rowCount++;
                    }
                    thisIndex++;
                }
                dr.Close();
            }
            return(results);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets instances of all classes available that implement the IPaymentProvider interface
        /// </summary>
        /// <returns>List of instances of classes implementing IPaymentProvider</returns>
        public static List <IPaymentProvider> GetProviders()
        {
            List <IPaymentProvider>  providers          = new List <IPaymentProvider>();
            List <string>            providerNames      = new List <string>();
            PaymentGatewayCollection configuredGateways = PaymentGatewayDataSource.LoadForStore();

            //LOOP THROUGH ALL THE ASSEMBLIES IN THE CURRENT DOMAIN
            foreach (System.Reflection.Assembly assemblyInstance in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    //LOOP THROUGH ALL TYPES IN THE ASSEMBLY
                    foreach (Type thisType in assemblyInstance.GetTypes())
                    {
                        //ONLY CHECK CLASSES THAT ARE NOT ABSTRACT
                        if ((thisType.IsClass && !thisType.IsAbstract))
                        {
                            //LOOP THROUGH ALL INTERFACES THIS TYPE IMPLMEMENTS
                            foreach (Type thisInterface in thisType.GetInterfaces())
                            {
                                //ONLY PROCEED IF THE CLASS IMPLEMENTS PAYMENT PROVIDER INTERFACE
                                string interfaceFullName = thisInterface.FullName;
                                if (!string.IsNullOrEmpty(interfaceFullName) && (interfaceFullName == "CommerceBuilder.Payments.Providers.IPaymentProvider"))
                                {
                                    //GET THE CLASS ID WITHOUT VERSION
                                    string classId = Utility.Misc.GetClassId(thisType);
                                    //MAKE SURE WE ONLY LIST THIS PROVIDER ONE TIME
                                    string loweredClassId = classId.ToLowerInvariant();
                                    if (!providerNames.Contains(loweredClassId))
                                    {
                                        //DO NOT INCLUDE GIFT CERTIFICATE PROVIDER
                                        if (classId != Utility.Misc.GetClassId(typeof(GiftCertificatePaymentProvider)))
                                        {
                                            //ONLY INCLUDE GATEWAYS NOT CONFIGURED ALREADY
                                            if (!IsConfigured(configuredGateways, classId))
                                            {
                                                try
                                                {
                                                    //ONLY INCLUDE GATEWAYS THAT WE CAN CREATE INSTANCES OF
                                                    IPaymentProvider instance = Activator.CreateInstance(Type.GetType(classId)) as IPaymentProvider;
                                                    if (instance != null)
                                                    {
                                                        providers.Add(instance);
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    Logger.Warn("Could not create an instance of the payment provider: " + classId, ex);
                                                    throw;
                                                }
                                            }
                                        }
                                        //DO NOT LIST THIS PROVIDER AGAIN
                                        providerNames.Add(loweredClassId);
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //ignore error
                }
            }

            //SORT BY NAME
            providers.Sort(CompareProvidersByName);
            return(providers);
        }