Beispiel #1
0
        public SubmitBookingResponse Submit(int bookingId)
        {
            try
            {
                using (var db = new LomsContext())
                {
                    db.Connection.Open();
                    using (var transaction = db.Connection.BeginTransaction())
                    {
                        var booking = db.Bookings.IncludeAll("ChargeInfo", "PaymentInfo.CreditCard", "PaymentInfo.CreditCard.Info", "PaymentInfo.CreditCard.Address", "PaymentInfo.BillingAccount", "PaymentInfo.BillingAccount.ChargeBack", "PaymentInfo.Items").FirstOrDefault(b => b.Id == bookingId);

                        booking.Status = BookingStatus.Confirmed;
                        db.Bookings.ApplyChanges(booking);
                        db.SaveChanges();

                        var response = new SubmitBookingResponse();
                        response.CreatorEmails = db.AssociationUserEmails.Where(e => e.AssociationUserId == booking.CreatorId && e.IsActivated).Select(e => e.Email).ToList();

                        if (booking.PaymentInfo != null)
                        {
                            if (booking.PaymentInfo.CreditCard != null)
                            {
                                //add to saved billing
                                if (!string.IsNullOrWhiteSpace(booking.PaymentInfo.CreditCard.SavedCreditCardNickname) && booking.PaymentInfo.CreditCard.SavedCreditCardId == 0)
                                {
                                    var CreditCard = new AssociationUserCreditCard();
                                    CreditCard.AssociationUserId = booking.PaymentInfo.AssociationUserId;
                                    CreditCard.TypeId = booking.PaymentInfo.CreditCard.TypeId;
                                    CreditCard.Holder = booking.PaymentInfo.CreditCard.Holder;
                                    CreditCard.ExpiryMonth = booking.PaymentInfo.CreditCard.ExpiryMonth;
                                    CreditCard.ExpiryYear = booking.PaymentInfo.CreditCard.ExpiryYear;
                                    CreditCard.Nickname = booking.PaymentInfo.CreditCard.SavedCreditCardNickname;
                                    db.AssociationUserCreditCards.ApplyChanges(CreditCard);
                                    db.SaveChanges();

                                    CreditCard.Info = new AssociationUserCreditCardInfo();
                                    CreditCard.Info.CardId = CreditCard.Id;
                                    CreditCard.Info.Number = booking.PaymentInfo.CreditCard.Info.Number;
                                    CreditCard.Info.CVC = booking.PaymentInfo.CreditCard.Info.CVC;
                                    db.AssociationUserCreditCardInfoes.ApplyChanges(CreditCard.Info);

                                    booking.PaymentInfo.CreditCard.SavedCreditCardId = CreditCard.Id;
                                    db.BookingPaymentCreditCards.ApplyChanges(booking.PaymentInfo.CreditCard);
                                    db.SaveChanges();
                                }

                                //make default billing
                                if (booking.PaymentInfo.CreditCard.SavedCrediCardIsDefaultBilling && booking.PaymentInfo.CreditCard.SavedCreditCardId != 0)
                                {
                                    var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == booking.PaymentInfo.AssociationUserId);
                                    if (defaultBilling != null && defaultBilling.CreditCardId != booking.PaymentInfo.CreditCard.SavedCreditCardId)
                                    {
                                        defaultBilling.BillingAccountId = null;
                                        defaultBilling.CreditCardId = booking.PaymentInfo.CreditCard.SavedCreditCardId;
                                    }
                                    else if (defaultBilling == null)
                                        defaultBilling = new AssociationUserDefaultBilling()
                                        {
                                            AssociationUserId = booking.PaymentInfo.AssociationUserId,
                                            CreditCardId = booking.PaymentInfo.CreditCard.SavedCreditCardId
                                        };

                                    db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                                    db.SaveChanges();
                                }
                            }
                            else if (booking.PaymentInfo.BillingAccount != null)
                            {
                                //add to saved billing
                                if (!string.IsNullOrWhiteSpace(booking.PaymentInfo.BillingAccount.SavedBillingAccountNickname) && booking.PaymentInfo.BillingAccount.SavedBillingAccountId == 0)
                                {
                                    var BillingAccount = new AssociationUserBillingAccount();
                                    BillingAccount.BillingAccountId = booking.PaymentInfo.BillingAccount.BillingAccountId;
                                    BillingAccount.BillingCode = booking.PaymentInfo.BillingAccount.BillingCode;
                                    BillingAccount.Email = booking.PaymentInfo.BillingAccount.Email;
                                    BillingAccount.ContactPerson = booking.PaymentInfo.BillingAccount.ContactPerson;
                                    BillingAccount.ContactNumber = booking.PaymentInfo.BillingAccount.ContactNumber;
                                    BillingAccount.Name = booking.PaymentInfo.BillingAccount.SavedBillingAccountNickname;
                                    db.AssociationUserBillingAccounts.ApplyChanges(BillingAccount);
                                    db.SaveChanges();

                                    booking.PaymentInfo.BillingAccount.SavedBillingAccountId = BillingAccount.Id;
                                    db.BookingPaymentBillingAccounts.ApplyChanges(booking.PaymentInfo.BillingAccount);
                                    db.SaveChanges();
                                }

                                //make default billing
                                if (booking.PaymentInfo.BillingAccount.SavedBillingAccountIsDefaultBilling && booking.PaymentInfo.BillingAccount.SavedBillingAccountId != 0)
                                {
                                    var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == booking.PaymentInfo.AssociationUserId);
                                    if (defaultBilling != null && defaultBilling.BillingAccountId != booking.PaymentInfo.BillingAccount.SavedBillingAccountId)
                                    {
                                        defaultBilling.CreditCardId = null;
                                        defaultBilling.BillingAccountId = booking.PaymentInfo.BillingAccount.SavedBillingAccountId;
                                    }
                                    else if (defaultBilling == null)
                                        defaultBilling = new AssociationUserDefaultBilling()
                                        {
                                            AssociationUserId = booking.PaymentInfo.AssociationUserId,
                                            BillingAccountId = booking.PaymentInfo.BillingAccount.SavedBillingAccountId
                                        };

                                    db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                                    db.SaveChanges();
                                }
                            }
                        }

                        string issuedBy = "";
                        if (HttpContext.Current.User.IsInRole(RoleName.StaffUser))
                            issuedBy = "LIMOBOOK / STAFF";
                        else if (HttpContext.Current.User.IsInRole(RoleName.Client))
                            issuedBy = "LIMOBOOK / ONLINE";

                        db.BookingDispatchings.ApplyChanges(
                            new BookingDispatching()
                            {
                                BookingId = booking.Id,
                                Status = DispatchingStatus.Waiting,
                                IssuedBy = issuedBy,
                                IssueDate = DateTime.UtcNow
                            }
                        );
                        db.SaveChanges();

                        //remove original booking
                        if (booking.OriginalId != 0)
                        {
                            db.Bookings.Where(b => b.Id == booking.OriginalId).ForEach(b => db.Bookings.DeleteObject(b));
                            db.SaveChanges();
                        }

                        var paymentApi = PaymentApi.Get(CurrentAssociationId);
                        db.BookingTransactionCreditCards.IncludeAll("Info").Where(t => t.BookingId == booking.Id && t.OrderDateTime == null).ForEach(t =>
                        {
                            t.PaymentApi = paymentApi.Name;
                            t.OrderNumber = booking.Id + "_" + DateTime.UtcNow.ToString("HHmmss");
                            t.OrderDateTime = DateTime.UtcNow;
                            db.BookingTransactionCreditCards.ApplyChanges(t);
                            db.SaveChanges();

                            //capture money
                            if (t.OrderType == OrderType.Capture)
                                paymentApi.Capture(t.Info.Number, t.Info.CVC, t.ExpiryYear, t.ExpiryMonth, t.OrderAmount, t.OrderNumber);
                            else if (t.OrderType == OrderType.Refund)
                                paymentApi.Refund(t.Info.Number, t.Info.CVC, t.ExpiryYear, t.ExpiryMonth, t.OrderAmount, t.OrderNumber, t.OriginalOrderNumber);
                        });

                        transaction.Commit();
                        return response;
                    }
                }
            }
            catch (PaymentApiException ex)
            {
                var response = new SubmitBookingResponse();
                response.AddError("CreditCard", ex.ToString());
                return response;
            }
            catch (Exception ex)
            {
                var response = new SubmitBookingResponse();
                response.AddError("Response", ex.ToString());
                return response;
            }
        }
        public AssociationUserBillingAccount SaveUserBillingAccount(AssociationUserBillingAccount billingAccount)
        {
            using (var db = new LomsContext())
            {
                db.Connection.Open();

                using (var transaction = db.Connection.BeginTransaction())
                {
                    db.AssociationUserBillingAccounts.ApplyChanges(billingAccount);
                    db.SaveChanges();

                    if (billingAccount.IsDefaultBilling)
                    {
                        var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == billingAccount.AssociationUserId);
                        if (defaultBilling != null && defaultBilling.BillingAccountId != billingAccount.Id)
                        {
                            defaultBilling.BillingAccountId = billingAccount.Id;
                            defaultBilling.CreditCardId = null;
                        }
                        else if (defaultBilling == null)
                            defaultBilling = new AssociationUserDefaultBilling() { AssociationUserId = billingAccount.AssociationUserId, BillingAccountId = billingAccount.Id };

                        db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                        db.SaveChanges();
                    }

                    transaction.Commit();
                }
            }

            using (var db = new LomsContext())
            {
                billingAccount = db.AssociationUserBillingAccounts.IncludeAll("BillingAccount", "BillingAccount.Country").FirstOrDefault(a => a.Id == billingAccount.Id);
                billingAccount.IsDefaultBilling = 0 != db.AssociationUserDefaultBillings.Count(b => b.AssociationUserId == billingAccount.AssociationUserId && b.BillingAccountId == billingAccount.Id);

                return billingAccount;
            }

        }
        public AssociationUserCreditCard SaveCreditCard(AssociationUserCreditCard card)
        {
            bool isAdd = card.Id == 0;

            using (var db = new LomsContext())
            {
                db.Connection.Open();

                using (var transaction = db.Connection.BeginTransaction())
                {
                    db.AssociationUserCreditCards.ApplyChanges(card);
                    db.SaveChanges();

                    if (isAdd)
                    {
                        var info = new AssociationUserCreditCardInfo();
                        info.CardId = card.Id;
                        info.Number = card.Number;
                        info.CVC = card.CVC;
                        db.AssociationUserCreditCardInfoes.ApplyChanges(info);
                        db.SaveChanges();
                    }

                    if (card.IsDefaultBilling)
                    {
                        var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == card.AssociationUserId);
                        if (defaultBilling != null && defaultBilling.CreditCardId != card.Id)
                        {
                            defaultBilling.CreditCardId = card.Id;
                            defaultBilling.BillingAccountId = null;
                        }
                        else if (defaultBilling == null)
                            defaultBilling = new AssociationUserDefaultBilling() { AssociationUserId = card.AssociationUserId, CreditCardId = card.Id };

                        db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                        db.SaveChanges();
                    }

                    if (isAdd)
                    {
                        try
                        {
                            var paymentApi = PaymentApi.Get(CurrentAssociationId);
                            paymentApi.PreAuth(card.Number, card.CVC, card.ExpiryYear, card.ExpiryMonth, "cc" + card.Id + "_" + DateTime.UtcNow.ToString("HHmmss"));
                        }
                        catch (Exception ex)
                        {
                            card.AddError("Entity", ex.Message);
                            return card;
                        }
                    }

                    transaction.Commit();
                }
            }

            using (var db = new LomsContext())
            {
                card = db.AssociationUserCreditCards.IncludeAll("Info").FirstOrDefault(a => a.Id == card.Id);
                card.Number = AssociationUserCreditCard.ObfuscateCreditCardNumber(card.Info.Number);
                card.Info = null;
                card.IsDefaultBilling = 0 != db.AssociationUserDefaultBillings.Count(b => b.AssociationUserId == card.AssociationUserId && b.CreditCardId == card.Id);
                card.AcceptChanges();
                return card;
            }
        }
     public bool Equals(AssociationUserDefaultBilling other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.AssociationUserId == 0 && AssociationUserId == 0)
 			return false;
 		else
 			return other.AssociationUserId == AssociationUserId;
     }
        public AssociationUserCreditCard SaveCreditCard(AssociationUserCreditCard card)
        {
            bool isAdd = card.Id == 0;

            using (var db = new LomsContext())
            {
                db.Connection.Open();

                using (var transaction = db.Connection.BeginTransaction())
                {
                    db.AssociationUserCreditCards.ApplyChanges(card);
                    db.SaveChanges();

                    if (isAdd)
                    {
                        var info = new AssociationUserCreditCardInfo();
                        info.CardId = card.Id;
                        info.Number = card.Number;
                        info.CVC = card.CVC;
                        db.AssociationUserCreditCardInfoes.ApplyChanges(info);
                        db.SaveChanges();
                    }

                    if (card.IsDefaultBilling)
                    {
                        var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == card.AssociationUserId);
                        if (defaultBilling != null && defaultBilling.CreditCardId != card.Id)
                        {
                            defaultBilling.BillingAccountId = null;
                            defaultBilling.CreditCardId = card.Id;
                        }
                        else if (defaultBilling == null)
                            defaultBilling = new AssociationUserDefaultBilling() { AssociationUserId = card.AssociationUserId, CreditCardId = card.Id };

                        db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                        db.SaveChanges();
                    }

                    transaction.Commit();
                }

            }

            using (var db = new LomsContext())
            {
                card = db.AssociationUserCreditCards.IncludeAll("Info").FirstOrDefault(a => a.Id == card.Id);
                card.Number = ObfuscateCreditCardNumber(card.Info.Number);
                card.Info = null;
                card.AcceptChanges();
                return card;
            }
        }
Beispiel #6
0
     private void FixupDefaultBilling(AssociationUserDefaultBilling previousValue)
     {
         // This is the principal end in an association that performs cascade deletes.
         // Update the event listener to refer to the new dependent.
         if (previousValue != null)
         {
             ChangeTracker.ObjectStateChanging -= previousValue.HandleCascadeDelete;
         }
 
         if (DefaultBilling != null)
         {
             ChangeTracker.ObjectStateChanging += DefaultBilling.HandleCascadeDelete;
         }
 
         if (IsDeserializing)
         {
             return;
         }
 
         if (DefaultBilling != null)
         {
             DefaultBilling.AssociationUserId = Id;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("DefaultBilling")
                 && (ChangeTracker.OriginalValues["DefaultBilling"] == DefaultBilling))
             {
                 ChangeTracker.OriginalValues.Remove("DefaultBilling");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("DefaultBilling", previousValue);
                 // This is the principal end of an identifying association, so the dependent must be deleted when the relationship is removed.
                 // If the current state of the dependent is Added, the relationship can be changed without causing the dependent to be deleted.
                 if (previousValue != null && previousValue.ChangeTracker.State != ObjectState.Added)
                 {
                     previousValue.MarkAsDeleted();
                 }
             }
             if (DefaultBilling != null && !DefaultBilling.ChangeTracker.ChangeTrackingEnabled)
             {
                 DefaultBilling.StartTracking();
             }
         }
     }