Пример #1
0
        public GenericSvcResult ActivateService(ProvisioningContext context)
        {
            GenericSvcResult result = new GenericSvcResult();

            // remeber svc state
            SaveObjectState(SERVICE_INFO, context.ServiceInfo);

            // concretize service to be provisioned
            DomainNameSvc domainSvc = (DomainNameSvc)context.ServiceInfo;
            // concretize parent service
            HostingPackageSvc packageSvc = (HostingPackageSvc)context.ParentSvcInfo;

            try
            {
                // LOG INFO
                TaskManager.StartTask(SystemTasks.SOURCE_ECOMMERCE, SystemTasks.SVC_ACTIVATE);
                TaskManager.WriteParameter(CONTRACT_PARAM, domainSvc.ContractId);
                TaskManager.WriteParameter(SVC_PARAM, domainSvc.ServiceName);
                TaskManager.WriteParameter(SVC_ID_PARAM, domainSvc.ServiceId);

                // 0. Do security checks
                if (!CheckOperationClientPermissions(result))
                {
                    // LOG ERROR
                    TaskManager.WriteError(ERROR_CLIENT_OPERATION_PERMISSIONS);
                    TaskManager.WriteParameter(RESULT_CODE_PARAM, result.ResultCode);
                    // EXIT
                    return(result);
                }
                //
                if (!CheckOperationClientStatus(result))
                {
                    // LOG ERROR
                    TaskManager.WriteError(ERROR_CLIENT_OPERATION_STATUS);
                    TaskManager.WriteParameter(RESULT_CODE_PARAM, result.ResultCode);
                    // EXIT
                    return(result);
                }

                // error: hosting addon should have parent svc assigned
                if (packageSvc == null || packageSvc.PackageId == 0)
                {
                    result.Succeed = false;
                    //
                    result.Error = PARENT_SVC_NOT_FOUND_MSG;
                    //
                    result.ResultCode = EcommerceErrorCodes.ERROR_PARENT_SVC_NOT_FOUND;

                    // LOG ERROR
                    TaskManager.WriteError(result.Error);
                    TaskManager.WriteParameter(RESULT_CODE_PARAM, result.ResultCode);

                    // EXIT
                    return(result);
                }

                // first of all - create internal domain in WebsitePanel
                if (domainSvc.Status == ServiceStatus.Ordered)
                {
                    // create domain info object
                    DomainInfo domain = ServerController.GetDomain(domainSvc.Fqdn);
                    //
                    if (domain != null)
                    {
                        domainSvc.DomainId = domain.DomainId;
                    }
                    //
                    if (domain == null)
                    {
                        domain                = new DomainInfo();
                        domain.DomainName     = domainSvc.Fqdn;
                        domain.HostingAllowed = false;
                        domain.PackageId      = packageSvc.PackageId;
                        // add internal domain
                        domainSvc.DomainId = ServerController.AddDomain(domain);
                        // check API result
                        if (domainSvc.DomainId < 1)
                        {
                            // ASSEMBLE ERROR
                            result.Succeed = false;
                            // try to find corresponding error code->error message mapping
                            if (ApiErrorCodesMap.ContainsKey(domainSvc.DomainId))
                            {
                                result.Error = ApiErrorCodesMap[domainSvc.DomainId];
                            }
                            else
                            {
                                result.Error = ERROR_ADD_INTERNAL_DOMAIN;
                            }
                            // copy result code
                            result.ResultCode = domainSvc.DomainId;

                            // LOG ERROR
                            TaskManager.WriteError(result.Error);
                            TaskManager.WriteParameter(RESULT_CODE_PARAM, result.ResultCode);

                            // EXIT
                            return(result);
                        }
                    }
                }

                // update nameservers only
                if (domainSvc["SPF_ACTION"] == "UPDATE_NS")
                {
                    // remove service here...
                    ServiceController.DeleteCustomerService(domainSvc.ServiceId);
                    //
                    result.Succeed = true;
                    // EXIT
                    return(result);
                }

                // load registrar wrapper
                IDomainRegistrar registrar = (IDomainRegistrar)SystemPluginController.GetSystemPluginInstance(
                    domainSvc.ContractId, domainSvc.PluginId, true);

                #region Commented operations
                // prepare consumer account information

                /*CommandParams cmdParams = PrepeareAccountParams(context.ConsumerInfo);
                 * // copy svc properties
                 * foreach (string keyName in domainSvc.GetAllKeys())
                 *      cmdParams[keyName] = domainSvc[keyName];
                 *
                 * // check registrar requires sub-account to be created
                 * if (registrar.SubAccountRequired)
                 * {
                 *      // 1. Load user's settings
                 *      UserSettings userSettings = LoadUserSettings(context.ConsumerInfo.UserId, registrar.PluginName);
                 *      // 2. Ensure user has account on registrar's side
                 *      if (userSettings.SettingsArray == null || userSettings.SettingsArray.Length == 0)
                 *      {
                 *              // 3. Check account exists
                 *              bool exists = registrar.CheckSubAccountExists(context.ConsumerInfo.Username, context.ConsumerInfo.Email);
                 *              //
                 *              AccountResult accResult = null;
                 *              //
                 *              if (!exists)
                 *              {
                 *                      // 4. Create user account
                 *                      accResult = registrar.CreateSubAccount(cmdParams);
                 *                      // copy keys & values
                 *                      foreach (string keyName in accResult.AllKeys)
                 *                      {
                 *                              userSettings[keyName] = accResult[keyName];
                 *                      }
                 *              }
                 *              else
                 *              {
                 *                      // 4a. Get sub-account info
                 *                      accResult = registrar.GetSubAccount(context.ConsumerInfo.Username,
                 *                              context.ConsumerInfo.Email);
                 *                      //
                 *                      foreach (string keyName in accResult.AllKeys)
                 *                              userSettings[keyName] = accResult[keyName];
                 *              }
                 *              // 5. Update user settings
                 *              int apiResult = UserController.UpdateUserSettings(userSettings);
                 *              // check API result
                 *              if (apiResult < 0)
                 *              {
                 *                      // BUILD ERROR
                 *                      result.Error = ERROR_UPDATE_USR_SETTINGS_MSG;
                 *                      result.Succeed = false;
                 *                      result.ResultCode = apiResult;
                 *                      // LOG ERROR
                 *                      TaskManager.WriteError(result.Error);
                 *                      TaskManager.WriteParameter(RESULT_CODE_PARAM, result.ResultCode);
                 *                      // ROLLBACK
                 *                      RollbackOperation(domainSvc.DomainId);
                 *                      // EXIT
                 *                      return result;
                 *              }
                 *      }
                 *      // copy registrar-specific data
                 *      foreach (string[] pair in userSettings.SettingsArray)
                 *      {
                 *              // copy 2
                 *              cmdParams[pair[0]] = pair[1];
                 *      }
                 * }*/
                #endregion

                // load NS settings
                PackageSettings nsSettings = PackageController.GetPackageSettings(packageSvc.PackageId, PackageSettings.NAME_SERVERS);
                // build name servers array
                string[] nameServers = null;
                if (!String.IsNullOrEmpty(nsSettings[PackageSettings.NAME_SERVERS]))
                {
                    nameServers = nsSettings[PackageSettings.NAME_SERVERS].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                }

                // register or renew domain
                if (domainSvc.Status == ServiceStatus.Ordered)
                {
                    // try to register domain
                    registrar.RegisterDomain(domainSvc, context.ConsumerInfo, nameServers);
                }
                else
                {
                    // try to renew domain
                    registrar.RenewDomain(domainSvc, context.ConsumerInfo, nameServers);
                }

                // change svc status to active
                domainSvc.Status = ServiceStatus.Active;
                // update service info
                int updResult = UpdateServiceInfo(domainSvc);
                // check update result for errors
                if (updResult < 0)
                {
                    // BUILD ERROR
                    result.ResultCode = updResult;
                    result.Succeed    = false;
                    result.Error      = ERROR_SVC_UPDATE_MSG;
                    // LOG ERROR
                    TaskManager.WriteError(result.Error);
                    TaskManager.WriteParameter(RESULT_CODE_PARAM, result.ResultCode);
                    // ROLLBACK
                    RollbackOperation(domainSvc.DomainId);
                    // EXIT
                    return(result);
                }
                //
                result.Succeed = true;
                //
                SetOutboundParameters(context);
            }
            catch (Exception ex)
            {
                // LOG ERROR
                TaskManager.WriteError(ex);
                result.Succeed = false;
                // ROLLBACK
                RollbackOperation(result.ResultCode);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
            //
            return(result);
        }
Пример #2
0
        public void InvoiceActiveServices()
        {
            DateTime dateTimeNow = DateTime.Now;
            // load store settings
            StoreSettings settings = StorehouseController.GetStoreSettings(SecurityContext.User.UserId,
                                                                           StoreSettings.SYSTEM_SETTINGS);
            //
            int threshold = Convert.ToInt32(settings["SvcInvoiceThreshold"]);
            // get expiring services today
            List <Service> services = ServiceController.GetServicesToInvoice(SecurityContext.User.UserId,
                                                                             dateTimeNow, threshold);
            // group services by users
            Dictionary <string, List <int> > usersServices = new Dictionary <string, List <int> >();

            // iterate
            foreach (Service service in services)
            {
                if (!usersServices.ContainsKey(service.ContractId))
                {
                    usersServices.Add(service.ContractId, new List <int>());
                }

                usersServices[service.ContractId].Add(service.ServiceId);
            }
            // generate invoice per contract
            foreach (string contractId in usersServices.Keys)
            {
                try
                {
                    TaskManager.Write("Creating invoice");
                    // TRACE
                    Contract        contract = ContractSystem.ContractController.GetContract(contractId);
                    ContractAccount account  = ContractSystem.ContractController.GetContractAccountSettings(contractId);
                    TaskManager.WriteParameter("ContractID", contractId);
                    TaskManager.WriteParameter("Username", account[ContractAccount.USERNAME]);
                    //
                    List <int> userSvcs = usersServices[contractId];
                    // build invoice items
                    List <InvoiceItem> invoiceLines = InvoiceController.CalculateInvoiceLinesForServices(userSvcs);
                    //
                    int resultCode = InvoiceController.AddInvoice(contractId, invoiceLines, null);
                    //
                    if (resultCode < 1)
                    {
                        TaskManager.WriteParameter("ResultCode", resultCode);
                        continue;
                    }
                    //
                    if (ServiceController.SetUsageRecordsClosed(userSvcs.ToArray()) != 0)
                    {
                        TaskManager.WriteWarning("Unable to close usage records automatically");
                    }
                    // TRACE
                    TaskManager.WriteParameter("InvoiceID", resultCode);
                    TaskManager.Write("Succeed");
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex);
                }
            }
        }