2023-11-29 00:18:08 +01:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2024-06-14 21:34:47 +02:00
|
|
|
|
using Bit.Core.Billing.Enums;
|
2022-01-03 15:44:28 +01:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Models.Business;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Bit.Core.Services;
|
2023-07-20 23:00:40 +02:00
|
|
|
|
using Bit.Core.Settings;
|
2022-01-03 15:44:28 +01:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
2021-09-27 23:01:13 +02:00
|
|
|
|
using Braintree;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
2023-04-07 11:45:18 +02:00
|
|
|
|
using Customer = Braintree.Customer;
|
|
|
|
|
using PaymentMethod = Braintree.PaymentMethod;
|
2022-01-03 15:44:28 +01:00
|
|
|
|
using PaymentMethodType = Bit.Core.Enums.PaymentMethodType;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
namespace Bit.Core.Test.Services;
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[SutProviderCustomize]
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public class StripePaymentServiceTests
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[BitAutoData(PaymentMethodType.BitPay)]
|
|
|
|
|
[BitAutoData(PaymentMethodType.BitPay)]
|
|
|
|
|
[BitAutoData(PaymentMethodType.Credit)]
|
|
|
|
|
[BitAutoData(PaymentMethodType.WireTransfer)]
|
|
|
|
|
[BitAutoData(PaymentMethodType.Check)]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Invalid(PaymentMethodType paymentMethodType, SutProvider<StripePaymentService> sutProvider)
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<GatewayException>(
|
2023-07-25 00:05:05 +02:00
|
|
|
|
() => sutProvider.Sut.PurchaseOrganizationAsync(null, paymentMethodType, null, null, 0, 0, false, null, false, -1, -1));
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.Equal("Payment method is not supported at this time.", exception.Message);
|
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2023-04-10 15:40:04 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Stripe_ProviderOrg_Coupon_Add(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo, bool provider = true)
|
2023-04-10 15:40:04 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2023-04-10 15:40:04 +02:00
|
|
|
|
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
});
|
2023-07-20 23:00:40 +02:00
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>()
|
|
|
|
|
.BaseServiceUri.CloudRegion
|
|
|
|
|
.Returns("US");
|
2023-04-10 15:40:04 +02:00
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan, 0, 0, false, taxInfo, provider);
|
2023-04-10 15:40:04 +02:00
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
Assert.Equal(GatewayType.Stripe, organization.Gateway);
|
|
|
|
|
Assert.Equal("C-1", organization.GatewayCustomerId);
|
|
|
|
|
Assert.Equal("S-1", organization.GatewaySubscriptionId);
|
|
|
|
|
Assert.True(organization.Enabled);
|
|
|
|
|
Assert.Equal(DateTime.Today.AddDays(10), organization.ExpirationDate);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().CustomerCreateAsync(Arg.Is<Stripe.CustomerCreateOptions>(c =>
|
|
|
|
|
c.Description == organization.BusinessName &&
|
|
|
|
|
c.Email == organization.BillingEmail &&
|
|
|
|
|
c.Source == paymentToken &&
|
|
|
|
|
c.PaymentMethod == null &&
|
|
|
|
|
c.Coupon == "msp-discount-35" &&
|
2023-07-20 23:00:40 +02:00
|
|
|
|
c.Metadata.Count == 1 &&
|
|
|
|
|
c.Metadata["region"] == "US" &&
|
2023-04-10 15:40:04 +02:00
|
|
|
|
c.InvoiceSettings.DefaultPaymentMethod == null &&
|
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState &&
|
|
|
|
|
c.TaxIdData == null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().SubscriptionCreateAsync(Arg.Is<Stripe.SubscriptionCreateOptions>(s =>
|
|
|
|
|
s.Customer == "C-1" &&
|
|
|
|
|
s.Expand[0] == "latest_invoice.payment_intent" &&
|
|
|
|
|
s.Metadata[organization.GatewayIdField()] == organization.Id.ToString() &&
|
|
|
|
|
s.Items.Count == 0
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 00:05:05 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_SM_Stripe_ProviderOrg_Coupon_Add(SutProvider<StripePaymentService> sutProvider, Organization organization,
|
2023-07-25 00:05:05 +02:00
|
|
|
|
string paymentToken, TaxInfo taxInfo, bool provider = true)
|
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
|
|
|
|
organization.UseSecretsManager = true;
|
2023-07-25 00:05:05 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>()
|
|
|
|
|
.BaseServiceUri.CloudRegion
|
|
|
|
|
.Returns("US");
|
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan, 1, 1,
|
2023-07-25 00:05:05 +02:00
|
|
|
|
false, taxInfo, provider, 1, 1);
|
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
Assert.Equal(GatewayType.Stripe, organization.Gateway);
|
|
|
|
|
Assert.Equal("C-1", organization.GatewayCustomerId);
|
|
|
|
|
Assert.Equal("S-1", organization.GatewaySubscriptionId);
|
|
|
|
|
Assert.True(organization.Enabled);
|
|
|
|
|
Assert.Equal(DateTime.Today.AddDays(10), organization.ExpirationDate);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().CustomerCreateAsync(Arg.Is<Stripe.CustomerCreateOptions>(c =>
|
|
|
|
|
c.Description == organization.BusinessName &&
|
|
|
|
|
c.Email == organization.BillingEmail &&
|
|
|
|
|
c.Source == paymentToken &&
|
|
|
|
|
c.PaymentMethod == null &&
|
|
|
|
|
c.Coupon == "msp-discount-35" &&
|
|
|
|
|
c.Metadata.Count == 1 &&
|
|
|
|
|
c.Metadata["region"] == "US" &&
|
|
|
|
|
c.InvoiceSettings.DefaultPaymentMethod == null &&
|
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState &&
|
|
|
|
|
c.TaxIdData == null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().SubscriptionCreateAsync(Arg.Is<Stripe.SubscriptionCreateOptions>(s =>
|
|
|
|
|
s.Customer == "C-1" &&
|
|
|
|
|
s.Expand[0] == "latest_invoice.payment_intent" &&
|
|
|
|
|
s.Metadata[organization.GatewayIdField()] == organization.Id.ToString() &&
|
|
|
|
|
s.Items.Count == 4
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Stripe(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
|
|
|
|
organization.UseSecretsManager = true;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
});
|
2023-07-20 23:00:40 +02:00
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>()
|
|
|
|
|
.BaseServiceUri.CloudRegion
|
|
|
|
|
.Returns("US");
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan, 0, 0
|
2023-07-25 00:05:05 +02:00
|
|
|
|
, false, taxInfo, false, 8, 10);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
Assert.Equal(GatewayType.Stripe, organization.Gateway);
|
|
|
|
|
Assert.Equal("C-1", organization.GatewayCustomerId);
|
|
|
|
|
Assert.Equal("S-1", organization.GatewaySubscriptionId);
|
|
|
|
|
Assert.True(organization.Enabled);
|
|
|
|
|
Assert.Equal(DateTime.Today.AddDays(10), organization.ExpirationDate);
|
|
|
|
|
await stripeAdapter.Received().CustomerCreateAsync(Arg.Is<Stripe.CustomerCreateOptions>(c =>
|
|
|
|
|
c.Description == organization.BusinessName &&
|
|
|
|
|
c.Email == organization.BillingEmail &&
|
|
|
|
|
c.Source == paymentToken &&
|
|
|
|
|
c.PaymentMethod == null &&
|
2023-07-20 23:00:40 +02:00
|
|
|
|
c.Metadata.Count == 1 &&
|
|
|
|
|
c.Metadata["region"] == "US" &&
|
2022-08-29 22:06:55 +02:00
|
|
|
|
c.InvoiceSettings.DefaultPaymentMethod == null &&
|
2023-04-07 11:45:18 +02:00
|
|
|
|
c.InvoiceSettings.CustomFields != null &&
|
|
|
|
|
c.InvoiceSettings.CustomFields[0].Name == "Organization" &&
|
2023-05-19 19:45:47 +02:00
|
|
|
|
c.InvoiceSettings.CustomFields[0].Value == organization.SubscriberName().Substring(0, 30) &&
|
2022-08-29 22:06:55 +02:00
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState &&
|
|
|
|
|
c.TaxIdData == null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().SubscriptionCreateAsync(Arg.Is<Stripe.SubscriptionCreateOptions>(s =>
|
|
|
|
|
s.Customer == "C-1" &&
|
|
|
|
|
s.Expand[0] == "latest_invoice.payment_intent" &&
|
|
|
|
|
s.Metadata[organization.GatewayIdField()] == organization.Id.ToString() &&
|
2023-07-25 00:05:05 +02:00
|
|
|
|
s.Items.Count == 2
|
2022-08-29 22:06:55 +02:00
|
|
|
|
));
|
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Stripe_PM(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
paymentToken = "pm_" + paymentToken;
|
|
|
|
|
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
});
|
2023-07-20 23:00:40 +02:00
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>()
|
|
|
|
|
.BaseServiceUri.CloudRegion
|
|
|
|
|
.Returns("US");
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan, 0, 0, false, taxInfo);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
Assert.Equal(GatewayType.Stripe, organization.Gateway);
|
|
|
|
|
Assert.Equal("C-1", organization.GatewayCustomerId);
|
|
|
|
|
Assert.Equal("S-1", organization.GatewaySubscriptionId);
|
|
|
|
|
Assert.True(organization.Enabled);
|
|
|
|
|
Assert.Equal(DateTime.Today.AddDays(10), organization.ExpirationDate);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().CustomerCreateAsync(Arg.Is<Stripe.CustomerCreateOptions>(c =>
|
|
|
|
|
c.Description == organization.BusinessName &&
|
|
|
|
|
c.Email == organization.BillingEmail &&
|
|
|
|
|
c.Source == null &&
|
|
|
|
|
c.PaymentMethod == paymentToken &&
|
2023-07-20 23:00:40 +02:00
|
|
|
|
c.Metadata.Count == 1 &&
|
|
|
|
|
c.Metadata["region"] == "US" &&
|
2022-08-29 22:06:55 +02:00
|
|
|
|
c.InvoiceSettings.DefaultPaymentMethod == paymentToken &&
|
2023-04-07 11:45:18 +02:00
|
|
|
|
c.InvoiceSettings.CustomFields != null &&
|
|
|
|
|
c.InvoiceSettings.CustomFields[0].Name == "Organization" &&
|
2023-05-19 19:45:47 +02:00
|
|
|
|
c.InvoiceSettings.CustomFields[0].Value == organization.SubscriberName().Substring(0, 30) &&
|
2022-08-29 22:06:55 +02:00
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState &&
|
|
|
|
|
c.TaxIdData == null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().SubscriptionCreateAsync(Arg.Is<Stripe.SubscriptionCreateOptions>(s =>
|
|
|
|
|
s.Customer == "C-1" &&
|
|
|
|
|
s.Expand[0] == "latest_invoice.payment_intent" &&
|
|
|
|
|
s.Metadata[organization.GatewayIdField()] == organization.Id.ToString() &&
|
|
|
|
|
s.Items.Count == 0
|
|
|
|
|
));
|
|
|
|
|
}
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Stripe_Declined(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
paymentToken = "pm_" + paymentToken;
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
Status = "incomplete",
|
|
|
|
|
LatestInvoice = new Stripe.Invoice
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Status = "requires_payment_method",
|
2022-01-03 15:44:28 +01:00
|
|
|
|
},
|
2022-08-29 22:06:55 +02:00
|
|
|
|
},
|
|
|
|
|
});
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<GatewayException>(
|
|
|
|
|
() => sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan, 0, 0, false, taxInfo));
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.Equal("Payment method was declined.", exception.Message);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await stripeAdapter.Received(1).CustomerDeleteAsync("C-1");
|
|
|
|
|
}
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2023-07-25 00:05:05 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_SM_Stripe_Declined(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2023-07-25 00:05:05 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2023-07-25 00:05:05 +02:00
|
|
|
|
paymentToken = "pm_" + paymentToken;
|
|
|
|
|
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
Status = "incomplete",
|
|
|
|
|
LatestInvoice = new Stripe.Invoice
|
|
|
|
|
{
|
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent
|
|
|
|
|
{
|
|
|
|
|
Status = "requires_payment_method",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<GatewayException>(
|
|
|
|
|
() => sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan,
|
|
|
|
|
1, 12, false, taxInfo, false, 10, 10));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Payment method was declined.", exception.Message);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received(1).CustomerDeleteAsync("C-1");
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Stripe_RequiresAction(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
Status = "incomplete",
|
|
|
|
|
LatestInvoice = new Stripe.Invoice
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Status = "requires_action",
|
|
|
|
|
ClientSecret = "clientSecret",
|
2022-01-03 15:44:28 +01:00
|
|
|
|
},
|
2022-08-29 22:06:55 +02:00
|
|
|
|
},
|
|
|
|
|
});
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan, 0, 0, false, taxInfo);
|
2023-07-25 00:05:05 +02:00
|
|
|
|
|
|
|
|
|
Assert.Equal("clientSecret", result);
|
|
|
|
|
Assert.False(organization.Enabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_SM_Stripe_RequiresAction(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2023-07-25 00:05:05 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2023-07-25 00:05:05 +02:00
|
|
|
|
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
Status = "incomplete",
|
|
|
|
|
LatestInvoice = new Stripe.Invoice
|
|
|
|
|
{
|
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent
|
|
|
|
|
{
|
|
|
|
|
Status = "requires_action",
|
|
|
|
|
ClientSecret = "clientSecret",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.Card, paymentToken, plan,
|
2023-07-25 00:05:05 +02:00
|
|
|
|
10, 10, false, taxInfo, false, 10, 10);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.Equal("clientSecret", result);
|
|
|
|
|
Assert.False(organization.Enabled);
|
|
|
|
|
}
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Paypal(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-20 23:00:40 +02:00
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>()
|
|
|
|
|
.BaseServiceUri.CloudRegion
|
|
|
|
|
.Returns("US");
|
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var customer = Substitute.For<Customer>();
|
|
|
|
|
customer.Id.ReturnsForAnyArgs("Braintree-Id");
|
|
|
|
|
customer.PaymentMethods.ReturnsForAnyArgs(new[] { Substitute.For<PaymentMethod>() });
|
|
|
|
|
var customerResult = Substitute.For<Result<Customer>>();
|
|
|
|
|
customerResult.IsSuccess().Returns(true);
|
|
|
|
|
customerResult.Target.ReturnsForAnyArgs(customer);
|
|
|
|
|
|
|
|
|
|
var braintreeGateway = sutProvider.GetDependency<IBraintreeGateway>();
|
|
|
|
|
braintreeGateway.Customer.CreateAsync(default).ReturnsForAnyArgs(customerResult);
|
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.PayPal, paymentToken, plan, 0, 0, false, taxInfo);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
Assert.Equal(GatewayType.Stripe, organization.Gateway);
|
|
|
|
|
Assert.Equal("C-1", organization.GatewayCustomerId);
|
|
|
|
|
Assert.Equal("S-1", organization.GatewaySubscriptionId);
|
|
|
|
|
Assert.True(organization.Enabled);
|
|
|
|
|
Assert.Equal(DateTime.Today.AddDays(10), organization.ExpirationDate);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().CustomerCreateAsync(Arg.Is<Stripe.CustomerCreateOptions>(c =>
|
|
|
|
|
c.Description == organization.BusinessName &&
|
|
|
|
|
c.Email == organization.BillingEmail &&
|
|
|
|
|
c.PaymentMethod == null &&
|
2023-07-20 23:00:40 +02:00
|
|
|
|
c.Metadata.Count == 2 &&
|
2022-08-29 22:06:55 +02:00
|
|
|
|
c.Metadata["btCustomerId"] == "Braintree-Id" &&
|
2023-07-20 23:00:40 +02:00
|
|
|
|
c.Metadata["region"] == "US" &&
|
2022-08-29 22:06:55 +02:00
|
|
|
|
c.InvoiceSettings.DefaultPaymentMethod == null &&
|
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState &&
|
|
|
|
|
c.TaxIdData == null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().SubscriptionCreateAsync(Arg.Is<Stripe.SubscriptionCreateOptions>(s =>
|
|
|
|
|
s.Customer == "C-1" &&
|
|
|
|
|
s.Expand[0] == "latest_invoice.payment_intent" &&
|
|
|
|
|
s.Metadata[organization.GatewayIdField()] == organization.Id.ToString() &&
|
|
|
|
|
s.Items.Count == 0
|
|
|
|
|
));
|
|
|
|
|
}
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2023-07-25 00:05:05 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_SM_Paypal(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2023-07-25 00:05:05 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
|
|
|
|
organization.UseSecretsManager = true;
|
2023-07-25 00:05:05 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var customer = Substitute.For<Customer>();
|
|
|
|
|
customer.Id.ReturnsForAnyArgs("Braintree-Id");
|
|
|
|
|
customer.PaymentMethods.ReturnsForAnyArgs(new[] { Substitute.For<PaymentMethod>() });
|
|
|
|
|
var customerResult = Substitute.For<Result<Customer>>();
|
|
|
|
|
customerResult.IsSuccess().Returns(true);
|
|
|
|
|
customerResult.Target.ReturnsForAnyArgs(customer);
|
|
|
|
|
|
|
|
|
|
var braintreeGateway = sutProvider.GetDependency<IBraintreeGateway>();
|
|
|
|
|
braintreeGateway.Customer.CreateAsync(default).ReturnsForAnyArgs(customerResult);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>()
|
|
|
|
|
.BaseServiceUri.CloudRegion
|
|
|
|
|
.Returns("US");
|
|
|
|
|
|
|
|
|
|
var additionalStorage = (short)2;
|
|
|
|
|
var additionalSeats = 10;
|
|
|
|
|
var additionalSmSeats = 5;
|
|
|
|
|
var additionalServiceAccounts = 20;
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.PayPal, paymentToken, plan,
|
2023-07-25 00:05:05 +02:00
|
|
|
|
additionalStorage, additionalSeats, false, taxInfo, false, additionalSmSeats, additionalServiceAccounts);
|
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
Assert.Equal(GatewayType.Stripe, organization.Gateway);
|
|
|
|
|
Assert.Equal("C-1", organization.GatewayCustomerId);
|
|
|
|
|
Assert.Equal("S-1", organization.GatewaySubscriptionId);
|
|
|
|
|
Assert.True(organization.Enabled);
|
|
|
|
|
Assert.Equal(DateTime.Today.AddDays(10), organization.ExpirationDate);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().CustomerCreateAsync(Arg.Is<Stripe.CustomerCreateOptions>(c =>
|
|
|
|
|
c.Description == organization.BusinessName &&
|
|
|
|
|
c.Email == organization.BillingEmail &&
|
|
|
|
|
c.PaymentMethod == null &&
|
|
|
|
|
c.Metadata.Count == 2 &&
|
|
|
|
|
c.Metadata["region"] == "US" &&
|
|
|
|
|
c.Metadata["btCustomerId"] == "Braintree-Id" &&
|
|
|
|
|
c.InvoiceSettings.DefaultPaymentMethod == null &&
|
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState &&
|
|
|
|
|
c.TaxIdData == null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received().SubscriptionCreateAsync(Arg.Is<Stripe.SubscriptionCreateOptions>(s =>
|
|
|
|
|
s.Customer == "C-1" &&
|
|
|
|
|
s.Expand[0] == "latest_invoice.payment_intent" &&
|
|
|
|
|
s.Metadata[organization.GatewayIdField()] == organization.Id.ToString() &&
|
|
|
|
|
s.Items.Count == 4 &&
|
2023-10-17 16:56:35 +02:00
|
|
|
|
s.Items.Count(i => i.Plan == plan.PasswordManager.StripeSeatPlanId && i.Quantity == additionalSeats) == 1 &&
|
|
|
|
|
s.Items.Count(i => i.Plan == plan.PasswordManager.StripeStoragePlanId && i.Quantity == additionalStorage) == 1 &&
|
|
|
|
|
s.Items.Count(i => i.Plan == plan.SecretsManager.StripeSeatPlanId && i.Quantity == additionalSmSeats) == 1 &&
|
|
|
|
|
s.Items.Count(i => i.Plan == plan.SecretsManager.StripeServiceAccountPlanId && i.Quantity == additionalServiceAccounts) == 1
|
2023-07-25 00:05:05 +02:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_Paypal_FailedCreate(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var customerResult = Substitute.For<Result<Customer>>();
|
|
|
|
|
customerResult.IsSuccess().Returns(false);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var braintreeGateway = sutProvider.GetDependency<IBraintreeGateway>();
|
|
|
|
|
braintreeGateway.Customer.CreateAsync(default).ReturnsForAnyArgs(customerResult);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<GatewayException>(
|
2023-10-17 16:56:35 +02:00
|
|
|
|
() => sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.PayPal, paymentToken, plan, 0, 0, false, taxInfo));
|
2023-07-25 00:05:05 +02:00
|
|
|
|
|
|
|
|
|
Assert.Equal("Failed to create PayPal customer record.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_SM_Paypal_FailedCreate(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2023-07-25 00:05:05 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2023-07-25 00:05:05 +02:00
|
|
|
|
|
|
|
|
|
var customerResult = Substitute.For<Result<Customer>>();
|
|
|
|
|
customerResult.IsSuccess().Returns(false);
|
|
|
|
|
|
|
|
|
|
var braintreeGateway = sutProvider.GetDependency<IBraintreeGateway>();
|
|
|
|
|
braintreeGateway.Customer.CreateAsync(default).ReturnsForAnyArgs(customerResult);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<GatewayException>(
|
2023-10-17 16:56:35 +02:00
|
|
|
|
() => sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.PayPal, paymentToken, plan,
|
2023-07-25 00:05:05 +02:00
|
|
|
|
1, 1, false, taxInfo, false, 8, 8));
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.Equal("Failed to create PayPal customer record.", exception.Message);
|
|
|
|
|
}
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task PurchaseOrganizationAsync_PayPal_Declined(SutProvider<StripePaymentService> sutProvider, Organization organization, string paymentToken, TaxInfo taxInfo)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plans = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
paymentToken = "pm_" + paymentToken;
|
|
|
|
|
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerCreateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "S-1",
|
|
|
|
|
CurrentPeriodEnd = DateTime.Today.AddDays(10),
|
|
|
|
|
Status = "incomplete",
|
|
|
|
|
LatestInvoice = new Stripe.Invoice
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Status = "requires_payment_method",
|
2022-01-03 15:44:28 +01:00
|
|
|
|
},
|
2022-08-29 22:06:55 +02:00
|
|
|
|
},
|
|
|
|
|
});
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var customer = Substitute.For<Customer>();
|
|
|
|
|
customer.Id.ReturnsForAnyArgs("Braintree-Id");
|
|
|
|
|
customer.PaymentMethods.ReturnsForAnyArgs(new[] { Substitute.For<PaymentMethod>() });
|
|
|
|
|
var customerResult = Substitute.For<Result<Customer>>();
|
|
|
|
|
customerResult.IsSuccess().Returns(true);
|
|
|
|
|
customerResult.Target.ReturnsForAnyArgs(customer);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var braintreeGateway = sutProvider.GetDependency<IBraintreeGateway>();
|
|
|
|
|
braintreeGateway.Customer.CreateAsync(default).ReturnsForAnyArgs(customerResult);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<GatewayException>(
|
2023-07-25 00:05:05 +02:00
|
|
|
|
() => sutProvider.Sut.PurchaseOrganizationAsync(organization, PaymentMethodType.PayPal, paymentToken, plans, 0, 0, false, taxInfo));
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.Equal("Payment method was declined.", exception.Message);
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await stripeAdapter.Received(1).CustomerDeleteAsync("C-1");
|
|
|
|
|
await braintreeGateway.Customer.Received(1).DeleteAsync("Braintree-Id");
|
|
|
|
|
}
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task UpgradeFreeOrganizationAsync_Success(SutProvider<StripePaymentService> sutProvider,
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Organization organization, TaxInfo taxInfo)
|
|
|
|
|
{
|
|
|
|
|
organization.GatewaySubscriptionId = null;
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerGetAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = "C-1",
|
|
|
|
|
Metadata = new Dictionary<string, string>
|
2022-01-03 15:44:28 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{ "btCustomerId", "B-123" },
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-01-29 15:48:59 +01:00
|
|
|
|
stripeAdapter.CustomerUpdateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
Metadata = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "btCustomerId", "B-123" },
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-08-29 22:06:55 +02:00
|
|
|
|
stripeAdapter.InvoiceUpcomingAsync(default).ReturnsForAnyArgs(new Stripe.Invoice
|
|
|
|
|
{
|
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent { Status = "requires_payment_method", },
|
|
|
|
|
AmountDue = 0
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription { });
|
2022-01-03 15:44:28 +01:00
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
2023-07-25 00:05:05 +02:00
|
|
|
|
|
|
|
|
|
var upgrade = new OrganizationUpgrade()
|
|
|
|
|
{
|
|
|
|
|
AdditionalStorageGb = 0,
|
|
|
|
|
AdditionalSeats = 0,
|
|
|
|
|
PremiumAccessAddon = false,
|
|
|
|
|
TaxInfo = taxInfo,
|
|
|
|
|
AdditionalSmSeats = 0,
|
|
|
|
|
AdditionalServiceAccounts = 0
|
|
|
|
|
};
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var result = await sutProvider.Sut.UpgradeFreeOrganizationAsync(organization, plan, upgrade);
|
2023-07-25 00:05:05 +02:00
|
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task UpgradeFreeOrganizationAsync_SM_Success(SutProvider<StripePaymentService> sutProvider,
|
2023-07-25 00:05:05 +02:00
|
|
|
|
Organization organization, TaxInfo taxInfo)
|
|
|
|
|
{
|
|
|
|
|
organization.GatewaySubscriptionId = null;
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
stripeAdapter.CustomerGetAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
Metadata = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "btCustomerId", "B-123" },
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-01-29 15:48:59 +01:00
|
|
|
|
stripeAdapter.CustomerUpdateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
Metadata = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "btCustomerId", "B-123" },
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-25 00:05:05 +02:00
|
|
|
|
stripeAdapter.InvoiceUpcomingAsync(default).ReturnsForAnyArgs(new Stripe.Invoice
|
|
|
|
|
{
|
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent { Status = "requires_payment_method", },
|
|
|
|
|
AmountDue = 0
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription { });
|
|
|
|
|
|
|
|
|
|
var upgrade = new OrganizationUpgrade()
|
|
|
|
|
{
|
|
|
|
|
AdditionalStorageGb = 1,
|
|
|
|
|
AdditionalSeats = 10,
|
|
|
|
|
PremiumAccessAddon = false,
|
|
|
|
|
TaxInfo = taxInfo,
|
|
|
|
|
AdditionalSmSeats = 5,
|
|
|
|
|
AdditionalServiceAccounts = 50
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-17 16:56:35 +02:00
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
|
|
|
|
var result = await sutProvider.Sut.UpgradeFreeOrganizationAsync(organization, plan, upgrade);
|
2022-01-03 18:11:11 +01:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.Null(result);
|
2019-07-06 05:35:54 +02:00
|
|
|
|
}
|
2024-01-29 15:48:59 +01:00
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-02-22 17:59:08 +01:00
|
|
|
|
public async Task UpgradeFreeOrganizationAsync_WhenCustomerHasNoAddress_UpdatesCustomerAddressWithTaxInfo(
|
2024-01-29 15:48:59 +01:00
|
|
|
|
SutProvider<StripePaymentService> sutProvider,
|
|
|
|
|
Organization organization,
|
|
|
|
|
TaxInfo taxInfo)
|
|
|
|
|
{
|
|
|
|
|
organization.GatewaySubscriptionId = null;
|
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
2024-02-01 19:21:17 +01:00
|
|
|
|
var featureService = sutProvider.GetDependency<IFeatureService>();
|
2024-01-29 15:48:59 +01:00
|
|
|
|
stripeAdapter.CustomerGetAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
Metadata = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "btCustomerId", "B-123" },
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.CustomerUpdateAsync(default).ReturnsForAnyArgs(new Stripe.Customer
|
|
|
|
|
{
|
|
|
|
|
Id = "C-1",
|
|
|
|
|
Metadata = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "btCustomerId", "B-123" },
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.InvoiceUpcomingAsync(default).ReturnsForAnyArgs(new Stripe.Invoice
|
|
|
|
|
{
|
|
|
|
|
PaymentIntent = new Stripe.PaymentIntent { Status = "requires_payment_method", },
|
|
|
|
|
AmountDue = 0
|
|
|
|
|
});
|
|
|
|
|
stripeAdapter.SubscriptionCreateAsync(default).ReturnsForAnyArgs(new Stripe.Subscription { });
|
|
|
|
|
|
|
|
|
|
var upgrade = new OrganizationUpgrade()
|
|
|
|
|
{
|
|
|
|
|
AdditionalStorageGb = 1,
|
|
|
|
|
AdditionalSeats = 10,
|
|
|
|
|
PremiumAccessAddon = false,
|
|
|
|
|
TaxInfo = taxInfo,
|
|
|
|
|
AdditionalSmSeats = 5,
|
|
|
|
|
AdditionalServiceAccounts = 50
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var plan = StaticStore.GetPlan(PlanType.EnterpriseAnnually);
|
|
|
|
|
_ = await sutProvider.Sut.UpgradeFreeOrganizationAsync(organization, plan, upgrade);
|
|
|
|
|
|
|
|
|
|
await stripeAdapter.Received()
|
|
|
|
|
.CustomerUpdateAsync(organization.GatewayCustomerId, Arg.Is<Stripe.CustomerUpdateOptions>(c =>
|
|
|
|
|
c.Address.Country == taxInfo.BillingAddressCountry &&
|
|
|
|
|
c.Address.PostalCode == taxInfo.BillingAddressPostalCode &&
|
|
|
|
|
c.Address.Line1 == taxInfo.BillingAddressLine1 &&
|
|
|
|
|
c.Address.Line2 == taxInfo.BillingAddressLine2 &&
|
|
|
|
|
c.Address.City == taxInfo.BillingAddressCity &&
|
|
|
|
|
c.Address.State == taxInfo.BillingAddressState));
|
|
|
|
|
}
|
2019-07-06 05:35:54 +02:00
|
|
|
|
}
|