1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-28 17:57:37 +01:00

process off session payment methods

This commit is contained in:
Kyle Spearrin 2019-08-09 14:06:07 -04:00
parent 3aea61cb63
commit 9686b4bf2b
2 changed files with 42 additions and 3 deletions

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Bit.Core; using Bit.Core;
using Stripe;
namespace Bit.Api.Controllers namespace Bit.Api.Controllers
{ {
@ -52,5 +53,19 @@ namespace Bit.Api.Controllers
var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayClientInvoice(_globalSettings)); var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayClientInvoice(_globalSettings));
return invoice.Url; 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;
}
} }
} }

View File

@ -292,6 +292,7 @@ namespace Bit.Core.Services
catch { } catch { }
} }
string stipeCustomerPaymentMethodId = null;
if(customer == null && !string.IsNullOrWhiteSpace(paymentToken)) if(customer == null && !string.IsNullOrWhiteSpace(paymentToken))
{ {
string stipeCustomerSourceToken = null; string stipeCustomerSourceToken = null;
@ -299,7 +300,14 @@ namespace Bit.Core.Services
if(stripePaymentMethod) if(stripePaymentMethod)
{ {
stipeCustomerSourceToken = paymentToken; if(paymentToken.StartsWith("pm_"))
{
stipeCustomerPaymentMethodId = paymentToken;
}
else
{
stipeCustomerSourceToken = paymentToken;
}
} }
else if(paymentMethodType == PaymentMethodType.PayPal) else if(paymentMethodType == PaymentMethodType.PayPal)
{ {
@ -332,8 +340,9 @@ namespace Bit.Core.Services
{ {
Description = user.Name, Description = user.Name,
Email = user.Email, Email = user.Email,
Source = stipeCustomerSourceToken, Metadata = stripeCustomerMetadata,
Metadata = stripeCustomerMetadata PaymentMethodId = stipeCustomerPaymentMethodId,
Source = stipeCustomerSourceToken
}); });
createdStripeCustomer = true; createdStripeCustomer = true;
} }
@ -346,6 +355,7 @@ namespace Bit.Core.Services
var subCreateOptions = new SubscriptionCreateOptions var subCreateOptions = new SubscriptionCreateOptions
{ {
CustomerId = customer.Id, CustomerId = customer.Id,
DefaultPaymentMethodId = stipeCustomerPaymentMethodId,
Items = new List<SubscriptionItemOption>(), Items = new List<SubscriptionItemOption>(),
Metadata = new Dictionary<string, string> 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(); var subscriptionService = new SubscriptionService();
subscription = await subscriptionService.CreateAsync(subCreateOptions); 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()) if(!stripePaymentMethod && subInvoiceMetadata.Any())
{ {