1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-22 12:04:27 -05:00

reuse btCustomerId when changing from pp => new pp

This commit is contained in:
Kyle Spearrin 2019-02-02 16:42:40 -05:00
parent 9cb7a0caaf
commit 0e4e3b22d1

View File

@ -721,25 +721,53 @@ namespace Bit.Core.Services
} }
} }
if(stripeCustomerMetadata.ContainsKey("btCustomerId")) var hadBtCustomer = stripeCustomerMetadata.ContainsKey("btCustomerId");
{
var nowSec = Utilities.CoreHelpers.ToEpocSeconds(DateTime.UtcNow);
stripeCustomerMetadata.Add($"btCustomerId_{nowSec}", stripeCustomerMetadata["btCustomerId"]);
stripeCustomerMetadata["btCustomerId"] = null;
}
if(stripePaymentMethod) if(stripePaymentMethod)
{ {
stipeCustomerSourceToken = paymentToken; stipeCustomerSourceToken = paymentToken;
} }
else if(paymentMethodType == PaymentMethodType.PayPal) else if(paymentMethodType == PaymentMethodType.PayPal)
{ {
var randomSuffix = Utilities.CoreHelpers.RandomString(3, upper: false, numeric: false); if(hadBtCustomer)
{
var pmResult = await _btGateway.PaymentMethod.CreateAsync(new Braintree.PaymentMethodRequest
{
CustomerId = stripeCustomerMetadata["btCustomerId"],
PaymentMethodNonce = paymentToken
});
if(pmResult.IsSuccess())
{
var customerResult = await _btGateway.Customer.UpdateAsync(
stripeCustomerMetadata["btCustomerId"], new Braintree.CustomerRequest
{
DefaultPaymentMethodToken = pmResult.Target.Token
});
if(customerResult.IsSuccess() && customerResult.Target.PaymentMethods.Length > 0)
{
braintreeCustomer = customerResult.Target;
}
else
{
await _btGateway.PaymentMethod.DeleteAsync(pmResult.Target.Token);
hadBtCustomer = false;
}
}
else
{
hadBtCustomer = false;
}
}
if(!hadBtCustomer)
{
var customerResult = await _btGateway.Customer.CreateAsync(new Braintree.CustomerRequest var customerResult = await _btGateway.Customer.CreateAsync(new Braintree.CustomerRequest
{ {
PaymentMethodNonce = paymentToken, PaymentMethodNonce = paymentToken,
Email = subscriber.BillingEmailAddress(), Email = subscriber.BillingEmailAddress(),
Id = subscriber.BraintreeCustomerIdPrefix() + subscriber.Id.ToString("N").ToLower() + randomSuffix Id = subscriber.BraintreeCustomerIdPrefix() + subscriber.Id.ToString("N").ToLower() +
Utilities.CoreHelpers.RandomString(3, upper: false, numeric: false)
}); });
if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0) if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0)
@ -748,13 +776,6 @@ namespace Bit.Core.Services
} }
braintreeCustomer = customerResult.Target; braintreeCustomer = customerResult.Target;
if(stripeCustomerMetadata.ContainsKey("btCustomerId"))
{
stripeCustomerMetadata["btCustomerId"] = braintreeCustomer.Id;
}
else
{
stripeCustomerMetadata.Add("btCustomerId", braintreeCustomer.Id);
} }
} }
else else
@ -762,6 +783,20 @@ namespace Bit.Core.Services
throw new GatewayException("Payment method is not supported at this time."); throw new GatewayException("Payment method is not supported at this time.");
} }
if(stripeCustomerMetadata.ContainsKey("btCustomerId"))
{
if(braintreeCustomer?.Id != stripeCustomerMetadata["btCustomerId"])
{
var nowSec = Utilities.CoreHelpers.ToEpocSeconds(DateTime.UtcNow);
stripeCustomerMetadata.Add($"btCustomerId_{nowSec}", stripeCustomerMetadata["btCustomerId"]);
}
stripeCustomerMetadata["btCustomerId"] = braintreeCustomer?.Id;
}
else if(!string.IsNullOrWhiteSpace(braintreeCustomer?.Id))
{
stripeCustomerMetadata.Add("btCustomerId", braintreeCustomer.Id);
}
try try
{ {
if(customer == null) if(customer == null)
@ -823,7 +858,7 @@ namespace Bit.Core.Services
} }
catch(Exception e) catch(Exception e)
{ {
if(braintreeCustomer != null) if(braintreeCustomer != null && !hadBtCustomer)
{ {
await _btGateway.Customer.DeleteAsync(braintreeCustomer.Id); await _btGateway.Customer.DeleteAsync(braintreeCustomer.Id);
} }