public IPaymentCommand CreateRefundCommand(XmlElement settings)
        {
            RefundCommand cmd = new RefundCommand(_transport, this);

            cmd.Status += RaiseIntermediateStatusEvent;
            ReadSettings(cmd, settings);
            return(cmd);
        }
예제 #2
0
        public JsonResult Refund(IEnumerable <CoinViewModel> deposit)
        {
            Contract.Requires(deposit != null);

            var depositCoins = deposit
                               .Select(x => new Coin(x.ParValue, x.Count))
                               .ToArray();
            var command = new RefundCommand(depositCoins);
            var @event  = _refundCommandHandler.Execute(command);

            return(Json(@event));
        }
예제 #3
0
        public async Task <IList <string> > Handle(RefundCommand command, CancellationToken cancellationToken)
        {
            var paymentTransaction = command.PaymentTransaction;

            if (paymentTransaction == null)
            {
                throw new ArgumentNullException(nameof(command.PaymentTransaction));
            }

            //if (!await CanRefund(order))
            //    throw new GrandException("Cannot do refund for order.");

            var request = new RefundPaymentRequest();
            RefundPaymentResult result = null;

            try
            {
                request.PaymentTransaction = paymentTransaction;
                request.AmountToRefund     = paymentTransaction.PaidAmount;
                request.IsPartialRefund    = false;
                result = await _paymentService.Refund(request);

                if (result.Success)
                {
                    paymentTransaction.TransactionStatus = result.NewTransactionStatus;
                    paymentTransaction.RefundedAmount   += request.AmountToRefund;
                    await _paymentTransactionService.UpdatePaymentTransaction(paymentTransaction);

                    var order = await _orderService.GetOrderByGuid(paymentTransaction.OrderGuid);

                    if (order == null)
                    {
                        throw new ArgumentNullException(nameof(order));
                    }

                    decimal totalAmountRefunded = order.RefundedAmount + request.AmountToRefund;

                    //update order info
                    order.RefundedAmount  = totalAmountRefunded;
                    order.PaymentStatusId = order.RefundedAmount == order.OrderTotal ? Domain.Payments.PaymentStatus.Refunded : Domain.Payments.PaymentStatus.PartiallyRefunded;
                    await _orderService.UpdateOrder(order);

                    //check order status
                    await _mediator.Send(new CheckOrderStatusCommand()
                    {
                        Order = order
                    });

                    //notifications
                    var orderRefundedStoreOwnerNotificationQueuedEmailId = await _messageProviderService.SendOrderRefundedStoreOwnerMessage(order, request.AmountToRefund, _languageSettings.DefaultAdminLanguageId);

                    if (orderRefundedStoreOwnerNotificationQueuedEmailId > 0)
                    {
                        await _orderService.InsertOrderNote(new OrderNote
                        {
                            Note = "Order refunded email (to store owner) has been queued.",
                            DisplayToCustomer = false,
                            CreatedOnUtc      = DateTime.UtcNow,
                            OrderId           = order.Id,
                        });
                    }

                    //notifications
                    var orderRefundedCustomerNotificationQueuedEmailId = await _messageProviderService.SendOrderRefundedCustomerMessage(order, request.AmountToRefund, order.CustomerLanguageId);

                    if (orderRefundedCustomerNotificationQueuedEmailId > 0)
                    {
                        await _orderService.InsertOrderNote(new OrderNote
                        {
                            Note = "Order refunded email (to customer) has been queued.",
                            DisplayToCustomer = false,
                            CreatedOnUtc      = DateTime.UtcNow,
                            OrderId           = order.Id,
                        });
                    }

                    //raise event
                    await _mediator.Publish(new PaymentTransactionRefundedEvent(paymentTransaction, request.AmountToRefund));
                }
            }
            catch (Exception exc)
            {
                if (result == null)
                {
                    result = new RefundPaymentResult();
                }
                result.AddError(string.Format("Error: {0}. Full exception: {1}", exc.Message, exc.ToString()));
            }

            //process errors
            string error = "";

            for (int i = 0; i < result.Errors.Count; i++)
            {
                error += string.Format("Error {0}: {1}", i, result.Errors[i]);
                if (i != result.Errors.Count - 1)
                {
                    error += ". ";
                }
            }
            if (!String.IsNullOrEmpty(error))
            {
                //log it
                string logError = string.Format("Error refunding order #{0}. Error: {1}", paymentTransaction.OrderCode, error);
                await _logger.InsertLog(LogLevel.Error, logError, logError);
            }
            return(result.Errors);
        }
        public async Task Consume(ConsumeContext <RefundCommand> context)
        {
            RefundCommand refundCommand = context.Message;

            await _paymentServiceClient.RefundAsync(refundCommand.CorrelationId);
        }