public bool Equals(BookingTransactionCreditCard other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.Id == 0 && Id == 0)
 			return false;
 		else
 			return other.Id == Id;
     }
Пример #2
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;
            }
        }
     private void FixupBookingNewTransactionCreditCard(BookingTransactionCreditCard previousValue)
     {
         if (IsDeserializing)
         {
             return;
         }
 
         if (previousValue != null && ReferenceEquals(previousValue.Address, this))
         {
             previousValue.Address = null;
         }
 
         if (BookingNewTransactionCreditCard != null)
         {
             BookingNewTransactionCreditCard.Address = this;
             CreditCardId = BookingNewTransactionCreditCard.Id;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("BookingNewTransactionCreditCard")
                 && (ChangeTracker.OriginalValues["BookingNewTransactionCreditCard"] == BookingNewTransactionCreditCard))
             {
                 ChangeTracker.OriginalValues.Remove("BookingNewTransactionCreditCard");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("BookingNewTransactionCreditCard", previousValue);
             }
             if (BookingNewTransactionCreditCard != null && !BookingNewTransactionCreditCard.ChangeTracker.ChangeTrackingEnabled)
             {
                 BookingNewTransactionCreditCard.StartTracking();
             }
         }
     }
Пример #4
0
        public GetAllowedPaymentMehodResponse GetAllowedPaymentMehod(int bookingId)
        {
            try
            {
                using (var db = new LomsContext())
                {
                    var booking = GetBookingWithChargeInfo(db, bookingId);
                    if (booking.OriginalId == 0 || booking.TransactionCreditCards.IsEmpty())
                        return new GetAllowedPaymentMehodResponse() { PaymentMehod = PaymentMehod.Any, ChargeInfo = booking.ChargeInfo };

                    var originalBooking = GetBookingWithChargeInfo(db, booking.OriginalId);
                    decimal amount = originalBooking.ChargeInfo.Amount - booking.ChargeInfo.Amount;
                    if (amount > 0)
                    {
                        //remove incompleted transations
                        booking.TransactionCreditCards.Where(item => item.OrderDateTime == null).ForEach(item => db.BookingTransactionCreditCards.DeleteObject(item));

                        var newTransactionCreditCards = new List<BookingTransactionCreditCard>();
                        foreach (var transaction in booking.TransactionCreditCards.Where(item => item.OrderDateTime != null).OrderByDescending(t => t.OrderDateTime))
                        {
                            if (transaction.OrderAmount - transaction.RefundedAmount <= 0)
                                continue;

                            //create refund transaction
                            var newTransaction = new BookingTransactionCreditCard();
                            newTransaction.BookingId = booking.Id;

                            newTransaction.TypeId = transaction.TypeId;
                            newTransaction.Holder = transaction.Holder;
                            newTransaction.ExpiryMonth = transaction.ExpiryMonth;
                            newTransaction.ExpiryYear = transaction.ExpiryYear;

                            newTransaction.PaymentApi = null;
                            newTransaction.OrderType = OrderType.Refund;
                            newTransaction.OriginalOrderNumber = transaction.OrderNumber;
                            newTransaction.OrderDateTime = null;
                            newTransaction.OrderAmount = amount > transaction.OrderAmount ? transaction.OrderAmount : amount;
                            db.BookingTransactionCreditCards.ApplyChanges(newTransaction);

                            transaction.RefundedAmount += newTransaction.OrderAmount;
                            db.BookingTransactionCreditCards.ApplyChanges(transaction);

                            db.SaveChanges();

                            if (transaction.Info == null)
                                transaction.Info = db.BookingTransactionCreditCardInfoes.Single(i => i.CreditCardId == transaction.Id);

                            var newTransactionCreditCardInfo = new BookingTransactionCreditCardInfo()
                            {
                                CreditCardId = newTransaction.Id,
                                Number = transaction.Info.Number,
                                CVC = transaction.Info.CVC

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

                            newTransaction.Number = AssociationUserCreditCard.ObfuscateCreditCardNumber(newTransactionCreditCardInfo.Number);
                            newTransactionCreditCards.Add(newTransaction);

                            amount -= newTransaction.OrderAmount;
                            if (amount == 0)
                                break;
                        }

                        return new GetAllowedPaymentMehodResponse() { PaymentMehod = PaymentMehod.SkipDueToRefundOrSamePrice, ChargeInfo = booking.ChargeInfo, NewTransactionCreditCards = newTransactionCreditCards };
                    }
                    else if (amount == 0)
                        return new GetAllowedPaymentMehodResponse() { PaymentMehod = PaymentMehod.SkipDueToRefundOrSamePrice, ChargeInfo = booking.ChargeInfo };
                    else
                        return new GetAllowedPaymentMehodResponse() { PaymentMehod = PaymentMehod.CreditCard, ChargeInfo = booking.ChargeInfo };
                }
            }
            catch (Exception ex)
            {
                var response = new GetAllowedPaymentMehodResponse();
                response.AddError("Response", ex.ToString());
                return response;
            }
        }