Exemple #1
0
 public async Task <int> Add(CreditCardCharge newItem)
 {
     return
         (await connection.ExecuteAsync(
              "INSERT INTO [CreditCardCharge] ([Id], [UserId], [Amount]) VALUES (@Id, @UserId, @Amount)",
              newItem));
 }
        public JsonResult Create(Payment payment, string type, CreditCardCharge cardCharge, TPCheck tPCheck)
        {
            payment.PaymentDate = DateTime.Now;
            payment.PaymentType = (PaymentType)Enum.Parse(typeof(PaymentType), type);
            db.Payments.Add(payment);
            db.SaveChanges();

            if (payment.PaymentType == PaymentType.CreditCard)
            {
                cardCharge.PaymentId = payment.Id;
                db.CreditCardCharges.Add(cardCharge);
                db.SaveChanges();
            }
            else if (payment.PaymentType == PaymentType.Check)
            {
                tPCheck.PaymentId = payment.Id;
                db.TPChecks.Add(tPCheck);
                db.SaveChanges();
            }
            var clientId = payment.ClientId;
            var amount   = payment.Amount;

            var clientBalance = db.ClientBalances.Find(clientId);

            clientBalance.Subtract(amount);

            Invoice.SubtractIB(amount, clientId);

            ClientWeek.SubtractBD(amount, clientId);

            db.SaveChanges();
            return(Json(new { Amount = payment.Amount }));
        }
        //private methods
        private TransactionResponse CreditCardCharge(int peopleId, decimal amt, string cardnumber, string expires, string description, int tranid, string cardcode, string email, string first, string last, string addr, string addr2, string city, string state, string country, string zip, string phone)
        {
            var cardCharge = new CreditCardCharge(
                _isTesting,
                _apiKey,
                _merch_cc_id,
                new CreditCard
            {
                CardNum        = cardnumber,
                CardExpiration = expires,
                CardCvv        = cardcode
            },
                new Payer
            {
                LastName  = last,
                FirstName = first,
                Address   = addr,
                Address2  = addr2,
                City      = city,
                State     = UPSStateCodes.FromStateCountry(state, country, db) ?? state,
                Country   = ISO3166.Alpha3FromName(country) ?? country,
                Zip       = zip,
                Email     = email,
                Phone     = phone
            },
                amt,
                tranid.ToString(CultureInfo.InvariantCulture),
                description);

            var response = cardCharge.Execute();

            return(new TransactionResponse
            {
                Approved = response.Response.Status == "success" ? true : false,
                AuthCode = response.Response.ProcessorResponseCode,
                Message = $"{response.Response.TransStatusMsg}#{response.Response.Items.First().IdString}",
                TransactionId = response.Response.TransIdStr
            });
        }
        public JsonResult Edit(Payment payment, string type, CreditCardCharge cardCharge, TPCheck tPCheck)
        {
            var clientId   = payment.ClientId;
            var oldPayment = db.Payments.Find(payment.Id);

            if (payment.Amount > oldPayment.Amount)
            {
                db.ClientBalances.Find(clientId).Subtract(payment.Amount - oldPayment.Amount);
                ClientWeek.SubtractBD(payment.Amount - oldPayment.Amount, clientId);
            }
            else if (payment.Amount < oldPayment.Amount)
            {
                db.ClientBalances.Find(clientId).Add(oldPayment.Amount - payment.Amount);
                ClientWeek.AddBD(oldPayment.Amount - payment.Amount, clientId);
            }
            payment.PaymentType = (PaymentType)Enum.Parse(typeof(PaymentType), type);
            db.Set <Payment>().AddOrUpdate(payment);
            db.SaveChanges();

            if (payment.PaymentType == PaymentType.CreditCard)
            {
                cardCharge.PaymentId = payment.Id;
                db.Set <CreditCardCharge>().AddOrUpdate(cardCharge);
                db.SaveChanges();
                var charge = db.CreditCardCharges.Include(c => c.Card).SingleOrDefault(c => c.PaymentId == payment.Id);
                var card   = db.CreditCards.Find(charge.CardId);
                return(Json(new { Id = payment.Id, PaymentDate = payment.PaymentDate, Amount = payment.Amount, PaymentType = payment.PaymentType, Name = card.Name, CardEnding = card.CardNumber.Substring(card.CardNumber.Length - 4), Location = charge.Location }));
            }
            else if (payment.PaymentType == PaymentType.Check)
            {
                tPCheck.PaymentId = payment.Id;
                db.Set <TPCheck>().AddOrUpdate(tPCheck);
                db.SaveChanges();
                var check = db.TPChecks.Find(payment.Id);
                return(Json(new { Id = payment.Id, PaymentDate = payment.PaymentDate, Amount = payment.Amount, PaymentType = payment.PaymentType, Memo = check.Memo }));
            }
            return(Json(payment));
        }
        protected override void Importing(OrderPart part, ImportContentContext context)
        {
            var xel = context.Data.Element(typeof(OrderPart).Name);

            if (xel == null)
            {
                return;
            }

            var itemsEl = xel.Element(ItemsName);

            var taxEl = xel.Element(TaxesName);
            var tax   = new TaxAmount();

            if (taxEl != null)
            {
                taxEl.With(tax)
                .FromAttr(t => t.Name)
                .FromAttr(t => t.Amount);
            }

            var shippingEl = xel.Element(ShippingName);
            var shipping   = new ShippingOption();

            if (shippingEl != null)
            {
                shippingEl.With(shipping)
                .FromAttr(s => s.Description)
                .FromAttr(s => s.ShippingCompany)
                .FromAttr(s => s.Price);
            }

            var cardEl = xel.Element(CardName);
            var card   = new CreditCardCharge();

            if (cardEl != null)
            {
                cardEl.With(card)
                .FromAttr(c => c.TransactionId)
                .FromAttr(c => c.Last4)
                .FromAttr(c => c.ExpirationMonth)
                .FromAttr(c => c.ExpirationYear);
            }

            var chargeEl = xel.Element(ChargeName);
            var charge   = new Charge();

            if (chargeEl != null)
            {
                chargeEl.With(charge)
                .FromAttr(c => c.TransactionId)
                .FromAttr(c => c.ChargeText);
            }

            var el = xel.With(part);

            part.Build(
                (ICharge)charge ?? (ICharge)card,
                itemsEl == null
                    ? null
                    : itemsEl.Elements(ItemName)
                .Select(i => {
                var checkoutItem = i.With(new CheckoutItem())
                                   .FromAttr(coi => coi.ProductId)
                                   .FromAttr(coi => coi.Title)
                                   .FromAttr(coi => coi.Quantity)
                                   .FromAttr(coi => coi.Price)
                                   .FromAttr(coi => coi.LinePriceAdjustment)
                                   .FromAttr(coi => coi.PromotionId)
                                   .Context;
                if (i.Element(AttributesName) != null)
                {
                    checkoutItem.Attributes =
                        i.Elements(AttributesName).Elements(AttributeName).Select(a =>
                                                                                  new {
                        Key   = Convert.ToInt32(a.Attr("Key")),
                        Value = new ProductAttributeValueExtended {
                            Value             = a.Attr("Value"),
                            ExtendedValue     = a.Attr("Extra"),
                            ExtensionProvider = a.Attr("ExtensionProvider")
                        }
                    }).ToDictionary(k => k.Key, k => k.Value);
                }
                return(checkoutItem);
            }),
                el.Attr(p => p.SubTotal),
                el.Attr(p => p.Total),
                tax,
                shipping,
                Address.Get(xel.Element(ShippingAddressName)),
                Address.Get(xel.Element(BillingAddressName)),
                el.Attr(p => p.CustomerEmail),
                el.Attr(p => p.CustomerPhone),
                el.Attr(p => p.SpecialInstructions),
                el.Attr(p => p.CurrencyCode),
                el.Attr(p => p.AmountPaid));
            el.With(part)
            .FromAttr(p => p.IsTestOrder)
            .FromAttr(p => p.Password)
            .FromAttr(p => p.Status)
            .FromAttr(p => p.TrackingUrl)
            .FromAttr(p => p.PurchaseOrder);

            var activityEl = xel.Element(ActivityName);

            if (activityEl != null)
            {
                part.Activity = activityEl.Elements(EventName)
                                .Select(e => e.With(new OrderEvent())
                                        .FromAttr(ev => ev.Date)
                                        .FromAttr(ev => ev.Category)
                                        .FromAttr(ev => ev.Description)
                                        .Context);
            }

            var userNameEl = xel.Element(UserName);

            if (userNameEl != null)
            {
                var userName = userNameEl.Attr("UserName");
                if (!String.IsNullOrEmpty(userName))
                {
                    part.User = _membershipService.GetUser(userName);
                }
            }
        }