feat(stripe): change to direct charges

This commit is contained in:
2025-08-11 12:24:00 -04:00
parent 165577ef22
commit d5ca6b9972

View File

@@ -21,57 +21,47 @@ internal class StripeTipProcessor(
Uri cancelUrl,
CancellationToken ct = default)
{
var applicationFeeAmount = Convert.ToInt64(amount * stripeOptions.Value.HutopyRate);
StripeConfiguration.ApiKey = stripeOptions.Value.SecretKey;
// Create Stripe customer for the user if not already created
CustomerService customerService = new();
var customer = await customerService
.CreateAsync(
new CustomerCreateOptions(),
cancellationToken: ct)
.ConfigureAwait(false);
var sessionService = new SessionService();
// Create paymentIntent for the user
SessionService sessionService = new();
var session = await sessionService.CreateAsync(
new SessionCreateOptions
var options = new SessionCreateOptions
{
ClientReferenceId = tipId.ToString(),
Mode = "payment",
LineItems =
[
new SessionLineItemOptions
{
ClientReferenceId = tipId.ToString(),
Customer = customer.Id,
PaymentMethodTypes = ["card"],
LineItems =
[
new SessionLineItemOptions
{
PriceData = new SessionLineItemPriceDataOptions
{
Currency = currency,
UnitAmountDecimal = amount, // Amount in cents
ProductData = new SessionLineItemPriceDataProductDataOptions
{
Name = $"Tip for {creator.Name}", // or any descriptive name for the tip
Metadata = new Dictionary<string, string> { { "creatorId", creator.Id.ToString() } }
}
},
Quantity = 1
}
],
Mode = "payment",
PaymentIntentData = new SessionPaymentIntentDataOptions
PriceData = new SessionLineItemPriceDataOptions
{
ApplicationFeeAmount = Convert.ToInt64(amount * stripeOptions.Value.HutopyRate), // Platform fee
TransferData = new SessionPaymentIntentDataTransferDataOptions
Currency = currency,
UnitAmountDecimal = amount, // Amount in cents
ProductData = new SessionLineItemPriceDataProductDataOptions
{
Destination = creator.StripeAccountId // Creator's Stripe account ID
Name = $"Tip for {creator.Name}",
Metadata = new Dictionary<string, string> { { "creatorId", creator.Id.ToString() } }
}
},
SuccessUrl = successUrl.ToString(), // Redirect after successful payment
CancelUrl = cancelUrl.ToString(), // Redirect after canceled payment
Metadata = new Dictionary<string, string>
{
{ "creatorId", creator.Id.ToString() }, { "creatorName", creator.Name }, { "message", message }
}
},
Quantity = 1
}
],
PaymentIntentData = new SessionPaymentIntentDataOptions { ApplicationFeeAmount = applicationFeeAmount },
Metadata = new Dictionary<string, string>
{
{ "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);