Exemple #1
0
        public void TestInvoicePaymentFailedNoCancel()
        {
            const string processorId     = "cus_123";
            const string id              = "9876";
            const string subscriptionId  = "sub_123";
            const string chargeId        = "ch_2468";
            const int    recurringGiftId = 123456;

            var e = new StripeEvent
            {
                LiveMode = true,
                Type     = "invoice.payment_failed",
                Created  = DateTime.Now.AddDays(-1),
                Data     = new StripeEventData
                {
                    Object = JObject.FromObject(new StripeInvoice()
                    {
                        Id           = id,
                        Customer     = processorId,
                        Charge       = chargeId,
                        Subscription = subscriptionId
                    })
                }
            };

            var gift = new MpCreateDonationDistDto
            {
                Frequency        = 1,
                RecurringGiftId  = recurringGiftId,
                StripeCustomerId = processorId
            };


            var charge = new StripeCharge
            {
                Id             = chargeId,
                FailureMessage = "Your card was declined.",
                FailureCode    = "card_declined"
            };

            _paymentProcessorService.Setup(m => m.GetCharge(It.IsAny <string>())).Returns(charge);
            _mpDonorService.Setup(mocked => mocked.ProcessRecurringGiftDecline(subscriptionId, "card_declined: Your card was declined."));
            _mpDonorService.Setup(mocked => mocked.GetRecurringGiftForSubscription(subscriptionId, "")).Returns(gift);

            Assert.IsNull(_fixture.ProcessStripeEvent(e));
            _fixture.ProcessStripeEvent(e);
            _mpDonorService.VerifyAll();
        }
Exemple #2
0
        public MpCreateDonationDistDto GetRecurringGiftForSubscription(string subscription, string optionalSourceId = "")
        {
            string searchStr;

            if (!string.IsNullOrEmpty(optionalSourceId))
            {
                searchStr = string.Format(",{0},,,,,,,,,,{1}", subscription, optionalSourceId);
            }
            else
            {
                searchStr = string.Format(",\"{0}\",", subscription);
            }
            MpCreateDonationDistDto createDonation = null;

            try
            {
                var records =
                    WithApiLogin(
                        apiToken => (_ministryPlatformService.GetPageViewRecords(_recurringGiftBySubscription, apiToken, searchStr)));
                if (records != null && records.Count > 0)
                {
                    var record = records.First();
                    createDonation = new MpCreateDonationDistDto
                    {
                        DonorId                 = record.ToInt("Donor_ID"),
                        Amount                  = record["Amount"] as decimal? ?? 0,
                        ProgramId               = record.ToString("Program_ID"),
                        CongregationId          = record.ToInt("Congregation_ID"),
                        PaymentType             = (int)AccountType.Checking == record.ToInt("Account_Type_ID") ? PaymentType.Bank.abbrv : PaymentType.CreditCard.abbrv,
                        RecurringGiftId         = record.ToNullableInt("Recurring_Gift_ID"),
                        DonorAccountId          = record.ToNullableInt("Donor_Account_ID"),
                        SubscriptionId          = record.ToString("Subscription_ID"),
                        Frequency               = record.ToInt("Frequency_ID"),
                        ConsecutiveFailureCount = record.ToInt("Consecutive_Failure_Count"),
                        StripeCustomerId        = record.ToString("Processor_ID"),
                        StripeAccountId         = record.ToString("Processor_Account_ID")
                    };
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(
                          string.Format("GetRecurringGift failed.  Subscription Id: {0}", subscription), ex);
            }

            return(createDonation);
        }
Exemple #3
0
        public MpCreateDonationDistDto GetRecurringGiftById(string authorizedUserToken, int recurringGiftId)
        {
            var searchStr = string.Format("\"{0}\",", recurringGiftId);
            MpCreateDonationDistDto createDonation = null;

            try
            {
                var records = _ministryPlatformService.GetPageViewRecords(_myHouseholdRecurringGiftsApiPageView, authorizedUserToken, searchStr);
                if (records != null && records.Any())
                {
                    var record = records.First();
                    createDonation = new MpCreateDonationDistDto
                    {
                        RecurringGiftId  = record.ToNullableInt("Recurring_Gift_ID"),
                        DonorId          = record.ToInt("Donor_ID"),
                        Frequency        = record.ToInt("Frequency_ID"),
                        DayOfWeek        = record.ToInt("Day_Of_Week_ID"),
                        DayOfMonth       = record.ToInt("Day_Of_Month"),
                        StartDate        = record.ToDate("Start_Date").ToUniversalTime().Date,
                        Amount           = (int)((record["Amount"] as decimal? ?? 0.00M) * Constants.StripeDecimalConversionValue),
                        ProgramId        = record.ToString("Program_ID"),
                        CongregationId   = record.ToInt("Congregation_ID"),
                        PaymentType      = (int)AccountType.Checking == record.ToInt("Account_Type_ID") ? PaymentType.Bank.abbrv : PaymentType.CreditCard.abbrv,
                        DonorAccountId   = record.ToNullableInt("Donor_Account_ID"),
                        SubscriptionId   = record.ToString("Subscription_ID"),
                        StripeCustomerId = record.ToString("Processor_ID"),
                        StripeAccountId  = record.ToString("Processor_Account_ID"),
                        Recurrence       = record.ToString("Recurrence"),
                        ProgramName      = record.ToString("Program_Name")
                    };
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(
                          string.Format("GetRecurringGift failed.  Recurring Gift Id: {0}", recurringGiftId), ex);
            }

            return(createDonation);
        }
Exemple #4
0
        public void TestInvoicePaymentFailedCancelPlanAndSubscription()
        {
            const string processorId             = "cus_123";
            const string donorAccountProcessorId = "cus_456";
            const string subscriptionId          = "sub_123";
            const int    failCount       = 3;
            const int    recurringGiftId = 123456;
            const int    donorId         = 3421;
            const int    frequency       = 2;
            const string id       = "9876";
            const string chargeId = "ch_2468";
            const string planId   = "Donor ID #3421 weekly";

            var e = new StripeEvent
            {
                LiveMode = true,
                Type     = "invoice.payment_failed",
                Created  = DateTime.Now.AddDays(-1),
                Data     = new StripeEventData
                {
                    Object = JObject.FromObject(new StripeInvoice()
                    {
                        Id           = id,
                        Customer     = processorId,
                        Charge       = chargeId,
                        Subscription = subscriptionId
                    })
                }
            };

            var gift = new MpCreateDonationDistDto
            {
                Frequency               = frequency,
                RecurringGiftId         = recurringGiftId,
                SubscriptionId          = subscriptionId,
                ConsecutiveFailureCount = failCount,
                DonorId          = donorId,
                StripeCustomerId = donorAccountProcessorId
            };

            var plan = new StripePlan
            {
                Id = planId
            };

            var subscription = new StripeSubscription
            {
                Plan = plan
            };

            var charge = new StripeCharge
            {
                Id             = chargeId,
                FailureMessage = "Your card was declined.",
                FailureCode    = "card_declined"
            };

            _mpDonorService.Setup(mocked => mocked.ProcessRecurringGiftDecline(subscriptionId, "card_declined: Your card was declined."));
            _paymentProcessorService.Setup(m => m.GetCharge(It.IsAny <string>())).Returns(charge);
            _mpDonorService.Setup(mocked => mocked.GetRecurringGiftForSubscription(subscriptionId, "")).Returns(gift);
            _paymentProcessorService.Setup(mocked => mocked.CancelSubscription(donorAccountProcessorId, subscriptionId)).Returns(subscription);
            _paymentProcessorService.Setup(mocked => mocked.CancelPlan(subscription.Plan.Id)).Returns(plan);
            _mpDonorService.Setup(mocked => mocked.CancelRecurringGift(recurringGiftId));

            Assert.IsNull(_fixture.ProcessStripeEvent(e));
            _fixture.ProcessStripeEvent(e);
            _mpDonorService.VerifyAll();
            _paymentProcessorService.VerifyAll();
        }
        private void SendRecurringGiftConfirmationEmail(string authorizedUserToken, int templateId, MpCreateDonationDistDto recurringGift, int?recurringGiftId = null)
        {
            try
            {
                if (recurringGift == null)
                {
                    recurringGift = _mpDonorService.GetRecurringGiftById(authorizedUserToken, recurringGiftId.GetValueOrDefault());
                }

                var acctType    = _mpDonorService.GetDonorAccountPymtType(recurringGift.DonorAccountId.GetValueOrDefault());
                var acct        = acctType != 3 ? 5 : acctType;
                var paymentType = MinistryPlatform.Translation.Enum.PaymentType.GetPaymentType(acct).name;
                var frequency   = recurringGift.Recurrence;
                var programName = recurringGift.ProgramName;
                var amt         = decimal.Round(recurringGift.Amount, 2, MidpointRounding.AwayFromZero) / Constants.StripeDecimalConversionValue;

                _mpDonorService.SendEmail(templateId, recurringGift.DonorId, (int)amt, paymentType, DateTime.Now, programName, string.Empty, frequency);
            }
            catch (Exception e)
            {
                _logger.Warn(string.Format("Could not send email for recurring gift {0}", recurringGift == null ? recurringGiftId : recurringGift.RecurringGiftId), e);
            }
        }