/// <summary>
        /// Refunds an order (from admin panel)
        /// </summary>
        /// <param name="order">Order</param>
        /// <returns>A list of errors; empty list if no errors</returns>
        public virtual IList<string> Refund(Order order)
        {
            if (order == null)
                throw new ArgumentNullException("order");

            if (!CanRefund(order))
                throw new SmartException("Cannot do refund for order.");

            var request = new RefundPaymentRequest();
            RefundPaymentResult result = null;
            try
            {
                request.Order = order;
                request.AmountToRefund = order.OrderTotal;
                request.IsPartialRefund = false;
                result = _paymentService.Refund(request);
                if (result.Success)
                {
                    //total amount refunded
                    decimal totalAmountRefunded = order.RefundedAmount + request.AmountToRefund;

                    //update order info
                    order.RefundedAmount = totalAmountRefunded;
                    order.PaymentStatus = result.NewPaymentStatus;
                    _orderService.UpdateOrder(order);

                    //add a note
                    order.OrderNotes.Add(new OrderNote()
                    {
                        Note = string.Format(T("OrderRefunded"), _priceFormatter.FormatPrice(request.AmountToRefund, true, false)),
                        DisplayToCustomer = false,
                        CreatedOnUtc = DateTime.UtcNow
                    });
                    _orderService.UpdateOrder(order);

                    //check order status
                    CheckOrderStatus(order);
                }

            }
            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))
            {
                //add a note
                order.OrderNotes.Add(new OrderNote()
                {
                    Note = string.Format(T("OrderRefundError"), error),
                    DisplayToCustomer = false,
                    CreatedOnUtc = DateTime.UtcNow
                });
                _orderService.UpdateOrder(order);

                //log it
                string logError = string.Format("Error refunding order '{0}'. Error: {1}", order.GetOrderNumber(), error);
                _logger.InsertLog(LogLevel.Error, logError, logError);
            }
            return result.Errors;
        }
        /// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="refundPaymentRequest">Request</param>
        /// <returns>Result</returns>
        public virtual RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            var paymentMethod = LoadPaymentMethodBySystemName(refundPaymentRequest.Order.PaymentMethodSystemName);
            if (paymentMethod == null)
                throw new SmartException("Payment method couldn't be loaded");

			try
			{
				return paymentMethod.Value.Refund(refundPaymentRequest);
			}
			catch (NotSupportedException)
			{
				var result = new RefundPaymentResult();
				result.AddError(_localizationService.GetResource("Common.Payment.NoRefundSupport"));
				return result;
			}
			catch
			{
				throw;
			}
        }
        public RefundPaymentResult Refund(RefundPaymentRequest request)
        {
            var result = new RefundPaymentResult()
            {
                NewPaymentStatus = request.Order.PaymentStatus
            };

            try
            {
                var settings = _services.Settings.LoadSetting<AmazonPaySettings>(request.Order.StoreId);
                var client = new AmazonPayClient(settings);

                string amazonRefundId = _api.Refund(client, request, result);

                if (amazonRefundId.HasValue() && request.Order.Id != 0)
                {
                    _genericAttributeService.InsertAttribute(new GenericAttribute()
                    {
                        EntityId = request.Order.Id,
                        KeyGroup = "Order",
                        Key = AmazonPayCore.AmazonPayRefundIdKey,
                        Value = amazonRefundId,
                        StoreId = request.Order.StoreId
                    });
                }
            }
            catch (OffAmazonPaymentsServiceException exc)
            {
                LogAmazonError(exc, errors: result.Errors);
            }
            catch (Exception exc)
            {
                LogError(exc, errors: result.Errors);
            }
            return result;
        }
Exemplo n.º 4
0
        public string Refund(AmazonPayClient client, RefundPaymentRequest refund, RefundPaymentResult result)
        {
            result.NewPaymentStatus = refund.Order.PaymentStatus;

            string amazonRefundId = null;
            var request = new RefundRequest();
            request.SellerId = client.Settings.SellerId;
            request.AmazonCaptureId = refund.Order.CaptureTransactionId;
            request.RefundReferenceId = GetRandomId("Refund");
            //request.SellerRefundNote = client.Settings.SellerNoteRefund.Truncate(255);

            request.RefundAmount = new Price()
            {
                Amount = refund.AmountToRefund.ToString("0.00", CultureInfo.InvariantCulture),
                CurrencyCode = refund.Order.CustomerCurrencyCode ?? "EUR"
            };

            var response = client.Service.Refund(request);

            if (response != null && response.IsSetRefundResult() && response.RefundResult.IsSetRefundDetails())
            {
                var details = response.RefundResult.RefundDetails;

                amazonRefundId = details.AmazonRefundId;

                if (details.IsSetRefundStatus() && details.RefundStatus.IsSetState())
                {
                    if (refund.IsPartialRefund)
                        result.NewPaymentStatus = Core.Domain.Payments.PaymentStatus.PartiallyRefunded;
                    else
                        result.NewPaymentStatus = Core.Domain.Payments.PaymentStatus.Refunded;
                }
            }
            return amazonRefundId;
        }
		/// <summary>
		/// Refunds a payment
		/// </summary>
		/// <param name="refundPaymentRequest">Request</param>
		/// <returns>Result</returns>
		public virtual RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
		{
			var result = new RefundPaymentResult();
			result.AddError(T("Common.Payment.NoRefundSupport"));
			return result;
		}
        /// <summary>
        /// Refunds a payment
        /// </summary>
        /// <param name="refundPaymentRequest">Request</param>
        /// <returns>Result</returns>
        public virtual RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            var paymentMethod = LoadPaymentMethodBySystemName(refundPaymentRequest.Order.PaymentMethodSystemName);
            if (paymentMethod == null)
                throw new SmartException(T("Payment.CouldNotLoadMethod"));

            try
            {
                return paymentMethod.Value.Refund(refundPaymentRequest);
            }
            catch (NotSupportedException)
            {
                var result = new RefundPaymentResult();
                result.AddError(T("Common.Payment.NoRefundSupport"));
                return result;
            }
            catch
            {
                throw;
            }
        }
 /// <summary>
 /// Refunds a payment
 /// </summary>
 /// <param name="refundPaymentRequest">Request</param>
 /// <returns>Result</returns>
 public override RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
 {
     var result = new RefundPaymentResult();
     result.AddError(_localizationService.GetResource("Common.Payment.NoRefundSupport"));
     return result;
 }
 /// <summary>
 /// Refunds a payment
 /// </summary>
 /// <param name="refundPaymentRequest">Request</param>
 /// <returns>Result</returns>
 public override RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
 {
     var result = new RefundPaymentResult();
     result.AddError("Refund method not supported");
     return result;
 }
        /// <summary>
        /// Refunds an order (from admin panel)
        /// </summary>
        /// <param name="order">Order</param>
        /// <returns>A list of errors; empty list if no errors</returns>
        public virtual IList<string> Refund(Order order)
        {
            if (order == null)
                throw new ArgumentNullException("order");

            if (!CanRefund(order))
                throw new SmartException(T("Order.CannotRefund"));

            var request = new RefundPaymentRequest();
            RefundPaymentResult result = null;
            try
            {
                request.Order = order;
                request.AmountToRefund = order.OrderTotal;
                request.IsPartialRefund = false;

                result = _paymentService.Refund(request);

                if (result.Success)
                {
                    //total amount refunded
                    decimal totalAmountRefunded = order.RefundedAmount + request.AmountToRefund;

                    //update order info
                    order.RefundedAmount = totalAmountRefunded;
                    order.PaymentStatus = result.NewPaymentStatus;

                    _orderService.UpdateOrder(order);

                    _orderService.AddOrderNote(order, T("Admin.OrderNotice.OrderRefunded", _priceFormatter.FormatPrice(request.AmountToRefund, true, false)));

                    //check order status
                    CheckOrderStatus(order);
                }

            }
            catch (Exception exception)
            {
                if (result == null)
                {
                    result = new RefundPaymentResult();
                }

                result.AddError(exception.ToAllMessages());
            }

            ProcessErrors(order, result.Errors, "Admin.OrderNotice.OrderRefundError");

            return result.Errors;
        }