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:
parent
9cb7a0caaf
commit
0e4e3b22d1
@ -721,40 +721,61 @@ 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 customerResult = await _btGateway.Customer.CreateAsync(new Braintree.CustomerRequest
|
|
||||||
{
|
{
|
||||||
PaymentMethodNonce = paymentToken,
|
var pmResult = await _btGateway.PaymentMethod.CreateAsync(new Braintree.PaymentMethodRequest
|
||||||
Email = subscriber.BillingEmailAddress(),
|
{
|
||||||
Id = subscriber.BraintreeCustomerIdPrefix() + subscriber.Id.ToString("N").ToLower() + randomSuffix
|
CustomerId = stripeCustomerMetadata["btCustomerId"],
|
||||||
});
|
PaymentMethodNonce = paymentToken
|
||||||
|
});
|
||||||
|
|
||||||
if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0)
|
if(pmResult.IsSuccess())
|
||||||
{
|
{
|
||||||
throw new GatewayException("Failed to create PayPal customer record.");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
braintreeCustomer = customerResult.Target;
|
if(!hadBtCustomer)
|
||||||
if(stripeCustomerMetadata.ContainsKey("btCustomerId"))
|
|
||||||
{
|
{
|
||||||
stripeCustomerMetadata["btCustomerId"] = braintreeCustomer.Id;
|
var customerResult = await _btGateway.Customer.CreateAsync(new Braintree.CustomerRequest
|
||||||
}
|
{
|
||||||
else
|
PaymentMethodNonce = paymentToken,
|
||||||
{
|
Email = subscriber.BillingEmailAddress(),
|
||||||
stripeCustomerMetadata.Add("btCustomerId", braintreeCustomer.Id);
|
Id = subscriber.BraintreeCustomerIdPrefix() + subscriber.Id.ToString("N").ToLower() +
|
||||||
|
Utilities.CoreHelpers.RandomString(3, upper: false, numeric: false)
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0)
|
||||||
|
{
|
||||||
|
throw new GatewayException("Failed to create PayPal customer record.");
|
||||||
|
}
|
||||||
|
|
||||||
|
braintreeCustomer = customerResult.Target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user