2022-08-25 20:09:12 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
namespace ASC.Web.Api.Controllers ;
2023-03-01 13:18:05 +00:00
///<summary>
/// Portal information access.
///</summary>
///<name>portal</name>
2022-08-25 20:09:12 +00:00
[Scope]
[DefaultRoute]
[ApiController]
2022-09-04 15:30:32 +00:00
[AllowNotPayment]
2022-08-25 20:09:12 +00:00
[ControllerName("portal")]
public class PaymentController : ControllerBase
{
private readonly ApiContext _apiContext ;
private readonly UserManager _userManager ;
private readonly TenantManager _tenantManager ;
private readonly ITariffService _tariffService ;
private readonly SecurityContext _securityContext ;
2022-08-26 14:33:55 +00:00
private readonly RegionHelper _regionHelper ;
2022-09-01 08:27:10 +00:00
private readonly QuotaHelper _quotaHelper ;
2022-09-04 15:30:32 +00:00
private readonly IMemoryCache _memoryCache ;
private readonly IHttpContextAccessor _httpContextAccessor ;
private readonly MessageService _messageService ;
private readonly StudioNotifyService _studioNotifyService ;
private readonly int _maxCount = 10 ;
private readonly int _expirationMinutes = 2 ;
2022-08-25 20:09:12 +00:00
protected Tenant Tenant { get { return _apiContext . Tenant ; } }
public PaymentController (
ApiContext apiContext ,
UserManager userManager ,
TenantManager tenantManager ,
ITariffService tariffService ,
SecurityContext securityContext ,
2022-08-30 08:39:43 +00:00
RegionHelper regionHelper ,
2022-09-04 15:30:32 +00:00
QuotaHelper tariffHelper ,
IMemoryCache memoryCache ,
IHttpContextAccessor httpContextAccessor ,
MessageService messageService ,
StudioNotifyService studioNotifyService )
2022-08-25 20:09:12 +00:00
{
_apiContext = apiContext ;
_userManager = userManager ;
_tenantManager = tenantManager ;
_tariffService = tariffService ;
_securityContext = securityContext ;
2022-08-26 14:33:55 +00:00
_regionHelper = regionHelper ;
2022-09-01 08:27:10 +00:00
_quotaHelper = tariffHelper ;
2022-09-04 15:30:32 +00:00
_memoryCache = memoryCache ;
_httpContextAccessor = httpContextAccessor ;
_messageService = messageService ;
_studioNotifyService = studioNotifyService ;
2022-08-25 20:09:12 +00:00
}
2023-03-01 13:18:05 +00:00
/// <summary>
/// Returns the URL to the payment page.
/// </summary>
/// <short>
/// Get the payment page URL
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-04-13 09:43:54 +00:00
/// <param type="ASC.Web.Api.Models.PaymentUrlRequestsDto, ASC.Web.Api.Models" name="inDto">Payment URL request parameters</param>
/// <returns type="System.Uri, System">The URL to the payment page</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/url</path>
/// <httpMethod>PUT</httpMethod>
2022-08-25 20:09:12 +00:00
[HttpPut("payment/url")]
2022-09-09 07:27:35 +00:00
public async Task < Uri > GetPaymentUrl ( PaymentUrlRequestsDto inDto )
2022-08-25 20:09:12 +00:00
{
2022-09-02 09:57:23 +00:00
if ( _tariffService . GetPayments ( Tenant . Id ) . Any ( ) | |
2022-10-18 11:22:02 +00:00
! _userManager . IsDocSpaceAdmin ( _securityContext . CurrentAccount . ID ) )
2022-08-25 20:09:12 +00:00
{
return null ;
}
2022-08-26 14:33:55 +00:00
var currency = _regionHelper . GetCurrencyFromRequest ( ) ;
2022-08-25 20:09:12 +00:00
2022-09-09 07:27:35 +00:00
return await _tariffService . GetShoppingUri ( Tenant . Id , currency ,
2022-08-25 20:09:12 +00:00
Thread . CurrentThread . CurrentCulture . TwoLetterISOLanguageName ,
_userManager . GetUsers ( _securityContext . CurrentAccount . ID ) . Email ,
inDto . Quantity ,
inDto . BackUrl ) ;
}
2023-03-01 13:18:05 +00:00
/// <summary>
/// Updates the quantity of payment.
/// </summary>
/// <short>
/// Update the payment quantity
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-04-13 09:43:54 +00:00
/// <param type="ASC.Web.Api.Models.PaymentUrlRequestsDto, ASC.Web.Api.Models" name="inDto">Payment URL request parameters</param>
/// <returns type="System.Boolean, System">Boolean value: true if the operation is successful</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/update</path>
/// <httpMethod>PUT</httpMethod>
2022-08-25 20:09:12 +00:00
[HttpPut("payment/update")]
2022-09-09 07:27:35 +00:00
public async Task < bool > PaymentUpdate ( PaymentUrlRequestsDto inDto )
2022-08-25 20:09:12 +00:00
{
2023-01-10 17:56:38 +00:00
var payerId = _tariffService . GetTariff ( Tenant . Id ) . CustomerId ;
var payer = _userManager . GetUserByEmail ( payerId ) ;
2022-08-26 14:33:55 +00:00
if ( ! _tariffService . GetPayments ( Tenant . Id ) . Any ( ) | |
2023-01-10 17:56:38 +00:00
_securityContext . CurrentAccount . ID ! = payer . Id )
2022-08-25 20:09:12 +00:00
{
return false ;
}
2022-09-09 07:27:35 +00:00
return await _tariffService . PaymentChange ( Tenant . Id , inDto . Quantity ) ;
2022-08-25 20:09:12 +00:00
}
2023-03-01 13:18:05 +00:00
/// <summary>
/// Returns the URL to the payment account.
/// </summary>
/// <short>
/// Get the payment account
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-03-17 08:44:04 +00:00
/// <param type="System.String, System" name="backUrl">Back URL</param>
2023-04-13 09:43:54 +00:00
/// <returns type="System.Uri, System">The URL to the payment account</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/account</path>
/// <httpMethod>GET</httpMethod>
2022-08-25 20:09:12 +00:00
[HttpGet("payment/account")]
public Uri GetPaymentAccount ( string backUrl )
{
var payerId = _tariffService . GetTariff ( Tenant . Id ) . CustomerId ;
2022-09-13 14:29:38 +00:00
var payer = _userManager . GetUserByEmail ( payerId ) ;
2022-08-25 20:09:12 +00:00
2022-09-13 14:29:38 +00:00
if ( _securityContext . CurrentAccount . ID ! = payer . Id & &
2022-08-26 14:33:55 +00:00
_securityContext . CurrentAccount . ID ! = Tenant . OwnerId )
{
2022-08-25 20:09:12 +00:00
return null ;
2022-08-26 14:33:55 +00:00
}
2022-08-25 20:09:12 +00:00
return _tariffService . GetAccountLink ( Tenant . Id , backUrl ) ;
}
2023-03-01 13:18:05 +00:00
/// <summary>
/// Returns the available portal prices.
/// </summary>
/// <short>
/// Get prices
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-04-13 09:43:54 +00:00
/// <returns type="System.Object, System">List of available portal prices</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/prices</path>
/// <httpMethod>GET</httpMethod>
2022-08-25 20:09:12 +00:00
[HttpGet("payment/prices")]
public object GetPrices ( )
{
2022-08-26 14:33:55 +00:00
var currency = _regionHelper . GetCurrencyFromRequest ( ) ;
2022-08-25 20:09:12 +00:00
var result = _tenantManager . GetProductPriceInfo ( )
. ToDictionary ( pr = > pr . Key , pr = > pr . Value . ContainsKey ( currency ) ? pr . Value [ currency ] : 0 ) ;
return result ;
}
2022-08-26 14:33:55 +00:00
2022-09-04 15:30:32 +00:00
2023-03-01 13:18:05 +00:00
/// <summary>
/// Returns the available portal currencies.
/// </summary>
/// <short>
/// Get currencies
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-04-13 09:43:54 +00:00
/// <returns type="System.Collections.Generic.IEnumerable{ASC.Web.Api.ApiModels.ResponseDto.CurrenciesDto}, System.Collections.Generic">List of available portal currencies</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/currencies</path>
/// <httpMethod>GET</httpMethod>
2023-04-13 09:43:54 +00:00
/// <collection>list</collection>
2022-08-26 14:33:55 +00:00
[HttpGet("payment/currencies")]
public IEnumerable < CurrenciesDto > GetCurrencies ( )
2022-08-25 20:09:12 +00:00
{
2022-08-26 14:33:55 +00:00
var defaultRegion = _regionHelper . GetDefaultRegionInfo ( ) ;
var currentRegion = _regionHelper . GetCurrentRegionInfo ( ) ;
yield return new CurrenciesDto ( defaultRegion ) ;
if ( ! currentRegion . Name . Equals ( defaultRegion . Name ) )
2022-08-25 20:09:12 +00:00
{
2022-08-26 14:33:55 +00:00
yield return new CurrenciesDto ( currentRegion ) ;
2022-08-25 20:09:12 +00:00
}
}
2022-08-30 08:39:43 +00:00
2023-03-01 13:18:05 +00:00
/// <summary>
/// Returns the available portal quotas.
/// </summary>
/// <short>
/// Get quotas
/// </short>
2023-03-28 15:29:50 +00:00
/// <category>Quota</category>
2023-04-13 09:43:54 +00:00
/// <returns type="System.Collections.Generic.IAsyncEnumerable{ASC.Web.Api.ApiModels.ResponseDto.QuotaDto}, System.Collections.Generic">List of available portal quotas</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/quotas</path>
/// <httpMethod>GET</httpMethod>
2023-04-13 09:43:54 +00:00
/// <collection>list</collection
2022-09-01 08:27:10 +00:00
[HttpGet("payment/quotas")]
2023-03-10 15:30:34 +00:00
public IAsyncEnumerable < QuotaDto > GetQuotas ( )
2022-08-30 08:39:43 +00:00
{
2022-09-01 08:27:10 +00:00
return _quotaHelper . GetQuotas ( ) ;
2022-08-30 08:39:43 +00:00
}
2022-09-04 15:30:32 +00:00
2023-03-01 13:18:05 +00:00
/// <summary>
2023-03-28 15:29:50 +00:00
/// Returns the payment information about the current portal quota.
2023-03-01 13:18:05 +00:00
/// </summary>
/// <short>
2023-03-28 15:29:50 +00:00
/// Get quota payment information
2023-03-01 13:18:05 +00:00
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-04-13 09:43:54 +00:00
/// <returns type="ASC.Web.Api.ApiModels.ResponseDto.QuotaDto, ASC.Web.Api.ApiModels.ResponseDto">Payment information about the current portal quota: ID, title, price, nonprofit or not, free or not, trial or not, tenant quota features</returns>
2023-03-01 13:18:05 +00:00
/// <path>api/2.0/portal/payment/quota</path>
/// <httpMethod>GET</httpMethod>
2022-09-08 16:57:36 +00:00
[HttpGet("payment/quota")]
2023-04-11 15:58:44 +00:00
public async Task < QuotaDto > GetQuota ( bool refresh )
2022-09-08 16:57:36 +00:00
{
2023-04-11 15:58:44 +00:00
return await _quotaHelper . GetCurrentQuota ( refresh ) ;
2022-09-08 16:57:36 +00:00
}
2023-03-01 13:18:05 +00:00
/// <summary>
/// Sends a request for portal payment.
/// </summary>
/// <short>
/// Send a payment request
/// </short>
2023-03-01 15:27:08 +00:00
/// <category>Payment</category>
2023-04-13 09:43:54 +00:00
/// <param type="ASC.Web.Api.ApiModels.RequestsDto.SalesRequestsDto, ASC.Web.Api.ApiModels.RequestsDto" name="inDto">Portal payment request parameters</param>
2023-03-01 13:18:05 +00:00
/// <returns></returns>
/// <path>api/2.0/portal/payment/request</path>
/// <httpMethod>POST</httpMethod>
2022-09-04 15:30:32 +00:00
[HttpPost("payment/request")]
public void SendSalesRequest ( SalesRequestsDto inDto )
{
if ( ! inDto . Email . TestEmailRegex ( ) )
{
throw new Exception ( Resource . ErrorNotCorrectEmail ) ;
}
if ( string . IsNullOrEmpty ( inDto . Message ) )
{
throw new Exception ( Resource . ErrorEmptyMessage ) ;
}
CheckCache ( "salesrequest" ) ;
_studioNotifyService . SendMsgToSales ( inDto . Email , inDto . UserName , inDto . Message ) ;
_messageService . Send ( MessageAction . ContactSalesMailSent ) ;
}
internal void CheckCache ( string basekey )
{
var key = _httpContextAccessor . HttpContext . Request . GetUserHostAddress ( ) + basekey ;
if ( _memoryCache . TryGetValue < int > ( key , out var count ) )
{
if ( count > _maxCount )
{
throw new Exception ( Resource . ErrorRequestLimitExceeded ) ;
}
}
_memoryCache . Set ( key , count + 1 , TimeSpan . FromMinutes ( _expirationMinutes ) ) ;
}
2022-08-25 20:09:12 +00:00
}