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:
parent
87ee144edd
commit
fca1ee4253
@ -14,6 +14,7 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
private const string PremiumPlanId = "premium-annually";
|
private const string PremiumPlanId = "premium-annually";
|
||||||
private const string StoragePlanId = "storage-gb-annually";
|
private const string StoragePlanId = "storage-gb-annually";
|
||||||
|
|
||||||
private readonly Braintree.BraintreeGateway _btGateway;
|
private readonly Braintree.BraintreeGateway _btGateway;
|
||||||
|
|
||||||
public StripePaymentService(
|
public StripePaymentService(
|
||||||
@ -281,22 +282,48 @@ namespace Bit.Core.Services
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var chargeService = new ChargeService();
|
var customerService = new CustomerService();
|
||||||
var charges = await chargeService.ListAsync(new ChargeListOptions
|
var customer = await customerService.GetAsync(subscriber.GatewayCustomerId);
|
||||||
|
if(customer == null)
|
||||||
{
|
{
|
||||||
CustomerId = subscriber.GatewayCustomerId
|
return;
|
||||||
});
|
}
|
||||||
|
|
||||||
if(charges?.Data != null)
|
if(customer.Metadata.ContainsKey("btCustomerId"))
|
||||||
{
|
{
|
||||||
var refundService = new RefundService();
|
var transactionRequest = new Braintree.TransactionSearchRequest()
|
||||||
foreach(var charge in charges.Data.Where(c => !c.Refunded))
|
.CustomerId.Is(customer.Metadata["btCustomerId"]);
|
||||||
|
var transactions = _btGateway.Transaction.Search(transactionRequest);
|
||||||
|
|
||||||
|
if((transactions?.MaximumCount ?? 0) > 0)
|
||||||
{
|
{
|
||||||
await refundService.CreateAsync(new RefundCreateOptions { ChargeId = charge.Id });
|
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
|
||||||
|
{
|
||||||
|
CustomerId = subscriber.GatewayCustomerId
|
||||||
|
});
|
||||||
|
|
||||||
|
if(charges?.Data != null)
|
||||||
|
{
|
||||||
|
var refundService = new RefundService();
|
||||||
|
foreach(var charge in charges.Data.Where(c => !c.Refunded))
|
||||||
|
{
|
||||||
|
await refundService.CreateAsync(new RefundCreateOptions { ChargeId = charge.Id });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var customerService = new CustomerService();
|
|
||||||
await customerService.DeleteAsync(subscriber.GatewayCustomerId);
|
await customerService.DeleteAsync(subscriber.GatewayCustomerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user