Beispiel #1
0
        public void Seed()
        {
            if (!_context.Organisation.Any())
            {
                var org = new Organisation()
                {
                    Name             = "Canterbury & New Zealand Business Association",
                    Code             = "ABNZ",
                    StreetAddressOne = "301 Tuam Street",
                    City             = "Christchurch",
                    Country          = "New Zealand",
                    CreatedAt        = DateTime.Now
                };
                _context.Organisation.Add(org);
                _context.SaveChanges();
            }

            if (!_context.User.Any())
            {
                var org = _context.Organisation.First();

                var user1 = new User()
                {
                    Email        = "*****@*****.**",
                    Name         = "guest",
                    Password     = _cryptography.HashMD5("guest"),
                    Active       = true,
                    Organisation = org
                };

                var user2 = new User()
                {
                    Email        = "*****@*****.**",
                    Name         = "Helerson Lage",
                    Password     = "******",
                    Active       = true,
                    Organisation = org
                };

                var user3 = new User()
                {
                    Email        = "*****@*****.**",
                    Name         = "John George",
                    Password     = _cryptography.HashMD5("john"),
                    Active       = true,
                    Organisation = org
                };

                _context.User.Add(user1);
                _context.User.Add(user2);
                _context.User.Add(user3);

                _context.SaveChanges();
            }

            if (!_context.Invoice.Any())
            {
                var user = _context.User.FirstOrDefault(u => u.Email == "*****@*****.**");

                var invoice1 = new Invoice()
                {
                    InvoiceNumber       = "ABNZ000420",
                    DateCreated         = new DateTime(2017, 10, 5),
                    DateDue             = new DateTime(2017, 10, 17),
                    ClientName          = "Electrocal Commission",
                    ClientContactPerson = "Glen Clarke",
                    ClientContact       = "530/546A Memorial Ave\\r\\nChristchurch Airport\\r\\nChristchurch 8053",
                    Email           = "*****@*****.**",
                    PaymentId       = _cryptography.GenerateTempPassword(12),
                    Status          = InvoiceStatus.Draft,
                    GstRate         = .15m,
                    GstNumber       = "96-712-561",
                    CharitiesNumber = "CC20097",
                    Creator         = user,
                    InvoiceLine     = new List <InvoiceLine>()
                    {
                        new InvoiceLine()
                        {
                            Description = "Fundraising Dinner",
                            Quantity    = 1,
                            UnitPrice   = 25,
                            Amount      = 25
                        }
                    }
                };

                var invoice2 = new Invoice()
                {
                    InvoiceNumber   = "ABNZ000421",
                    DateCreated     = new DateTime(2017, 11, 13),
                    DateDue         = new DateTime(2017, 11, 27),
                    ClientName      = "Jason Carpets",
                    ClientContact   = "297 Moorhouse Ave\\r\\nSydenham\\r\\nChristchurch 8011",
                    Email           = "*****@*****.**",
                    PaymentId       = _cryptography.GenerateTempPassword(12),
                    Status          = InvoiceStatus.Issued,
                    GstRate         = .15m,
                    GstNumber       = "96-712-561",
                    CharitiesNumber = "CC20097",
                    Creator         = user,
                    InvoiceLine     = new List <InvoiceLine>()
                    {
                        new InvoiceLine()
                        {
                            Description = "Fundraising Dinner",
                            Quantity    = 2,
                            UnitPrice   = 25,
                            Amount      = 50
                        }
                    }
                };

                var invoice3 = new Invoice()
                {
                    InvoiceNumber   = "ABNZ000422",
                    DateCreated     = new DateTime(2017, 9, 9),
                    DateDue         = new DateTime(2017, 9, 30),
                    ClientName      = "Transtellar",
                    ClientContact   = "52 Solmine Ave\\r\\nRiccarton\\r\\nChristchurch 8025",
                    Email           = "*****@*****.**",
                    PaymentId       = _cryptography.GenerateTempPassword(12),
                    Status          = InvoiceStatus.Paid,
                    GstRate         = .15m,
                    GstNumber       = "96-712-561",
                    CharitiesNumber = "CC20097",
                    Creator         = user,
                    InvoiceLine     = new List <InvoiceLine>()
                    {
                        new InvoiceLine()
                        {
                            Description = "Fundraising Dinner",
                            Quantity    = 1,
                            UnitPrice   = 21.74M,
                            Amount      = 21.74M
                        },
                        new InvoiceLine()
                        {
                            Description = "Bookkeeping 2 hours @21.74 per hour",
                            Quantity    = 2,
                            UnitPrice   = 21.74M,
                            Amount      = 43.48M
                        }
                    }
                };

                _context.Add(invoice1);
                _context.Add(invoice2);
                _context.Add(invoice3);

                _context.SaveChanges();
            }
        }
        public PaymentResponse ChargeCard(PaymentRequest paymentRequest)
        {
            PaymentResponse response = new PaymentResponse();
            Invoice         invoice  = _invoiceService.GetInvoiceByPaymentId(paymentRequest.PaymentId);

            // TODO add logic for repayment

            if (invoice == null)
            {
                response.Status  = "failed";
                response.Message = "Invalid invoice number";
            }
            else if (invoice.Status == InvoiceStatus.Issued)
            {
                // TODO Change test key
                string apiKey = _env.IsDevelopment() ? "sk_test_bt2EhY73T2WBSgIi5ukAMKjx" : "sk_test_bt2EhY73T2WBSgIi5ukAMKjx";
                StripeConfiguration.SetApiKey(apiKey);

                var myCharge = new StripeChargeCreateOptions();
                myCharge.SourceTokenOrExistingSourceId = paymentRequest.TokenId;
                myCharge.Amount                = Decimal.ToInt32(invoice.GrandTotal * 100);
                myCharge.Currency              = "nzd";
                myCharge.Description           = "Invoice No - " + invoice.InvoiceNumber;
                myCharge.Metadata              = new Dictionary <string, string>();
                myCharge.Metadata["PaymentId"] = paymentRequest.PaymentId;

                var          chargeService = new StripeChargeService();
                StripeCharge stripeCharge  = chargeService.Create(myCharge);

                // TODO Handle messages better. Seller message is for devs, not customers
                response.Status  = stripeCharge.Status;
                response.Message = stripeCharge.Outcome.SellerMessage;

                PaymentModel paymentModel = new PaymentModel()
                {
                    Amount      = invoice.GrandTotal,
                    Charge      = JsonConvert.SerializeObject(stripeCharge),
                    ChargeId    = stripeCharge.Id,
                    Type        = paymentRequest.Type,
                    Gateway     = paymentRequest.Gateway,
                    InvoiceNo   = invoice.InvoiceNumber,
                    Status      = stripeCharge.Status,
                    Token       = paymentRequest.TokenObj,
                    TokenId     = paymentRequest.TokenId,
                    PaymentId   = paymentRequest.PaymentId,
                    paymentDate = DateTime.Now
                };

                _context.Add <PaymentModel>(paymentModel);

                if (response.Status.Equals("succeeded"))
                {
                    // TODO Update invoice status, send email
                    invoice.Status = InvoiceStatus.Paid;
                }

                _context.SaveChanges();
            }
            else if (invoice.Status == InvoiceStatus.Paid || invoice.Status == InvoiceStatus.Cancelled)
            {
                response.Status  = "failed";
                response.Message = "Invoice has already been paid";
            }
            else if (invoice.Status == InvoiceStatus.Draft || invoice.Status == InvoiceStatus.New)
            {
                response.Status  = "failed";
                response.Message = "Invoice has not been sent";
            }

            return(response);
        }