feat(stripe): change to direct charges
This commit is contained in:
@@ -21,57 +21,47 @@ 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(
|
ClientReferenceId = tipId.ToString(),
|
||||||
new SessionCreateOptions
|
Mode = "payment",
|
||||||
|
LineItems =
|
||||||
|
[
|
||||||
|
new SessionLineItemOptions
|
||||||
{
|
{
|
||||||
ClientReferenceId = tipId.ToString(),
|
PriceData = new SessionLineItemPriceDataOptions
|
||||||
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
|
|
||||||
{
|
{
|
||||||
ApplicationFeeAmount = Convert.ToInt64(amount * stripeOptions.Value.HutopyRate), // Platform fee
|
Currency = currency,
|
||||||
TransferData = new SessionPaymentIntentDataTransferDataOptions
|
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
|
Quantity = 1
|
||||||
CancelUrl = cancelUrl.ToString(), // Redirect after canceled payment
|
}
|
||||||
Metadata = new Dictionary<string, string>
|
],
|
||||||
{
|
PaymentIntentData = new SessionPaymentIntentDataOptions { ApplicationFeeAmount = applicationFeeAmount },
|
||||||
{ "creatorId", creator.Id.ToString() }, { "creatorName", creator.Name }, { "message", message }
|
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)
|
cancellationToken: ct)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user