/// <summary>
 /// Raises the Finalizing event.
 /// </summary>
 /// <param name="result">
 /// The result.
 /// </param>
 protected void OnFinalizing(IPaymentResult result)
 {
     if (Finalizing != null)
     {
         Finalizing.RaiseEvent(new CheckoutEventArgs <IPaymentResult>(Context.Customer, result), this);
     }
 }
        /// <summary>
        /// Authorizes and Captures a Payment
        /// </summary>
        /// <param name="paymentGatewayMethod">The <see cref="IPaymentMethod"/></param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult AuthorizeCapturePayment(IPaymentGatewayMethod paymentGatewayMethod, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(paymentGatewayMethod, "paymentGatewayMethod");

            if (!IsReadyToInvoice())
            {
                return(new PaymentResult(Attempt <IPayment> .Fail(new InvalidOperationException("SalesPreparation is not ready to invoice")), null, false));
            }

            // invoice
            var invoice = PrepareInvoice(new InvoiceBuilderChain(this));

            MerchelloContext.Services.InvoiceService.Save(invoice);

            var result = invoice.AuthorizeCapturePayment(paymentGatewayMethod, args);

            Finalizing.RaiseEvent(new SalesPreparationEventArgs <IPaymentResult>(result), this);

            return(result);
        }