Add Stripe patronage checkout integration
This commit is contained in:
parent
ba01b78d68
commit
861dff3ed7
13 changed files with 812 additions and 32 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using Bifrost.Web.Data;
|
||||
using Bifrost.Web.Features.Patronage;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
|
@ -8,6 +9,8 @@ using Microsoft.EntityFrameworkCore.Storage;
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net;
|
||||
|
||||
namespace Bifrost.Web.Tests.Support;
|
||||
|
||||
|
|
@ -34,7 +37,13 @@ public sealed class TestWebApplicationFactory : WebApplicationFactory<Program>
|
|||
["Host:PublicBaseUrl"] = "https://localhost",
|
||||
["Host:ExpectedHost"] = "localhost",
|
||||
["Host:RequireStrictHostValidation"] = "false",
|
||||
["Heimdall:PatronSupportIntakeSecret"] = "test-heimdall-intake-secret"
|
||||
["Heimdall:PatronSupportIntakeSecret"] = "test-heimdall-intake-secret",
|
||||
["Stripe:EnableCheckout"] = "true",
|
||||
["Stripe:SecretKey"] = "sk_test_configured",
|
||||
["Stripe:WebhookSecret"] = "whsec_test_webhook_secret",
|
||||
["Stripe:GeneralPatronageGitHubLogin"] = "test-admin",
|
||||
["Stripe:SuccessUrl"] = "https://velvet.gamecult.org/?patronage=success",
|
||||
["Stripe:CancelUrl"] = "https://velvet.gamecult.org/?patronage=cancelled"
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -64,6 +73,52 @@ public sealed class TestWebApplicationFactory : WebApplicationFactory<Program>
|
|||
options.LoginPath = "/auth/sign-in";
|
||||
options.AccessDeniedPath = "/Membership/Status";
|
||||
});
|
||||
|
||||
services.RemoveAll<StripeCheckoutService>();
|
||||
services.AddTransient(serviceProvider =>
|
||||
{
|
||||
var client = new HttpClient(new TestStripeCheckoutHandler())
|
||||
{
|
||||
BaseAddress = new Uri("https://api.stripe.test/")
|
||||
};
|
||||
|
||||
return new StripeCheckoutService(
|
||||
client,
|
||||
serviceProvider.GetRequiredService<IOptions<Bifrost.Web.Configuration.StripeOptions>>());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private sealed class TestStripeCheckoutHandler : HttpMessageHandler
|
||||
{
|
||||
protected override async Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var form = await request.Content!.ReadAsStringAsync(cancellationToken);
|
||||
var hasExpectedMetadata =
|
||||
form.Contains("metadata%5Bpurpose%5D=general_patronage", StringComparison.Ordinal) &&
|
||||
form.Contains("metadata%5Bledger%5D=bifrost", StringComparison.Ordinal) &&
|
||||
form.Contains("metadata%5Bbifrost_user_account_id%5D=", StringComparison.Ordinal);
|
||||
|
||||
if (request.RequestUri?.PathAndQuery != "/v1/checkout/sessions" || !hasExpectedMetadata)
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.BadRequest)
|
||||
{
|
||||
Content = new StringContent("{}")
|
||||
};
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.OK)
|
||||
{
|
||||
Content = new StringContent(
|
||||
"""
|
||||
{
|
||||
"id": "cs_test_velvet",
|
||||
"url": "https://checkout.stripe.com/c/pay/cs_test_velvet"
|
||||
}
|
||||
""")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue