Added endpoint to create a sessionCheckout with stripe
This commit is contained in:
@@ -37,6 +37,7 @@
|
|||||||
<PackageVersion Include="NUnit.Analyzers" Version="3.9.0" />
|
<PackageVersion Include="NUnit.Analyzers" Version="3.9.0" />
|
||||||
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
|
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||||
<PackageVersion Include="Respawn" Version="6.1.0" />
|
<PackageVersion Include="Respawn" Version="6.1.0" />
|
||||||
|
<PackageVersion Include="Stripe.net" Version="43.19.0" />
|
||||||
<PackageVersion Include="Testcontainers.MsSql" Version="3.6.0" />
|
<PackageVersion Include="Testcontainers.MsSql" Version="3.6.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
7
src/Application/Common/Interfaces/IStripeService.cs
Normal file
7
src/Application/Common/Interfaces/IStripeService.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Hutopy.Application.Common.Interfaces;
|
||||||
|
|
||||||
|
|
||||||
|
public interface IStripeService
|
||||||
|
{
|
||||||
|
public Task<string> CreateCheckoutSession(int price, string currency);
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using Hutopy.Application.Common.Interfaces;
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hutopy.Application.Stripe.Commands;
|
||||||
|
public record CreateSessionCheckoutCommand : IRequest<string>
|
||||||
|
{
|
||||||
|
public required int Price { get; init; }
|
||||||
|
|
||||||
|
public string Currency { get; init; } = "cad";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class CreateSessionCheckoutCommandHandler : IRequestHandler<CreateSessionCheckoutCommand, string>
|
||||||
|
{
|
||||||
|
private readonly IApplicationDbContext _context;
|
||||||
|
private readonly IStripeService _stripeService;
|
||||||
|
|
||||||
|
|
||||||
|
public CreateSessionCheckoutCommandHandler(IApplicationDbContext context, IStripeService stripeService)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
_stripeService = stripeService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> Handle(CreateSessionCheckoutCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var stripeSecret = await _stripeService.CreateCheckoutSession(request.Price, request.Currency);
|
||||||
|
|
||||||
|
return stripeSecret;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ using Hutopy.Domain.Constants;
|
|||||||
using Hutopy.Infrastructure.Data;
|
using Hutopy.Infrastructure.Data;
|
||||||
using Hutopy.Infrastructure.Data.Interceptors;
|
using Hutopy.Infrastructure.Data.Interceptors;
|
||||||
using Hutopy.Infrastructure.Identity;
|
using Hutopy.Infrastructure.Identity;
|
||||||
|
using Hutopy.Infrastructure.Stripe;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
@@ -49,6 +50,7 @@ public static class DependencyInjection
|
|||||||
|
|
||||||
services.AddSingleton(TimeProvider.System);
|
services.AddSingleton(TimeProvider.System);
|
||||||
services.AddTransient<IIdentityService, IdentityService>();
|
services.AddTransient<IIdentityService, IdentityService>();
|
||||||
|
services.AddTransient<IStripeService, StripeService>();
|
||||||
|
|
||||||
services.AddAuthorization(options =>
|
services.AddAuthorization(options =>
|
||||||
options.AddPolicy(Policies.CanPurge, policy => policy.RequireRole(Roles.Administrator)));
|
options.AddPolicy(Policies.CanPurge, policy => policy.RequireRole(Roles.Administrator)));
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" />
|
||||||
|
<PackageReference Include="Stripe.net" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Application\Application.csproj" />
|
<ProjectReference Include="..\Application\Application.csproj" />
|
||||||
|
|||||||
46
src/Infrastructure/Stripe/StripeService.cs
Normal file
46
src/Infrastructure/Stripe/StripeService.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using Stripe;
|
||||||
|
using Stripe.Checkout;
|
||||||
|
using Hutopy.Application.Common.Interfaces;
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hutopy.Infrastructure.Stripe;
|
||||||
|
|
||||||
|
public class StripeService : IStripeService
|
||||||
|
{
|
||||||
|
public StripeService()
|
||||||
|
{
|
||||||
|
// I removed the key to push. Will need to be in config.
|
||||||
|
StripeConfiguration.ApiKey = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> CreateCheckoutSession(int price, string currency = "cad")
|
||||||
|
{
|
||||||
|
var options = new SessionCreateOptions
|
||||||
|
{
|
||||||
|
LineItems = new List<SessionLineItemOptions>
|
||||||
|
{
|
||||||
|
new SessionLineItemOptions
|
||||||
|
{
|
||||||
|
PriceData = new SessionLineItemPriceDataOptions
|
||||||
|
{
|
||||||
|
UnitAmount = price,
|
||||||
|
Currency = currency,
|
||||||
|
ProductData = new SessionLineItemPriceDataProductDataOptions
|
||||||
|
{
|
||||||
|
Name = "Tip",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Quantity = 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Mode = "payment",
|
||||||
|
UiMode = "embedded",
|
||||||
|
ReturnUrl = "http://localhost:5174",
|
||||||
|
};
|
||||||
|
|
||||||
|
var service = new SessionService();
|
||||||
|
Session session = await service.CreateAsync(options);
|
||||||
|
|
||||||
|
return session.ClientSecret;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/Web/Endpoints/Stripe.cs
Normal file
17
src/Web/Endpoints/Stripe.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using Hutopy.Application.Stripe.Commands;
|
||||||
|
|
||||||
|
namespace Hutopy.Web.Endpoints;
|
||||||
|
|
||||||
|
public class Stripe : EndpointGroupBase
|
||||||
|
{
|
||||||
|
public override void Map(WebApplication app)
|
||||||
|
{
|
||||||
|
app.MapGroup(this)
|
||||||
|
.MapPost(CreateSessionCheckout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<string> CreateSessionCheckout(ISender sender, CreateSessionCheckoutCommand command)
|
||||||
|
{
|
||||||
|
return sender.Send(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,6 +39,38 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/api/Stripe": {
|
||||||
|
"post": {
|
||||||
|
"tags": [
|
||||||
|
"Stripe"
|
||||||
|
],
|
||||||
|
"operationId": "CreateSessionCheckout",
|
||||||
|
"requestBody": {
|
||||||
|
"x-name": "command",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/CreateSessionCheckoutCommand"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": true,
|
||||||
|
"x-position": 1
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/api/TodoItems": {
|
"/api/TodoItems": {
|
||||||
"get": {
|
"get": {
|
||||||
"tags": [
|
"tags": [
|
||||||
@@ -814,6 +846,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"CreateSessionCheckoutCommand": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"price": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
},
|
||||||
|
"currency": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"PaginatedListOfTodoItemBriefDto": {
|
"PaginatedListOfTodoItemBriefDto": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
|
|||||||
Reference in New Issue
Block a user