Add Stripe patronage checkout integration
This commit is contained in:
parent
ba01b78d68
commit
861dff3ed7
13 changed files with 812 additions and 32 deletions
|
|
@ -7,6 +7,7 @@ using Bifrost.Web.Features.Patronage;
|
|||
using Bifrost.Web.Data;
|
||||
using Bifrost.Web.Domain;
|
||||
using Bifrost.Web.Tests.Support;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bifrost.Web.Tests;
|
||||
|
|
@ -228,6 +229,116 @@ public sealed class PlatformEndpointsTests : IClassFixture<TestWebApplicationFac
|
|||
Assert.Empty(currentPatronTiers);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Velvet_patronage_checkout_redirects_to_stripe_checkout()
|
||||
{
|
||||
using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
});
|
||||
|
||||
using var response = await client.GetAsync("/patronage/velvet/checkout?tier=velvet-room");
|
||||
|
||||
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
||||
Assert.Equal("https://checkout.stripe.com/c/pay/cs_test_velvet", response.Headers.Location?.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Velvet_patronage_checkout_requires_bifrost_account()
|
||||
{
|
||||
using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
});
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "/patronage/velvet/checkout?tier=velvet-room");
|
||||
request.Headers.Add("X-Test-Anonymous", "1");
|
||||
|
||||
using var response = await client.SendAsync(request);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
||||
Assert.Equal(
|
||||
"/auth/sign-in?returnUrl=%2Fpatronage%2Fvelvet%2Fcheckout%3Ftier%3Dvelvet-room",
|
||||
response.Headers.Location?.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Sign_in_chooser_lists_supported_transport_providers()
|
||||
{
|
||||
using var client = _factory.CreateClient();
|
||||
|
||||
using var response = await client.GetAsync("/auth/sign-in?returnUrl=%2Fpatronage%2Fvelvet%2Fcheckout%3Ftier%3Dvelvet-room");
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var html = await response.Content.ReadAsStringAsync();
|
||||
Assert.Contains("GitHub", html);
|
||||
Assert.Contains("/auth/heimdall/discord", html);
|
||||
Assert.Contains("/auth/heimdall/patreon", html);
|
||||
Assert.Contains("/auth/heimdall/twitch", html);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Stripe_checkout_completed_webhook_records_general_patronage()
|
||||
{
|
||||
using var client = _factory.CreateClient();
|
||||
_ = await client.GetAsync("/App");
|
||||
Guid patronageAccountId;
|
||||
using (var setupScope = _factory.Services.CreateScope())
|
||||
{
|
||||
var setupContext = setupScope.ServiceProvider.GetRequiredService<BifrostDbContext>();
|
||||
patronageAccountId = setupContext.UserAccounts.Single(x => x.NormalizedGitHubLogin == "TEST-ADMIN").Id;
|
||||
}
|
||||
|
||||
var created = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
var payload = JsonSerializer.Serialize(new
|
||||
{
|
||||
id = $"evt_{Guid.NewGuid():N}",
|
||||
type = "checkout.session.completed",
|
||||
created,
|
||||
data = new
|
||||
{
|
||||
@object = new
|
||||
{
|
||||
id = "cs_test_velvet_paid",
|
||||
customer = "cus_velvet_1",
|
||||
currency = "usd",
|
||||
amount_total = 3900,
|
||||
payment_status = "paid",
|
||||
metadata = new Dictionary<string, string>
|
||||
{
|
||||
["source"] = "velvet.gamecult.org",
|
||||
["ledger"] = "bifrost",
|
||||
["purpose"] = "general_patronage",
|
||||
["model"] = "deru",
|
||||
["tier"] = "after-hours-bundle",
|
||||
["bifrost_user_account_id"] = patronageAccountId.ToString("N"),
|
||||
["bifrost_github_login"] = "test-admin"
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "/patronage/stripe/webhook");
|
||||
request.Headers.Add("Stripe-Signature", ComputeStripeSignature("whsec_test_webhook_secret", payload));
|
||||
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
|
||||
|
||||
using var response = await client.SendAsync(request);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
|
||||
|
||||
using var scope = _factory.Services.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
|
||||
var patronageAccount = dbContext.UserAccounts.Single(x => x.NormalizedGitHubLogin == "TEST-ADMIN");
|
||||
var supportEvent = dbContext.PatronSupportEvents.Single(x => x.Provider == ExternalPatronProvider.Stripe);
|
||||
|
||||
Assert.Equal(patronageAccount.Id, supportEvent.UserAccountId);
|
||||
Assert.Equal("cs_test_velvet_paid", supportEvent.ExternalSupportId);
|
||||
Assert.Equal(39m, supportEvent.Amount);
|
||||
Assert.Equal("USD", supportEvent.CurrencyCode);
|
||||
Assert.Contains("after-hours-bundle", supportEvent.Notes);
|
||||
Assert.Contains(dbContext.AuditEvents, x => x.Action == "patron-support.recorded");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Eve_governance_surface_returns_motion_verse()
|
||||
{
|
||||
|
|
@ -1021,6 +1132,16 @@ public sealed class PlatformEndpointsTests : IClassFixture<TestWebApplicationFac
|
|||
return $"sha256={Convert.ToHexString(hash).ToLowerInvariant()}";
|
||||
}
|
||||
|
||||
private static string ComputeStripeSignature(string secret, string payload)
|
||||
{
|
||||
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
|
||||
using var hasher = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
|
||||
var signature = Convert.ToHexString(
|
||||
hasher.ComputeHash(Encoding.UTF8.GetBytes($"{timestamp}.{payload}"))).ToLowerInvariant();
|
||||
|
||||
return $"t={timestamp},v1={signature}";
|
||||
}
|
||||
|
||||
private static HttpRequestMessage CreateSignedHeimdallRequest(string payload)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "/heimdall/patron-support/events");
|
||||
|
|
|
|||
|
|
@ -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