CoreCommon: update subscription

This commit is contained in:
Sergey Linnik 2022-08-05 15:36:00 +03:00
parent ef7419c9ae
commit b6559781ea
5 changed files with 59 additions and 1 deletions

View File

@ -185,6 +185,18 @@ public class BillingClient
return paymentUrl; 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<bool>(result);
return changed;
}
public IDictionary<string, Dictionary<string, decimal>> GetProductPriceInfo(params string[] productIds) public IDictionary<string, Dictionary<string, decimal>> GetProductPriceInfo(params string[] productIds)
{ {
ArgumentNullException.ThrowIfNull(productIds); ArgumentNullException.ThrowIfNull(productIds);

View File

@ -41,4 +41,5 @@ public interface ITariffService
void SaveButton(int tariffId, string partnerId, string buttonUrl); void SaveButton(int tariffId, string partnerId, string buttonUrl);
void SetTariff(int tenantId, Tariff tariff); void SetTariff(int tenantId, Tariff tariff);
Uri GetAccountLink(int tenant, string backUrl); Uri GetAccountLink(int tenant, string backUrl);
bool PaymentChange(int tenant, Dictionary<string, int> quantity);
} }

View File

@ -227,6 +227,35 @@ public class TariffService : ITariffService
return tariff; return tariff;
} }
public bool PaymentChange(int tenantId, Dictionary<string, int> 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) public void SetTariff(int tenantId, Tariff tariff)
{ {
ArgumentNullException.ThrowIfNull(tariff); ArgumentNullException.ThrowIfNull(tariff);

View File

@ -83,6 +83,11 @@ public class PaymentManager
return _tariffService.GetAccountLink(tenantId, backUrl); return _tariffService.GetAccountLink(tenantId, backUrl);
} }
public bool ChangePayment(Dictionary<string, int> quantity)
{
return _tariffService.PaymentChange(_tenantManager.GetCurrentTenant().Id, quantity);
}
public void ActivateKey(string key) public void ActivateKey(string key)
{ {
ArgumentNullOrEmptyException.ThrowIfNullOrEmpty(key); ArgumentNullOrEmptyException.ThrowIfNullOrEmpty(key);
@ -119,5 +124,4 @@ public class PaymentManager
return $"ASC :{now}:{hash}"; return $"ASC :{now}:{hash}";
} }
} }

View File

@ -211,6 +211,18 @@ public class PortalController : ControllerBase
inDto.Quantity, inDto.Quantity,
inDto.BackUrl); 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")] [HttpGet("payment/account")]
public Uri GetPaymentAccount(string backUrl) public Uri GetPaymentAccount(string backUrl)