예제 #1
0
        public SaveBookingPaymentInfoResponse SaveBookingPaymentInfo(int bookingId, BookingPayment info)
        {
            var response = new SaveBookingPaymentInfoResponse();

            try
            {
                using (var db = new LomsContext())
                {
                    var booking = GetBookingWithChargeInfo(db, bookingId);
                    response.ChargeInfo = booking.ChargeInfo;

                    var payment = db.BookingPayments.IncludeAll("CreditCard", "CreditCard.Info", "CreditCard.Address", "BillingAccount", "Items").FirstOrDefault(b => b.BookingId == bookingId);
                    if (payment == null)
                        payment = new BookingPayment() { BookingId = bookingId };

                    payment.AssociationUserId = info.AssociationUserId;
                    db.BookingPayments.ApplyChanges(payment);
                    db.SaveChanges();

                    if (info.CreditCard != null)
                    {
                        if (payment.BillingAccount != null)
                            db.BookingPaymentBillingAccounts.DeleteObject(payment.BillingAccount);

                        if (payment.CreditCard == null)
                            payment.CreditCard = new BookingPaymentCreditCard() { CreditCardId = bookingId };

                        if (info.CreditCard.SavedCreditCardId != 0) //saved card
                        {
                            var savedCreditCard = db.AssociationUserCreditCards.IncludeAll("Info").FirstOrDefault(cc => cc.Id == info.CreditCard.SavedCreditCardId);

                            payment.CreditCard.TypeId = savedCreditCard.TypeId;
                            payment.CreditCard.Holder = savedCreditCard.Holder;
                            payment.CreditCard.ExpiryMonth = savedCreditCard.ExpiryMonth;
                            payment.CreditCard.ExpiryYear = savedCreditCard.ExpiryYear;
                            payment.CreditCard.SavedCreditCardId = info.CreditCard.SavedCreditCardId;
                            payment.CreditCard.SavedCreditCardNickname = null;
                            payment.CreditCard.SavedCrediCardIsDefaultBilling = info.CreditCard.SavedCrediCardIsDefaultBilling;

                            db.BookingPaymentCreditCards.ApplyChanges(payment.CreditCard);
                            db.SaveChanges();

                            if (payment.CreditCard.Info == null)
                                payment.CreditCard.Info = new BookingPaymentCreditCardInfo();

                            payment.CreditCard.Info.CardId = payment.CreditCard.CreditCardId;
                            payment.CreditCard.Info.Number = savedCreditCard.Info.Number;
                            payment.CreditCard.Info.CVC = savedCreditCard.Info.CVC;

                            db.BookingPaymentCreditCardInfoes.ApplyChanges(payment.CreditCard.Info);
                            db.SaveChanges();
                        }
                        else  //new credit card
                        {
                            payment.CreditCard.TypeId = info.CreditCard.TypeId;
                            payment.CreditCard.Holder = info.CreditCard.Holder;
                            payment.CreditCard.ExpiryMonth = info.CreditCard.ExpiryMonth;
                            payment.CreditCard.ExpiryYear = info.CreditCard.ExpiryYear;
                            payment.CreditCard.SavedCreditCardId = info.CreditCard.SavedCreditCardId;
                            payment.CreditCard.SavedCreditCardNickname = info.CreditCard.SavedCreditCardNickname;
                            payment.CreditCard.SavedCrediCardIsDefaultBilling = info.CreditCard.SavedCrediCardIsDefaultBilling;

                            db.BookingPaymentCreditCards.ApplyChanges(payment.CreditCard);
                            db.SaveChanges();

                            if (payment.CreditCard.Info == null)
                                payment.CreditCard.Info = new BookingPaymentCreditCardInfo();

                            payment.CreditCard.Info.CardId = payment.CreditCard.CreditCardId;
                            payment.CreditCard.Info.Number = info.CreditCard.Number;
                            payment.CreditCard.Info.CVC = info.CreditCard.CVC;

                            db.BookingPaymentCreditCardInfoes.ApplyChanges(payment.CreditCard.Info);
                            db.SaveChanges();
                        }

                        //remove old transactions
                        db.BookingTransactionCreditCards.Where(p => p.BookingId == bookingId && p.OrderDateTime == null).ForEach(item => db.BookingTransactionCreditCards.DeleteObject(item));
                        db.BookingTransactionBillingAccounts.Where(p => p.BillingAccountId == bookingId).ForEach(item => db.BookingTransactionBillingAccounts.DeleteObject(item));

                        //create transaction
                        decimal amount = booking.ChargeInfo.Amount;
                        bool existedPaidTransactions = booking.TransactionCreditCards.Where(p => p.BookingId == bookingId && p.OrderDateTime != null).HasItems();
                        if (booking.OriginalId != 0 && existedPaidTransactions)
                        {
                            var originalBooking = GetBookingWithChargeInfo(db, booking.OriginalId);
                            amount = amount - originalBooking.ChargeInfo.Amount;
                        }

                        var transactionCreditCard = new BookingTransactionCreditCard();
                        transactionCreditCard.BookingId = booking.Id;
                        transactionCreditCard.TypeId = payment.CreditCard.TypeId;
                        transactionCreditCard.Holder = payment.CreditCard.Holder;
                        transactionCreditCard.ExpiryMonth = payment.CreditCard.ExpiryMonth;
                        transactionCreditCard.ExpiryYear = payment.CreditCard.ExpiryYear;
                        transactionCreditCard.PaymentApi = null;
                        transactionCreditCard.OrderType = OrderType.Capture;
                        transactionCreditCard.OrderNumber = null;
                        transactionCreditCard.OrderDateTime = null;
                        transactionCreditCard.OrderAmount = amount;
                        db.BookingTransactionCreditCards.ApplyChanges(transactionCreditCard);
                        db.SaveChanges();

                        var transactionCreditCardInfo = new BookingTransactionCreditCardInfo()
                        {
                            CreditCardId = transactionCreditCard.Id,
                            Number = payment.CreditCard.Info.Number,
                            CVC = payment.CreditCard.Info.CVC

                        };
                        db.BookingTransactionCreditCardInfoes.ApplyChanges(transactionCreditCardInfo);
                        db.SaveChanges();

                        transactionCreditCard.Number = AssociationUserCreditCard.ObfuscateCreditCardNumber(transactionCreditCardInfo.Number);
                        response.NewTransactionCreditCard = transactionCreditCard;
                    }
                    else if (info.BillingAccount != null)
                    {
                        if (payment.CreditCard != null)
                            db.BookingPaymentCreditCards.DeleteObject(payment.CreditCard);

                        if (payment.BillingAccount == null)
                            payment.BillingAccount = new BookingPaymentBillingAccount() { BillingAccountId = bookingId };

                        if (info.BillingAccount.SavedBillingAccountId != 0) //saved Billing Account
                        {
                            var savedBillingAccount = db.AssociationUserBillingAccounts.FirstOrDefault(cc => cc.Id == info.BillingAccount.SavedBillingAccountId);

                            payment.BillingAccount.ChargeBackId = savedBillingAccount.BillingAccountId;
                            payment.BillingAccount.BillingCode = savedBillingAccount.BillingCode;
                            payment.BillingAccount.Email = savedBillingAccount.Email;
                            payment.BillingAccount.ContactPerson = savedBillingAccount.ContactPerson;
                            payment.BillingAccount.ContactNumber = savedBillingAccount.ContactNumber;

                            payment.BillingAccount.SavedBillingAccountNickname = null;
                        }
                        else  //new Billing Account
                        {
                            payment.BillingAccount.ChargeBackId = info.BillingAccount.ChargeBackId;
                            payment.BillingAccount.BillingCode = info.BillingAccount.BillingCode;
                            payment.BillingAccount.Email = info.BillingAccount.Email;
                            payment.BillingAccount.ContactPerson = info.BillingAccount.ContactPerson;
                            payment.BillingAccount.ContactNumber = info.BillingAccount.ContactNumber;

                            payment.BillingAccount.SavedBillingAccountNickname = info.BillingAccount.SavedBillingAccountNickname;
                        }

                        payment.BillingAccount.SavedBillingAccountId = info.BillingAccount.SavedBillingAccountId;
                        payment.BillingAccount.SavedBillingAccountIsDefaultBilling = info.BillingAccount.SavedBillingAccountIsDefaultBilling;

                        db.BookingPaymentBillingAccounts.ApplyChanges(payment.BillingAccount);
                        db.SaveChanges();

                        //remove old transactions
                        db.BookingTransactionCreditCards.Where(p => p.BookingId == bookingId && p.OrderDateTime == null).ForEach(item => db.BookingTransactionCreditCards.DeleteObject(item));

                        //create transaction
                        var transactionBillingAccount = db.BookingTransactionBillingAccounts.SingleOrDefault(t => t.BillingAccountId == bookingId);
                        if (transactionBillingAccount == null)
                            transactionBillingAccount = new BookingTransactionBillingAccount() { BillingAccountId = bookingId };
                        transactionBillingAccount.ChargeBackId = payment.BillingAccount.ChargeBackId;
                        transactionBillingAccount.BillingCode = payment.BillingAccount.BillingCode;
                        transactionBillingAccount.Email = payment.BillingAccount.Email;
                        transactionBillingAccount.ContactPerson = payment.BillingAccount.ContactPerson;
                        transactionBillingAccount.ContactNumber = payment.BillingAccount.ContactNumber;
                        db.BookingTransactionBillingAccounts.ApplyChanges(transactionBillingAccount);
                        db.SaveChanges();

                        response.NewTransactionBillingAccount = transactionBillingAccount;
                    }

                }
                using (var db = new LomsContext())
                {
                    response.PaymentInfo = db.BookingPayments.IncludeAll("CreditCard", "CreditCard.Info", "CreditCard.Address", "BillingAccount", "BillingAccount.ChargeBack", "BillingAccount.ChargeBack.Country", "Items").FirstOrDefault(b => b.BookingId == bookingId);
                    if (response.PaymentInfo.CreditCard != null)
                    {
                        if (response.PaymentInfo.CreditCard.Info == null)
                            response.PaymentInfo.CreditCard.Info = db.BookingPaymentCreditCardInfoes.SingleOrDefault(i => i.CardId == bookingId);

                        response.PaymentInfo.CreditCard.Number = AssociationUserCreditCard.ObfuscateCreditCardNumber(response.PaymentInfo.CreditCard.Info.Number);
                        response.PaymentInfo.CreditCard.Info = null;
                        response.PaymentInfo.CreditCard.AcceptChanges();
                    }

                    response.NewTransactionBillingAccount = db.BookingTransactionBillingAccounts.IncludeAll("ChargeBack", "ChargeBack.Country").SingleOrDefault(t => t.BillingAccountId == bookingId);

                    return response;
                }
            }
            catch (Exception ex)
            {
                response = new SaveBookingPaymentInfoResponse();
                response.AddError("Response", ex.ToString());
                return response;
            }
        }
     public bool Equals(BookingTransactionBillingAccount other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.BillingAccountId == 0 && BillingAccountId == 0)
 			return false;
 		else
 			return other.BillingAccountId == BillingAccountId;
     }
예제 #3
0
파일: Booking.cs 프로젝트: felixthehat/Limo
     private void FixupTransactionBillingAccount(BookingTransactionBillingAccount 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 (TransactionBillingAccount != null)
         {
             ChangeTracker.ObjectStateChanging += TransactionBillingAccount.HandleCascadeDelete;
         }
 
         if (IsDeserializing)
         {
             return;
         }
 
         if (TransactionBillingAccount != null)
         {
             TransactionBillingAccount.BillingAccountId = Id;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("TransactionBillingAccount")
                 && (ChangeTracker.OriginalValues["TransactionBillingAccount"] == TransactionBillingAccount))
             {
                 ChangeTracker.OriginalValues.Remove("TransactionBillingAccount");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("TransactionBillingAccount", 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 (TransactionBillingAccount != null && !TransactionBillingAccount.ChangeTracker.ChangeTrackingEnabled)
             {
                 TransactionBillingAccount.StartTracking();
             }
         }
     }