Exemplo n.º 1
0
        public IActionResult Create([FromBody] CreateDonationDTO createdDonation)
        {
            var donorId = _donationAppService.Create(createdDonation);

            return(new ObjectResult(donorId)
            {
                StatusCode = (int)HttpStatusCode.Created
            });
        }
Exemplo n.º 2
0
        private void LogDonationError(string methodName, Exception exception, CreateDonationDTO dto, MpContactDonor donor)
        {
            int    donorId     = donor?.DonorId ?? 0;
            string processorId = donor?.ProcessorId ?? "";

            _logger.Error($"{methodName} exception (DonorId = {donorId}, ProcessorId = {processorId})", exception);

            // include donation in error log (serialized json); ignore exceptions during serialization
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                Error = (serializer, err) => err.ErrorContext.Handled = true
            };
            string json = JsonConvert.SerializeObject(dto, settings);

            _logger.Error($"{methodName} data {json}");
        }
Exemplo n.º 3
0
        public int Create(CreateDonationDTO createDonationDTO)
        {
            DateTime createdAt      = DateTime.UtcNow;
            var      donor          = _donorRepository.GetById(int.Parse(createDonationDTO.DonorId));
            var      bloodcenter    = _bloodcenterRepository.GetById(int.Parse(createDonationDTO.BloodCenterId));
            var      parsedSchedule = DateTime.Parse(createDonationDTO.Schedule);

            Donation donation = new Donation(int.Parse(createDonationDTO.DonorId),
                                             int.Parse(createDonationDTO.BloodCenterId),
                                             createDonationDTO.Status,
                                             parsedSchedule,
                                             createdAt
                                             );

            return(_donationRepository.Create(donation));
        }
Exemplo n.º 4
0
        public void testPostToCreateDonationAndDistributionAuthenticatedPayment()
        {
            var contactId = 999999;
            var payment   = new MpPaymentDetailReturn();

            payment.PaymentId = 46546;

            var charge = new StripeCharge()
            {
                Id = "ch_crdscharge86868",
                BalanceTransaction = new StripeBalanceTransaction()
                {
                    Fee = 987
                }
            };

            var createDonationDTO = new CreateDonationDTO
            {
                Amount          = 86868,
                DonorId         = 394256,
                EmailAddress    = "*****@*****.**",
                PaymentType     = "bank",
                TransactionType = "PAYMENT",
                InvoiceId       = 88
            };

            var donor = new MpContactDonor
            {
                ContactId       = contactId,
                DonorId         = 424242,
                SetupDate       = new DateTime(),
                StatementFreq   = "1",
                StatementMethod = "2",
                StatementType   = "3",
                ProcessorId     = "cus_test1234567",
                Email           = "moc.tset@tset"
            };

            contactRepositoryMock.Setup(mocked => mocked.GetContactId(authType + " " + authToken)).Returns(contactId);

            donorServiceMock.Setup(mocked => mocked.GetContactDonor(contactId))
            .Returns(donor);

            stripeServiceMock.Setup(
                mocked => mocked.ChargeCustomer(donor.ProcessorId, createDonationDTO.Amount, donor.DonorId, true))
            .Returns(charge);

            invoiceServiceMock.Setup(mocked => mocked.InvoiceExists(It.IsAny <int>()))
            .Returns(true);

            paymentServiceMock.Setup(mocked => mocked.PostPayment(It.IsAny <MpDonationAndDistributionRecord>()))
            .Returns(payment);

            IHttpActionResult result = fixture.Post(createDonationDTO);

            authenticationServiceMock.VerifyAll();
            donorServiceMock.VerifyAll();
            stripeServiceMock.VerifyAll();
            donorServiceMock.VerifyAll();

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(OkNegotiatedContentResult <DonationDTO>), result);
            var okResult = (OkNegotiatedContentResult <DonationDTO>)result;

            Assert.AreEqual(46546, payment.PaymentId);
        }
Exemplo n.º 5
0
        public void testPostToCreateDonationAndDistributionUnauthenticated()
        {
            var contactId  = 999999;
            var donationId = 6186818;
            var charge     = new StripeCharge()
            {
                Id = "ch_crdscharge86868",
                BalanceTransaction = new StripeBalanceTransaction()
                {
                    Fee = 987
                }
            };

            var createDonationDTO = new CreateDonationDTO
            {
                ProgramId    = "3", //crossroads
                Amount       = 86868,
                DonorId      = 394256,
                EmailAddress = "*****@*****.**",
                PaymentType  = "bank"
            };

            var donor = new MpContactDonor
            {
                ContactId       = contactId,
                DonorId         = 424242,
                SetupDate       = new DateTime(),
                StatementFreq   = "1",
                StatementMethod = "2",
                StatementType   = "3",
                ProcessorId     = "cus_test1234567",
                Email           = "moc.tset@tset",
                Details         = new MpContactDetails
                {
                    FirstName = "Bart",
                    LastName  = "Simpson"
                }
            };

            fixture.Request.Headers.Authorization = null;
            gatewayDonorServiceMock.Setup(mocked => mocked.GetContactDonorForEmail(createDonationDTO.EmailAddress)).Returns(donor);

            stripeServiceMock.Setup(mocked => mocked.ChargeCustomer(donor.ProcessorId, createDonationDTO.Amount, donor.DonorId, false)).
            Returns(charge);

            donorServiceMock.Setup(mocked => mocked.
                                   CreateDonationAndDistributionRecord(It.Is <MpDonationAndDistributionRecord>(
                                                                           d => d.DonationAmt == createDonationDTO.Amount &&
                                                                           d.FeeAmt == charge.BalanceTransaction.Fee &&
                                                                           d.DonorId == donor.DonorId &&
                                                                           d.ProgramId.Equals(createDonationDTO.ProgramId) &&
                                                                           d.PledgeId == null &&
                                                                           d.ChargeId.Equals(charge.Id) &&
                                                                           d.PymtType.Equals(createDonationDTO.PaymentType) &&
                                                                           d.ProcessorId.Equals(donor.ProcessorId) &&
                                                                           !d.RegisteredDonor &&
                                                                           !d.Anonymous &&
                                                                           !d.RecurringGift &&
                                                                           d.RecurringGiftId == null &&
                                                                           d.DonorAcctId == null &&
                                                                           d.CheckScannerBatchName == null &&
                                                                           d.DonationStatus == null &&
                                                                           d.CheckNumber == null), true))
            .Returns(donationId);

            IHttpActionResult result = fixture.Post(createDonationDTO);

            donorServiceMock.VerifyAll();
            stripeServiceMock.VerifyAll();
            donorServiceMock.VerifyAll();

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(OkNegotiatedContentResult <DonationDTO>), result);
            var okResult = (OkNegotiatedContentResult <DonationDTO>)result;

            Assert.AreEqual(6186818, donationId);

            var resultDto = ((OkNegotiatedContentResult <DonationDTO>)result).Content;

            Assert.IsNotNull(resultDto);
            Assert.AreEqual(donor.Email, resultDto.Email);
        }
Exemplo n.º 6
0
        public void testPostToCreateDonationAndDistributionWithPledgeUnauthenticated()
        {
            var contactId  = 999999;
            var donationId = 6186818;
            var charge     = new StripeCharge()
            {
                Id = "ch_crdscharge86868",
                BalanceTransaction = new StripeBalanceTransaction()
                {
                    Fee = 987
                }
            };

            var createDonationDTO = new CreateDonationDTO
            {
                ProgramId        = "3", //crossroads
                Amount           = 86868,
                DonorId          = 394256,
                EmailAddress     = "*****@*****.**",
                PledgeCampaignId = 23,
                PledgeDonorId    = 42,
                GiftMessage      = "Don't look a Gift Horse in the Mouth!",
                PaymentType      = "card",
                SourceUrl        = "www.ninjas.com",
                PredefinedAmount = 86868
            };

            var donor = new MpContactDonor
            {
                ContactId       = contactId,
                DonorId         = 424242,
                SetupDate       = new DateTime(),
                StatementFreq   = "1",
                StatementMethod = "2",
                StatementType   = "3",
                ProcessorId     = "cus_test1234567",
                Email           = "moc.tset@tset",
                Details         = new MpContactDetails
                {
                    FirstName = "Bart",
                    LastName  = "Simpson"
                }
            };

            var pledgeId = 3456;
            var pledge   = new MpPledge
            {
                DonorId          = 1,
                PledgeCampaignId = 2,
                PledgeId         = pledgeId,
                PledgeStatusId   = 1
            };


            fixture.Request.Headers.Authorization = null;
            gatewayDonorServiceMock.Setup(mocked => mocked.GetContactDonorForEmail(createDonationDTO.EmailAddress)).Returns(donor);

            mpPledgeService.Setup(mocked => mocked.GetPledgeByCampaignAndDonor(createDonationDTO.PledgeCampaignId.Value, createDonationDTO.PledgeDonorId.Value)).Returns(pledge);

            // it doesn't seem right to have donationId passed into this, but it's in the function now
            mpDonationService.Setup(mocked => mocked.SendMessageFromDonor(pledgeId, donationId, createDonationDTO.GiftMessage, "Daddy Warbucks"));

            stripeServiceMock.Setup(mocked => mocked.ChargeCustomer(donor.ProcessorId, createDonationDTO.Amount, donor.DonorId, false)).
            Returns(charge);


            donorServiceMock.Setup(mocked => mocked.
                                   CreateDonationAndDistributionRecord(It.Is <MpDonationAndDistributionRecord>(
                                                                           d => d.DonationAmt == createDonationDTO.Amount &&
                                                                           d.FeeAmt == charge.BalanceTransaction.Fee &&
                                                                           d.DonorId == donor.DonorId &&
                                                                           d.ProgramId.Equals(createDonationDTO.ProgramId) &&
                                                                           d.PledgeId == pledgeId &&
                                                                           d.ChargeId.Equals(charge.Id) &&
                                                                           d.PymtType.Equals(createDonationDTO.PaymentType) &&
                                                                           d.ProcessorId.Equals(donor.ProcessorId) &&
                                                                           !d.RegisteredDonor &&
                                                                           !d.Anonymous &&
                                                                           !d.RecurringGift &&
                                                                           d.RecurringGiftId == null &&
                                                                           d.DonorAcctId == null &&
                                                                           d.CheckScannerBatchName == null &&
                                                                           d.DonationStatus == null &&
                                                                           d.CheckNumber == null &&
                                                                           d.PredefinedAmount == createDonationDTO.PredefinedAmount &&
                                                                           d.SourceUrl == createDonationDTO.SourceUrl), true))
            .Returns(donationId);


            IHttpActionResult result = fixture.Post(createDonationDTO);


            donorServiceMock.VerifyAll();
            stripeServiceMock.VerifyAll();
            donorServiceMock.VerifyAll();
            mpPledgeService.VerifyAll();

            donorServiceMock.VerifyAll();
            stripeServiceMock.VerifyAll();
            donorServiceMock.VerifyAll();

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(OkNegotiatedContentResult <DonationDTO>), result);
            var okResult = (OkNegotiatedContentResult <DonationDTO>)result;

            Assert.AreEqual(6186818, donationId);

            var resultDto = ((OkNegotiatedContentResult <DonationDTO>)result).Content;

            Assert.IsNotNull(resultDto);
            Assert.AreEqual(donor.Email, resultDto.Email);
        }
        private IHttpActionResult CreateDonationAndDistributionUnauthenticated(CreateDonationDTO dto)
        {
            bool isPayment = false;

            try
            {
                var donor    = _gatewayDonorService.GetContactDonorForEmail(dto.EmailAddress);
                var charge   = _stripeService.ChargeCustomer(donor.ProcessorId, dto.Amount, donor.DonorId, isPayment);
                var fee      = charge.BalanceTransaction != null ? charge.BalanceTransaction.Fee : null;
                int?pledgeId = null;
                if (dto.PledgeCampaignId != null && dto.PledgeDonorId != null)
                {
                    var pledge = _mpPledgeService.GetPledgeByCampaignAndDonor(dto.PledgeCampaignId.Value, dto.PledgeDonorId.Value);
                    if (pledge != null)
                    {
                        pledgeId = pledge.PledgeId;
                    }
                }

                var donationAndDistribution = new MpDonationAndDistributionRecord
                {
                    DonationAmt      = dto.Amount,
                    FeeAmt           = fee,
                    DonorId          = donor.DonorId,
                    ProgramId        = dto.ProgramId,
                    PledgeId         = pledgeId,
                    ChargeId         = charge.Id,
                    PymtType         = dto.PaymentType,
                    ProcessorId      = donor.ProcessorId,
                    SetupDate        = DateTime.Now,
                    RegisteredDonor  = false,
                    Anonymous        = dto.Anonymous,
                    PredefinedAmount = dto.PredefinedAmount,
                    SourceUrl        = dto.SourceUrl
                };

                var donationId = _mpDonorService.CreateDonationAndDistributionRecord(donationAndDistribution);
                if (!dto.GiftMessage.IsNullOrWhiteSpace() && pledgeId != null)
                {
                    SendMessageFromDonor(pledgeId.Value, donationId, dto.GiftMessage);
                }

                var response = new DonationDTO()
                {
                    ProgramId = dto.ProgramId,
                    Amount    = (int)dto.Amount,
                    Id        = donationId.ToString(),
                    Email     = donor.Email
                };

                return(Ok(response));
            }
            catch (PaymentProcessorException stripeException)
            {
                return(stripeException.GetStripeResult());
            }
            catch (Exception exception)
            {
                var apiError = new ApiErrorDto("Donation Post Failed", exception);
                throw new HttpResponseException(apiError.HttpResponseMessage);
            }
        }
        private IHttpActionResult CreateDonationAndDistributionAuthenticated(String token, CreateDonationDTO dto)
        {
            var isPayment = (dto.TransactionType != null && dto.TransactionType.Equals("PAYMENT"));

            try
            {
                if (isPayment)
                {
                    //check if invoice exists before create Stripe Charge
                    if (dto.InvoiceId != null && !_invoiceRepository.InvoiceExists(dto.InvoiceId.Value))
                    {
                        var apiError = new ApiErrorDto("Invoice Not Found", new InvoiceNotFoundException(dto.InvoiceId.Value));
                        throw new HttpResponseException(apiError.HttpResponseMessage);
                    }
                }

                var contactId = _authenticationService.GetContactId(token);
                var donor     = _mpDonorService.GetContactDonor(contactId);
                var charge    = _stripeService.ChargeCustomer(donor.ProcessorId, dto.Amount, donor.DonorId, isPayment);
                var fee       = charge.BalanceTransaction != null ? charge.BalanceTransaction.Fee : null;

                int?pledgeId = null;
                if (dto.PledgeCampaignId != null && dto.PledgeDonorId != null)
                {
                    var pledge = _mpPledgeService.GetPledgeByCampaignAndDonor(dto.PledgeCampaignId.Value, dto.PledgeDonorId.Value);
                    if (pledge != null)
                    {
                        pledgeId = pledge.PledgeId;
                    }
                }

                if (!isPayment)
                {
                    var donationAndDistribution = new MpDonationAndDistributionRecord
                    {
                        DonationAmt      = dto.Amount,
                        FeeAmt           = fee,
                        DonorId          = donor.DonorId,
                        ProgramId        = dto.ProgramId,
                        PledgeId         = pledgeId,
                        ChargeId         = charge.Id,
                        PymtType         = dto.PaymentType,
                        ProcessorId      = donor.ProcessorId,
                        SetupDate        = DateTime.Now,
                        RegisteredDonor  = true,
                        Anonymous        = dto.Anonymous,
                        SourceUrl        = dto.SourceUrl,
                        PredefinedAmount = dto.PredefinedAmount
                    };

                    var donationId = _mpDonorService.CreateDonationAndDistributionRecord(donationAndDistribution, !dto.TripDeposit);
                    if (!dto.GiftMessage.IsNullOrWhiteSpace() && pledgeId != null)
                    {
                        SendMessageFromDonor(pledgeId.Value, donationId, dto.GiftMessage);
                    }
                    var response = new DonationDTO
                    {
                        ProgramId = dto.ProgramId,
                        Amount    = (int)dto.Amount,
                        Id        = donationId.ToString(),
                        Email     = donor.Email
                    };

                    return(Ok(response));
                }
                else //Payment flow (non-contribution transaction)
                {
                    if (!ModelState.IsValid)
                    {
                        var errors    = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message);
                        var dataError = new ApiErrorDto("Payment data Invalid", new InvalidOperationException("Invalid Payment Data" + errors));
                        throw new HttpResponseException(dataError.HttpResponseMessage);
                    }

                    try
                    {
                        var invoiceId = dto.InvoiceId != null ? dto.InvoiceId.Value : 0;
                        var payment   = new MpDonationAndDistributionRecord
                        {
                            DonationAmt = dto.Amount,
                            PymtType    = dto.PaymentType,
                            ProcessorId = charge.Id,
                            ContactId   = contactId,
                            InvoiceId   = invoiceId,
                            FeeAmt      = fee
                        };
                        var paymentReturn = _paymentService.PostPayment(payment);
                        var response      = new DonationDTO
                        {
                            Amount    = (int)dto.Amount,
                            Email     = donor.Email,
                            PaymentId = paymentReturn.PaymentId
                        };
                        return(Ok(response));
                    }
                    catch (InvoiceNotFoundException e)
                    {
                        var apiError = new ApiErrorDto("Invoice Not Found", e);
                        throw new HttpResponseException(apiError.HttpResponseMessage);
                    }
                    catch (ContactNotFoundException e)
                    {
                        var apiError = new ApiErrorDto("Contact Not Found", e);
                        throw new HttpResponseException(apiError.HttpResponseMessage);
                    }
                    catch (PaymentTypeNotFoundException e)
                    {
                        var apiError = new ApiErrorDto("PaymentType Not Found", e);
                        throw new HttpResponseException(apiError.HttpResponseMessage);
                    }
                    catch (Exception e)
                    {
                        var apiError = new ApiErrorDto("SavePayment Failed", e);
                        throw new HttpResponseException(apiError.HttpResponseMessage);
                    }
                }
            }
            catch (PaymentProcessorException stripeException)
            {
                return(stripeException.GetStripeResult());
            }
            catch (Exception exception)
            {
                var apiError = new ApiErrorDto("Donation/Payment Post Failed", exception);
                throw new HttpResponseException(apiError.HttpResponseMessage);
            }
        }
 public IHttpActionResult Post([FromBody] CreateDonationDTO dto)
 {
     return(Authorized(token =>
                       CreateDonationAndDistributionAuthenticated(token, dto),
                       () => CreateDonationAndDistributionUnauthenticated(dto)));
 }
Exemplo n.º 10
0
        private IHttpActionResult CreateDonationAndDistributionUnauthenticated(CreateDonationDTO dto)
        {
            bool           isPayment = false;
            MpContactDonor donor     = null;

            try
            {
                donor = _gatewayDonorService.GetContactDonorForEmail(dto.EmailAddress);
                var charge   = _stripeService.ChargeCustomer(donor.ProcessorId, dto.Amount, donor.DonorId, isPayment);
                var fee      = charge.BalanceTransaction != null ? charge.BalanceTransaction.Fee : null;
                int?pledgeId = null;
                if (dto.PledgeCampaignId != null && dto.PledgeDonorId != null)
                {
                    var pledge = _mpPledgeService.GetPledgeByCampaignAndDonor(dto.PledgeCampaignId.Value, dto.PledgeDonorId.Value);
                    if (pledge != null)
                    {
                        pledgeId = pledge.PledgeId;
                    }
                }

                var donationAndDistribution = new MpDonationAndDistributionRecord
                {
                    DonationAmt      = dto.Amount,
                    FeeAmt           = fee,
                    DonorId          = donor.DonorId,
                    ProgramId        = dto.ProgramId,
                    PledgeId         = pledgeId,
                    ChargeId         = charge.Id,
                    PymtType         = dto.PaymentType,
                    ProcessorId      = donor.ProcessorId,
                    SetupDate        = DateTime.Now,
                    RegisteredDonor  = false,
                    Anonymous        = dto.Anonymous,
                    PredefinedAmount = dto.PredefinedAmount,
                    SourceUrl        = dto.SourceUrl
                };

                var from = dto.Anonymous ? "Anonymous" : donor.Details.FirstName + " " + donor.Details.LastName;

                var donationId = _mpDonorService.CreateDonationAndDistributionRecord(donationAndDistribution);
                if (!dto.GiftMessage.IsNullOrWhiteSpace() && pledgeId != null)
                {
                    SendMessageFromDonor(pledgeId.Value, donationId, dto.GiftMessage, from);
                }

                var response = new DonationDTO()
                {
                    ProgramId = dto.ProgramId,
                    Amount    = (int)dto.Amount,
                    Id        = donationId.ToString(),
                    Email     = donor.Email
                };

                _analyticsService.Track(donor.ContactId.ToString(), "PaymentSucceededServerSide", new EventProperties()
                {
                    { "Url", dto.SourceUrl }, { "FundingMethod", dto.PaymentType }, { "Email", donor.Email }, { "CheckoutType", "Guest" }, { "Amount", dto.Amount }
                });
                return(Ok(response));
            }
            catch (PaymentProcessorException stripeException)
            {
                LogDonationError("CreateDonationAndDistributionUnauthenticated", stripeException, dto, donor);
                return(stripeException.GetStripeResult());
            }
            catch (Exception exception)
            {
                LogDonationError("CreateDonationAndDistributionUnauthenticated", exception, dto, donor);
                var apiError = new ApiErrorDto("Donation Post Failed", exception);
                throw new HttpResponseException(apiError.HttpResponseMessage);
            }
        }