From 9882815e4abef1eda79dfa5f6b5713880ce65511 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 1 Feb 2019 09:18:34 -0500 Subject: [PATCH] custom id fields for paypal --- src/Core/Models/Table/ISubscriber.cs | 2 ++ src/Core/Models/Table/Organization.cs | 10 ++++++ src/Core/Models/Table/User.cs | 10 ++++++ .../BraintreePaymentService.cs | 2 +- .../Implementations/StripePaymentService.cs | 32 +++++++++++++++---- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/Core/Models/Table/ISubscriber.cs b/src/Core/Models/Table/ISubscriber.cs index 9b2ffdbdbc..302025881d 100644 --- a/src/Core/Models/Table/ISubscriber.cs +++ b/src/Core/Models/Table/ISubscriber.cs @@ -13,6 +13,8 @@ namespace Bit.Core.Models.Table string BillingEmailAddress(); string BillingName(); string BraintreeCustomerIdPrefix(); + string BraintreeIdField(); + string GatewayIdField(); IPaymentService GetPaymentService(GlobalSettings globalSettings); } } diff --git a/src/Core/Models/Table/Organization.cs b/src/Core/Models/Table/Organization.cs index 09d56ac4a1..ed9a0dad7a 100644 --- a/src/Core/Models/Table/Organization.cs +++ b/src/Core/Models/Table/Organization.cs @@ -68,6 +68,16 @@ namespace Bit.Core.Models.Table return "o"; } + public string BraintreeIdField() + { + return "organization_id"; + } + + public string GatewayIdField() + { + return "organizationId"; + } + public long StorageBytesRemaining() { if(!MaxStorageGb.HasValue) diff --git a/src/Core/Models/Table/User.cs b/src/Core/Models/Table/User.cs index 2850254c06..2c99e89a93 100644 --- a/src/Core/Models/Table/User.cs +++ b/src/Core/Models/Table/User.cs @@ -63,6 +63,16 @@ namespace Bit.Core.Models.Table return "u"; } + public string BraintreeIdField() + { + return "user_id"; + } + + public string GatewayIdField() + { + return "userId"; + } + public Dictionary GetTwoFactorProviders() { if(string.IsNullOrWhiteSpace(TwoFactorProviders)) diff --git a/src/Core/Services/Implementations/BraintreePaymentService.cs b/src/Core/Services/Implementations/BraintreePaymentService.cs index 96d5de8b68..344c7db1de 100644 --- a/src/Core/Services/Implementations/BraintreePaymentService.cs +++ b/src/Core/Services/Implementations/BraintreePaymentService.cs @@ -230,7 +230,7 @@ namespace Bit.Core.Services 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); var subRequest = new SubscriptionRequest diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index 18d4fb280c..a51ca29d4d 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -54,7 +54,7 @@ namespace Bit.Core.Services { PaymentMethodNonce = paymentToken, 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) @@ -76,7 +76,7 @@ namespace Bit.Core.Services Items = new List(), Metadata = new Dictionary { - ["organizationId"] = org.Id.ToString() + [org.GatewayIdField()] = org.Id.ToString() } }; @@ -174,7 +174,7 @@ namespace Bit.Core.Services { PaymentMethodNonce = paymentToken, 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) @@ -204,7 +204,7 @@ namespace Bit.Core.Services Items = new List(), Metadata = new Dictionary { - ["userId"] = user.Id.ToString() + [user.GatewayIdField()] = user.Id.ToString() } }; @@ -248,7 +248,18 @@ namespace Bit.Core.Services { Amount = btInvoiceAmount, 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 + { + [user.BraintreeIdField()] = user.Id.ToString() + } }); if(!transactionResult.IsSuccess()) @@ -256,6 +267,7 @@ namespace Bit.Core.Services throw new GatewayException("Failed to charge PayPal customer."); } + braintreeTransaction = transactionResult.Target; subInvoiceMetadata.Add("btTransactionId", braintreeTransaction.Id); subInvoiceMetadata.Add("btPayPalTransactionId", braintreeTransaction.PayPalDetails.AuthorizationId); @@ -525,7 +537,15 @@ namespace Bit.Core.Services CustomerId = customer.Metadata["btCustomerId"], Options = new Braintree.TransactionOptionsRequest { - SubmitForSettlement = true + SubmitForSettlement = true, + PayPal = new Braintree.TransactionOptionsPayPalRequest + { + CustomField = $"{subscriber.BraintreeIdField()}:{subscriber.Id}" + } + }, + CustomFields = new Dictionary + { + [subscriber.BraintreeIdField()] = subscriber.Id.ToString() } });