diff --git a/src/Billing/Controllers/PayPalController.cs b/src/Billing/Controllers/PayPalController.cs index 65240efee9..0403d02735 100644 --- a/src/Billing/Controllers/PayPalController.cs +++ b/src/Billing/Controllers/PayPalController.cs @@ -94,28 +94,32 @@ namespace Bit.Billing.Controllers return new BadRequestResult(); } - saleTransaction.RefundedAmount = refund.TotalRefundedAmount.ValueAmount; - if(saleTransaction.RefundedAmount == saleTransaction.Amount) + if(!saleTransaction.Refunded.GetValueOrDefault() && + saleTransaction.RefundedAmount.GetValueOrDefault() < refund.TotalRefundedAmount.ValueAmount) { - 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 + saleTransaction.RefundedAmount = refund.TotalRefundedAmount.ValueAmount; + if(saleTransaction.RefundedAmount == saleTransaction.Amount) { - 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 - }); + 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 + }); + } } } } diff --git a/src/Billing/Controllers/StripeController.cs b/src/Billing/Controllers/StripeController.cs index 0bccd4723a..69dd9ae332 100644 --- a/src/Billing/Controllers/StripeController.cs +++ b/src/Billing/Controllers/StripeController.cs @@ -237,34 +237,40 @@ namespace Bit.Billing.Controllers throw new Exception("Cannot find refunded charge."); } - chargeTransaction.RefundedAmount = charge.AmountRefunded / 100M; - if(charge.Refunded) - { - chargeTransaction.Refunded = true; - } - await _transactionRepository.ReplaceAsync(chargeTransaction); + var amountRefunded = charge.AmountRefunded / 100M; - foreach(var refund in charge.Refunds) + if(!chargeTransaction.Refunded.GetValueOrDefault() && + chargeTransaction.RefundedAmount.GetValueOrDefault() < amountRefunded) { - var refundTransaction = await _transactionRepository.GetByGatewayIdAsync( - GatewayType.Stripe, refund.Id); - if(refundTransaction != null) + chargeTransaction.RefundedAmount = amountRefunded; + if(charge.Refunded) { - continue; + chargeTransaction.Refunded = true; } + await _transactionRepository.ReplaceAsync(chargeTransaction); - await _transactionRepository.CreateAsync(new Transaction + foreach(var refund in charge.Refunds) { - Amount = refund.Amount / 100M, - CreationDate = refund.Created, - OrganizationId = chargeTransaction.OrganizationId, - UserId = chargeTransaction.UserId, - Type = TransactionType.Refund, - Gateway = GatewayType.Stripe, - GatewayId = refund.Id, - PaymentMethodType = chargeTransaction.PaymentMethodType, - Details = chargeTransaction.Details - }); + var refundTransaction = await _transactionRepository.GetByGatewayIdAsync( + GatewayType.Stripe, refund.Id); + if(refundTransaction != null) + { + continue; + } + + await _transactionRepository.CreateAsync(new Transaction + { + Amount = refund.Amount / 100M, + CreationDate = refund.Created, + OrganizationId = chargeTransaction.OrganizationId, + UserId = chargeTransaction.UserId, + Type = TransactionType.Refund, + Gateway = GatewayType.Stripe, + GatewayId = refund.Id, + PaymentMethodType = chargeTransaction.PaymentMethodType, + Details = chargeTransaction.Details + }); + } } }