using Hutopy.Infrastructure.Payments.Stripe.Configuration; using Hutopy.Modules.Creators.Contracts; using Hutopy.Modules.Memberships.Contracts; using Microsoft.Extensions.Options; using Stripe; using Stripe.Checkout; namespace Hutopy.Infrastructure.Payments.Stripe.Services; public class MembershipPaymentProcessor( IOptions stripeOptions) : IMembershipPaymentProcessor { public async Task CreateCheckoutSessionAsync( Guid userId, CreatorReference creatorReference, Guid tierId, string priceId, string successUrl, string cancelUrl) { StripeConfiguration.ApiKey = stripeOptions.Value.SecretKey; // Create Stripe customer for the user if not already created CustomerService customerService = new(); Customer? customer = await customerService.CreateAsync( new CustomerCreateOptions { Metadata = new Dictionary { { "userId", userId.ToString() } } }); // Create Checkout Session for the subscription SessionService sessionService = new(); Session? session = await sessionService.CreateAsync( new SessionCreateOptions { Customer = customer.Id, PaymentMethodTypes = ["card"], LineItems = [ new SessionLineItemOptions { Price = priceId, Quantity = 1 } ], Mode = "subscription", SubscriptionData = new SessionSubscriptionDataOptions { ApplicationFeePercent = stripeOptions.Value.HutopyRate, TransferData = new SessionSubscriptionDataTransferDataOptions { Destination = creatorReference.StripeAccountId } }, SuccessUrl = successUrl, // Redirect after successful payment CancelUrl = cancelUrl, // Redirect after canceled payment Metadata = new Dictionary { { "userId", userId.ToString() }, { "creatorId", creatorReference.Id.ToString() }, { "creatorName", creatorReference.Name }, { "tierId", tierId.ToString() } } }); return new MembershipCheckoutSession( session.Id, session.Url); } }