//Complete the registration
        private async Task <bool> CompleteRegistration(string orderId)
        {
            var order = await _context.InitiatedOrders.FindAsync(orderId);

            if (order == null)
            {
                return(false);
            }

            var course = _courseService.GetCourse(order.CourseId);
            var user   = await _context.Users.FindAsync(order.UserId);

            //Check if user or course is null
            if (user == null || order.Reason != EnumList.PaymentReason.Register || course == null)
            {
                return(false);
            }


            //Add the device made the registration

            var isNew       = _info.IsDeviceNew(user.Id, order.AuthCookies, order.Ip);
            var groupNumber = _info.GetDeviceGroupNumber(user.Id, order.AuthCookies, order.Ip);
            var device      = new UserDevice
            {
                User = user, Activity = EnumList.Activity.CourseRegister, Ip = order.Ip, DeviceType = order.DeviceType, AuthCookies = order.AuthCookies,
                New  = isNew, GroupNumber = groupNumber, OperatingSystem = order.OperatingSystem
            };
            await _context.AddAsync(device);

            await _context.SaveChangesAsync();

            //Add payment activity
            var device2 = new UserDevice
            {
                User = user, Activity = EnumList.Activity.Payment, Ip = order.Ip, DeviceType = order.DeviceType, AuthCookies = order.AuthCookies,
                New  = isNew, GroupNumber = groupNumber, OperatingSystem = order.OperatingSystem
            };

            _context.Add(device2);
            await _context.SaveChangesAsync();

            //Determine registration expire date
            var expireDate = _courseService.EstimateRegExpireDate(order.CourseId);

            //Add registration
            await _courseService.AddOrRenewRegistration(order.CourseId, order.UserId, device);



            //Add invoices

            var paidInvoice = new MatterixInvoice();

            if (order.PayAllNow)
            {
                paidInvoice = new MatterixInvoice
                {
                    Course = course, User = user, Amount = course.Price, CurrentAmount = course.Price, Paid = true, Reason = EnumList.InvoiceReason.Registration, OriginalDeadline = Format.NorwayDateTimeNow().AddDays(15), CurrentDeadline = Format.NorwayDateTimeNow().AddDays(15), OriginalDueDate = Format.NorwayDateTimeNow().Date, CurrentDueDate = Format.NorwayDateTimeNow().Date, InvoiceType = EnumList.InvoiceType.Invoice,
                };
//                paidInvoice = paidInvoice;

                //This invoice is paid now
                await _context.AddAsync(paidInvoice);

                await _context.SaveChangesAsync();
            }
            else
            {
                var invoices = _paymentService.CreateOnRegisterInvoices(order.CourseId, user);

                foreach (var inv in invoices)
                {
                    if (inv.Reason == EnumList.InvoiceReason.Registration)
                    {
                        paidInvoice = inv;
                        await _context.AddAsync(paidInvoice);

                        await _context.SaveChangesAsync();
                    }
                    else if (inv.Reason == EnumList.InvoiceReason.MonthlyPayment)
                    {
                        await _context.AddAsync(inv);

                        await _context.SaveChangesAsync();
                    }
                }
            }

            //Add Payment
            var payment = new MatterixPayment
            {
                Course = course, User = user, Reason = EnumList.PaymentReason.Register, Invoice = paidInvoice,
                Amount = paidInvoice.Amount, Method = order.PaymentMethod, PaymentServiceRef = orderId
            };

            await _context.AddAsync(payment);

            await _context.SaveChangesAsync();

            paidInvoice.Paid             = true;
            paidInvoice.PaymentId        = payment.Id;
            paidInvoice.NextNotification = DateTime.MaxValue;

            _context.Update(paidInvoice);
            await _context.SaveChangesAsync();


            //Here the invoice and payments are done -- send the email
            await _email.InvoicePaidEmail(paidInvoice.Number);


            //Here the registration is done -- send email

            await _email.CourseRegisterCompletedEmail(order.UserId, order.CourseId, expireDate);

            order.Status = EnumList.OrderStatus.Completed;
            _context.Update(order);
            await _context.SaveChangesAsync();

            return(true);
        }