public void SetBooking(Booking booking, SubmitBookingResponse submitBookingResponse) { if (booking == null) return; _booking = booking; Email3 = Email4 = null; CreatorEmails = new ObservableCollection<string>(submitBookingResponse.CreatorEmails); if (!string.IsNullOrWhiteSpace(_booking.Creator.Email) && !CreatorEmails.Contains(_booking.Creator.Email)) CreatorEmails.Insert(0, _booking.Creator.Email); OnPropertyChanged(() => CreatorEmails); }
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; } }