/// <summary> /// Does the actual work of refunding a payment /// </summary> /// <param name="invoice">The <see cref="IInvoice"/></param> /// <param name="payment">The previously Authorize payment to be captured</param> /// <param name="amount">The amount to be refunded</param> /// <param name="args">Any arguments required to process the payment.</param> /// <returns>The <see cref="IPaymentResult"/></returns> protected override IPaymentResult PerformRefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args) { var result = _processor.RefundPayment(invoice, payment, amount); if (!result.Payment.Success) { GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, result.Payment.Exception.Message, 0); return(result); } // use the overloaded AppliedPayments method here for testing if we don't have // a MerchelloContext foreach (var applied in payment.AppliedPayments(GatewayProviderService)) { applied.TransactionType = AppliedPaymentType.Refund; applied.Amount = 0; applied.Description += " - Refunded"; GatewayProviderService.Save(applied); } payment.Amount = payment.Amount - amount; if (payment.Amount != 0) { GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "To show partial payment remaining after refund", payment.Amount); } GatewayProviderService.Save(payment); return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, false)); }