1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-17 17:45:36 -05:00

custom id fields for paypal

This commit is contained in:
Kyle Spearrin 2019-02-01 09:18:34 -05:00
parent 11f353050f
commit 9882815e4a
5 changed files with 49 additions and 7 deletions

View File

@ -13,6 +13,8 @@ namespace Bit.Core.Models.Table
string BillingEmailAddress(); string BillingEmailAddress();
string BillingName(); string BillingName();
string BraintreeCustomerIdPrefix(); string BraintreeCustomerIdPrefix();
string BraintreeIdField();
string GatewayIdField();
IPaymentService GetPaymentService(GlobalSettings globalSettings); IPaymentService GetPaymentService(GlobalSettings globalSettings);
} }
} }

View File

@ -68,6 +68,16 @@ namespace Bit.Core.Models.Table
return "o"; return "o";
} }
public string BraintreeIdField()
{
return "organization_id";
}
public string GatewayIdField()
{
return "organizationId";
}
public long StorageBytesRemaining() public long StorageBytesRemaining()
{ {
if(!MaxStorageGb.HasValue) if(!MaxStorageGb.HasValue)

View File

@ -63,6 +63,16 @@ namespace Bit.Core.Models.Table
return "u"; return "u";
} }
public string BraintreeIdField()
{
return "user_id";
}
public string GatewayIdField()
{
return "userId";
}
public Dictionary<TwoFactorProviderType, TwoFactorProvider> GetTwoFactorProviders() public Dictionary<TwoFactorProviderType, TwoFactorProvider> GetTwoFactorProviders()
{ {
if(string.IsNullOrWhiteSpace(TwoFactorProviders)) if(string.IsNullOrWhiteSpace(TwoFactorProviders))

View File

@ -230,7 +230,7 @@ namespace Bit.Core.Services
throw new GatewayException("Failed to create customer."); throw new GatewayException("Failed to create customer.");
} }
var subId = "u" + user.Id.ToString("N").ToLower() + var subId = user.BraintreeCustomerIdPrefix() + user.Id.ToString("N").ToLower() +
Utilities.CoreHelpers.RandomString(3, upper: false, numeric: false); Utilities.CoreHelpers.RandomString(3, upper: false, numeric: false);
var subRequest = new SubscriptionRequest var subRequest = new SubscriptionRequest

View File

@ -54,7 +54,7 @@ namespace Bit.Core.Services
{ {
PaymentMethodNonce = paymentToken, PaymentMethodNonce = paymentToken,
Email = org.BillingEmail, Email = org.BillingEmail,
Id = "o" + org.Id.ToString("N").ToLower() + randomSuffix Id = org.BraintreeCustomerIdPrefix() + org.Id.ToString("N").ToLower() + randomSuffix
}); });
if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0) if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0)
@ -76,7 +76,7 @@ namespace Bit.Core.Services
Items = new List<SubscriptionItemOption>(), Items = new List<SubscriptionItemOption>(),
Metadata = new Dictionary<string, string> Metadata = new Dictionary<string, string>
{ {
["organizationId"] = org.Id.ToString() [org.GatewayIdField()] = org.Id.ToString()
} }
}; };
@ -174,7 +174,7 @@ namespace Bit.Core.Services
{ {
PaymentMethodNonce = paymentToken, PaymentMethodNonce = paymentToken,
Email = user.Email, Email = user.Email,
Id = "u" + user.Id.ToString("N").ToLower() + randomSuffix Id = user.BraintreeCustomerIdPrefix() + user.Id.ToString("N").ToLower() + randomSuffix
}); });
if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0) if(!customerResult.IsSuccess() || customerResult.Target.PaymentMethods.Length == 0)
@ -204,7 +204,7 @@ namespace Bit.Core.Services
Items = new List<SubscriptionItemOption>(), Items = new List<SubscriptionItemOption>(),
Metadata = new Dictionary<string, string> Metadata = new Dictionary<string, string>
{ {
["userId"] = user.Id.ToString() [user.GatewayIdField()] = user.Id.ToString()
} }
}; };
@ -248,7 +248,18 @@ namespace Bit.Core.Services
{ {
Amount = btInvoiceAmount, Amount = btInvoiceAmount,
CustomerId = braintreeCustomer.Id, CustomerId = braintreeCustomer.Id,
Options = new Braintree.TransactionOptionsRequest { SubmitForSettlement = true } Options = new Braintree.TransactionOptionsRequest
{
SubmitForSettlement = true,
PayPal = new Braintree.TransactionOptionsPayPalRequest
{
CustomField = $"{user.BraintreeIdField()}:{user.Id}"
}
},
CustomFields = new Dictionary<string, string>
{
[user.BraintreeIdField()] = user.Id.ToString()
}
}); });
if(!transactionResult.IsSuccess()) if(!transactionResult.IsSuccess())
@ -256,6 +267,7 @@ namespace Bit.Core.Services
throw new GatewayException("Failed to charge PayPal customer."); throw new GatewayException("Failed to charge PayPal customer.");
} }
braintreeTransaction = transactionResult.Target;
subInvoiceMetadata.Add("btTransactionId", braintreeTransaction.Id); subInvoiceMetadata.Add("btTransactionId", braintreeTransaction.Id);
subInvoiceMetadata.Add("btPayPalTransactionId", subInvoiceMetadata.Add("btPayPalTransactionId",
braintreeTransaction.PayPalDetails.AuthorizationId); braintreeTransaction.PayPalDetails.AuthorizationId);
@ -525,7 +537,15 @@ namespace Bit.Core.Services
CustomerId = customer.Metadata["btCustomerId"], CustomerId = customer.Metadata["btCustomerId"],
Options = new Braintree.TransactionOptionsRequest Options = new Braintree.TransactionOptionsRequest
{ {
SubmitForSettlement = true SubmitForSettlement = true,
PayPal = new Braintree.TransactionOptionsPayPalRequest
{
CustomField = $"{subscriber.BraintreeIdField()}:{subscriber.Id}"
}
},
CustomFields = new Dictionary<string, string>
{
[subscriber.BraintreeIdField()] = subscriber.Id.ToString()
} }
}); });