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