From d5ca6b99724e93fd0a89213bee673355b81a4a42 Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Mon, 11 Aug 2025 12:24:00 -0400 Subject: [PATCH] feat(stripe): change to direct charges --- .../Stripe/Services/StripeTipProcessor.cs | 76 ++++++++----------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/backend/Infrastructure/Payments/Stripe/Services/StripeTipProcessor.cs b/backend/Infrastructure/Payments/Stripe/Services/StripeTipProcessor.cs index 735ea26..71b0f4a 100644 --- a/backend/Infrastructure/Payments/Stripe/Services/StripeTipProcessor.cs +++ b/backend/Infrastructure/Payments/Stripe/Services/StripeTipProcessor.cs @@ -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 { { "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 { { "creatorId", creator.Id.ToString() } } } }, - SuccessUrl = successUrl.ToString(), // Redirect after successful payment - CancelUrl = cancelUrl.ToString(), // Redirect after canceled payment - Metadata = new Dictionary - { - { "creatorId", creator.Id.ToString() }, { "creatorName", creator.Name }, { "message", message } - } - }, + 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);