예제 #1
0
        public override IEnumerable <LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext)
        {
            APIResult <FidelityReward> res = (APIResult <FidelityReward>)(workflowContext.Tokens["result"]);

            if (res.success)
            {
                FidelityReward rew = res.data;
                workflowContext.SetState <string>("rewardId", rew.Id);
                if (!string.IsNullOrWhiteSpace(rew.Description))
                {
                    workflowContext.SetState <string>("rewardDescription", rew.Description);
                }
                if (!string.IsNullOrWhiteSpace(rew.Name))
                {
                    workflowContext.SetState <string>("rewardName", rew.Name);
                }
                else
                {
                    workflowContext.SetState <string>("rewardName", "");
                }

                yield return(T("Success"));
            }
            else
            {
                yield return(T("Error"));
            }
        }
예제 #2
0
        public virtual APIResult <FidelityReward> GiveReward(string rewardId, string campaignId, string customerId)
        {
            APIResult <FidelityReward> res = new APIResult <FidelityReward>();
            FidelityCustomer           customer;

            if (customerId == null)
            {
                customer = GetCustomerFromAuthenticatedUser();
            }
            else
            {
                customer = GetCustomerFromIdOrEmail(customerId);
            }

            if (customer != null)
            {
                FidelityReward reward = new FidelityReward();
                reward.Id = rewardId;
                FidelityCampaign campaign = new FidelityCampaign();
                campaign.Id = campaignId;
                APIResult <bool> resGive = _sendService.SendGiveReward(settingsPart, customer, reward, campaign);
                if (!resGive.success)
                {
                    res = new APIResult <FidelityReward> {
                        success = false, data = null, message = resGive.message
                    };
                }
                else
                {
                    APIResult <FidelityCampaign> resCamp = _sendService.SendCampaignData(settingsPart, campaign);
                    if (!resCamp.success)
                    {
                        res = new APIResult <FidelityReward> {
                            success = false, data = null, message = resCamp.message
                        };
                    }
                    else
                    {
                        res.message = resGive.message;
                        res.success = resGive.success;
                        res.data    = resCamp.data.Rewards.Where(r => r.Id.Equals(rewardId)).First();
                    }
                }
            }
            else
            {
                res = new APIResult <FidelityReward> {
                    success = false, data = null, message = "The user is not configured to use " + GetProviderName()
                };
            }
            _workflowManager.TriggerEvent("RedeemFidelityReward", null, () => new Dictionary <string, object> {
                { "result", res }
            });
            return(res);
        }
 //partendo dal JToken che rappresenta il catalogo setta la propietà catalogo dalla campaign
 private void AddRewardsInCampaignFromToken(JToken tokenRewards, FidelityCampaign campaign)
 {
     if (tokenRewards.Children().Count() > 0)
     {
         foreach (JToken tokenRew in tokenRewards.Children())
         {
             FidelityReward reward = new FidelityReward();
             reward.Id          = tokenRew.Value <string>("id");
             reward.Description = tokenRew.Value <string>("description");
             campaign.Rewards.Add(reward);
             campaign.AddReward(reward.Id, tokenRew.Value <string>("level"));
         }
     }
 }
        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);
        }
        /// <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);
        }
        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);
        }