/// <summary> /// Generates token by given model. /// Validates whether the given model is valid, then gets the symmetric key. /// Encrypt the token and returns it. /// </summary> /// <param name="model"></param> /// <returns>Generated token.</returns> public string GenerateToken(PaymentWithBKOnline paymentModel) { var TockenID = new RandomCode().RandomNumber(4); var ExpireSeconds = 60; var SecretKeyValue = ConfigurationManager.AppSettings["SecretKey"]; var APIKey = ConfigurationManager.AppSettings["AppKey"]; var payload = new Dictionary <string, object> { { "iss", APIKey }, { "iat", DateTimeOffset.Now.ToUnixTimeSeconds() }, { "exp", DateTimeOffset.Now.AddSeconds(ExpireSeconds).ToUnixTimeSeconds() }, { "jti", TockenID }, { "form_params", paymentModel } }; IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var token = encoder.Encode(payload, SecretKeyValue); return(token); }
public async Task <JsonResult> CallApiToBK(InvoiceModel invoicemodel) { var cartDetail = GetCarDetail(); var baseUrl = ConfigurationManager.AppSettings["BKUrl"]; PaymentWithBKOnline paymentBK = new PaymentWithBKOnline { mrc_order_id = invoicemodel.ID_Invoice, total_amount = 100000, description = "Thanh Toán Hóa đơn số" + invoicemodel.ID_Invoice, url_success = GetUrlOrgin() + "/PaymentResult" }; _paymentLibrary.AddRequestData("mrc_order_id", invoicemodel.ID_Invoice); _paymentLibrary.AddRequestData("total_amount", "100000"); _paymentLibrary.AddRequestData("description", "Thanh toan don hang so:" + invoicemodel.ID_Invoice); _paymentLibrary.AddRequestData("jwt", new JWTService().GenerateToken(paymentBK)); _paymentLibrary.AddRequestData("url_success", GetUrlOrgin() + "/PaymentResult/ShowTest1"); _paymentLibrary.AddRequestData("webhooks", GetUrlOrgin() + "/PaymentResult/WebHookResult"); string paymentUrl = _paymentLibrary.CreateRequestUrl(baseUrl + "api/v4/order/send"); using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(paymentUrl); //client.DefaultRequestHeaders.Accept.Clear(); //client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.PostAsync(paymentUrl, null); if (response.IsSuccessStatusCode) { var data = await response.Content.ReadAsStringAsync(); var table = JsonConvert.DeserializeObject <PaymentResult>(data); return(table.code == 0 ? Json(new { Error = table.data.redirect_url, Status = 1 }): Json(new { Error = table.data.redirect_url, Status = 1 })); } else { return(Json(new { Error = "Xảy ra lỗi trong quá trình Thanh Toán", Status = 0 })); } } }