mirror of
https://github.com/bitwarden/server.git
synced 2024-12-27 17:47:37 +01:00
process off session payment methods
This commit is contained in:
parent
3aea61cb63
commit
9686b4bf2b
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Core;
|
||||
using Stripe;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
{
|
||||
@ -52,5 +53,19 @@ namespace Bit.Api.Controllers
|
||||
var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayClientInvoice(_globalSettings));
|
||||
return invoice.Url;
|
||||
}
|
||||
|
||||
[Authorize("Application")]
|
||||
[HttpPost("~/setup-payment")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<string> PostSetupPayment()
|
||||
{
|
||||
var options = new SetupIntentCreateOptions
|
||||
{
|
||||
Usage = "off_session"
|
||||
};
|
||||
var service = new SetupIntentService();
|
||||
var setupIntent = await service.CreateAsync(options);
|
||||
return setupIntent.ClientSecret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -292,6 +292,7 @@ namespace Bit.Core.Services
|
||||
catch { }
|
||||
}
|
||||
|
||||
string stipeCustomerPaymentMethodId = null;
|
||||
if(customer == null && !string.IsNullOrWhiteSpace(paymentToken))
|
||||
{
|
||||
string stipeCustomerSourceToken = null;
|
||||
@ -299,7 +300,14 @@ namespace Bit.Core.Services
|
||||
|
||||
if(stripePaymentMethod)
|
||||
{
|
||||
stipeCustomerSourceToken = paymentToken;
|
||||
if(paymentToken.StartsWith("pm_"))
|
||||
{
|
||||
stipeCustomerPaymentMethodId = paymentToken;
|
||||
}
|
||||
else
|
||||
{
|
||||
stipeCustomerSourceToken = paymentToken;
|
||||
}
|
||||
}
|
||||
else if(paymentMethodType == PaymentMethodType.PayPal)
|
||||
{
|
||||
@ -332,8 +340,9 @@ namespace Bit.Core.Services
|
||||
{
|
||||
Description = user.Name,
|
||||
Email = user.Email,
|
||||
Source = stipeCustomerSourceToken,
|
||||
Metadata = stripeCustomerMetadata
|
||||
Metadata = stripeCustomerMetadata,
|
||||
PaymentMethodId = stipeCustomerPaymentMethodId,
|
||||
Source = stipeCustomerSourceToken
|
||||
});
|
||||
createdStripeCustomer = true;
|
||||
}
|
||||
@ -346,6 +355,7 @@ namespace Bit.Core.Services
|
||||
var subCreateOptions = new SubscriptionCreateOptions
|
||||
{
|
||||
CustomerId = customer.Id,
|
||||
DefaultPaymentMethodId = stipeCustomerPaymentMethodId,
|
||||
Items = new List<SubscriptionItemOption>(),
|
||||
Metadata = new Dictionary<string, string>
|
||||
{
|
||||
@ -460,8 +470,22 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
subCreateOptions.OffSession = true;
|
||||
subCreateOptions.AddExpand("latest_invoice.payment_intent");
|
||||
var subscriptionService = new SubscriptionService();
|
||||
subscription = await subscriptionService.CreateAsync(subCreateOptions);
|
||||
if(subscription.Status == "incomplete" && subscription.LatestInvoice?.PaymentIntent != null)
|
||||
{
|
||||
if(subscription.LatestInvoice.PaymentIntent.Status == "requires_payment_method")
|
||||
{
|
||||
await subscriptionService.CancelAsync(subscription.Id, new SubscriptionCancelOptions());
|
||||
throw new GatewayException("Payment method failed.");
|
||||
}
|
||||
else if(subscription.LatestInvoice.PaymentIntent.Status == "requires_action")
|
||||
{
|
||||
// Needs SCA. Send email? Should be handled by Stripe.
|
||||
}
|
||||
}
|
||||
|
||||
if(!stripePaymentMethod && subInvoiceMetadata.Any())
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user