Exemplo n.º 1
0
        public override async Task <bool> ProcessEvent(Event stripeEvent)
        {
            Subscription subscription = stripeEvent.Data.Object as Subscription;

            if (subscription.Status.Equals(SubscriptionStatuses.Canceled))
            {
                //Check if Subscription Canceled in mongodb
                var gym = await GetGymFromStripeSubscription(subscription);

                //If Subscription not canceled then cancel
                if (IsGymSubscribed(gym, subscription))
                {
                    var metadata = subscription.Plan?.Metadata;
                    if (!metadata.TryGetValue("PackageId", out string packageId))
                    {
                        throw new CustomException("PackageId not found in subscription metadata in stripe", StatusCode.NotFound);
                    }

                    var customer = await customerService.GetAsync(subscription.CustomerId);

                    if (!customer.Metadata.TryGetValue("UserId", out string userId))
                    {
                        throw new CustomException("UserId not in customer Metadata", StatusCode.NotFound);
                    }

                    var product = await productService.GetAsync(subscription.Plan.ProductId);

                    if (await paymentBL.CancelSubscription(packageId, gym.Id, userId, processInfo))
                    {
                        //send email to user of service canceled
                        string htmlMessage = string.Format(
                            EmailResources.CancelingSubscription,
                            customer.Name,
                            product.Name
                            );
                        await emailSender.SendEmailAsync(
                            email : customer.Email,
                            subject : EmailResources.CancelingSubscriptionDueUnsuccessfulAutomaticCharge_Subject,
                            htmlMessage : htmlMessage
                            );

                        //Register Event
                        return(await RegisterEvent(
                                   stripeEvent,
                                   $"Subscription cancelation '{subscription.Id}' for customer '{subscription.CustomerId}' - Notification sent to: '{customer.Email}'"
                                   ));
                    }
                    else
                    {
                        throw new CustomException("Problems canceling subscription in local DB", StatusCode.InternalServerError);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public override async Task <bool> ProcessEvent(Event stripeEvent)
        {
            Subscription subscription = stripeEvent.Data.Object as Subscription;
            var          customer     = await customerService.GetAsync(subscription.CustomerId);

            var product = await productService.GetAsync(subscription.Plan.ProductId);

            if (subscription.Status.Equals(SubscriptionStatuses.PastDue))
            {
                string htmlMessage = string.Format(
                    EmailResources.ProblemsChargingSubscription,
                    customer.Name,
                    product.Name
                    );

                await emailSender.SendEmailAsync(
                    email : customer.Email,
                    subject : EmailResources.ProblemsChargingSubscription_Subject,
                    htmlMessage : htmlMessage
                    );

                return(await RegisterEvent(stripeEvent,
                                           $"Problems charging subscription {subscription.Id} to customer{subscription.CustomerId}: Notification sent to{customer.Email}"
                                           ));
            }

            else if (subscription.Status.Equals(SubscriptionStatuses.Canceled) || subscription.Status.Equals(SubscriptionStatuses.Unpaid))
            {
                var metadata = subscription.Plan?.Metadata;

                if (!metadata.TryGetValue("PackageId", out string packageId))
                {
                    throw new CustomException("PackageId not found in subscription metadata in stripe", StatusCode.NotFound);
                }

                if (!subscription.Metadata.TryGetValue("GymId", out string gymId))
                {
                    throw new CustomException("GymId not in subsctiption Metadata", StatusCode.NotFound);
                }

                if (!customer.Metadata.TryGetValue("UserId", out string userId))
                {
                    throw new CustomException("UserId not in customer Metadata", StatusCode.NotFound);
                }

                await paymentBL.CancelSubscription(packageId, gymId, userId, processInfo);

                //Notify User
                string htmlMessage = string.Format(EmailResources.CancelingSubscriptionDueUnsuccessfulAutomaticCharge, customer.Name, product.Name);
                await emailSender.SendEmailAsync(
                    email : customer.Email,
                    subject : EmailResources.CancelingSubscriptionDueUnsuccessfulAutomaticCharge_Subject,
                    htmlMessage : htmlMessage
                    );

                //Register Event
                return(await RegisterEvent(
                           stripeEvent,
                           $"Problems charging subscription {subscription.Id} to customer{subscription.CustomerId}: Notification sent to{customer.Email}"
                           ));
            }
            return(true);
        }