mirror of
https://github.com/bitwarden/server.git
synced 2025-07-05 10:02:47 -05:00
replace paypal webhook with ipn
This commit is contained in:
@ -6,7 +6,6 @@ using Bit.Core.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
@ -18,7 +17,6 @@ namespace Bit.Billing.Controllers
|
||||
public class PayPalController : Controller
|
||||
{
|
||||
private readonly BillingSettings _billingSettings;
|
||||
private readonly PayPalClient _paypalClient;
|
||||
private readonly PayPalIpnClient _paypalIpnClient;
|
||||
private readonly ITransactionRepository _transactionRepository;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
@ -29,7 +27,6 @@ namespace Bit.Billing.Controllers
|
||||
|
||||
public PayPalController(
|
||||
IOptions<BillingSettings> billingSettings,
|
||||
PayPalClient paypalClient,
|
||||
PayPalIpnClient paypalIpnClient,
|
||||
ITransactionRepository transactionRepository,
|
||||
IOrganizationRepository organizationRepository,
|
||||
@ -39,7 +36,6 @@ namespace Bit.Billing.Controllers
|
||||
ILogger<PayPalController> logger)
|
||||
{
|
||||
_billingSettings = billingSettings?.Value;
|
||||
_paypalClient = paypalClient;
|
||||
_paypalIpnClient = paypalIpnClient;
|
||||
_transactionRepository = transactionRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
@ -49,116 +45,6 @@ namespace Bit.Billing.Controllers
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost("webhook")]
|
||||
public async Task<IActionResult> PostWebhook([FromQuery] string key)
|
||||
{
|
||||
if(key != _billingSettings.PayPal.WebhookKey)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
var verified = await _paypalClient.VerifyWebhookAsync(body, HttpContext.Request.Headers,
|
||||
_billingSettings.PayPal.WebhookId);
|
||||
if(!verified)
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
if(body.Contains("\"PAYMENT.SALE.COMPLETED\""))
|
||||
{
|
||||
var ev = JsonConvert.DeserializeObject<PayPalClient.Event<PayPalClient.Sale>>(body);
|
||||
var sale = ev.Resource;
|
||||
var saleTransaction = await _transactionRepository.GetByGatewayIdAsync(
|
||||
GatewayType.PayPal, sale.Id);
|
||||
if(saleTransaction == null)
|
||||
{
|
||||
var ids = sale.GetIdsFromCustom();
|
||||
if(ids.Item1.HasValue || ids.Item2.HasValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _transactionRepository.CreateAsync(new Core.Models.Table.Transaction
|
||||
{
|
||||
Amount = sale.Amount.TotalAmount,
|
||||
CreationDate = sale.CreateTime,
|
||||
OrganizationId = ids.Item1,
|
||||
UserId = ids.Item2,
|
||||
Type = TransactionType.Charge,
|
||||
Gateway = GatewayType.PayPal,
|
||||
GatewayId = sale.Id,
|
||||
PaymentMethodType = PaymentMethodType.PayPal,
|
||||
Details = sale.Id
|
||||
});
|
||||
}
|
||||
// Catch foreign key violations because user/org could have been deleted.
|
||||
catch(SqlException e) when(e.Number == 547) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(body.Contains("\"PAYMENT.SALE.REFUNDED\""))
|
||||
{
|
||||
var ev = JsonConvert.DeserializeObject<PayPalClient.Event<PayPalClient.Refund>>(body);
|
||||
var refund = ev.Resource;
|
||||
var refundTransaction = await _transactionRepository.GetByGatewayIdAsync(
|
||||
GatewayType.PayPal, refund.Id);
|
||||
if(refundTransaction == null)
|
||||
{
|
||||
var saleTransaction = await _transactionRepository.GetByGatewayIdAsync(
|
||||
GatewayType.PayPal, refund.SaleId);
|
||||
if(saleTransaction == null)
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
if(!saleTransaction.Refunded.GetValueOrDefault() &&
|
||||
saleTransaction.RefundedAmount.GetValueOrDefault() < refund.TotalRefundedAmount.ValueAmount)
|
||||
{
|
||||
saleTransaction.RefundedAmount = refund.TotalRefundedAmount.ValueAmount;
|
||||
if(saleTransaction.RefundedAmount == saleTransaction.Amount)
|
||||
{
|
||||
saleTransaction.Refunded = true;
|
||||
}
|
||||
await _transactionRepository.ReplaceAsync(saleTransaction);
|
||||
|
||||
var ids = refund.GetIdsFromCustom();
|
||||
if(ids.Item1.HasValue || ids.Item2.HasValue)
|
||||
{
|
||||
await _transactionRepository.CreateAsync(new Core.Models.Table.Transaction
|
||||
{
|
||||
Amount = refund.Amount.TotalAmount,
|
||||
CreationDate = refund.CreateTime,
|
||||
OrganizationId = ids.Item1,
|
||||
UserId = ids.Item2,
|
||||
Type = TransactionType.Refund,
|
||||
Gateway = GatewayType.PayPal,
|
||||
GatewayId = refund.Id,
|
||||
PaymentMethodType = PaymentMethodType.PayPal,
|
||||
Details = refund.Id
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
[HttpPost("ipn")]
|
||||
public async Task<IActionResult> PostIpn()
|
||||
{
|
||||
@ -226,13 +112,6 @@ namespace Bit.Billing.Controllers
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
var isAccountCredit = ipnTransaction.IsAccountCredit();
|
||||
if(!isAccountCredit)
|
||||
{
|
||||
// Only processing credits via IPN for now
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
if(ipnTransaction.PaymentStatus == "Completed")
|
||||
{
|
||||
var transaction = await _transactionRepository.GetByGatewayIdAsync(
|
||||
@ -243,6 +122,7 @@ namespace Bit.Billing.Controllers
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
var isAccountCredit = ipnTransaction.IsAccountCredit();
|
||||
try
|
||||
{
|
||||
var tx = new Transaction
|
||||
@ -259,7 +139,7 @@ namespace Bit.Billing.Controllers
|
||||
};
|
||||
await _transactionRepository.CreateAsync(tx);
|
||||
|
||||
if(ipnTransaction.IsAccountCredit())
|
||||
if(isAccountCredit)
|
||||
{
|
||||
string billingEmail = null;
|
||||
if(tx.OrganizationId.HasValue)
|
||||
|
Reference in New Issue
Block a user