Exemplo n.º 1
0
        public virtual OrderOperation ToModel(OrderOperation operation)
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            operation.InjectFrom(this);

            operation.ChildrenOperations = OperationUtilities.GetAllChildOperations(operation);
            return(operation);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object retVal;
            var    obj = JObject.Load(reader);

            if (objectType == typeof(PaymentMethod))
            {
                var paymentGatewayCode = obj["code"].Value <string>();
                retVal = _paymentMethodsService.GetAllPaymentMethods().FirstOrDefault(x => x.Code.EqualsInvariant(paymentGatewayCode));
            }
            else if (objectType == typeof(ShippingMethod))
            {
                var shippingGatewayCode = obj["code"].Value <string>();
                retVal = _shippingMethodsService.GetAllShippingMethods().FirstOrDefault(x => x.Code.EqualsInvariant(shippingGatewayCode));
            }
            else
            {
                var tryCreateInstance = typeof(AbstractTypeFactory <>).MakeGenericType(objectType).GetMethods().FirstOrDefault(x => x.Name.EqualsInvariant("TryCreateInstance") && x.GetParameters().Length == 0);
                retVal = tryCreateInstance?.Invoke(null, null);
            }
            //Reset ChildrenOperations property to prevent polymorphic deserialization  error
            var operation = retVal as IOperation;

            if (operation != null)
            {
                obj.Remove("childrenOperations");
            }

            var payment = operation as PaymentIn;

            if (payment != null)
            {
                var paymentStatus  = obj["paymentStatus"].Value <string>();
                var hasStatusValue = Enum.IsDefined(typeof(PaymentStatus), paymentStatus);
                if (!hasStatusValue)
                {
                    obj["paymentStatus"] = PaymentStatus.Custom.ToString();
                }
            }

            serializer.Populate(obj.CreateReader(), retVal);

            // The ChildrenOperations property was reset on lines 57-61, so now we rebuild it
            if (operation != null)
            {
                operation.ChildrenOperations = OperationUtilities.GetAllChildOperations(operation);
            }

            return(retVal);
        }