2019-09-19 16:43:06 -04:00
|
|
|
|
using System;
|
2019-09-13 09:56:01 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using System.Text.Json;
|
2019-09-13 09:56:01 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2019-09-19 16:43:06 -04:00
|
|
|
|
using Bit.Core;
|
2021-11-08 15:55:42 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
2019-09-13 09:56:01 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Billing.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("apple")]
|
|
|
|
|
|
public class AppleController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly BillingSettings _billingSettings;
|
|
|
|
|
|
private readonly ILogger<AppleController> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public AppleController(
|
|
|
|
|
|
IOptions<BillingSettings> billingSettings,
|
|
|
|
|
|
ILogger<AppleController> logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
_billingSettings = billingSettings?.Value;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("iap")]
|
|
|
|
|
|
public async Task<IActionResult> PostIap()
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (HttpContext?.Request?.Query == null)
|
2019-09-13 09:56:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var key = HttpContext.Request.Query.ContainsKey("key") ?
|
|
|
|
|
|
HttpContext.Request.Query["key"].ToString() : null;
|
2021-11-08 15:55:42 -05:00
|
|
|
|
if (!CoreHelpers.FixedTimeEquals(key, _billingSettings.AppleWebhookKey))
|
2019-09-13 09:56:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string body = null;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var reader = new StreamReader(HttpContext.Request.Body, Encoding.UTF8))
|
2019-09-13 09:56:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
body = await reader.ReadToEndAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(body))
|
2019-09-13 09:56:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-19 16:43:06 -04:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-01-21 09:36:25 -05:00
|
|
|
|
var json = JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonDocument>(body), JsonHelpers.Indented);
|
2022-05-31 10:55:56 -04:00
|
|
|
|
_logger.LogInformation(Bit.Core.Constants.BypassFiltersEventId, "Apple IAP Notification:\n\n{0}", json);
|
2019-09-19 16:43:06 -04:00
|
|
|
|
return new OkResult();
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
catch (Exception e)
|
2019-09-19 16:43:06 -04:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "Error processing IAP status notification.");
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
2019-09-13 09:56:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|