public async Task HandleNewCustomerSubscriptionAsync(string json)
    {
        var paymentHandlerEvent = _paymentHandlerEventService.FromJson(json);

        if (string.IsNullOrEmpty(paymentHandlerEvent.SubscriptionId))
        {
            _logger.LogWarning("Payment handler subscriptionId is null or empty", json);
        }
        var paymentAmount = _paymentHandlerInvoice.GetPaymentAmount(json);

        var newSubscriberIsAlreadyMember = await IsNewCustomerSubscriptionWithEmailAlreadyMember(paymentHandlerEvent.SubscriptionId);

        if (newSubscriberIsAlreadyMember)
        {
            _logger.LogInformation("New subscriber is an existing devBetter member", json);

            await HandleNewCustomerSubscriptionWithEmailAlreadyMember(paymentHandlerEvent.SubscriptionId, paymentAmount);
        }
        else
        {
            var status = _paymentHandlerSubscription.GetStatus(paymentHandlerEvent.SubscriptionId);
            _logger.LogInformation($"Subscription status: {status}");

            if (status == "active")
            {
                var customerId             = _paymentHandlerSubscription.GetCustomerId(paymentHandlerEvent.SubscriptionId);
                var paymentHandlerCustomer = _paymentHandlerCustomerService.GetCustomer(customerId);

                if (string.IsNullOrEmpty(paymentHandlerCustomer.Email))
                {
                    throw new InvalidEmailException();
                }

                Invitation invite = await _newMemberService.CreateInvitationAsync(paymentHandlerCustomer.Email, paymentHandlerEvent.SubscriptionId);

                var webhookMessage = $"A new customer with email {paymentHandlerCustomer.Email} has subscribed to DevBetter. They will be receiving a registration email.";
                await _webhook.Send($"Webhook:\n{webhookMessage}");

                await _newMemberService.SendRegistrationEmailAsync(invite);

                // moved to NewMemberRegister.cshtml.cs
                //await AddNewSubscriberBillingActivity(paymentHandlerSubscriptionId, email, paymentAmount);
            }
        }
    }
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            _webhook.Content = $"{json}";
            await _webhook.Send();

            return(Ok());

            try
            {
                var stripeEvent     = EventUtility.ParseEvent(json);
                var stripeEventType = stripeEvent.Type;

                if (stripeEventType.Equals("customer.subscription.created"))
                {
                    var subscription   = stripeEvent.Data.Object as Stripe.Subscription;
                    var subscriptionId = subscription !.Id;
                    var customerId     = subscription.CustomerId;
                    var email          = _paymentHandlerCustomer.GetCustomerEmail(customerId);

                    Invitation invite = await _newMemberService.CreateInvitationAsync(email, subscriptionId);

                    await _newMemberService.SendRegistrationEmailAsync(invite);
                }
                else
                {
                    _logger.LogError("Unhandled event type: {0}", stripeEvent.Type);
                }
                return(Ok());
            }
            catch (StripeException)
            {
                return(BadRequest());
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
        public async Task HandleNewCustomerSubscriptionAsync(string json)
        {
            var subscriptionId = _paymentHandlerEvent.GetSubscriptionId(json);
            var paymentAmount  = _paymentHandlerInvoice.GetPaymentAmount(json);

            var newSubscriberIsAlreadyMember = await NewCustomerSubscriptionWithEmailAlreadyMember(subscriptionId);

            if (newSubscriberIsAlreadyMember)
            {
                await HandleNewCustomerSubscriptionWithEmailAlreadyMember(subscriptionId, paymentAmount);
            }
            else
            {
                var status = _paymentHandlerSubscription.GetStatus(subscriptionId);

                if (status == "active")
                {
                    var customerId = _paymentHandlerSubscription.GetCustomerId(subscriptionId);
                    var email      = _paymentHandlerCustomer.GetCustomerEmail(customerId);

                    if (string.IsNullOrEmpty(email))
                    {
                        throw new InvalidEmailException();
                    }

                    Invitation invite = await _newMemberService.CreateInvitationAsync(email, subscriptionId);

                    var webhookMessage = $"A new customer with email {email} has subscribed to DevBetter. They will be receiving a registration email.";
                    await _webhook.Send($"Webhook:\n{webhookMessage}");

                    await _newMemberService.SendRegistrationEmailAsync(invite);

                    await AddNewSubscriberBillingActivity(subscriptionId, email, paymentAmount);
                }
            }
        }
Example #4
0
        public async Task SendsRegistrationEmail()
        {
            await _newMemberService.SendRegistrationEmailAsync(_invitation);

            _emailService.Verify(e => e.SendEmailAsync(_email, It.IsAny <string>(), It.IsAny <string>()), Times.Once);
        }