Exemplo n.º 1
0
        private Transaction ProcessPaymentTransaction(OnlineRegModel m, PaymentForm pf)
        {
            Transaction ti = null;
            if (m != null && m.Transaction != null)
                ti = PaymentForm.CreateTransaction(DbUtil.Db, m.Transaction, pf.AmtToPay);
            else
                ti = pf.CreateTransaction(DbUtil.Db);

            int? pid = null;
            if (m != null)
            {
                m.ParseSettings();
                var terms = Util.PickFirst(m.Terms, "");
                if (terms.HasValue())
                    ViewData["Terms"] = terms;
                pid = m.UserPeopleId;
                if (m.TranId == null)
                    m.TranId = ti.Id;
            }

            if (!pid.HasValue)
            {
                var pds = DbUtil.Db.FindPerson(pf.First, pf.Last, null, pf.Email, pf.Phone);
                if (pds.Count() == 1)
                    pid = pds.Single().PeopleId.Value;
            }
            TransactionResponse tinfo;
            var gateway = OnlineRegModel.GetTransactionGateway();
            if (gateway == "authorizenet")
                if (pf.SavePayInfo == true)
                {
                    var anet = new AuthorizeNet(DbUtil.Db, pf.testing);
                    tinfo = anet.createCustomerProfileTransactionRequest(
                        pid ?? 0,
                        pf.AmtToPay ?? 0,
                        pf.Description,
                        pf.TranId ?? 0);
                }
                else
                    if (pf.Type == "B")
                        tinfo = OnlineRegModel.PostECheck(
                            pf.Routing, pf.Account,
                            pf.AmtToPay ?? 0,
                            ti.Id, pf.Description,
                            pid ?? 0, pf.First, pf.Last,
                            pf.Address, pf.City, pf.State, pf.Zip,
                            pf.testing);
                    else
                        tinfo = OnlineRegModel.PostTransaction(
                            pf.CreditCard, pf.CCV,
                            Payments.NormalizeExpires(pf.Expires),
                            pf.AmtToPay ?? 0,
                            ti.Id, pf.Description,
                            pid ?? 0, pf.Email, pf.First, pf.Last,
                            pf.Address, pf.City, pf.State, pf.Zip,
                            pf.testing);
            else if (gateway == "sage")
                if (pf.SavePayInfo == true)
                {
                    var sage = new SagePayments(DbUtil.Db, pf.testing);
                    tinfo = sage.createVaultTransactionRequest(
                        pid ?? 0,
                        pf.AmtToPay ?? 0,
                        pf.Description,
                        ti.Id,
                        pf.Type);
                }
                else
                    if (pf.Type == "B")
                        tinfo = OnlineRegModel.PostVirtualCheckTransactionSage(
                            pf.Routing, pf.Account,
                            pf.AmtToPay ?? 0,
                            ti.Id, pf.Description,
                            pid ?? 0, pf.Email, pf.First, pf.MiddleInitial, pf.Last, pf.Suffix,
                            pf.Address, pf.City, pf.State, pf.Zip, pf.Phone,
                            pf.testing);
                    else
                        tinfo = OnlineRegModel.PostTransactionSage(
                            pf.CreditCard, pf.CCV,
                            Payments.NormalizeExpires(pf.Expires),
                            pf.AmtToPay ?? 0,
                            ti.Id, pf.Description,
                            pid ?? 0, pf.Email, pf.First, pf.MiddleInitial, pf.Last, pf.Suffix,
                            pf.Address, pf.City, pf.State, pf.Zip, pf.Phone,
                            pf.testing);

            else
                throw new Exception("unknown gateway " + gateway);

            ti.TransactionId = tinfo.TransactionId;
            if (ti.Testing == true && !ti.TransactionId.Contains("(testing)"))
                ti.TransactionId += "(testing)";
            ti.Approved = tinfo.Approved;
            if (ti.Approved == false)
            {
                ti.Amtdue += ti.Amt;
                if (m != null && m.OnlineGiving())
                    ti.Amtdue = 0;
            }
            ti.Message = tinfo.Message;
            ti.AuthCode = tinfo.AuthCode;
            ti.TransactionDate = DateTime.Now;
            DbUtil.Db.SubmitChanges();
            return ti;
        }
Exemplo n.º 2
0
Arquivo: Coupon.cs Projeto: vs06/bvcms
        public ActionResult ApplyCoupon(PaymentForm pf)
        {
            OnlineRegModel m = null;
            ExtraDatum ed = null;
            if (pf.PayBalance == false)
            {
                ed = DbUtil.Db.ExtraDatas.SingleOrDefault(e => e.Id == pf.DatumId);
                m = Util.DeSerialize<OnlineRegModel>(ed.Data);
                m.ParseSettings();
            }

            if (!pf.Coupon.HasValue())
                return Json(new { error = "empty coupon" });
            string coupon = pf.Coupon.ToUpper().Replace(" ", "");
            string admincoupon = DbUtil.Db.Setting("AdminCoupon", "ifj4ijweoij").ToUpper().Replace(" ", "");
            if (coupon == admincoupon)
                return Json(new { confirm = "/onlinereg/Confirm/{0}?TransactionID=Coupon(Admin)".Fmt(pf.DatumId) });

            var c = DbUtil.Db.Coupons.SingleOrDefault(cp => cp.Id == coupon);
            if (c == null)
                return Json(new { error = "coupon not found" });

            if (pf.OrgId.HasValue && c.Organization != null && c.Organization.OrgPickList.HasValue())
            {
                var a = c.Organization.OrgPickList.SplitStr(",").Select(ss => ss.ToInt()).ToArray();
                if(!a.Contains(pf.OrgId.Value))
                    return Json(new {error = "coupon and org do not match"});
            }
            else if (pf.OrgId != c.OrgId)
                return Json(new {error = "coupon and org do not match"});
            if (c.Used.HasValue && c.Id.Length == 12)
                return Json(new { error = "coupon already used" });
            if (c.Canceled.HasValue)
                return Json(new { error = "coupon canceled" });

            var ti = pf.CreateTransaction(DbUtil.Db, c.Amount);
            if (m != null) // Start this transaction in the chain
            {
                m.History.Add("ApplyCoupon");
                m.TranId = ti.OriginalId;
                ed.Data = Util.Serialize<OnlineRegModel>(m);
            }
            var tid = "Coupon({0:n2})".Fmt(Util.fmtcoupon(coupon));

            if(!pf.PayBalance)
                ConfirmDuePaidTransaction(ti, tid, sendmail: false);

            var msg = "<i class='red'>Your coupon for {0:n2} has been applied, your balance is now {1:n2}</i>."
                .Fmt(c.Amount, ti.Amtdue );
            if(ti.Amt < pf.AmtToPay)
                msg += "You still must complete this transaction with a payment";

            if (m != null)
                m.UseCoupon(ti.TransactionId, ti.Amt ?? 0);
            else
                c.UseCoupon(ti.FirstTransactionPeopleId(), ti.Amt ?? 0);
            DbUtil.Db.SubmitChanges();

            if (pf.PayBalance)
                return Json(new { confirm = "/onlinereg/ConfirmDuePaid/{0}?TransactionID=Coupon({1})&Amount={2}".Fmt(ti.Id, Util.fmtcoupon(coupon), ti.Amt) });
            pf.AmtToPay -= ti.Amt;
            if (pf.AmtToPay <= 0)
                return Json( new { confirm = "/OnlineReg/Confirm/{0}?TransactionId={1}".Fmt(pf.DatumId, "Coupon") });
            return Json(new { tiamt = pf.AmtToPay, amtdue=ti.Amtdue, amt=pf.AmtToPay.ToString2("N2"), msg });
        }