/// <summary>
 /// Add new transaction to the data base
 /// </summary>
 /// <param name="item">Transaction to add</param>
 /// <returns>The transaction added with date and id inserted</returns>
 public Transaction AddTransaction(Transaction item)
 {
     item.Id = ObjectId.GenerateNewId().ToString();
     item.Date = DateTime.UtcNow;
     try
     {
         transactions.InsertOne(item);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         return null;
     }           
     return item;
 }
 /// <summary>
 /// Update one specific transaction from the database
 /// </summary>
 /// <param name="id">id assigned when the transaction was inserted in the database</param>
 /// <param name="item"></param>
 /// <returns>True if it was successful or false if not</returns>
 public bool UpdateTransaction(string id, Transaction item)
 {
     try
     {
         var filter = Builders<Transaction>.Filter.Eq("_id", id);
         var update = Builders<Transaction>.Update.Set("MundipaggCreated", item.MundipaggCreated)
         .Set("Date", item.Date)
         .Set("CardCVV", item.CardCVV)
         .Set("CardType", item.CardType)
         .Set("CardNumber", item.CardNumber)
         .Set("CardExpMonth", item.CardExpMonth)
         .Set("CardExpYear", item.CardExpYear)
         .Set("CardName", item.CardName)
         .Set("Value", item.Value);
         var result = transactions.UpdateOne(filter, update);
         return result.ModifiedCount == 1;
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         return false;
     }   
 }
        //------Private Methods-----

        //send the transaction to Mundipagg API
        private bool AuthorizeTransaction(Transaction item)
        {

            var cardBrand = CardTypetoEnum(item.CardType);
            //if the card is not supported by Mundipagg Api return
            if (cardBrand == null) return false;           

            // Creates the credit card transaction.
            var transaction = new CreditCardTransaction()
            {
                AmountInCents = (long)double.Parse(item.Value) * 100,
                CreditCard = new CreditCard()
                {
                    CreditCardNumber = item.CardNumber,
                    CreditCardBrand = cardBrand,
                    ExpMonth = item.CardExpMonth,
                    ExpYear = item.CardExpYear,
                    SecurityCode = item.CardCVV,
                    HolderName = item.CardName,
                }
            };

            try
            {
                // Creates the client that will send the transaction.
                var guid = new Guid("85328786-8BA6-420F-9948-5352F5A183EB");
                var uri = new Uri("https://sandbox.mundipaggone.com");
                var serviceClient = new GatewayServiceClient(guid,uri);

                // Authorizes the credit card transaction and returns the gateway response.
                var httpResponse = serviceClient.Sale.Create(transaction);

                // API response code
                Debug.WriteLine("Status: {0}", httpResponse.HttpStatusCode);

                var createSaleResponse = httpResponse.Response;
                if (httpResponse.HttpStatusCode == HttpStatusCode.Created)
                {
                    foreach (var creditCardTransaction in createSaleResponse.CreditCardTransactionResultCollection)
                    {
                        Debug.WriteLine(creditCardTransaction.AcquirerMessage);                        
                    }
                    return true;
                }
                else
                {
                    if (createSaleResponse.ErrorReport != null)
                    {
                        foreach (ErrorItem errorItem in createSaleResponse.ErrorReport.ErrorItemCollection)
                        {
                            Debug.WriteLine("Error {0}: {1}", errorItem.ErrorCode, errorItem.Description);                            
                        }
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;         
            }
        }