Ejemplo n.º 1
0
        public ActionResult <ItemResponse <Stripe.Subscription> > GetSubscription()
        {
            int          responseCode = 200;
            BaseResponse responseData = null;

            try
            {
                int userId = _authService.GetCurrentUserId();
                StripeConfiguration.ApiKey = _appKeys.StripeApiKey;
                Sabio.Models.Domain.Stripe.Subscription subscription = _service.GetSubscriptionByUserId(userId);
                if (subscription == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Subscription not found.");
                    return(StatusCode(responseCode, responseData));
                }

                string subscriptionId = subscription.SubscriptionId;
                Newtonsoft.Json.Linq.JObject subscriptionRawJObject = GetStripeSubscription(subscriptionId).RawJObject;
                string subscriptionString = subscriptionRawJObject.ToString();
                Stripe.Subscription stripeSubscription = JsonConvert.DeserializeObject <Stripe.Subscription>(subscriptionString);
                responseData = new ItemResponse <Stripe.Subscription>()
                {
                    Item = stripeSubscription
                };
            }
            catch (Exception exception)
            {
                responseCode = 500;
                responseData = new ErrorResponse($"Generic Error: {exception.Message}");
                base.Logger.LogError(exception.ToString());
            }

            return(StatusCode(responseCode, responseData));
        }
Ejemplo n.º 2
0
        private Stripe.Subscription UpdateStripeSubscriptionPlan(StripeSubUpdatePlanRequest request)
        {
            StripeConfiguration.ApiKey = _appKeys.StripeApiKey;

            var service = new SubscriptionService();

            Stripe.Subscription subscription = service.Get(request.SubscriptionId);

            var items = new List <SubscriptionItemOptions>
            {
                new SubscriptionItemOptions
                {
                    Id    = subscription.Items.Data[0].Id,
                    Price = request.PriceId,
                }
            };

            var options = new SubscriptionUpdateOptions
            {
                Items = items,
            };

            options.AddExpand("latest_invoice.payment_intent");
            subscription = service.Update(request.SubscriptionId, options);
            return(subscription);
        }
        private BillingPeriod GetSubscriptionBillingInterval(Stripe.Subscription subscription)
        {
            var item   = subscription.Items.Data[0];
            var period = item.Price.Recurring.Interval;

            if (period == "month")
            {
                return(BillingPeriod.Month);
            }
            if (period == "year")
            {
                return(BillingPeriod.Year);
            }
            throw new InvalidBillingPeriodException();
        }
Ejemplo n.º 4
0
        public ActionResult <ItemResponse <Stripe.Subscription> > UpdateSubscriptionPlan(StripeSubUpdatePlanRequest model)
        {
            int          responseCode = 200;
            BaseResponse responseData = null;

            try
            {
                StripeConfiguration.ApiKey = _appKeys.StripeApiKey;
                // Update stripe subscription
                Newtonsoft.Json.Linq.JObject subscriptionRawJObject = UpdateStripeSubscriptionPlan(model).RawJObject;
                string subscriptionString = subscriptionRawJObject.ToString();
                Stripe.Subscription stripeSubscription = JsonConvert.DeserializeObject <Stripe.Subscription>(subscriptionString);
                responseData = new ItemResponse <Stripe.Subscription>()
                {
                    Item = stripeSubscription
                };

                if (stripeSubscription == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Subscription was not updated.");
                    return(StatusCode(responseCode, responseData));
                }
                ;

                // Update database subscription
                SubscriptionAddRequest request = new SubscriptionAddRequest();
                request.SubscriptionId       = stripeSubscription.Id;
                request.UserId               = _authService.GetCurrentUserId();
                request.CustomerId           = stripeSubscription.CustomerId;
                request.IsSubscriptionActive = stripeSubscription.Status == "active";
                request.DateEnded            = stripeSubscription.EndedAt;

                _service.UpdateSubscription(request);
            }
            catch (Exception exception)
            {
                responseCode = 500;
                responseData = new ErrorResponse($"Generic Error: {exception.Message}");
                base.Logger.LogError(exception.ToString());
            }

            return(StatusCode(responseCode, responseData));
        }
Ejemplo n.º 5
0
        public ActionResult <SuccessResponse> CancelSubscription(string id)
        {
            int          responseCode = 200;
            BaseResponse responseData = null;

            try
            {
                StripeConfiguration.ApiKey = _appKeys.StripeApiKey;
                Stripe.Subscription stripeSubscription = CancelStripeSubscription(id);

                if (stripeSubscription == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Item was not found");
                    return(StatusCode(responseCode, responseData));
                }

                SubscriptionAddRequest request = new SubscriptionAddRequest();
                request.SubscriptionId       = id;
                request.UserId               = _authService.GetCurrentUserId();
                request.CustomerId           = stripeSubscription.CustomerId;
                request.IsSubscriptionActive = stripeSubscription.Status == "active";
                request.DateEnded            = stripeSubscription.EndedAt;

                _service.UpdateSubscription(request);

                responseData = new SuccessResponse();
            }
            catch (Exception exception)
            {
                responseCode = 500;
                responseData = new ErrorResponse($"Generic Error: {exception.Message}");
                base.Logger.LogError(exception.ToString());
            }

            return(StatusCode(responseCode, responseData));
        }
Ejemplo n.º 6
0
        public ActionResult <ItemResponse <Stripe.Subscription> > UpdateSubscriptionPayment(StripeSubUpdatePaymentRequest model)
        {
            int          responseCode = 200;
            BaseResponse responseData = null;

            try
            {
                StripeConfiguration.ApiKey = _appKeys.StripeApiKey;
                //Update Customer Billing Options
                Customer customer = UpdateStripeCustomer(model.CustomerId, model.BillingDetails);
                if (customer == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Customer information was not updated.");
                    return(StatusCode(responseCode, responseData));
                }
                // Attach payment method to customer
                PaymentMethod paymentMethod = AttachStripePaymentMethod(model.CustomerId, model.PaymentMethodId);
                if (paymentMethod == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Payment method was not attached.");
                    return(StatusCode(responseCode, responseData));
                }
                ;

                // Update stripe subscription
                Newtonsoft.Json.Linq.JObject subscriptionRawJObject = UpdateStripeSubscriptionPayment(model).RawJObject;
                string subscriptionString = subscriptionRawJObject.ToString();
                Stripe.Subscription stripeSubscription = JsonConvert.DeserializeObject <Stripe.Subscription>(subscriptionString);

                if (stripeSubscription == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Subscription was not updated.");
                    return(StatusCode(responseCode, responseData));
                }
                else
                {
                    responseData = new ItemResponse <Stripe.Subscription>()
                    {
                        Item = stripeSubscription
                    }
                };

                // Update database subscription
                SubscriptionAddRequest request = new SubscriptionAddRequest();
                request.SubscriptionId       = stripeSubscription.Id;
                request.UserId               = _authService.GetCurrentUserId();
                request.CustomerId           = stripeSubscription.CustomerId;
                request.IsSubscriptionActive = stripeSubscription.Status == "active";
                request.DateEnded            = stripeSubscription.EndedAt;

                _service.UpdateSubscription(request);
            }
            catch (Exception exception)
            {
                responseCode = 500;
                responseData = new ErrorResponse($"Generic Error: {exception.Message}");
                base.Logger.LogError(exception.ToString());
            }

            return(StatusCode(responseCode, responseData));
        }
Ejemplo n.º 7
0
        public ActionResult <ItemResponse <Stripe.Subscription> > CreateSubscription(StripeSubAddRequest model)
        {
            int          responseCode = 200;
            BaseResponse responseData = null;

            try
            {
                /* add step to check if user is subscribed? */
                int userId = _authService.GetCurrentUserId();
                StripeConfiguration.ApiKey = _appKeys.StripeApiKey;
                Sabio.Models.Domain.Stripe.Subscription existingSubscription = _service.GetSubscriptionByUserId(userId);

                if (existingSubscription != null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Subscription already exists.");
                    return(StatusCode(responseCode, responseData));
                }

                Customer customer = UpdateStripeCustomer(model.CustomerId, model.BillingDetails);
                if (customer == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Customer information was not updated.");
                    return(StatusCode(responseCode, responseData));
                }

                // Attach payment method to customer
                PaymentMethod paymentMethod = AttachStripePaymentMethod(model.CustomerId, model.PaymentMethodId);
                if (paymentMethod == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Payment method was not attached.");
                    return(StatusCode(responseCode, responseData));
                }
                ;

                // Create stripe subscription

                Newtonsoft.Json.Linq.JObject subscriptionRawJObject = CreateStripeSubscription(model).RawJObject;
                string subscriptionString = subscriptionRawJObject.ToString();
                Stripe.Subscription stripeSubscription = JsonConvert.DeserializeObject <Stripe.Subscription>(subscriptionString);

                if (stripeSubscription == null)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Subscription was not created.");
                    return(StatusCode(responseCode, responseData));
                }
                else
                {
                    responseData = new ItemResponse <Stripe.Subscription>()
                    {
                        Item = stripeSubscription
                    }
                };

                // Create database subscription
                SubscriptionAddRequest request = new SubscriptionAddRequest();
                request.SubscriptionId = stripeSubscription.Id;
                request.UserId         = _authService.GetCurrentUserId();
                request.CustomerId     = stripeSubscription.CustomerId;
                if (stripeSubscription.Status == "active")
                {
                    request.IsSubscriptionActive = true;
                }
                else
                {
                    request.IsSubscriptionActive = false;
                }
                request.DateEnded = stripeSubscription.EndedAt;

                int id = _service.AddSubscription(request);
                if (id == 0)
                {
                    responseCode = 404;
                    responseData = new ErrorResponse("Subscription was not saved.");
                    return(StatusCode(responseCode, responseData));
                }
                ;
            }
            catch (StripeException e)
            {
                switch (e.StripeError.Type)
                {
                case "card_error":
                    responseCode = (int)e.HttpStatusCode;
                    responseData = new ErrorResponse(e.StripeError.Message);
                    base.Logger.LogError(e.ToString());
                    break;

                case "api_connection_error":
                    break;

                case "api_error":
                    break;

                case "authentication_error":
                    break;

                case "invalid_request_error":
                    break;

                case "rate_limit_error":
                    break;

                case "validation_error":
                    break;

                default:
                    // Unknown Error Type
                    break;
                }
            }
            catch (Exception exception)
            {
                responseCode = 500;
                responseData = new ErrorResponse($"Generic Error: {exception.Message}");
                base.Logger.LogError(exception.ToString());
            }

            return(StatusCode(responseCode, responseData));
        }
        private DateTime GetStartDate(Stripe.Subscription subscription)
        {
            DateTime startDate = subscription.CurrentPeriodStart;

            return(startDate);
        }
        private DateTime GetEndDate(Stripe.Subscription subscription)
        {
            DateTime endDate = subscription.CurrentPeriodEnd;

            return(endDate);
        }