Example #1
0
        private IHttpActionResult CreateDonorForUnauthenticatedUser(CreateDonorDTO dto)
        {
            MpContactDonor donor;

            try
            {
                donor = _donorService.GetContactDonorForEmail(dto.email_address);
            }
            catch (Exception e)
            {
                var msg = "Error getting donor for email " + dto.email_address;
                logger.Error(msg, e);
                var apiError = new ApiErrorDto(msg, e);
                throw new HttpResponseException(apiError.HttpResponseMessage);
            }
            int existingDonorId =
                (donor == null) ?
                0 :
                donor.DonorId;

            try
            {
                donor = _donorService.CreateOrUpdateContactDonor(donor, String.Empty, dto.first_name, dto.last_name, dto.email_address, dto.stripe_token_id, DateTime.Now);
            }
            catch (PaymentProcessorException e)
            {
                return(e.GetStripeResult());
            }
            catch (Exception e)
            {
                var msg = "Error creating donor for email " + dto.email_address;
                logger.Error(msg, e);
                var apiError = new ApiErrorDto(msg, e);
                throw new HttpResponseException(apiError.HttpResponseMessage);
            }

            var responseBody = new DonorDTO
            {
                Id             = donor.DonorId,
                ProcessorId    = donor.ProcessorId,
                RegisteredUser = false,
                Email          = donor.Email
            };

            // HTTP StatusCode should be 201 (Created) if we created a donor, or 200 (Ok) if returning an existing donor
            var statusCode =
                (existingDonorId == donor.DonorId) ?
                HttpStatusCode.OK :
                HttpStatusCode.Created;

            return(ResponseMessage(Request.CreateResponse(statusCode, responseBody)));
        }
        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);
            }
        }
Example #3
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);
            }
        }