/// <summary>
 /// Performs a refund request for the given invoice.
 /// </summary>
 /// <param name="onlinePayment"></param>
 /// <param name="invoice">The invoice that must be refunded.</param>
 public static IPaymentRefundResult Refund(this IOnlinePayment onlinePayment, RefundInvoice invoice)
 => onlinePayment.RefundAsync(invoice)
 .GetAwaiter()
 .GetResult();
Exemplo n.º 2
0
        /// <summary>
        /// Refunds a specific payment by its Order Number.
        /// </summary>
        /// <param name="refundInvoice">RefundInvoice object</param>
        public static async Task <RefundResult> RefundAsync(RefundInvoice refundInvoice)
        {
            if (refundInvoice == null)
            {
                throw new ArgumentNullException(nameof(refundInvoice));
            }

            var paymentData = await SelectPaymentDataByOrderNumberAsync(refundInvoice.OrderNumber);

            if (paymentData == null)
            {
                return(new RefundResult(0, 0, RefundResultStatus.Failed, $"No payment found with order's number: {refundInvoice.OrderNumber}"));
            }

            if (refundInvoice.AmountToRefund > paymentData.Amount)
            {
                return(new RefundResult(0, 0, RefundResultStatus.Failed, $"Amount To Refund cannot be greater than original amount. Original Amount: {paymentData.Amount:N0}. Amount To Refund: {refundInvoice.AmountToRefund:N0}"));
            }

            ThrowExceptionIfGatewayIsNotConfigured(paymentData.Gateway);

            var gateway = GatewayFactory.CreateGateway(paymentData.Gateway);

            var amountToRefund = refundInvoice.AmountToRefund > 0 ? refundInvoice.AmountToRefund : paymentData.Amount;

            var gatewayRefundPaymentContext = new GatewayRefundPaymentContext(paymentData.OrderNumber, amountToRefund, paymentData.ReferenceId, paymentData.TransactionId, paymentData.AdditionalData);

            try
            {
                var refundResult = await gateway.RefundAsync(gatewayRefundPaymentContext);

                //  Log
                TryLog(() => new Log
                {
                    Type          = LogType.Refund,
                    Gateway       = paymentData.Gateway,
                    OrderNumber   = paymentData.OrderNumber,
                    Amount        = paymentData.Amount,
                    Message       = refundResult.Message,
                    CreatedOn     = DateTime.Now,
                    ReferenceId   = paymentData.ReferenceId,
                    TransactionId = paymentData.TransactionId,
                    Status        = refundResult.Status.ToString()
                });

                if (refundResult.IsSuccess())
                {
                    paymentData.Status = PaymentDataStatus.Refunded;

                    await UpdatePaymentDataAsync(paymentData);
                }

                return(refundResult);
            }
            catch (Exception exception)
            {
                //  Log
                TryLog(() => new Log
                {
                    Type          = LogType.Error,
                    Gateway       = paymentData.Gateway,
                    OrderNumber   = paymentData.OrderNumber,
                    Amount        = amountToRefund,
                    Message       = exception.Message,
                    CreatedOn     = DateTime.Now,
                    ReferenceId   = paymentData.ReferenceId,
                    TransactionId = paymentData.TransactionId,
                    Status        = string.Empty
                });

                return(new RefundResult(paymentData.Gateway, 0, RefundResultStatus.Failed, exception.Message));
            }
        }