public IHttpActionResult GetAllSubscriptionCustomer() { try { var assClient = new AssinaturasClient( new Uri(IlevusDBContext.SystemConfiguration.MoipBaseUrl), IlevusDBContext.SystemConfiguration.MoipToken, IlevusDBContext.SystemConfiguration.MoipKey ); var customersResponse = assClient.GetCustomers(); return(Ok(customersResponse.Customers)); } catch (Exception e) { return(InternalServerError(e)); } }
public IHttpActionResult GetSubscriptionCustomer(string Id) { try { var assClient = new AssinaturasClient( new Uri(IlevusDBContext.SystemConfiguration.MoipBaseUrl), IlevusDBContext.SystemConfiguration.MoipToken, IlevusDBContext.SystemConfiguration.MoipKey ); var customerResponse = assClient.GetCustomer(Id); return(Ok(customerResponse)); } catch (MoipException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { return(BadRequest("Cliente MOIP não encontrado")); } return(InternalServerError(e)); } }
public async Task <IHttpActionResult> UpdateUserSubscription(MoipSubscriptionBindingModel model) { var assClient = new AssinaturasClient( new Uri(IlevusDBContext.SystemConfiguration.MoipBaseUrl), IlevusDBContext.SystemConfiguration.MoipToken, IlevusDBContext.SystemConfiguration.MoipKey ); var db = IlevusDBContext.Create(); var collection = db.GetSubscriptionsCollection(); var filters = Builders <IlevusSubscription> .Filter; try { var user = await UserManager.FindByNameAsync(User.Identity.Name); if (user == null) { return(BadRequest("You must be logged in.")); } IlevusSubscription result = (await collection.FindAsync(filters.Eq("Id", model.Id))).FirstOrDefault(); if (result == null) { return(BadRequest("Invalid subscription.")); } if (!result.UserId.Equals(user.Id)) { return(BadRequest("Essa não é sua assinatura.")); } SubscriptionResponse moipSub; try { moipSub = assClient.GetSubscription(result.Id); } catch (MoipException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { moipSub = null; } else { return(InternalServerError(e)); } } if (moipSub == null) { CustomerRequest customer = model.Customer; customer.Code = user.Id; bool newCustomer = false; try { CustomerResponse moipCustomer = assClient.GetCustomer(user.Id); newCustomer = false; } catch (MoipException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { newCustomer = true; } else { return(InternalServerError(e)); } } if (newCustomer) { assClient.CreateCustomer(customer, true); } else { assClient.UpdateCustomer(user.Id, customer); assClient.UpdateBillingInfo(user.Id, customer.BillingInfo); } Subscription subscription = new Subscription() { Code = result.Id, Customer = customer, PaymentMethod = Plan.PaymentMethodPlan.CREDIT_CARD, Plan = new Plan() { Code = IlevusDBContext.SystemConfiguration.MoipSubscriptionCode } }; moipSub = assClient.CreateSubscription(subscription, false); } if (!moipSub.Status.Equals(Subscription.SubscriptionStatus.ACTIVE)) { assClient.ActivateSubscription(moipSub.Code); } result.Amount = moipSub.Invoice.Amount; result.Invoice = moipSub.Invoice; result.NextInvoiceDate = moipSub.NextInvoiceDate; result.Status = moipSub.Status.ToString(); await collection.ReplaceOneAsync(filters.Eq("Id", model.Id), result); DateTime payedUntil = DateTime.Today; if ((result.NextInvoiceDate.Year != null) && (result.NextInvoiceDate.Month != null) && (result.NextInvoiceDate.Day != null)) { payedUntil = new DateTime(result.NextInvoiceDate.Year.Value, result.NextInvoiceDate.Month.Value, result.NextInvoiceDate.Day.Value); } user.Premium = new UserPremiumMembership() { Active = true, Late = false, PayedUntil = payedUntil }; await UserManager.UpdateAsync(user); return(Ok(result)); } catch (Exception e) { return(InternalServerError(e)); } }
public async Task <IHttpActionResult> SuspendUserSubscription() { var assClient = new AssinaturasClient( new Uri(IlevusDBContext.SystemConfiguration.MoipBaseUrl), IlevusDBContext.SystemConfiguration.MoipToken, IlevusDBContext.SystemConfiguration.MoipKey ); var db = IlevusDBContext.Create(); var collection = db.GetSubscriptionsCollection(); var filters = Builders <IlevusSubscription> .Filter; try { var user = await UserManager.FindByNameAsync(User.Identity.Name); if (user == null) { return(BadRequest("You must be logged in.")); } if (!user.IsProfessional) { return(BadRequest("Você precisa ser um profissional para ter um plano de assinatura premium.")); } IlevusSubscription sub = (await collection.FindAsync(filters.And( filters.Eq("UserId", user.Id), filters.Ne("Status", "CANCELLED") ))).FirstOrDefault(); if (sub == null) { return(BadRequest("You must have a subscription to suspend")); } try { SubscriptionResponse moipSub = assClient.GetSubscription(sub.Id); assClient.SuspendSubscription(moipSub.Code); sub.Status = Subscription.SubscriptionStatus.SUSPENDED.ToString(); await collection.ReplaceOneAsync(filters.Eq("Id", sub.Id), sub); user.Premium.Suspended = true; await UserManager.UpdateAsync(user); return(Ok(user.Premium)); } catch (MoipException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { return(BadRequest("Assiantura MOIP não encontrada.")); } else { return(InternalServerError(e)); } } } catch (Exception e) { return(InternalServerError(e)); } }