From b6559781eaa249ddd4b4c971c0a4cf522c6e8b70 Mon Sep 17 00:00:00 2001 From: Sergey Linnik Date: Fri, 5 Aug 2022 15:36:00 +0300 Subject: [PATCH] CoreCommon: update subscription --- .../ASC.Core.Common/Billing/BillingClient.cs | 12 ++++++++ .../ASC.Core.Common/Billing/ITariffService.cs | 1 + .../ASC.Core.Common/Billing/TariffService.cs | 29 +++++++++++++++++++ .../Context/Impl/PaymentManager.cs | 6 +++- web/ASC.Web.Api/Api/PortalController.cs | 12 ++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) diff --git a/common/ASC.Core.Common/Billing/BillingClient.cs b/common/ASC.Core.Common/Billing/BillingClient.cs index 7707f42ea0..5600585058 100644 --- a/common/ASC.Core.Common/Billing/BillingClient.cs +++ b/common/ASC.Core.Common/Billing/BillingClient.cs @@ -185,6 +185,18 @@ public class BillingClient return paymentUrl; } + public bool ChangePayment(string portalId, string[] products, int[] quantity) + { + var parameters = products.Select(p => Tuple.Create("ProductId", p)) + .Concat(quantity.Select(q => Tuple.Create("ProductQty", q.ToString()))) + .ToArray(); + + var result = Request("ChangeSubscription", portalId, parameters); + var changed = JsonConvert.DeserializeObject(result); + + return changed; + } + public IDictionary> GetProductPriceInfo(params string[] productIds) { ArgumentNullException.ThrowIfNull(productIds); diff --git a/common/ASC.Core.Common/Billing/ITariffService.cs b/common/ASC.Core.Common/Billing/ITariffService.cs index 7a0da49818..58198ec8b1 100644 --- a/common/ASC.Core.Common/Billing/ITariffService.cs +++ b/common/ASC.Core.Common/Billing/ITariffService.cs @@ -41,4 +41,5 @@ public interface ITariffService void SaveButton(int tariffId, string partnerId, string buttonUrl); void SetTariff(int tenantId, Tariff tariff); Uri GetAccountLink(int tenant, string backUrl); + bool PaymentChange(int tenant, Dictionary quantity); } diff --git a/common/ASC.Core.Common/Billing/TariffService.cs b/common/ASC.Core.Common/Billing/TariffService.cs index 565ce44695..4a6d825b1a 100644 --- a/common/ASC.Core.Common/Billing/TariffService.cs +++ b/common/ASC.Core.Common/Billing/TariffService.cs @@ -227,6 +227,35 @@ public class TariffService : ITariffService return tariff; } + public bool PaymentChange(int tenantId, Dictionary quantity) + { + if (quantity == null || !quantity.Any() + || !_billingClient.Configured) + return false; + + var productIds = _quotaService.GetTenantQuotas() + .Where(q => + !string.IsNullOrEmpty(q.ProductId) + && quantity.ContainsKey(q.Name)) + .Select(q => q.ProductId); + + try + { + var client = GetBillingClient(); + var changed = client.ChangePayment(GetPortalId(tenantId), productIds.ToArray(), quantity.Values.ToArray()); + + if (!changed) return false; + + ClearCache(tenantId); + } + catch (Exception error) + { + _logger.ErrorWithException(error); + } + + return true; + } + public void SetTariff(int tenantId, Tariff tariff) { ArgumentNullException.ThrowIfNull(tariff); diff --git a/common/ASC.Core.Common/Context/Impl/PaymentManager.cs b/common/ASC.Core.Common/Context/Impl/PaymentManager.cs index a7e9e8adde..44dbfd0aa5 100644 --- a/common/ASC.Core.Common/Context/Impl/PaymentManager.cs +++ b/common/ASC.Core.Common/Context/Impl/PaymentManager.cs @@ -83,6 +83,11 @@ public class PaymentManager return _tariffService.GetAccountLink(tenantId, backUrl); } + public bool ChangePayment(Dictionary quantity) + { + return _tariffService.PaymentChange(_tenantManager.GetCurrentTenant().Id, quantity); + } + public void ActivateKey(string key) { ArgumentNullOrEmptyException.ThrowIfNullOrEmpty(key); @@ -119,5 +124,4 @@ public class PaymentManager return $"ASC :{now}:{hash}"; } - } diff --git a/web/ASC.Web.Api/Api/PortalController.cs b/web/ASC.Web.Api/Api/PortalController.cs index ced260ec8f..841a5bd8d3 100644 --- a/web/ASC.Web.Api/Api/PortalController.cs +++ b/web/ASC.Web.Api/Api/PortalController.cs @@ -211,6 +211,18 @@ public class PortalController : ControllerBase inDto.Quantity, inDto.BackUrl); } + + [HttpPut("payment/update")] + public bool PaymentUpdate(PaymentUrlRequestsDto inDto) + { + if (!_paymentManager.GetTariffPayments(Tenant.Id).Any() + || !_userManager.GetUsers(_securityContext.CurrentAccount.ID).IsAdmin(_userManager)) + { + return false; + } + + return _paymentManager.ChangePayment(inDto.Quantity); + } [HttpGet("payment/account")] public Uri GetPaymentAccount(string backUrl)