Exemple #1
0
        public void TestProcessEventSynchronously()
        {
            var e = new StripeEvent
            {
                LiveMode = true,
                Type     = "charge.succeeded",
                Created  = DateTime.Now.AddDays(-1),
                Data     = new StripeEventData
                {
                    Object = JObject.FromObject(new StripeCharge
                    {
                        Id = "9876"
                    })
                }
            };

            var expectedResponse = new StripeEventResponseDTO();

            _stripeEventService.Setup(mocked => mocked.ProcessStripeEvent(e)).Returns(expectedResponse);
            var result = _fixture.ProcessStripeEvent(e);

            Assert.IsInstanceOf <RestHttpActionResult <StripeEventResponseDTO> >(result);
            Assert.AreEqual(HttpStatusCode.OK, ((RestHttpActionResult <StripeEventResponseDTO>)result).StatusCode);
            var dto = ((RestHttpActionResult <StripeEventResponseDTO>)result).Content;

            Assert.IsNotNull(dto);
            Assert.AreSame(expectedResponse, dto);
            _stripeEventService.VerifyAll();
            _messageQueueFactory.VerifyAll();
            _messageFactory.VerifyAll();
        }
        public IHttpActionResult ProcessStripeEvent([FromBody] StripeEvent stripeEvent)
        {
            if (stripeEvent == null || !ModelState.IsValid)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Received invalid Stripe event " + stripeEvent);
                }
                return(BadRequest(ModelState));
            }

            _logger.Debug("Received Stripe Event " + stripeEvent.Type);
            if (_liveMode != stripeEvent.LiveMode)
            {
                _logger.Debug("Dropping Stripe Event " + stripeEvent.Type + " because LiveMode was " + stripeEvent.LiveMode);
                return(Ok());
            }

            StripeEventResponseDTO response = null;

            try
            {
                if (_asynchronous)
                {
                    _logger.Debug("Enqueueing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was true");
                    var message = _messageFactory.CreateMessage(stripeEvent);
                    _eventQueue.Send(message, MessageQueueTransactionType.None);
                    response = new StripeEventResponseDTO
                    {
                        Message = "Queued event for asynchronous processing"
                    };
                }
                else
                {
                    _logger.Debug("Processing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was false");
                    response = _stripeEventService.ProcessStripeEvent(stripeEvent);
                }
            }
            catch (Exception e)
            {
                var msg = "Unexpected error processing Stripe Event " + stripeEvent.Type;
                _logger.Error(msg, e);
                var responseDto = new StripeEventResponseDTO()
                {
                    Exception = new ApplicationException(msg, e),
                    Message   = msg
                };
                return(RestHttpActionResult <StripeEventResponseDTO> .ServerError(responseDto));
            }

            return(response == null ? Ok() : (IHttpActionResult)RestHttpActionResult <StripeEventResponseDTO> .Ok(response));
        }
        public IHttpActionResult ProcessStripeEvent([FromBody]StripeEvent stripeEvent)
        {
            if (stripeEvent == null || !ModelState.IsValid)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Received invalid Stripe event " + stripeEvent);
                }
                return (BadRequest(ModelState));
            }

            _logger.Debug("Received Stripe Event " + stripeEvent.Type);
            if (_liveMode != stripeEvent.LiveMode)
            {
                _logger.Debug("Dropping Stripe Event " + stripeEvent.Type + " because LiveMode was " + stripeEvent.LiveMode);
                return (Ok());
            }

            StripeEventResponseDTO response = null;
            try
            {
                if (_asynchronous)
                {
                    _logger.Debug("Enqueueing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was true");
                    var message = _messageFactory.CreateMessage(stripeEvent);
                    _eventQueue.Send(message, MessageQueueTransactionType.None);
                    response = new StripeEventResponseDTO
                    {
                        Message = "Queued event for asynchronous processing"
                    };
                }
                else
                {
                    _logger.Debug("Processing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was false");
                    response = _stripeEventService.ProcessStripeEvent(stripeEvent);
                }

            }
            catch (Exception e)
            {
                var msg = "Unexpected error processing Stripe Event " + stripeEvent.Type;
                _logger.Error(msg, e);
                var responseDto = new StripeEventResponseDTO()
                {
                    Exception = new ApplicationException(msg, e),
                    Message = msg
                };
                return (RestHttpActionResult<StripeEventResponseDTO>.ServerError(responseDto));
            }

            return (response == null ? Ok() : (IHttpActionResult)RestHttpActionResult<StripeEventResponseDTO>.Ok(response));
        }
        public void TestProcessEventSynchronously()
        {
            var e = new StripeEvent
            {
                LiveMode = true,
                Type = "charge.succeeded",
                Created = DateTime.Now.AddDays(-1),
                Data = new StripeEventData
                {
                    Object = JObject.FromObject(new StripeCharge
                    {
                        Id = "9876"
                    })
                }
            };

            var expectedResponse = new StripeEventResponseDTO();
            _stripeEventService.Setup(mocked => mocked.ProcessStripeEvent(e)).Returns(expectedResponse);
            var result = _fixture.ProcessStripeEvent(e);
            Assert.IsInstanceOf<RestHttpActionResult<StripeEventResponseDTO>>(result);
            Assert.AreEqual(HttpStatusCode.OK, ((RestHttpActionResult<StripeEventResponseDTO>)result).StatusCode);
            var dto = ((RestHttpActionResult<StripeEventResponseDTO>) result).Content;
            Assert.IsNotNull(dto);
            Assert.AreSame(expectedResponse, dto);
            _stripeEventService.VerifyAll();
            _messageQueueFactory.VerifyAll();
            _messageFactory.VerifyAll();

        }
        public void TestCreatePaymentProcessorEventError()
        {
            var stripeEvent = new StripeEvent
            {
                Created = DateTime.Now,
                Data = new StripeEventData
                {
                    Object = new StripeTransfer
                    {
                        Amount = 1000
                    }
                },
                LiveMode = true,
                Id = "123",
                Type = "transfer.paid"
            };

            var stripeEventResponse = new StripeEventResponseDTO
            {
                Exception = new ApplicationException()
            };

            var eventString = JsonConvert.SerializeObject(stripeEvent, Formatting.Indented);
            var responseString = JsonConvert.SerializeObject(stripeEventResponse, Formatting.Indented);

            _mpDonationService.Setup(
                mocked =>
                    mocked.CreatePaymentProcessorEventError(stripeEvent.Created, stripeEvent.Id, stripeEvent.Type,
                        eventString, responseString));

            _fixture.CreatePaymentProcessorEventError(stripeEvent, stripeEventResponse);
            _mpDonationService.VerifyAll();
        }
 public void CreatePaymentProcessorEventError(StripeEvent stripeEvent, StripeEventResponseDTO stripeEventResponse)
 {
     _mpDonationService.CreatePaymentProcessorEventError(stripeEvent.Created, stripeEvent.Id, stripeEvent.Type, JsonConvert.SerializeObject(stripeEvent, Formatting.Indented), JsonConvert.SerializeObject(stripeEventResponse, Formatting.Indented));
 }
Exemple #7
0
        public IHttpActionResult ProcessStripeEvent([FromBody] StripeEvent stripeEvent)
        {
            if (stripeEvent == null || !ModelState.IsValid)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Received invalid Stripe event " + stripeEvent);
                }
                return(BadRequest(ModelState));
            }

            _logger.Debug("Received Stripe Event " + stripeEvent.Type);
            if (_liveMode != stripeEvent.LiveMode)
            {
                _logger.Debug("Dropping Stripe Event " + stripeEvent.Type + " because LiveMode was " + stripeEvent.LiveMode);
                return(Ok());
            }

            StripeEventResponseDTO response;

            try
            {
                if (_asynchronous)
                {
                    _logger.Debug("Enqueueing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was true");
                    var message = _messageFactory.CreateMessage(stripeEvent);
                    _eventQueue.Send(message, MessageQueueTransactionType.None);
                    response = new StripeEventResponseDTO
                    {
                        Message = "Queued event for asynchronous processing"
                    };
                }
                else
                {
                    _logger.Debug("Processing Stripe event " + stripeEvent.Type + " because AsynchronousProcessingMode was false");
                    response = _stripeEventService.ProcessStripeEvent(stripeEvent);
                }
            }
            catch (Exception e)
            {
                var msg = "Unexpected error processing Stripe Event " + stripeEvent.Type;

                if (e is DonationNotFoundException)
                {
                    // Sometimes we receive a webhook callback before the donation has been
                    // added to the database.  This is a known issue, so just do minimal
                    // logging without a full stack trace.
                    _logger.Error($"ProcessStripeEvent: Donation not found processing {stripeEvent.Type}: {e.Message}");
                }
                else
                {
                    _logger.Error(msg, e);
                }

                var responseDto = new StripeEventResponseDTO()
                {
                    Exception = new ApplicationException(msg, e),
                    Message   = msg
                };
                return(RestHttpActionResult <StripeEventResponseDTO> .ServerError(responseDto));
            }

            return(response == null ? Ok() : (IHttpActionResult)RestHttpActionResult <StripeEventResponseDTO> .Ok(response));
        }