/// <summary> /// Does the actual work capturing 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 capture</param> /// <param name="args">Any arguments required to process the payment.</param> /// <returns>The <see cref="IPaymentResult"/></returns> protected override IPaymentResult PerformCapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args) { // We need to determine if the entire amount authorized has been collected before marking // the payment collected. var appliedPayments = GatewayProviderService.GetAppliedPaymentsByPaymentKey(payment.Key); var applied = appliedPayments.Sum(x => x.Amount); payment.Collected = (amount + applied) == payment.Amount; payment.Authorized = true; GatewayProviderService.Save(payment); GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "Purchase Order Payment", amount); return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, CalculateTotalOwed(invoice).CompareTo(amount) <= 0)); }