mirror of
https://github.com/bitwarden/server.git
synced 2025-04-18 19:48:12 -05:00
update payment method with stripe card payment method
This commit is contained in:
parent
b11fd2fab8
commit
efcf626999
@ -960,6 +960,7 @@ namespace Bit.Core.Services
|
|||||||
var createdCustomer = false;
|
var createdCustomer = false;
|
||||||
Braintree.Customer braintreeCustomer = null;
|
Braintree.Customer braintreeCustomer = null;
|
||||||
string stipeCustomerSourceToken = null;
|
string stipeCustomerSourceToken = null;
|
||||||
|
string stipeCustomerPaymentMethodId = null;
|
||||||
var stripeCustomerMetadata = new Dictionary<string, string>();
|
var stripeCustomerMetadata = new Dictionary<string, string>();
|
||||||
var stripePaymentMethod = paymentMethodType == PaymentMethodType.Card ||
|
var stripePaymentMethod = paymentMethodType == PaymentMethodType.Card ||
|
||||||
paymentMethodType == PaymentMethodType.BankAccount;
|
paymentMethodType == PaymentMethodType.BankAccount;
|
||||||
@ -967,6 +968,7 @@ namespace Bit.Core.Services
|
|||||||
var cardService = new CardService();
|
var cardService = new CardService();
|
||||||
var bankSerice = new BankAccountService();
|
var bankSerice = new BankAccountService();
|
||||||
var customerService = new CustomerService();
|
var customerService = new CustomerService();
|
||||||
|
var paymentMethodService = new PaymentMethodService();
|
||||||
Customer customer = null;
|
Customer customer = null;
|
||||||
|
|
||||||
if(!string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
|
if(!string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
|
||||||
@ -981,7 +983,14 @@ namespace Bit.Core.Services
|
|||||||
var hadBtCustomer = stripeCustomerMetadata.ContainsKey("btCustomerId");
|
var hadBtCustomer = stripeCustomerMetadata.ContainsKey("btCustomerId");
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -1066,8 +1075,9 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
Description = subscriber.BillingName(),
|
Description = subscriber.BillingName(),
|
||||||
Email = subscriber.BillingEmailAddress(),
|
Email = subscriber.BillingEmailAddress(),
|
||||||
|
Metadata = stripeCustomerMetadata,
|
||||||
Source = stipeCustomerSourceToken,
|
Source = stipeCustomerSourceToken,
|
||||||
Metadata = stripeCustomerMetadata
|
PaymentMethodId = stipeCustomerPaymentMethodId,
|
||||||
});
|
});
|
||||||
|
|
||||||
subscriber.Gateway = GatewayType.Stripe;
|
subscriber.Gateway = GatewayType.Stripe;
|
||||||
@ -1078,9 +1088,10 @@ namespace Bit.Core.Services
|
|||||||
if(!createdCustomer)
|
if(!createdCustomer)
|
||||||
{
|
{
|
||||||
string defaultSourceId = null;
|
string defaultSourceId = null;
|
||||||
|
string defaultPaymentMethodId = null;
|
||||||
if(stripePaymentMethod)
|
if(stripePaymentMethod)
|
||||||
{
|
{
|
||||||
if(paymentToken.StartsWith("btok_"))
|
if(!string.IsNullOrWhiteSpace(stipeCustomerSourceToken) && paymentToken.StartsWith("btok_"))
|
||||||
{
|
{
|
||||||
var bankAccount = await bankSerice.CreateAsync(customer.Id, new BankAccountCreateOptions
|
var bankAccount = await bankSerice.CreateAsync(customer.Id, new BankAccountCreateOptions
|
||||||
{
|
{
|
||||||
@ -1088,13 +1099,11 @@ namespace Bit.Core.Services
|
|||||||
});
|
});
|
||||||
defaultSourceId = bankAccount.Id;
|
defaultSourceId = bankAccount.Id;
|
||||||
}
|
}
|
||||||
else
|
else if(!string.IsNullOrWhiteSpace(stipeCustomerPaymentMethodId))
|
||||||
{
|
{
|
||||||
var card = await cardService.CreateAsync(customer.Id, new CardCreateOptions
|
await paymentMethodService.AttachAsync(stipeCustomerPaymentMethodId,
|
||||||
{
|
new PaymentMethodAttachOptions { CustomerId = customer.Id });
|
||||||
Source = paymentToken,
|
defaultPaymentMethodId = stipeCustomerPaymentMethodId;
|
||||||
});
|
|
||||||
defaultSourceId = card.Id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1110,6 +1119,16 @@ namespace Bit.Core.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cardPaymentMethods = paymentMethodService.ListAutoPaging(new PaymentMethodListOptions
|
||||||
|
{
|
||||||
|
CustomerId = customer.Id,
|
||||||
|
Type = "card"
|
||||||
|
});
|
||||||
|
foreach(var cardMethod in cardPaymentMethods.Where(m => m.Id != defaultPaymentMethodId))
|
||||||
|
{
|
||||||
|
await paymentMethodService.DetachAsync(cardMethod.Id, new PaymentMethodDetachOptions());
|
||||||
|
}
|
||||||
|
|
||||||
customer = await customerService.UpdateAsync(customer.Id, new CustomerUpdateOptions
|
customer = await customerService.UpdateAsync(customer.Id, new CustomerUpdateOptions
|
||||||
{
|
{
|
||||||
Metadata = stripeCustomerMetadata,
|
Metadata = stripeCustomerMetadata,
|
||||||
@ -1219,10 +1238,9 @@ namespace Bit.Core.Services
|
|||||||
if(billingInfo.PaymentSource == null)
|
if(billingInfo.PaymentSource == null)
|
||||||
{
|
{
|
||||||
var paymentMethodService = new PaymentMethodService();
|
var paymentMethodService = new PaymentMethodService();
|
||||||
var paymentMethods = paymentMethodService.ListAutoPaging(
|
var cardPaymentMethods = paymentMethodService.ListAutoPaging(
|
||||||
new PaymentMethodListOptions { CustomerId = customer.Id, Type = "card" });
|
new PaymentMethodListOptions { CustomerId = customer.Id, Type = "card" });
|
||||||
var paymentMethod = paymentMethods.Where(m => m.Card != null)
|
var paymentMethod = cardPaymentMethods.OrderByDescending(m => m.Created).FirstOrDefault();
|
||||||
.OrderByDescending(m => m.Created).FirstOrDefault();
|
|
||||||
if(paymentMethod != null)
|
if(paymentMethod != null)
|
||||||
{
|
{
|
||||||
billingInfo.PaymentSource = new BillingInfo.BillingSource(paymentMethod);
|
billingInfo.PaymentSource = new BillingInfo.BillingSource(paymentMethod);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user