public static void OnTransactionConfirmed(JToken jsonParams)
        {
            TransactionConfirmedModel model = jsonParams.ToObject <TransactionConfirmedModel>();

            using (DBEntities dbe = new DBEntities())
            {
                InvoicePayment payment = dbe.InvoicePayment.Include("Invoice").Include("Invoice.CreatedBy").SingleOrDefault(p => p.Address == model.Address && p.CurrencyCode == model.CurrencyCode);
                if (payment != null)
                {
                    double amountRequired = GetAmountRequired(payment.Invoice.FiatAmount, (double)payment.ExchangeRate, model.CurrencyCode);

                    if (model.Amount >= amountRequired)
                    {
                        payment.Invoice.State         = (int)InvoiceState.TRANSACTION_CONFIRMED;
                        payment.Invoice.TransactionId = model.TXID;
                        payment.Invoice.DateReceived  = DateTime.Now;
                        dbe.SaveChanges();

                        // send mail
                        EmailManager.SendMailToPaymentReciever(payment.Invoice, model);
                        EmailManager.SendMailToPaymentSender(payment.Invoice, model);
                    }
                    else
                    {
                        var transactionTime = DateTimeOffset.FromUnixTimeSeconds(model.Timestamp).UtcDateTime;

                        // Special case: if the transaction is less that 3 minutes late, previous exchange rate is still alowed
                        if (payment.Invoice.ExchangeRateSetTime != null && transactionTime.Subtract(payment.Invoice.ExchangeRateSetTime.Value).TotalMinutes < 3)
                        {
                            double previousAmountRequired = GetAmountRequired(payment.Invoice.FiatAmount, (double)payment.PreviousExchangeRate, model.CurrencyCode);
                            if (model.Amount >= previousAmountRequired)
                            {
                                payment.Invoice.State         = (int)InvoiceState.TRANSACTION_SEEN;
                                payment.Invoice.TransactionId = model.TXID;
                                dbe.SaveChanges();
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public static void SendMailToPaymentSender(Invoice invoice, TransactionConfirmedModel confirmation)
        {
            string url = $"https://live.blockcypher.com/{confirmation.CurrencyCode.ToLower()}/tx/{confirmation.TXID}";

            string subject = $"CryptoMail - outcoming payment to {invoice.CreatedBy.Email} confirmed";
            string body    = System.IO.File.ReadAllText("wwwroot/web-api-static/templates/email/paymentConfirmationSender.html");

            body = body.Replace("{Title}", subject)
                   .Replace("{Reciever}", invoice.CreatedBy.Email)
                   .Replace("{Invoice}", invoice.Name)
                   .Replace("{AmountFiat}", invoice.FiatAmount.ToString())
                   .Replace("{CurrencyFiat}", invoice.FiatCurrencyCode)
                   .Replace("{AmountCrypto}", confirmation.Amount.ToString())
                   .Replace("{CurrencyCrypto}", confirmation.CurrencyCode)
                   .Replace("{TargetAddressCrypto}", confirmation.Address)
                   .Replace("{TransactionTimestamp}", invoice.DateReceived.ToString())
                   .Replace("{BlockchainUrl}", $"<a href=\"{url}\">{url}</a>");
            string attachment = !string.IsNullOrEmpty(invoice.File) ? $"{invoice.File}|{invoice.FileName}|{invoice.FileMime}" : "";

            EmailSender sender = new EmailSender(_configuration);
            Email       email  = sender.CreateEmailEntity("*****@*****.**", invoice.Recipient, body, subject, attachment);

            sender.AddEmailToQueue(email);
        }