Exemplo n.º 1
0
        static void Main(string[] args)
        {
            String URL      = ConfigurationManager.AppSettings["URL"];
            String ClientId = ConfigurationManager.AppSettings["ClientId"];
            String ClienKey = ConfigurationManager.AppSettings["ClientKey"];

            AirtimeClient airtime = new AirtimeClient(URL, ClientId, ClienKey);
            Network       net     = AirtimeClient.Parse(args[0]);
            String        msisdn  = args[1];
            int           amount  = int.Parse(args[2]);

            try
            {
                BalanceResult balance = airtime.Balance();
                Console.WriteLine("balance: {0} discount: {1}%", balance.balance, balance.discount);

                String        xref   = Guid.NewGuid().ToString("N");
                AirtimeResult credit = airtime.Credit(net, msisdn, amount, xref);
                Console.WriteLine("credit id: {0} xref: {1} message: {2}", credit.id, credit.xref, credit.message);

                // Gice a few seconds for the databases to sync
                // System.Threading.Thread.Sleep(1000);

                CheckResult check = airtime.Check(xref);
                if (check != null)
                {
                    Console.WriteLine("check id: {0} net: {1} msisdn: {2} amount: {3} xref: {4}", check.id, check.net, check.msisdn, check.amount, check.xref);
                }
            }
            catch (System.Net.WebException wex)
            {
                Console.WriteLine("HTTP Status: {0} {1}", wex.Status, wex.Message);
            }
        }
Exemplo n.º 2
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 = ComputeSignature(Nonce, http.QueryString);

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

                String response = http.DownloadString(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 = httpStatus.ToString();
                    result.message    = wex.Message;
                    result.xref       = xref;
                }
            }

            return(result);
        }