public IActionResult Messages([FromBody] DeliveredTransactionRequest deliveredTransactionRequest) { if (deliveredTransactionRequest == null) { throw new ArgumentNullException(nameof(deliveredTransactionRequest)); } DeliveredTransactionResponse transactionResponse; try { // Kiểm tra các điều kiện // Lấy chữ ký từ Body encode Base64 byte[] signature = Convert.FromBase64String(deliveredTransactionRequest.Signature); // Lấy byteData từ Body encode Base64 byte[] byteData = Convert.FromBase64String(deliveredTransactionRequest.Payload); // Lấy json string từ byteData (Đã thưc hiện getbytes UTF8) string jsonData = Encoding.UTF8.GetString(byteData); // Lấy public key của SAMI-S kiểm tra var cryptoServiceProvider = _systemService.SamiPublicKey; if (!_rsaCrypto.VerifyHashData(signature, byteData, cryptoServiceProvider)) { return(BadRequest("Invalid signature!")); } // Validate Deserialize object JsonTextReader textReader = new JsonTextReader(new StringReader(jsonData)); JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(textReader) { Schema = _systemService.DeliveredTransactionSchema }; JsonSerializer serializer = new JsonSerializer(); MessagesDeliveredTransaction deliveredTransaction = serializer.Deserialize <MessagesDeliveredTransaction>(validatingReader); _logger.LogInformation($"Transaction Decoded: {JsonConvert.SerializeObject(deliveredTransaction)}"); if (deliveredTransaction == null) { throw new ArgumentException("The data is not in the correct format, reference https://"); } if (deliveredTransaction.MessagesDelivered.Count() >= 200) { return(BadRequest(new ErrorResponse { ErrorCode = 400, ErrorMessage = "Not allowed to send more than 200 messages delivered per transaction" })); } // Chuan bi cac ban tin luu vao database _messagesDeliveredService.PushMessages(deliveredTransaction.MessagesDelivered); // Lưu vào trong Database (Repository) _messagesDeliveredService.Save(); transactionResponse = new DeliveredTransactionResponse { Status = _messagesDeliveredService.DeliveredReports.Count, Message = "SUCCESS", ResponseTime = DateTime.Now, Data = new DeliveredData { TransactionID = deliveredTransaction.TransactionId, DeliveredReports = _messagesDeliveredService.DeliveredReports } }; return(Ok(transactionResponse)); } catch (Exception ex) { _logger.LogError("DeliveredMessages", ex.Message); transactionResponse = new DeliveredTransactionResponse { Status = -1, Message = ex.Message, ResponseTime = DateTime.Now, Data = new DeliveredData { TransactionID = null, DeliveredReports = null, } }; } return(BadRequest(transactionResponse)); }
/// <summary> /// Test Delivered API /// </summary> /// <param name="args"></param> /// <returns></returns> static async Task Main(string[] args) { Console.WriteLine("Test Delivered API"); var postClient = new PostClient("https://*****:*****@".\PrivateKey\test-message-delivered.key"); // Chuẩn bị dữ liệu đầu vào List <MessageDelivered> messagesDelivered = new List <MessageDelivered>(); // Tao ban tin SMS MessageDelivered delivered = new MessageDelivered { SmsInGuid = Guid.NewGuid(), Message = "tesst", ShortCode = "8050", Subscriber = "94912656901", OperatorId = 1, ReceivedTime = DateTime.Now, CooperateId = 46045, Status = "PENDING" }; messagesDelivered.Add(delivered); // Tạo transaction MessagesDeliveredTransaction messagesDeliveredTransaction = new MessagesDeliveredTransaction { // ID giao dịch TransactionId = Guid.NewGuid().ToString(), // Mã định danh của đối tác CooperateId = 46044, // Thời gian tạo transaction CreateTime = DateTime.Now, // Danh sách Delivered cần gửi MessagesDelivered = messagesDelivered }; // Chuyển transaction sang chuỗi Json string jsonTran = JsonConvert.SerializeObject(messagesDeliveredTransaction); // Chuyển chuỗi json sang bytes để tiến hành ký // Chú ý sử dụng Encoding UTF8 var byteData = Encoding.UTF8.GetBytes(jsonTran); // Chuyển byteData sang Base64 để đưa vào payload var payload = Convert.ToBase64String(byteData); Crypto.IRsaCrypto rsaCrypto = new Crypto.RsaCrypto(); // Ký vào byteData sử dụng khóa Private do provider cung cấp var signature = Convert.ToBase64String(rsaCrypto.SignHash(byteData, provider)); // Chuẩn bị transaction DeliveredTransactionRequest deliveredTransactionRequest = new DeliveredTransactionRequest { Payload = payload, Signature = signature }; // Chuyển transaction thành JSON string jsonTransactionRequest = JsonConvert.SerializeObject(deliveredTransactionRequest); // Chuyển jsonTransactionRequest thành dạng bytes mã hóa UTF8 byte[] bodyRawBytes = Encoding.UTF8.GetBytes(jsonTransactionRequest); if (postClient.IsAlive()) { // Bạn nên log mỗi cuộc gọi và kết quả trả về var response = await postClient.SendAsync(bodyRawBytes); } else { Console.WriteLine($"Url {postClient.Url} is not available"); } }