Files
server/src/Billing/Controllers/AppleController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.8 KiB
C#
Raw Normal View History

using System.Text;
using System.Text.Json;
using Bit.Core.Utilities;
2019-09-13 09:56:01 -04:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
2022-08-29 16:06:55 -04:00
namespace Bit.Billing.Controllers;
[Route("apple")]
public class AppleController : Controller
2019-09-13 09:56:01 -04:00
{
2022-08-29 16:06:55 -04:00
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()
2019-09-13 09:56:01 -04:00
{
2022-08-29 16:06:55 -04:00
if (HttpContext?.Request?.Query == null)
{
return new BadRequestResult();
}
2019-09-13 09:56:01 -04:00
2022-08-29 16:06:55 -04:00
var key = HttpContext.Request.Query.ContainsKey("key") ?
HttpContext.Request.Query["key"].ToString() : null;
if (!CoreHelpers.FixedTimeEquals(key, _billingSettings.AppleWebhookKey))
2019-09-13 09:56:01 -04:00
{
2022-08-29 16:06:55 -04:00
return new BadRequestResult();
2019-09-13 09:56:01 -04:00
}
2022-08-29 16:06:55 -04:00
string body = null;
using (var reader = new StreamReader(HttpContext.Request.Body, Encoding.UTF8))
{
body = await reader.ReadToEndAsync();
}
if (string.IsNullOrWhiteSpace(body))
{
return new BadRequestResult();
}
try
{
var json = JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonDocument>(body), JsonHelpers.Indented);
_logger.LogInformation(Bit.Core.Constants.BypassFiltersEventId, "Apple IAP Notification:\n\n{0}", json);
return new OkResult();
}
catch (Exception e)
2019-09-13 09:56:01 -04:00
{
2022-08-29 16:06:55 -04:00
_logger.LogError(e, "Error processing IAP status notification.");
return new BadRequestResult();
2019-09-13 09:56:01 -04:00
}
}
}