1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-29 07:14:50 -05:00

CancelAndRecoverChargesAsync for braintree

This commit is contained in:
Kyle Spearrin 2019-01-31 09:00:44 -05:00
parent 87ee144edd
commit fca1ee4253

View File

@ -14,6 +14,7 @@ namespace Bit.Core.Services
{
private const string PremiumPlanId = "premium-annually";
private const string StoragePlanId = "storage-gb-annually";
private readonly Braintree.BraintreeGateway _btGateway;
public StripePaymentService(
@ -281,6 +282,32 @@ namespace Bit.Core.Services
return;
}
var customerService = new CustomerService();
var customer = await customerService.GetAsync(subscriber.GatewayCustomerId);
if(customer == null)
{
return;
}
if(customer.Metadata.ContainsKey("btCustomerId"))
{
var transactionRequest = new Braintree.TransactionSearchRequest()
.CustomerId.Is(customer.Metadata["btCustomerId"]);
var transactions = _btGateway.Transaction.Search(transactionRequest);
if((transactions?.MaximumCount ?? 0) > 0)
{
var txs = transactions.Cast<Braintree.Transaction>().Where(c => c.RefundedTransactionId == null);
foreach(var transaction in txs)
{
await _btGateway.Transaction.RefundAsync(transaction.Id);
}
}
await _btGateway.Customer.DeleteAsync(customer.Metadata["btCustomerId"]);
}
else
{
var chargeService = new ChargeService();
var charges = await chargeService.ListAsync(new ChargeListOptions
{
@ -295,8 +322,8 @@ namespace Bit.Core.Services
await refundService.CreateAsync(new RefundCreateOptions { ChargeId = charge.Id });
}
}
}
var customerService = new CustomerService();
await customerService.DeleteAsync(subscriber.GatewayCustomerId);
}