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)); }
public ActionResult <ItemResponse <Sabio.Models.Domain.Stripe.Subscription> > GetSubscriptionStatus() { 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)); } responseData = new ItemResponse <Sabio.Models.Domain.Stripe.Subscription> { Item = subscription }; } catch (Exception exception) { responseCode = 500; responseData = new ErrorResponse($"Generic Error: {exception.Message}"); base.Logger.LogError(exception.ToString()); } return(StatusCode(responseCode, responseData)); }
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)); }