Beispiel #1
0
        public override AirtimeResult TopUp(string phoneNumber, int amount)
        {
            AirtimeResult result = new AirtimeResult();

            result.ResultCode = 0;

            try
            {
                var http  = this.CreateWebClientWithHeader();
                var nonce = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                http.Headers.Add("Nonce", nonce);

                http.QueryString.Add("msisdn", phoneNumber);
                http.QueryString.Add("amount", amount.ToString());

                var response = this.NetworkResponse(http, this.Url + this.TopupEndpoint);
                result            = this.NetworkResponseMapToObject <AirtimeResult>(response);
                result.ResultCode = 200;

                EventAggregator.Instance.Publish(result);

                this.SaveToDatabase(result);
            }
            catch (WebException wex)
            {
                if (wex.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpStatusCode httpStatus = ((HttpWebResponse)wex.Response).StatusCode;
                    result.ResultCode = (int)httpStatus;
                    result.Message    = wex.Message;
                }
            }

            return(result);
        }
Beispiel #2
0
        public AirtimeResult TopUp(string phoneNumber, int amount)
        {
            var airtimeResult = new AirtimeResult()
            {
                ResultCode = 400
            };

            if (customer.Balance < amount)
            {
                Console.WriteLine("Running out of balance");
                airtimeResult.ResultCode = 400;
                airtimeResult.Message    = "Running out of balance";
                return(airtimeResult);
            }

            var rechargeService = new RechargeService();
            var isRecharged     = rechargeService.TopUp(phoneNumber, amount);

            if (isRecharged)
            {
                customer.Balance = customer.Balance - (amount * (100.0 + ConstantValues.Charge) / 100.0)
                                   + (amount * (customer.Discount) / 100.0);
                airtimeResult = new AirtimeResult()
                {
                    ResultCode = 200,
                    Amount     = amount,
                    Balance    = customer.Balance,
                    Charge     = ConstantValues.Charge,
                    Message    = "sceessfully recharge"
                };
                return(airtimeResult);
            }
            return(airtimeResult);//or throw exception
        }
Beispiel #3
0
        private void NotifyAdmins(AirtimeResult result)
        {
            //Console.WriteLine("Notify Admins");

            if (result.Balance < 2000)
            {
                Console.WriteLine("Notify Admins: balance is low");
                //notify admins
            }
            else if (result.Balance < 10)
            {
                Console.WriteLine("Fatal error: Notify Admins");
                //temprary stop gp recharge
                //notify admins
            }
        }
Beispiel #4
0
        /// <summary>
        /// The network response.
        /// </summary>
        /// <param name="http">
        /// The http.
        /// </param>
        /// <param name="url">
        /// The url.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        protected string NetworkResponse(WebClient http, string url)
        {
            // string response = http.DownloadString(url);

            // demo data for test purpose
            int rechargeAmount = Convert.ToInt32(http.QueryString["amount"]);
            var airtimeResult  = new AirtimeResult();

            airtimeResult.Id         = "123";
            airtimeResult.ResultCode = 200;
            airtimeResult.Balance    = 1000;
            airtimeResult.Charge     = 1;
            airtimeResult.Message    = "asdf";

            var response = JsonConvert.SerializeObject(airtimeResult);

            return(response);
        }
Beispiel #5
0
        public AirtimeResult DataCredit(Network net, String msisdn, int amount, string xref)
        {
            AirtimeResult result = new AirtimeResult();

            result.ResultCode = 0;
            String Nonce = DateTime.Now.ToString("yyyyMMddHHmmssfff");

            msisdn = Regex.Replace(msisdn, @"(?<PREFIX>^0)(?<TAIL>\d*)", @"234${TAIL}");

            try
            {
                WebClient http = new WebClient();
                http.QueryString.Add("net", net.ToString());
                http.QueryString.Add("msisdn", msisdn);
                http.QueryString.Add("amount", amount.ToString());
                http.QueryString.Add("xref", xref);

                String Signature = this.ComputeSignature(Nonce, http.QueryString);

                http.Headers.Add("ClientId", this.ClientId);
                http.Headers.Add("Signature", Signature);
                http.Headers.Add("Nonce", Nonce);

                String response = http.DownloadString(this.URL + "/data/Credit");
                result            = JsonConvert.DeserializeObject <AirtimeResult>(response);
                result.ResultCode = 200;
            }
            catch (WebException wex)
            {
                if (wex.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpStatusCode httpStatus = ((HttpWebResponse)wex.Response).StatusCode;
                    result.ResultCode = (int)httpStatus;
                    result.message    = wex.Message;
                    result.xref       = xref;
                }
            }

            return(result);
        }
Beispiel #6
0
 /// <summary>
 /// The save to database.
 /// </summary>
 /// <param name="airtimeResult">
 /// The airtime result.
 /// </param>
 protected void SaveToDatabase(AirtimeResult airtimeResult)
 {
     // save
     Console.WriteLine("write to database");
 }