PostUnattendedContribution() public method

public PostUnattendedContribution ( CMSDataContext db, decimal amt, int fund, string description, bool pledge = false, int typecode = null, int tranid = null ) : Contribution
db CMSDataContext
amt decimal
fund int
description string
pledge bool
typecode int
tranid int
return Contribution
Example #1
0
        public int DoGiving(CMSDataContext Db)
        {
            var          gateway = Db.Setting("TransactionGateway", "");
            AuthorizeNet anet    = null;
            SagePayments sage    = null;

            if (gateway == "AuthorizeNet")
            {
                anet = new AuthorizeNet(Db, testing: false);
            }
            else if (gateway == "Sage")
            {
                sage = new SagePayments(Db, testing: false);
            }
            else
            {
                return(0);
            }

            TransactionResponse ret = null;
            var total = (from a in Db.RecurringAmounts
                         where a.PeopleId == PeopleId
                         where a.ContributionFund.FundStatusId == 1
                         select a.Amt).Sum();

            if (!total.HasValue || total == 0)
            {
                return(0);
            }

            var preferredtype = (from pi in Db.PaymentInfos
                                 where pi.PeopleId == PeopleId
                                 select pi.PreferredGivingType).Single();

            var t = new Transaction
            {
                TransactionDate    = DateTime.Now,
                TransactionId      = "started",
                First              = Person.FirstName,
                MiddleInitial      = Person.MiddleName.Truncate(1) ?? "",
                Last               = Person.LastName,
                Suffix             = Person.SuffixCode,
                Amt                = total,
                Description        = "Recurring Giving",
                Testing            = false,
                TransactionGateway = gateway,
                Financeonly        = true
            };

            Db.Transactions.InsertOnSubmit(t);
            Db.SubmitChanges();

            if (gateway == "AuthorizeNet")
            {
                ret = anet.createCustomerProfileTransactionRequest(PeopleId, total ?? 0, "Recurring Giving", t.Id);
            }
            else
            {
                ret = sage.createVaultTransactionRequest(PeopleId, total ?? 0, "Recurring Giving", t.Id, preferredtype);
            }
            t.TransactionPeople.Add(new TransactionPerson {
                PeopleId = PeopleId, Amt = total
            });

            t.Message       = ret.Message;
            t.AuthCode      = ret.AuthCode;
            t.Approved      = ret.Approved;
            t.TransactionId = ret.TransactionId;
            var systemEmail = Db.Setting("SystemEmailAddress", "*****@*****.**");

            var contributionemail = (from ex in Person.PeopleExtras
                                     where ex.Field == "ContributionEmail"
                                     select ex.Data).SingleOrDefault();

            if (contributionemail.HasValue())
            {
                contributionemail = contributionemail.Trim();
            }
            if (!Util.ValidEmail(contributionemail))
            {
                contributionemail = Person.FromEmail;
            }
            var gift   = Db.Setting("NameForPayment", "gift");
            var church = Db.Setting("NameOfChurch", Db.CmsHost);

            if (ret.Approved)
            {
                var q = from a in Db.RecurringAmounts
                        where a.PeopleId == PeopleId
                        select a;

                foreach (var a in q)
                {
                    if (a.ContributionFund.FundStatusId == 1 && a.Amt > 0)
                    {
                        Person.PostUnattendedContribution(Db,
                                                          a.Amt ?? 0,
                                                          a.FundId,
                                                          "Recurring Giving", pledge: false);
                    }
                }
                var tot = q.Where(aa => aa.ContributionFund.FundStatusId == 1).Sum(aa => aa.Amt);
                NextDate = FindNextDate(DateTime.Today.AddDays(1));
                Db.SubmitChanges();
                if (tot > 0)
                {
                    Util.SendMsg(systemEmail, Db.CmsHost, Util.TryGetMailAddress(contributionemail),
                                 "Recurring {0} for {1}".Fmt(gift, church),
                                 "Your payment of ${0:N2} was processed this morning.".Fmt(tot),
                                 Util.ToMailAddressList(contributionemail), 0, null);
                }
            }
            else
            {
                Db.SubmitChanges();
                var failedGivingMessage = Db.ContentHtml("FailedGivingMessage", Resources.ManagedGiving_FailedGivingMessage);
                var adminEmail          = Db.Setting("AdminMail", systemEmail);
                Util.SendMsg(systemEmail, Db.CmsHost, Util.TryGetMailAddress(contributionemail),
                             "Recurring {0} failed for {1}".Fmt(gift, church),
                             failedGivingMessage.Replace("{first}", Person.PreferredName),
                             Util.ToMailAddressList(contributionemail), 0, null);
                foreach (var p in Db.FinancePeople())
                {
                    Util.SendMsg(systemEmail, Db.CmsHost, Util.TryGetMailAddress(adminEmail),
                                 "Recurring Giving Failed on " + Db.CmsHost,
                                 "<a href='{0}Manage/Transactions/Index/{2}'>message: {1}, tranid:{2}</a>".Fmt(Db.CmsHost, ret.Message, t.Id),
                                 Util.ToMailAddressList(p.EmailAddress), 0, null);
                }
            }
            return(1);
        }
Example #2
0
        public int DoGiving(CMSDataContext db)
        {
            var total = (from a in db.RecurringAmounts
                         where a.PeopleId == PeopleId
                         where a.ContributionFund.FundStatusId == 1
                         where a.ContributionFund.OnlineSort != null
                         select a.Amt).Sum();

            if (!total.HasValue || total == 0)
            {
                return(0);
            }

            var paymentInfo   = db.PaymentInfos.Single(x => x.PeopleId == PeopleId);
            var preferredType = paymentInfo.PreferredGivingType;

            var gw = GetGateway(db, paymentInfo);

            var t = new Transaction
            {
                TransactionDate    = DateTime.Now,
                TransactionId      = "started",
                First              = Person.FirstName,
                MiddleInitial      = Person.MiddleName.Truncate(1) ?? "",
                Last               = Person.LastName,
                Suffix             = Person.SuffixCode,
                Amt                = total,
                Description        = "Recurring Giving",
                Testing            = false,
                TransactionGateway = gw.GatewayType,
                Financeonly        = true,
                PaymentType        = preferredType,
                LastFourCC         = preferredType == PaymentType.CreditCard ? paymentInfo.MaskedCard.Last(4) : null,
                LastFourACH        = preferredType == PaymentType.Ach ? paymentInfo.MaskedAccount.Last(4) : null
            };

            db.Transactions.InsertOnSubmit(t);
            db.SubmitChanges();

            var ret = gw.PayWithVault(PeopleId, total ?? 0, "Recurring Giving", t.Id, preferredType);

            t.Message       = ret.Message;
            t.AuthCode      = ret.AuthCode;
            t.Approved      = ret.Approved;
            t.TransactionId = ret.TransactionId;
            var systemEmail = db.Setting("SystemEmailAddress", "*****@*****.**");

            var contributionemail = (from ex in Person.PeopleExtras
                                     where ex.Field == "ContributionEmail"
                                     select ex.Data).SingleOrDefault();

            if (contributionemail.HasValue())
            {
                contributionemail = contributionemail.Trim();
            }
            if (!Util.ValidEmail(contributionemail))
            {
                contributionemail = Person.FromEmail;
            }
            var gift   = db.Setting("NameForPayment", "gift");
            var church = db.Setting("NameOfChurch", db.CmsHost);
            var q      = from a in db.RecurringAmounts
                         where a.PeopleId == PeopleId
                         select a;
            var tot = q.Where(aa => aa.ContributionFund.FundStatusId == 1).Sum(aa => aa.Amt);

            t.TransactionPeople.Add(new TransactionPerson
            {
                PeopleId = Person.PeopleId,
                Amt      = tot,
            });
            if (ret.Approved)
            {
                foreach (var a in q)
                {
                    if (a.ContributionFund.FundStatusId == 1 && a.ContributionFund.OnlineSort != null && a.Amt > 0)
                    {
                        Person.PostUnattendedContribution(db, a.Amt ?? 0, a.FundId, "Recurring Giving", tranid: t.Id);
                    }
                }

                NextDate = FindNextDate(Util.Now.Date.AddDays(1));
                db.SubmitChanges();
                if (tot > 0)
                {
                    var msg = db.Content("RecurringGiftNotice") ?? new Content
                    {
                        Title = $"Recurring {gift} for {{church}}",
                        Body  = "Your payment of {total} was processed this morning."
                    };
                    var subject = msg.Title.Replace("{church}", church);
                    var body    = msg.Body.Replace("{total}", $"${tot:N2}");
                    var from    = Util.TryGetMailAddress(contributionemail);
                    var m       = new EmailReplacements(db, body, from);
                    body = m.DoReplacements(db, Person);
                    Util.SendMsg(systemEmail, db.CmsHost, from, subject, body,
                                 Util.ToMailAddressList(contributionemail), 0, Person.PeopleId);
                }
            }
            else
            {
                db.SubmitChanges();
                var msg = db.Content("RecurringGiftFailedNotice") ?? new Content
                {
                    Title = $"Recurring {gift} for {{church}} did not succeed",
                    Body  = @"Your payment of {total} failed to process this morning.<br>
The message was '{message}'.
Please contact the Finance office at the church."
                };
                var subject = msg.Title.Replace("{church}", church);
                var body    = msg.Body.Replace("{total}", $"${tot:N2}")
                              .Replace("{message}", ret.Message);
                var from = Util.TryGetMailAddress(contributionemail);
                var m    = new EmailReplacements(db, body, from);
                body = m.DoReplacements(db, Person);

                var adminEmail = db.Setting("AdminMail", systemEmail);
                Util.SendMsg(systemEmail, db.CmsHost, from, subject, body,
                             Util.ToMailAddressList(contributionemail), 0, Person.PeopleId);
                foreach (var p in db.RecurringGivingNotifyPersons())
                {
                    Util.SendMsg(systemEmail, db.CmsHost, Util.TryGetMailAddress(adminEmail),
                                 "Recurring Giving Failed on " + db.CmsHost,
                                 $"<a href='{db.CmsHost}/Transactions/{t.Id}'>message: {ret.Message}, tranid:{t.Id}</a>",
                                 Util.ToMailAddressList(p.EmailAddress), 0, Person.PeopleId);
                }
            }
            return(1);
        }