diff --git a/src/Billing/BillingSettings.cs b/src/Billing/BillingSettings.cs index 63d6e07d00..f02380af3f 100644 --- a/src/Billing/BillingSettings.cs +++ b/src/Billing/BillingSettings.cs @@ -5,6 +5,7 @@ public virtual string JobsKey { get; set; } public virtual string StripeWebhookKey { get; set; } public virtual string StripeWebhookSecret { get; set; } + public virtual string BitPayWebhookKey { get; set; } public virtual PayPalSettings PayPal { get; set; } = new PayPalSettings(); public class PayPalSettings diff --git a/src/Billing/Controllers/BitPayController.cs b/src/Billing/Controllers/BitPayController.cs new file mode 100644 index 0000000000..5159212467 --- /dev/null +++ b/src/Billing/Controllers/BitPayController.cs @@ -0,0 +1,68 @@ +using Bit.Core.Repositories; +using Bit.Core.Services; +using Bit.Core.Utilities; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using System.IO; +using System.Text; +using System.Threading.Tasks; + +namespace Bit.Billing.Controllers +{ + [Route("bitpay")] + public class BitPayController : Controller + { + private readonly BillingSettings _billingSettings; + private readonly BitPayClient _bitPayClient; + private readonly ITransactionRepository _transactionRepository; + private readonly IOrganizationRepository _organizationRepository; + private readonly IUserRepository _userRepository; + private readonly IMailService _mailService; + private readonly IPaymentService _paymentService; + + public BitPayController( + IOptions billingSettings, + BitPayClient bitPayClient, + ITransactionRepository transactionRepository, + IOrganizationRepository organizationRepository, + IUserRepository userRepository, + IMailService mailService, + IPaymentService paymentService) + { + _billingSettings = billingSettings?.Value; + _bitPayClient = bitPayClient; + _transactionRepository = transactionRepository; + _organizationRepository = organizationRepository; + _userRepository = userRepository; + _mailService = mailService; + _paymentService = paymentService; + } + + [HttpPost("ipn")] + public async Task PostIpn([FromQuery] string key) + { + if(key != _billingSettings.BitPayWebhookKey) + { + return new BadRequestResult(); + } + + if(HttpContext?.Request == null) + { + return new BadRequestResult(); + } + + string body = null; + using(var reader = new StreamReader(HttpContext.Request.Body, Encoding.UTF8)) + { + body = await reader.ReadToEndAsync(); + } + + if(string.IsNullOrWhiteSpace(body)) + { + return new BadRequestResult(); + } + + return new OkResult(); + } + } +} diff --git a/src/Billing/appsettings.json b/src/Billing/appsettings.json index c52a94e2b9..e37ecf3abd 100644 --- a/src/Billing/appsettings.json +++ b/src/Billing/appsettings.json @@ -61,6 +61,7 @@ "jobsKey": "SECRET", "stripeWebhookKey": "SECRET", "stripeWebhookSecret": "SECRET", + "bitPayWebhookKey": "SECRET", "payPal": { "production": false, "businessId": "AD3LAUZSNVPJY",