예제 #1
0
        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 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;
            }
        }