/// <summary>
        /// Controlla se l'utente ha abbastanza punti per ricervere un regalo
        /// </summary>
        /// <param name="kvpList">Elenco dei parametri per fare le chiamate</param>
        /// <returns>Restituisce una stringa in formato json</returns>
        protected bool checkCustomerPoints(FidelitySettingsPart setPart, FidelityCustomer customer, FidelityReward reward, FidelityCampaign campaign)
        {
            APIResult <FidelityCustomer> resCustomer = this.SendCustomerDetails(setPart, customer);

            if (!resCustomer.success)
            {
                throw new Exception("customer not fuond");
            }

            APIResult <FidelityCampaign> resCampaign = this.SendCampaignData(setPart, campaign);

            if (!resCampaign.success)
            {
                throw new Exception("campaign " + campaign.Id + " not found");
            }
            customer = resCustomer.data;
            campaign = resCampaign.data;

            if (!campaign.Catalog.ContainsKey(reward.Id))
            {
                throw new Exception("campaign: " + campaign.Id + " not contaign reward: " + reward.Id);
            }

            if (!customer.PointsInCampaign.ContainsKey(campaign.Id))
            {
                throw new Exception("customer: " + customer.Username + " not have points in campaign: " + campaign.Id);
            }


            if (customer.PointsInCampaign[campaign.Id] < campaign.Catalog[reward.Id])
            {
                return(false);
            }
            return(true);
        }
Exemple #2
0
 public ActionCampaignController(IRepository <ActionInCampaignRecord> repository, IEnumerable <IFidelityServices> services, IOrchardServices orchardServ)
 {
     _actionRepository = repository;
     if (services.Count() > 0)
     {
         _fidelityService = services.OrderBy(a => a.GetProviderName()).ToList()[0];
     }
     _orchardServices = orchardServ;
     settingsPart     = _orchardServices.WorkContext.CurrentSite.As <FidelitySettingsPart>();
 }
Exemple #3
0
 public FidelityBaseServices(IOrchardServices orchardServices, IEncryptionService encryptionService,
                             IAuthenticationService authenticationService, IMembershipService membershipService,
                             ISendService sendService, IRepository <ActionInCampaignRecord> repository, IWorkflowManager workfloManager)
 {
     _orchardServices       = orchardServices;
     _encryptionService     = encryptionService;
     _authenticationService = authenticationService;
     _membershipService     = membershipService;
     _sendService           = sendService;
     _actionInCampaign      = repository;
     settingsPart           = _orchardServices.WorkContext.CurrentSite.As <FidelitySettingsPart>();
     _workflowManager       = workfloManager;
 }
        public APIResult <IEnumerable <FidelityCampaign> > SendCampaignList(FidelitySettingsPart setPart)
        {
            APIResult <IEnumerable <FidelityCampaign> > result = new APIResult <IEnumerable <FidelityCampaign> >();
            List <FidelityCampaign> listCamp = new List <FidelityCampaign> {
                SendCampaignData(setPart, new FidelityCampaign {
                    Id = setPart.DefaultCampaign
                }).data
            };

            result.data    = listCamp;
            result.success = true;
            result.message = "Loyalzoo place id returned (no multiple campaign)";
            return(result);
        }
        public APIResult <IDictionary <string, string> > GetOtherSettings(FidelitySettingsPart setPart)
        {
            Dictionary <string, string> dictionary           = new Dictionary <string, string>();
            APIResult <IDictionary <string, string> > result = new APIResult <IDictionary <string, string> >();

            result.data = dictionary;
            string responseString = string.Empty;

            result.success = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("username", setPart.UserID),
                    new KeyValuePair <string, string>("password", setPart.Password),
                };
                responseString = SendRequest(setPart, APIType.merchant, "login", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <bool>("success");
                    if (result.success)
                    {
                        JToken response = data.SelectToken("response");

                        result.data.Add("merchantId", response.Value <string>("session_id"));

                        result.data.Add("placeId", response.Value <string>("place_id"));

                        result.message = "Loyalzoo merchant login success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("response").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "Exception: " + ex.Message + " in Loyalzoo Login.";
            }
            return(result);
        }
        public APIResult <FidelityCustomer> SendCustomerRegistration(FidelitySettingsPart setPart, FidelityCustomer customer, string campaignId)
        {
            APIResult <FidelityCustomer> result = new APIResult <FidelityCustomer>();

            result.data = null;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("customer_username", customer.Username),
                    new KeyValuePair <string, string>("customer_password", customer.Password),
                    new KeyValuePair <string, string>("first_name", customer.Username),
                    new KeyValuePair <string, string>("email", customer.Email),
                    new KeyValuePair <string, string>("campaign_id", campaignId),
                    new KeyValuePair <string, string>("customer_action", "new"),
                    new KeyValuePair <string, string>("card_number_generate", "10")
                };
                string responseString = SendRequest(setPart, "record_customer", kvpList);

                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <string>("status").Equals("success");
                    if (result.success)
                    {
                        customer.Id    = data.SelectToken("customer").Value <string>("code");
                        result.data    = customer;
                        result.message = "registrazione effettuata con successo";
                    }
                    else
                    {
                        result.message = data.SelectToken("errors").ToString();
                    }
                }
                else
                {
                    result.success = false;
                    result.message = "no response from Simsol server.";
                }
            }
            catch (Exception ex)
            {
                result.success = false;
                result.message = "exception: " + ex.Message + ".";
            }
            return(result);
        }
        public APIResult <bool> SendGiveReward(FidelitySettingsPart setPart, FidelityCustomer customer, FidelityReward reward, FidelityCampaign campaign)
        {
            APIResult <bool> result = new APIResult <bool>();

            result.data = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("place_id", campaign.Id),
                    new KeyValuePair <string, string>("session_id", setPart.AccountID),
                    new KeyValuePair <string, string>("customer_id", customer.Id),
                    new KeyValuePair <string, string>("reward_id", reward.Id),
                };
                if (!checkCustomerPoints(setPart, customer, reward, campaign))
                {
                    result.data    = false;
                    result.message = "Il cliente " + customer.Username + " non ha abbastanza punti per il reward: " + reward.Id + " nella campagna: " + campaign.Id + ".";
                    return(result);
                }
                string responseString = SendRequest(setPart, APIType.merchant, "giveReward", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <bool>("success");
                    if (result.success)
                    {
                        result.data    = true;
                        result.message = "Loyalzoo reward gived with success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("response").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "exception: " + ex.Message + ".";
            }
            return(result);
        }
        public APIResult <FidelityCustomer> SendCustomerRegistration(FidelitySettingsPart setPart, FidelityCustomer customer, string campaignId) //TODO campaignId non viene usato
        {
            APIResult <FidelityCustomer> result = new APIResult <FidelityCustomer>();

            result.data = null;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("username", customer.Username),
                    new KeyValuePair <string, string>("password", customer.Password),
                    new KeyValuePair <string, string>("email", customer.Email),
                    new KeyValuePair <string, string>("first_name", customer.Username)//TODO vedeere se è oggnligatorio inserire il first-name
                };
                string responseString = SendRequest(setPart, APIType.customer, "create", kvpList);

                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <bool>("success");
                    if (result.success)
                    {
                        customer.Data = this.DictionaryFromResponseToken(data.SelectToken("response"));
                        RemoveCustomerPropertyFromDataDictionary(customer);
                        result.data    = customer;
                        result.message = "Loyalzoo registration success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("response").ToString();
                    }
                }
                else
                {
                    result.success = false;
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.success = false;
                result.message = "exception: " + ex.Message + ".";
            }
            return(result);
        }
        private APIResult <FidelityCustomer> loginCustomer(FidelitySettingsPart setPart, FidelityCustomer customer)
        {
            string responseString = string.Empty;
            APIResult <FidelityCustomer> result = new APIResult <FidelityCustomer>();

            result.data    = null;
            result.success = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("username", customer.Username),
                    new KeyValuePair <string, string>("password", customer.Password),
                };
                responseString = SendRequest(setPart, APIType.customer, "login", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <bool>("success");
                    if (result.success)
                    {
                        customer.Data = this.DictionaryFromResponseToken(data.SelectToken("response"));
                        RemoveCustomerPropertyFromDataDictionary(customer);
                        result.data    = customer;
                        result.message = "Loyalzoo customer login success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("response").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "Exception: " + ex.Message + " in Loyalzoo Login.";
            }
            return(result);
        }
        public APIResult <FidelityCampaign> SendCampaignData(FidelitySettingsPart setPart, FidelityCampaign campaign)
        {
            APIResult <FidelityCampaign> result = new APIResult <FidelityCampaign>();

            result.data = null;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("place_id", campaign.Id),
                    new KeyValuePair <string, string>("session_id", setPart.AccountID),
                };
                string responseString = SendRequest(setPart, APIType.merchant, "place", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <bool>("success");
                    if (result.success)
                    {
                        campaign.Data = this.DictionaryFromResponseToken(data.SelectToken("response"));
                        RemoveCampaignPropertyFromDataDictionary(campaign);
                        result.data    = campaign;
                        result.message = "Loyalzoo place data request success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("response").ToString();
                    }
                }
                else
                {
                    result.success = false;
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.success = false;
                result.message = "exception: " + ex.Message + ";";
            }
            return(result);
        }
        public APIResult <FidelityCustomer> SendCustomerDetails(FidelitySettingsPart setPart, FidelityCustomer customer)
        {
            string responseString = string.Empty;
            APIResult <FidelityCustomer> result = new APIResult <FidelityCustomer>();

            result.data    = null;
            result.success = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("code", customer.Id),
                };
                responseString = SendRequest(setPart, "customer_info", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <string>("status").Equals("success");
                    if (result.success)
                    {
                        customer.Data = this.DictionaryFromResponseToken(data.SelectToken("customer"));
                        RemoveCustomerPropertyFromDataDictionary(customer);
                        AddPointsCampaignToCustomer(data.SelectToken("campaigns"), customer);
                        result.data    = customer;
                        result.message = "Simsol customer login success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("errors").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Simsol server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "Exception: " + ex.Message + " in Simsol Login.";
            }
            return(result);
        }
        public APIResult <bool> SendAddPoints(FidelitySettingsPart setPart, FidelityCustomer customer, FidelityCampaign campaign, string points)
        {
            APIResult <bool> result = new APIResult <bool>();

            result.success = false;
            result.data    = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("place_id", campaign.Id),
                    new KeyValuePair <string, string>("session_id", setPart.AccountID),
                    new KeyValuePair <string, string>("customer_id", customer.Id),
                    new KeyValuePair <string, string>("points", points),
                };
                string responseString = SendRequest(setPart, APIType.merchant, "givePoints", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <bool>("success");
                    if (result.success)
                    {
                        result.data    = true;
                        result.message = "Loyalzoo points added with success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("response").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "exception: " + ex.Message + ".";
            }
            return(result);
        }
        public APIResult <bool> SendAddPoints(FidelitySettingsPart setPart, FidelityCustomer customer, FidelityCampaign campaign, string points)
        {
            APIResult <bool> result = new APIResult <bool>();

            result.success = false;
            result.data    = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("campaign_id", campaign.Id),
                    new KeyValuePair <string, string>("code", customer.Id),
                    new KeyValuePair <string, string>("amount", points),
                };
                string responseString = SendRequest(setPart, "record_activity", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <string>("status").Equals("success");
                    if (result.success)
                    {
                        result.data    = true;
                        result.message = "Simsol points added with success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("errors").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Simsol server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "exception: " + ex.Message + ".";
            }
            return(result);
        }
        public APIResult <FidelityCampaign> SendCampaignData(FidelitySettingsPart setPart, FidelityCampaign campaign)
        {
            APIResult <FidelityCampaign> result = new APIResult <FidelityCampaign>();

            result.data = null;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("campaign_id", campaign.Id)
                };
                string responseString = SendRequest(setPart, "campaign_rewards", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <string>("status").Equals("success");
                    if (result.success)
                    {
                        AddRewardsInCampaignFromToken(data.SelectToken("rewards"), campaign);
                        result.data    = campaign;
                        result.message = "Simsol campaign data request success.";
                    }
                    else
                    {
                        result.message = data.SelectToken("errors").ToString();
                    }
                }
                else
                {
                    result.success = false;
                    result.message = "no response from Simsol server.";
                }
            }
            catch (Exception ex)
            {
                result.success = false;
                result.message = "exception: " + ex.Message + ";";
            }
            return(result);
        }
        /// <summary>
        /// Invia la richiesta all'URL API
        /// </summary>
        /// <param name="APIType">Tipologia di API da richiamare <see cref="APIType"/></param>
        /// <param name="APIMetodo">Metodo da richiamare</param>
        /// <param name="kvpList">Elenco dei parametri da passare al provider come parametri;</param>
        /// <returns>Restituisce una stringa in formato json</returns>
        private static string SendRequest(FidelitySettingsPart setPart, string APIMetodo, List <KeyValuePair <string, string> > kvpList)
        {
            string responseString = string.Empty;

            try
            {
                using (var client = new HttpClient())
                {
                    Uri address = new Uri(setPart.ApiURL);
                    kvpList.Add(new KeyValuePair <string, string>("user_id", setPart.UserID));
                    kvpList.Add(new KeyValuePair <string, string>("user_password", setPart.DeveloperKey));
                    kvpList.Add(new KeyValuePair <string, string>("account_id", setPart.AccountID));
                    kvpList.Add(new KeyValuePair <string, string>("type", APIMetodo));
                    kvpList.Add(new KeyValuePair <string, string>("output", "JSON"));
                    var content = new FormUrlEncodedContent(kvpList);
                    HttpResponseMessage result = client.PostAsync(address, content).Result;
                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        responseString = result.Content.ReadAsStringAsync().Result;
                        if (responseString.Contains("\"status\":errors"))
                        {
                            throw new Exception(responseString);
                        }                                                                                          //TODO vedere se ha senso mantenere il lancio dell'eccezione qui e tutti i controlli sui metodi dopo
                    }
                    else if (result.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new Exception("Direttiva non valida");
                    }
                    else
                    {
                        throw new Exception(result.Content.ReadAsStringAsync().Result);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(responseString);
        }
        public APIResult <bool> SendGiveReward(FidelitySettingsPart setPart, FidelityCustomer customer, FidelityReward reward, FidelityCampaign campaign)
        {
            APIResult <bool> result = new APIResult <bool>();

            result.data = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("campaign_id", campaign.Id),
                    new KeyValuePair <string, string>("code", customer.Id),
                    new KeyValuePair <string, string>("reward_to_redeem", reward.Id),
                };
                string responseString = SendRequest(setPart, "redeem", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <string>("status").Equals("success");
                    if (result.success)
                    {
                        result.data    = true;
                        result.message = "Simsol reward gived with success.";
                    }
                    else
                    {
                        result.message = "The reward level selected exceeds the points available";
                    }
                }
                else
                {
                    result.message = "no response from Loyalzoo server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "exception: " + ex.Message + ".";
            }
            return(result);
        }
        public APIResult <IEnumerable <FidelityCampaign> > SendCampaignList(FidelitySettingsPart setPart)
        {
            APIResult <IEnumerable <FidelityCampaign> > result = new APIResult <IEnumerable <FidelityCampaign> >();
            List <FidelityCampaign> listCamp = new List <FidelityCampaign>();

            result.data    = null;
            result.success = false;
            try
            {
                List <KeyValuePair <string, string> > kvpList = new List <KeyValuePair <string, string> >();
                string responseString = SendRequest(setPart, "campaigns_list", kvpList);
                if (!string.IsNullOrWhiteSpace(responseString))
                {
                    JObject data = JObject.Parse(responseString);
                    result.success = data.Value <string>("status").Equals("success");
                    if (result.success)
                    {
                        result.message = "campaign list";
                        result.data    = CreateCampaignListFromToken(data.SelectToken("campaigns"));
                    }
                    else
                    {
                        result.message = data.SelectToken("errors").ToString();
                    }
                }
                else
                {
                    result.message = "no response from Simsol server.";
                }
            }
            catch (Exception ex)
            {
                result.message = "Exception: " + ex.Message + " in Simsol Campaign List.";
            }
            return(result);
        }
        /// <summary>
        /// Invia la richiesta all'URL API
        /// </summary>
        /// <param name="APIType">Tipologia di API da richiamare <see cref="APIType"/></param>
        /// <param name="APIMetodo">Metodo da richiamare</param>
        /// <param name="kvpList">Elenco dei parametri da passare al provider come parametri;</param>
        /// <returns>Restituisce una stringa in formato json</returns>
        private static string SendRequest(FidelitySettingsPart setPart, APIType APIType, string APIMetodo, List <KeyValuePair <string, string> > kvpList)
        {
            string responseString = string.Empty;

            try
            {
                using (var client = new HttpClient())
                {
                    // client.BaseAddress = new Uri(setPart.ApiURL + APIType.ToString() + "/" + setPart.DeveloperKey + "/" + APIMetodo);
                    Uri address = new Uri(setPart.ApiURL + APIType.ToString() + "/" + setPart.DeveloperKey + "/" + APIMetodo);
                    var content = new FormUrlEncodedContent(kvpList);
                    HttpResponseMessage result = client.PostAsync(address, content).Result;
                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        responseString = result.Content.ReadAsStringAsync().Result;
                        if (responseString.Contains("\"success\":false"))
                        {
                            throw new Exception(responseString);
                        }                                                                                          //TODO vedere se ha senso mantenere il lancio dell'eccezione qui e tutti i controlli sui metodi dopo
                    }
                    else if (result.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new Exception("Direttiva non valida");
                    }
                    else
                    {
                        throw new Exception(result.Content.ReadAsStringAsync().Result);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(responseString);
        }
 public APIResult <IDictionary <string, string> > GetOtherSettings(FidelitySettingsPart setPart)
 {
     throw new NotImplementedException();
 }
 public APIResult <string> SendGetMerchantId(FidelitySettingsPart setPart)
 {
     throw new NotImplementedException();
 }
 public APIResult <FidelityCustomer> SendCustomerDetails(FidelitySettingsPart setPart, FidelityCustomer customer)
 {
     return(loginCustomer(setPart, customer));
 }