using Hutopy.Infrastructure.Payments.Stripe.Configuration; using Hutopy.Modules.Creators.Contracts; using Hutopy.Modules.Tipping.Contracts; using Microsoft.Extensions.Options; using Stripe; using Stripe.Checkout; namespace Hutopy.Infrastructure.Payments.Stripe.Services; internal class StripeTipProcessor( IOptions stripeOptions) : ITipProcessor { public async Task CreateCheckoutSessionAsync( Guid tipId, CreatorReference creator, decimal amount, string currency, string message, Uri successUrl, Uri cancelUrl, CancellationToken ct = default) { var applicationFeeAmount = Convert.ToInt64(amount * stripeOptions.Value.HutopyRate); StripeConfiguration.ApiKey = stripeOptions.Value.SecretKey; var sessionService = new SessionService(); var options = new SessionCreateOptions { ClientReferenceId = tipId.ToString(), Mode = "payment", LineItems = [ new SessionLineItemOptions { PriceData = new SessionLineItemPriceDataOptions { Currency = currency, UnitAmountDecimal = amount, // Amount in cents ProductData = new SessionLineItemPriceDataProductDataOptions { Name = $"Tip for {creator.Name}", Metadata = new Dictionary { { "creatorId", creator.Id.ToString() } } } }, Quantity = 1 } ], PaymentIntentData = new SessionPaymentIntentDataOptions { ApplicationFeeAmount = applicationFeeAmount }, Metadata = new Dictionary { { "creatorId", creator.Id.ToString() }, { "creatorName", creator.Name }, { "message", message } }, SuccessUrl = successUrl.ToString(), // Redirect after successful payment CancelUrl = cancelUrl.ToString(), // Redirect after canceled payment }; var requestOptions = new RequestOptions { StripeAccount = creator.StripeAccountId }; var session = await sessionService.CreateAsync( options, requestOptions, cancellationToken: ct) .ConfigureAwait(false); return new TipCheckoutSession(session.Id, session.Url); } }