66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
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> stripeOptions)
|
|
: IMembershipPaymentProcessor
|
|
{
|
|
public async Task<MembershipCheckoutSession> 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
|
|
var customerService = new CustomerService();
|
|
var customer = await customerService.CreateAsync(
|
|
new CustomerCreateOptions
|
|
{
|
|
Metadata = new Dictionary<string, string> { { "userId", userId.ToString() } }
|
|
});
|
|
|
|
// Create Checkout Session for the subscription
|
|
var sessionService = new SessionService();
|
|
var 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<string, string>
|
|
{
|
|
{ "userId", userId.ToString() },
|
|
{ "creatorId", creatorReference.Id.ToString() },
|
|
{ "creatorName", creatorReference.Name },
|
|
{ "tierId", tierId.ToString() }
|
|
}
|
|
});
|
|
|
|
return new MembershipCheckoutSession(
|
|
session.Id,
|
|
session.Url);
|
|
}
|
|
|
|
}
|