Beispiel #1
0
        /// <summary>
        /// Voids a payment
        /// </summary>
        /// <param name="invoice">The invoice associated with the payment to be voided</param>
        /// <param name="payment">The payment to be voided</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult VoidPayment(IInvoice invoice, IPayment payment, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");
            if (!invoice.HasIdentity)
            {
                return(new PaymentResult(Attempt <IPayment> .Fail(new InvalidOperationException("Cannot void a payment on an invoice that cannot have payments")), invoice, false));
            }

            var operationData = new PaymentOperationData()
            {
                Invoice       = invoice,
                Payment       = payment,
                PaymentMethod = this.PaymentMethod,
                ProcessorArgumentCollection = args
            };

            Voiding.RaiseEvent(new SaveEventArgs <PaymentOperationData>(operationData), this);

            var response = PerformVoidPayment(invoice, payment, args);

            VoidAttempted.RaiseEvent(new PaymentAttemptEventArgs <IPaymentResult>(response), this);

            if (!response.Payment.Success)
            {
                return(response);
            }

            var appliedPayments = payment.AppliedPayments().Where(x => x.TransactionType != AppliedPaymentType.Void);

            foreach (var appliedPayment in appliedPayments)
            {
                appliedPayment.TransactionType = AppliedPaymentType.Void;
                appliedPayment.Amount          = 0;

                GatewayProviderService.Save(appliedPayment);
            }

            // Assert the payment has been voided
            if (!payment.Voided)
            {
                payment.Voided = true;
                GatewayProviderService.Save(payment);
            }

            AssertInvoiceStatus(response.Invoice);

            // Force the ApproveOrderCreation to false
            if (response.ApproveOrderCreation)
            {
                ((PaymentResult)response).ApproveOrderCreation = false;
            }

            // give response
            return(response);
        }
Beispiel #2
0
        /// <summary>
        /// Captures a payment for the <see cref="IInvoice"/>
        /// </summary>
        /// <param name="invoice">The invoice to be paid</param>
        /// <param name="payment">The payment to capture</param>
        /// <param name="amount">The amount of the payment to be captured</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult CapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");

            var operationData = new PaymentOperationData()
            {
                Invoice       = invoice,
                Payment       = payment,
                Amount        = amount,
                PaymentMethod = this.PaymentMethod,
                ProcessorArgumentCollection = args
            };

            Capturing.RaiseEvent(new SaveEventArgs <PaymentOperationData>(operationData), this);

            // persist the invoice
            if (!invoice.HasIdentity)
            {
                GatewayProviderService.Save(invoice);
            }

            var response = PerformCapturePayment(invoice, payment, amount, args);

            //((PaymentResult)response).ApproveOrderCreation = this.EnsureApproveOrderCreation(response, invoice);

            // Special case where the order has already been created due to configuration override.
            if (invoice.Orders.Any() && response.ApproveOrderCreation)
            {
                ((PaymentResult)response).ApproveOrderCreation = false;
            }

            CaptureAttempted.RaiseEvent(new PaymentAttemptEventArgs <IPaymentResult>(response), this);

            if (!response.Payment.Success)
            {
                return(response);
            }

            AssertPaymentApplied(response, response.Invoice);

            AssertInvoiceStatus(response.Invoice);


            // give response
            return(response);
        }
Beispiel #3
0
        /// <summary>
        /// Authorizes and Captures a Payment
        /// </summary>
        /// <param name="invoice">The invoice to be paid</param>
        /// <param name="amount">The amount of the payment to the invoice</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult AuthorizeCapturePayment(IInvoice invoice, decimal amount, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");

            var operationData = new PaymentOperationData()
            {
                Invoice = invoice,
                Amount  = amount,
                ProcessorArgumentCollection = args
            };

            Capturing.RaiseEvent(new SaveEventArgs <PaymentOperationData>(operationData), this);

            // persist the invoice
            if (!invoice.HasIdentity)
            {
                GatewayProviderService.Save(invoice);
            }

            // authorize and capture the payment
            var response = PerformAuthorizeCapturePayment(invoice, amount, args);

            //((PaymentResult)response).ApproveOrderCreation = this.EnsureApproveOrderCreation(response, invoice);

            AuthorizeCaptureAttempted.RaiseEvent(new PaymentAttemptEventArgs <IPaymentResult>(response), this);

            if (!response.Payment.Success)
            {
                return(response);
            }

            AssertPaymentApplied(response, invoice);

            AssertInvoiceStatus(invoice);

            // Check configuration for override on ApproveOrderCreation
            if (!response.ApproveOrderCreation)
            {
                ((PaymentResult)response).ApproveOrderCreation = MerchelloConfiguration.Current.AlwaysApproveOrderCreation;
            }

            // give response
            return(response);
        }
Beispiel #4
0
        /// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="invoice">The invoice to be the payment was applied</param>
        /// <param name="payment">The payment to be refunded</param>
        /// <param name="amount">The amount to be refunded</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult RefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");
            if (!invoice.HasIdentity)
            {
                return(new PaymentResult(Attempt <IPayment> .Fail(new InvalidOperationException("Cannot refund a payment on an invoice that cannot have payments")), invoice, false));
            }

            var operationData = new PaymentOperationData()
            {
                Invoice       = invoice,
                Payment       = payment,
                Amount        = amount,
                PaymentMethod = this.PaymentMethod,
                ProcessorArgumentCollection = args
            };

            Refunding.RaiseEvent(new SaveEventArgs <PaymentOperationData>(operationData), this);

            var response = PerformRefundPayment(invoice, payment, amount, args);

            RefundAttempted.RaiseEvent(new PaymentAttemptEventArgs <IPaymentResult>(response), this);

            if (!response.Payment.Success)
            {
                return(response);
            }

            AssertInvoiceStatus(response.Invoice);

            // Force the ApproveOrderCreation to false
            if (response.ApproveOrderCreation)
            {
                ((PaymentResult)response).ApproveOrderCreation = false;
            }

            // give response
            return(response);
        }
        /// <summary>
        /// Voids a payment
        /// </summary>
        /// <param name="invoice">The invoice associated with the payment to be voided</param>
        /// <param name="payment">The payment to be voided</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult VoidPayment(IInvoice invoice, IPayment payment, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");
            if (!invoice.HasIdentity) return new PaymentResult(Attempt<IPayment>.Fail(new InvalidOperationException("Cannot void a payment on an invoice that cannot have payments")), invoice, false);

            var operationData = new PaymentOperationData()
            {
                Invoice = invoice,
                Payment = payment,
                ProcessorArgumentCollection = args
            };

            Voiding.RaiseEvent(new SaveEventArgs<PaymentOperationData>(operationData), this);

            var response = PerformVoidPayment(invoice, payment, args);

            VoidAttempted.RaiseEvent(new PaymentAttemptEventArgs<IPaymentResult>(response), this);

            if (!response.Payment.Success) return response;

            var appliedPayments = payment.AppliedPayments().Where(x => x.TransactionType != AppliedPaymentType.Void);
            foreach (var appliedPayment in appliedPayments)
            {
                appliedPayment.TransactionType = AppliedPaymentType.Void;
                appliedPayment.Amount = 0;

                GatewayProviderService.Save(appliedPayment);
            }

            // Assert the payment has been voided
            if (!payment.Voided)
            {
                payment.Voided = true;
                GatewayProviderService.Save(payment);
            }

            AssertInvoiceStatus(invoice);

            // Force the ApproveOrderCreation to false
            if (response.ApproveOrderCreation)
                ((PaymentResult)response).ApproveOrderCreation = false;

            // give response
            return response;
        }
        /// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="invoice">The invoice to be the payment was applied</param>
        /// <param name="payment">The payment to be refunded</param>
        /// <param name="amount">The amount to be refunded</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult RefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");
            if (!invoice.HasIdentity) return new PaymentResult(Attempt<IPayment>.Fail(new InvalidOperationException("Cannot refund a payment on an invoice that cannot have payments")), invoice, false);

            var operationData = new PaymentOperationData()
            {
                Invoice = invoice,
                Payment = payment,
                Amount = amount,
                ProcessorArgumentCollection = args
            };

            Refunding.RaiseEvent(new SaveEventArgs<PaymentOperationData>(operationData), this);

            var response = PerformRefundPayment(invoice, payment, amount, args);

            RefundAttempted.RaiseEvent(new PaymentAttemptEventArgs<IPaymentResult>(response), this);

            if (!response.Payment.Success) return response;

            AssertInvoiceStatus(invoice);

            // Force the ApproveOrderCreation to false
            if (response.ApproveOrderCreation) ((PaymentResult)response).ApproveOrderCreation = false;

            // give response
            return response;
        }
        /// <summary>
        /// Captures a payment for the <see cref="IInvoice"/>
        /// </summary>
        /// <param name="invoice">The invoice to be paid</param>
        /// <param name="payment">The payment to capture</param>
        /// <param name="amount">The amount of the payment to be captured</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult CapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");

            var operationData = new PaymentOperationData()
                                    {
                                        Invoice = invoice,
                                        Payment = payment,
                                        Amount = amount,
                                        ProcessorArgumentCollection = args
                                    };

            Capturing.RaiseEvent(new SaveEventArgs<PaymentOperationData>(operationData), this);

            // persist the invoice
            if (!invoice.HasIdentity)
                GatewayProviderService.Save(invoice);

            var response = PerformCapturePayment(invoice, payment, amount, args);

            // Special case where the order has already been created due to configuration override.
            if (invoice.Orders.Any() && response.ApproveOrderCreation)
                ((PaymentResult)response).ApproveOrderCreation = false;

            CaptureAttempted.RaiseEvent(new PaymentAttemptEventArgs<IPaymentResult>(response), this);

            if (!response.Payment.Success) return response;

            AssertPaymentApplied(response, invoice);

            AssertInvoiceStatus(invoice);

            // give response
            return response;
        }
        /// <summary>
        /// Processes a payment for the <see cref="IInvoice"/>
        /// </summary>
        /// <param name="invoice">The invoice to be paid</param>
        /// <param name="args">Additional arguments required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        public virtual IPaymentResult AuthorizePayment(IInvoice invoice, ProcessorArgumentCollection args)
        {
            Mandate.ParameterNotNull(invoice, "invoice");

            var operationData = new PaymentOperationData()
            {
                Invoice = invoice,
                ProcessorArgumentCollection = args
            };

            Authorizing.RaiseEvent(new SaveEventArgs<PaymentOperationData>(operationData), this);

            // persist the invoice
            if (!invoice.HasIdentity)
                GatewayProviderService.Save(invoice);

            // collect the payment authorization
            var response = PerformAuthorizePayment(invoice, args);

            AuthorizeAttempted.RaiseEvent(new PaymentAttemptEventArgs<IPaymentResult>(response), this);

            if (!response.Payment.Success) return response;

            AssertPaymentApplied(response, invoice);

            // Check configuration for override on ApproveOrderCreation
            if (!response.ApproveOrderCreation)
                ((PaymentResult)response).ApproveOrderCreation = MerchelloConfiguration.Current.AlwaysApproveOrderCreation;

            // give response
            return response;
        }