public async Task <LoyaltyPoint> GiveLoyaltyPoint(Object data, string privateKey, string accessToken, string environment)
        {
            string compactJson = SignatureUtil.GenerateCompactJson(data);
            string method      = "post";
            string nonceStr    = RandomString.GenerateRandomString(32);
            string requestUrl  = "";

            if (environment == "sandbox")
            {
                requestUrl = String.Concat(Url.SandBoxOpen, "/v3/loyalty/reward");
            }
            else if (environment == "production")
            {
                requestUrl = String.Concat(Url.ProductionOpen, "/v3/loyalty/reward");
            }
            string    signType        = "sha256";
            string    timestamp       = Convert.ToString((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
            Signature signature       = new Signature();
            string    signatureResult = "";

            signatureResult = signature.GenerateSignature(compactJson, method, nonceStr, privateKey, requestUrl, signType, timestamp, environment);
            signatureResult = "sha256 " + signatureResult;
            LoyaltyPoint result = new LoyaltyPoint();

            try
            {
                var content     = JsonConvert.SerializeObject(data);
                var buffer      = System.Text.Encoding.UTF8.GetBytes(content);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                client.DefaultRequestHeaders.Add("X-Nonce-Str", nonceStr);
                client.DefaultRequestHeaders.Add("X-Signature", signatureResult);
                client.DefaultRequestHeaders.Add("X-Timestamp", timestamp);
                var response = await client.PostAsync(requestUrl, byteContent);

                var responseStr = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var jsonAsString = await response.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <LoyaltyPoint>(jsonAsString);
                }
                else
                {
                    result = JsonConvert.DeserializeObject <LoyaltyPoint>(response.Content.ReadAsStringAsync().Result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error", ex.Message);
            }
            return(result);
        }
Exemple #2
0
        public ActionResult Create(LoyaltyPointViewModel model)
        {
            if (ModelState.IsValid)
            {
                var LoyaltyPoint = new LoyaltyPoint();
                AutoMapper.Mapper.Map(model, LoyaltyPoint);
                LoyaltyPoint.IsDeleted      = false;
                LoyaltyPoint.CreatedUserId  = WebSecurity.CurrentUserId;
                LoyaltyPoint.ModifiedUserId = WebSecurity.CurrentUserId;
                LoyaltyPoint.AssignedUserId = WebSecurity.CurrentUserId;
                LoyaltyPoint.CreatedDate    = DateTime.Now;
                LoyaltyPoint.ModifiedDate   = DateTime.Now;
                LoyaltyPointRepository.InsertLoyaltyPoint(LoyaltyPoint);

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }