public IActionResult CreateVirtualTicket() { byte[] encryptedTicketInNDEFMessage = new byte[] { }; SmartTicket virtualTicket = null; if (!_dbContext.SmartTickets.Any(s => s.Username == User.Identity.Name && s.Virtual && !s.Deactivated)) { try { byte[] virtualTicketId = Guid.NewGuid().ToByteArray(); virtualTicket = new SmartTicket() { CardId = BitConverter.ToString(virtualTicketId), Credit = 0, TicketType = "BIT", Username = User.Identity.Name, Virtual = true, UsageTimestamp = DateTime.Now, Deactivated = false }; byte[] encryptedTicket = TicketEncryption.EncryptTicket(Utility.ConvertToEncryptableSmartTicket(virtualTicket), TicketEncryption.GetPaddedIV(virtualTicketId)); encryptedTicketInNDEFMessage = new NDEFMessage(encryptedTicket, NDEFRecordType.Types.Text).GetFormattedBlock(); _dbContext.SmartTickets.Add(virtualTicket); _dbContext.SaveChanges(); } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } return(Ok(BitConverter.ToString(encryptedTicketInNDEFMessage))); } return(StatusCode((int)HttpStatusCode.InternalServerError, "The user already has an associated virtual ticket")); }
public IActionResult AddCredit([FromBody] CreditRecharge recharge) { byte[] encryptedTicketInNDEFMessage; SmartTicket ticket = _dbContext.SmartTickets.Find(recharge.TicketId); // Add online payments logic here to authorize the balance increase if (ticket != null) { try { ticket.Credit += recharge.Amount; EncryptableSmartTicket encryptableTicket = Utility.ConvertToEncryptableSmartTicket(ticket); byte[] encryptedTicket = TicketEncryption.EncryptTicket(encryptableTicket, TicketEncryption.GetPaddedIV(encryptableTicket.CardID)); encryptedTicketInNDEFMessage = new NDEFMessage(encryptedTicket, NDEFRecordType.Types.Text).GetFormattedBlock(); _dbContext.SaveChanges(); _dbContext.CreditTransactions.Add(new CreditTransaction() { Amount = recharge.Amount, CardId = recharge.TicketId, Date = DateTime.Now, Location = "online" }); _dbContext.SaveChanges(); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } return(Ok(encryptedTicketInNDEFMessage)); } else { return(StatusCode((int)HttpStatusCode.InternalServerError, "The ticket has not been found.")); } }