Skip to content

Commit

Permalink
Merge pull request #236 from InternAcademy/dev
Browse files Browse the repository at this point in the history
Fix service
  • Loading branch information
dpS1lence authored Sep 11, 2024
2 parents e0dace4 + e1a992a commit 676c927
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions src/server/CookingApp/Services/Stripe/StripeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<IEnumerable<StripeProduct>> GetProductsAsync()

foreach (var product in products)
{

var price = await priceService.GetAsync(product.DefaultPriceId);
result.Add(new StripeProduct()
{
Expand All @@ -62,7 +62,7 @@ public async Task<IEnumerable<StripeProduct>> GetProductsAsync()
/// Once the initial Invoice is payed the status then is set to active.
/// If the Invoice is not payed in 23 hours the status then is set to "incomplete_expired"
/// </summary>
public async Task<InvoiceCreationResponse> CreateSubscriptionAsync(InvoiceCreation model)
public async Task<InvoiceCreationResponse> CreateSubscriptionAsync(InvoiceCreation model)
{
if (model == null ||
string.IsNullOrEmpty(model.Email) ||
Expand All @@ -73,33 +73,37 @@ public async Task<InvoiceCreationResponse> CreateSubscriptionAsync(InvoiceCreati

var userId = GetUser.ProfileId(httpContextAccessor);

var profile = await userRepo.GetFirstOrDefaultAsync(x => x.UserId == userId);

var profile = await userRepo.GetFirstOrDefaultAsync(x=>x.UserId == userId);

if (profile is null)
if(profile is null)
{
throw new NotFoundException();
}

var options = new SessionCreateOptions
{
SuccessUrl = $"{stripeOptions.Value.SuccessRoute}",
Mode = "subscription",
LineItems = new List<SessionLineItemOptions>
{
SuccessUrl = $"{stripeOptions.Value.SuccessRoute}",
Mode = "subscription",
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
Price = model.PriceId,
Quantity = 1,
},
},
AutomaticTax = new SessionAutomaticTaxOptions
{
Enabled = true
}
AutomaticTax = new SessionAutomaticTaxOptions
{
Enabled = true
},
CustomerUpdate = new SessionCustomerUpdateOptions
{
Address = "auto"
}
};

if (profile.StripeId is not null)
if(profile.StripeId is not null)
{
var customer = await customerService.GetAsync(profile.StripeId);
var subscriptionListOptions = new SubscriptionListOptions
Expand All @@ -108,25 +112,25 @@ public async Task<InvoiceCreationResponse> CreateSubscriptionAsync(InvoiceCreati
};

var subscriptions = await subscriptionService.ListAsync(subscriptionListOptions);
if (subscriptions.Any())
if(subscriptions.Any())
{
if (subscriptions.Any(sub => sub.Status == "active"))
if(subscriptions.Any(sub=>sub.Status=="active"))
{
throw new ArgumentException(Stripe.TheUserIsAlreadySubscribed);
}
}
options.Customer = profile.StripeId;
options.Customer=profile.StripeId;
}

else
{
var customer = await customerService.CreateAsync(new CustomerCreateOptions() { Email = model.Email });
var customer = await customerService.CreateAsync(new CustomerCreateOptions(){Email=model.Email});
options.Customer = customer.Id;
profile.StripeId = customer.Id;
await userRepo.UpdateAsync(profile);
}

var session = await sessionService.CreateAsync(options);
var session = await sessionService.CreateAsync(options);


return new InvoiceCreationResponse(
Expand Down Expand Up @@ -168,6 +172,10 @@ public async Task<InvoiceCreationResponse> BuyPackAsync(InvoiceCreation model)
AutomaticTax = new SessionAutomaticTaxOptions
{
Enabled = true
},
CustomerUpdate = new SessionCustomerUpdateOptions
{
Address = "auto"
}
};

Expand Down Expand Up @@ -203,7 +211,7 @@ public async Task<SubscriptionCancellationResponse> CancelSubscriptionAsync(stri
var sub = await subscriptionService.GetAsync(subscriptionId);
var options = new SubscriptionUpdateOptions
{
CancelAtPeriodEnd = true
CancelAtPeriodEnd=true
};
subscriptionService.Update(subscriptionId, options);

Expand Down Expand Up @@ -247,14 +255,14 @@ public async Task<CustomerData> GetSubscription()

var sub = await subscriptionService.GetAsync(subscriptions.First().Id);
if (sub.Items != null && sub.Items.Data.Count > 0)
{
{
var subscriptionItem = sub.Items.Data.First();
var priceId = subscriptionItem.Price.Id;
var price = await priceService.GetAsync(priceId);
customer.Subscriptions.First().Price = price.UnitAmountDecimal / 100;
customer.Subscriptions.First().Price = price.UnitAmountDecimal/100;

}

return customer;
}

Expand Down

0 comments on commit 676c927

Please sign in to comment.