Example #1
0
        /// <summary>Initializes a new instance of the <see cref="Gameball"/> class.</summary>
        /// <param name="apiKey">The API key used by the client to make requests.</param>
        /// <param name="transactionKey">The Transaction key used by the client to make requests.</param>
        /// <exception cref="ArgumentNullException">if <c>apiKey</c> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">
        /// if <c>apiKey</c> is empty or contains whitespace.
        /// if <c>transactionKey</c> is empty or contains whitespace.
        /// </exception>
        public Gameball(string apiKey, string transactionKey = null)
        {
            Client = new HttpClient();
            if (apiKey == null || apiKey.Length == 0)
            {
                throw new ArgumentException("API key cannot be null or an empty string.", nameof(apiKey));
            }

            if (apiKey != null && GameballUtils.ContainsWhitespace(apiKey))
            {
                throw new ArgumentException("API key cannot contain whitespace.", nameof(apiKey));
            }

            if (transactionKey != null && GameballUtils.ContainsWhitespace(transactionKey))
            {
                throw new ArgumentException("Transaction key cannot contain whitespace.", nameof(transactionKey));
            }

            if (transactionKey != null && transactionKey.Length == 0)
            {
                throw new ArgumentException("Transaction key cannot be the empty string.", nameof(transactionKey));
            }



            this.ApiBase        = GameballConstants.BaseURL;
            this.ApiKey         = apiKey;
            this.TransactionKey = transactionKey;
            this.Lang           = GameballLang.English;
            Client.DefaultRequestHeaders.Clear();
            Client.DefaultRequestHeaders.Add("APIKey", ApiKey);
        }
Example #2
0
        /// <summary>
        /// This Async service For Get the Player's Info.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public async Task <PlayerInfo> GetPlayerInfoAsync(string playerUniqueId, GameballLang Language = 0)
        {
            if (Language != 0)
            {
                Client.DefaultRequestHeaders.Add("lang", GameballUtils.ToValidLang(Language));
            }
            PlayerInfoRequest Info = new PlayerInfoRequest()
            {
                PlayerUniqueId = playerUniqueId,
                Hash           = GameballUtils.GetSHA1(playerUniqueId, TransactionKey)
            };

            Info.Validate();
            var response = await this.Client.PostAsync(ApiBase + GameballConstants.PlayerInfo, new StringContent(Info.Serialize(), Encoding.UTF8, "application/json"));

            //Removes Overhead of lang header if not necessary in next calls.
            Client.DefaultRequestHeaders.Remove("lang");

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <PlayerInfo>(await response.Content.ReadAsStringAsync()));
            }
        }
        internal override bool Validate()
        {
            if (PlayerUniqueId == null || PlayerUniqueId.Length == 0)
            {
                throw new ArgumentException("Player Unique ID cannot be null or an empty string.", nameof(PlayerUniqueId));
            }

            if (PlayerUniqueId != null && GameballUtils.ContainsWhitespace(PlayerUniqueId))
            {
                throw new ArgumentException("Player Unique ID cannot contain whitespace.", nameof(PlayerUniqueId));
            }

            if (this.HoldReference != null && this.HoldReference.Length == 0)
            {
                throw new ArgumentException("Hold Reference cannot be the empty string.", nameof(HoldReference));
            }

            if (this.HoldReference != null && GameballUtils.ContainsWhitespace(HoldReference))
            {
                throw new ArgumentException("Hold Reference cannot contain whitespace.", nameof(HoldReference));
            }


            if (Amount < 0)
            {
                throw new ArgumentException("Amount cannot be negative", nameof(Amount));
            }

            return(true);
        }
        internal override bool Validate()
        {
            if (PlayerUniqueId == null || PlayerUniqueId.Length == 0)
            {
                throw new ArgumentException("Player Unique ID cannot be null or an empty string.", nameof(PlayerUniqueId));
            }

            if (PlayerUniqueId != null && GameballUtils.ContainsWhitespace(PlayerUniqueId))
            {
                throw new ArgumentException("Player Unique ID cannot contain whitespace.", nameof(PlayerUniqueId));
            }

            if (this.HoldReference == null || this.HoldReference.Length == 0)
            {
                throw new ArgumentException("Hold Reference cannot be null or an empty string.", nameof(HoldReference));
            }

            if (this.HoldReference != null && GameballUtils.ContainsWhitespace(HoldReference))
            {
                throw new ArgumentException("Hold Reference cannot contain whitespace.", nameof(HoldReference));
            }

            if (TransactionId == null)
            {
                throw new ArgumentException("Transaction on client system Id cannot be null.", nameof(TransactionId));
            }

            if (PlayerAttributes != null)
            {
                PlayerAttributes.Validate();
            }

            return(true);
        }
Example #5
0
        internal override bool Validate()
        {
            if (PlayerUniqueId == null || PlayerUniqueId.Length == 0)
            {
                throw new ArgumentException("Player Unique ID cannot be null or an empty string.", nameof(PlayerUniqueId));
            }

            if (PlayerUniqueId != null && GameballUtils.ContainsWhitespace(PlayerUniqueId))
            {
                throw new ArgumentException("Player Unique ID cannot contain whitespace.", nameof(PlayerUniqueId));
            }

            if (PlayerCode == null || PlayerCode.Length == 0)
            {
                throw new ArgumentException("Player Code cannot be null or an empty string.", nameof(PlayerCode));
            }

            if (PlayerCode != null && GameballUtils.ContainsWhitespace(PlayerCode))
            {
                throw new ArgumentException("Player Code cannot contain whitespace.", nameof(PlayerCode));
            }

            if (PlayerAttributes != null)
            {
                PlayerAttributes.Validate();
            }

            if (SessionInfo != null)
            {
                SessionInfo.Validate();
            }

            return(true);
        }
Example #6
0
        internal override bool Validate()
        {
            if (this.HoldReference != null && this.HoldReference.Length == 0)
            {
                throw new ArgumentException("Hold Reference cannot be the empty string.", nameof(HoldReference));
            }

            if (this.HoldReference != null && GameballUtils.ContainsWhitespace(HoldReference))
            {
                throw new ArgumentException("Hold Reference cannot contain whitespace.", nameof(HoldReference));
            }

            try
            {
                int Amount = Convert.ToInt32(RewardAmount);
            }

            catch
            {
                throw new ArgumentException("Reward Amount must be a valid Amount ", nameof(RewardAmount));
            }

            if (Convert.ToInt32(RewardAmount) < 0)
            {
                throw new ArgumentException("Reward Amount cannot be negative", nameof(RewardAmount));
            }

            return(true);
        }
Example #7
0
        /// <summary>
        /// This Async service For Creating a Discount Coupon.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public async Task <CreateCouponResponse> CreateDiscountCouponAsync(CreateCouponRequest coupon)
        {
            coupon.Validate();
            coupon.TransactionTime = DateTime.UtcNow;
            string hash = GameballUtils.GetSHA1(coupon.PlayerUniqueId, TransactionKey);

            coupon.Hash = hash;
            var response = await this.Client.PostAsync(ApiBase + GameballConstants.Coupon, new StringContent(coupon.Serialize(), Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <CreateCouponResponse>(await response.Content.ReadAsStringAsync()));
            }
        }
Example #8
0
        /// <summary>
        /// This Async service is used by Clients to send Purchase Rewards (could have a discount)
        /// and Events to Gameball based on Events triggered by the Players Actions on the Client's Interface.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public async Task <ActionResponse> SendActionAsync(ActionRequest action)
        {
            action.Validate();
            action.PointsTransaction.TransactionTime = DateTime.UtcNow;
            string hash = GameballUtils.GetSHA1(action.PlayerUniqueId, TransactionKey, GameballUtils.ToUtcTime(action.PointsTransaction.TransactionTime), GameballUtils.ToValidAmount(action.PointsTransaction.RewardAmount));

            action.PointsTransaction.Hash = hash;
            var response = await this.Client.PostAsync(ApiBase + GameballConstants.Action, new StringContent(action.Serialize(), Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <ActionResponse>(await response.Content.ReadAsStringAsync()));
            }
        }
Example #9
0
        /// <summary>
        /// This service For Reversing a Transaction.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public TransactionResponse ReverseTransaction(ReverseTransactionRequest reverse)
        {
            reverse.Validate();
            reverse.TransactionTime = DateTime.UtcNow;
            string hash = GameballUtils.GetSHA1(reverse.PlayerUniqueId, TransactionKey, GameballUtils.ToUtcTime(reverse.TransactionTime), GameballUtils.ToValidAmount(reverse.Amount));

            reverse.Hash = hash;
            var response = this.Client.PostAsync(ApiBase + GameballConstants.Cancel, new StringContent(reverse.Serialize(), Encoding.UTF8, "application/json")).Result;

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <TransactionResponse>(response.Content.ReadAsStringAsync().Result));
            }
        }
Example #10
0
        /// <summary>
        /// This Async service For Redeem Points Based on the Amount send and the Client Transaction Config.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public async Task <TransactionResponse> RedeemPointsAsync(RedeemPointsRequest redeem)
        {
            redeem.Validate();
            redeem.TransactionTime = DateTime.UtcNow;
            string hash = GameballUtils.GetSHA1(redeem.PlayerUniqueId, TransactionKey, GameballUtils.ToUtcTime(redeem.TransactionTime));

            redeem.Hash = hash;
            var response = await this.Client.PostAsync(ApiBase + GameballConstants.Redeem, new StringContent(redeem.Serialize(), Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <TransactionResponse>(await response.Content.ReadAsStringAsync()));
            }
        }
        internal override bool Validate()
        {
            if (Email != null && !GameballUtils.IsValidEmail(Email))
            {
                throw new ArgumentException("This is not a valid Email address.", nameof(Email));
            }

            if (DateOfBirth != DateTime.MinValue && DateTime.Compare(DateOfBirth, DateTime.Now) > 0)
            {
                throw new ArgumentException("Birthdate cannot be a future date.", nameof(DateOfBirth));
            }

            if (JoinDate != DateTime.MinValue && DateTime.Compare(JoinDate, DateTime.Now) > 0)
            {
                throw new ArgumentException("Joindate cannot be a future date.", nameof(JoinDate));
            }
            return(true);
        }
        internal override bool Validate()
        {
            if (PlayerUniqueId == null || PlayerUniqueId.Length == 0)
            {
                throw new ArgumentException("Player Unique ID cannot be null or an empty string.", nameof(PlayerUniqueId));
            }

            if (PlayerUniqueId != null && GameballUtils.ContainsWhitespace(PlayerUniqueId))
            {
                throw new ArgumentException("Player Unique ID cannot contain whitespace.", nameof(PlayerUniqueId));
            }

            if (StartAt != null && EndsAt != null && DateTime.Compare((DateTime)StartAt, (DateTime)EndsAt) > 0)
            {
                throw new ArgumentException("A Coupon cannot have a start date after its end date.", nameof(StartAt));
            }

            return(true);
        }
Example #13
0
        /// <summary>
        /// This Async service For Get Player Balances.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public async Task <PlayerBalance> GetPlayerBalanceAsync(string playerUniqueId)
        {
            PlayerBalanceRequest Balance = new PlayerBalanceRequest()
            {
                PlayerUniqueId = playerUniqueId,
                Hash           = GameballUtils.GetSHA1(playerUniqueId, TransactionKey)
            };

            Balance.Validate();
            var response = await this.Client.PostAsync(ApiBase + GameballConstants.Balance, new StringContent(Balance.Serialize(), Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <PlayerBalance>(await response.Content.ReadAsStringAsync()));
            }
        }
Example #14
0
        /// <summary>
        /// This async service For Validate Discount Coupon.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public async Task <ValidateCouponResponse> ValidateDiscountCouponAsync(string playerUniqueId, string code)
        {
            ValidateCouponRequest Coupon = new ValidateCouponRequest()
            {
                PlayerUniqueId  = playerUniqueId,
                Hash            = GameballUtils.GetSHA1(playerUniqueId, TransactionKey),
                TransactionTime = DateTime.UtcNow,
                Code            = code
            };

            Coupon.Validate();
            var response = await this.Client.PostAsync(ApiBase + GameballConstants.ValidateDiscount, new StringContent(Coupon.Serialize(), Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(JsonConvert.DeserializeObject <ValidateCouponResponse>(await response.Content.ReadAsStringAsync()));
            }
        }
Example #15
0
        /// <summary>
        /// This service For Redeem Discount Coupon.
        /// </summary>
        /// <exception cref="GameballException">Thrown if the request fails..</exception>
        public bool RedeemDiscountCoupon(string playerUniqueId, string code)
        {
            RedeemCouponRequest Coupon = new RedeemCouponRequest()
            {
                PlayerUniqueId  = playerUniqueId,
                Hash            = GameballUtils.GetSHA1(playerUniqueId, TransactionKey),
                TransactionTime = DateTime.UtcNow,
                Code            = code
            };

            Coupon.Validate();
            var response = this.Client.PostAsync(ApiBase + GameballConstants.RedeemDiscount, new StringContent(Coupon.Serialize(), Encoding.UTF8, "application/json")).Result;

            if (!response.IsSuccessStatusCode)
            {
                throw (BuildGameballException(response));
            }
            else
            {
                return(response.IsSuccessStatusCode);
            }
        }