public async Task <IActionResult> AddHeidelpayPayment([FromBody] ODataActionParameters value)
        {
            CommandsController commandsController = this;

            if (!commandsController.ModelState.IsValid || value == null)
            {
                return(new BadRequestObjectResult(commandsController.ModelState));
            }

            if (!value.ContainsKey("cartId") || !value.ContainsKey("payment"))
            {
                return(new BadRequestObjectResult(value));
            }

            string cartId  = value["cartId"].ToString();
            string payment = value["payment"].ToString();

            if (string.IsNullOrEmpty(cartId) || string.IsNullOrEmpty(payment))
            {
                return(new BadRequestObjectResult(value));
            }

            HeidelpayPaymentComponent paymentComponent = JsonConvert.DeserializeObject <HeidelpayPaymentComponent>(payment);
            AddPaymentsCommand        command          = commandsController.Command <AddPaymentsCommand>();

            Cart cart = await command.Process(commandsController.CurrentContext, cartId, new List <PaymentComponent>() {
                paymentComponent
            });

            return(new ObjectResult(command));
        }
        public override void Process(ServicePipelineArgs args)
        {
            Assert.IsNotNull(args, "args");
            Assert.IsNotNull(args.Request, "args.Request");
            Assert.IsNotNull(args.Result, "args.Result");
            Assert.IsTrue(args.Request is AddPaymentInfoRequest, "args.Request is AddPaymentInfoRequest");
            Assert.IsTrue(args.Result is AddPaymentInfoResult, "args.Result is AddPaymentInfoResult");
            var request = (AddPaymentInfoRequest)args.Request;
            var result  = (AddPaymentInfoResult)args.Result;

            Assert.IsNotNull(request.Cart, "request.Cart");
            Assert.IsNotNull(request.Payments, "request.Payments");
            Cart cart = request.Cart;

            Container container = GetContainer(cart.ShopName, cart.UserId, cart.CustomerId, "", args.Request.CurrencyCode, new DateTime?());

            var heidelpayPayments = request.Payments.OfType <HeidelpayPaymentInfo>();

            if (!heidelpayPayments.Any())
            {
                return;
            }

            foreach (var heidelpayPayment in heidelpayPayments)
            {
                CommerceParty entity = cart.Parties.FirstOrDefault(a => a.ExternalId != null && a.ExternalId.Equals(heidelpayPayment.PartyID, StringComparison.OrdinalIgnoreCase)) as CommerceParty;
                Assert.IsNotNull(entity, "Billing address can not be null");

                var heidelpayPaymentComponent = new HeidelpayPaymentComponent
                {
                    Id            = Guid.NewGuid().ToString("N"),
                    PaymentMethod = new EntityReference()
                    {
                        EntityTarget = heidelpayPayment.PaymentMethodID
                    },
                    Amount       = Money.CreateMoney(heidelpayPayment.Amount),
                    BillingParty = TranslateEntityToParty(entity, new Party(), result)
                };

                heidelpayPaymentComponent.BillingParty.ExternalId = entity.ExternalId;

                var command = Proxy.DoCommand(container.AddHeidelpayPayment(cart.ExternalId, heidelpayPaymentComponent));
            }

            Sitecore.Commerce.Plugin.Carts.Cart cart1 = GetCart(cart.UserId, cart.ShopName, cart.ExternalId, cart.CustomerId, args.Request.CurrencyCode);
            if (cart1 != null)
            {
                result.Cart     = TranslateCartToEntity(cart1, result);
                result.Payments = result.Cart.Payment.ToList();
            }

            // Abort the rest of the pipeline. Because the default commerce engine connect processors will fail
            // if there is a payment other than creditcard or giftcard.
            args.AbortPipeline();
        }
        private static void UpdatePayment(Dictionary <string, string> parameters, HeidelpayPaymentComponent payment)
        {
            var status    = parameters["PROCESSING.RESULT"];
            var timestamp = parameters["PROCESSING.TIMESTAMP"];

            payment.IsSettled = string.Compare("ACK", status, true) == 0;

            DateTime settledAt;

            if (DateTime.TryParse(timestamp, out settledAt))
            {
                payment.SettledAt = settledAt;
            }
        }
        protected virtual void PopulatePaymentDetails(EntityView view, HeidelpayPaymentComponent heidelpayPayment)
        {
            if (view == null || heidelpayPayment == null)
            {
                return;
            }

            view.Properties.Add(new ViewProperty
            {
                Name       = "ItemId",
                IsReadOnly = true,
                IsHidden   = true,
                RawValue   = heidelpayPayment.Id
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "Type",
                IsReadOnly = true,
                RawValue   = heidelpayPayment.GetType().Name
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = nameof(heidelpayPayment.Amount),
                IsReadOnly = true,
                RawValue   = heidelpayPayment.Amount
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = nameof(heidelpayPayment.IsSettled),
                IsReadOnly = true,
                RawValue   = heidelpayPayment.IsSettled
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = nameof(heidelpayPayment.SettledAt),
                IsReadOnly = true,
                RawValue   = heidelpayPayment.SettledAt
            });

            PopulateBillingParty(view, heidelpayPayment);
        }
        public override async Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
        {
            Condition.Requires(entityView).IsNotNull(string.Format("{0}: The argument cannot be null.", Name));

            var request = context.CommerceContext.GetObject <EntityViewArgument>();

            if (string.IsNullOrEmpty(request != null ? request.ViewName : null) ||
                !request.ViewName.Equals(context.GetPolicy <KnownPaymentsViewsPolicy>().OrderPayments, StringComparison.OrdinalIgnoreCase) &&
                !request.ViewName.Equals(context.GetPolicy <KnownOrderViewsPolicy>().Master, StringComparison.OrdinalIgnoreCase) &&
                !request.ViewName.Equals(context.GetPolicy <KnownPaymentsViewsPolicy>().OrderPaymentDetails, StringComparison.OrdinalIgnoreCase) ||
                (!(request.Entity is Order) || !string.IsNullOrEmpty(request.ForAction)))
            {
                return(entityView);
            }

            Order order = (Order)request.Entity;

            if (!order.HasComponent <PaymentComponent>())
            {
                return(entityView);
            }

            List <PaymentComponent> payments;

            if (order.HasComponent <OnHoldOrderComponent>())
            {
                payments = (await _getOnHoldOrderCartCommand.Process(context.CommerceContext, order)).Components.OfType <PaymentComponent>().ToList();
            }
            else
            {
                payments = order.Components.OfType <PaymentComponent>().ToList();
            }

            HeidelpayPaymentComponent heidelpayComponent = null;

            if (request.ViewName.Equals(context.GetPolicy <KnownOrderViewsPolicy>().Master, StringComparison.OrdinalIgnoreCase) ||
                request.ViewName.Equals(context.GetPolicy <KnownPaymentsViewsPolicy>().OrderPayments, StringComparison.OrdinalIgnoreCase))
            {
                EntityView childPaymentView = entityView;
                if (request.ViewName.Equals(context.GetPolicy <KnownOrderViewsPolicy>().Master, StringComparison.OrdinalIgnoreCase))
                {
                    childPaymentView = entityView.ChildViews.Cast <EntityView>().FirstOrDefault(
                        ev => ev.Name.Equals(context.GetPolicy <KnownPaymentsViewsPolicy>().OrderPayments, StringComparison.OrdinalIgnoreCase));
                }

                if (childPaymentView != null)
                {
                    childPaymentView.ChildViews
                    .Where(cv => cv.Name.Equals(context.GetPolicy <KnownPaymentsViewsPolicy>().OrderPaymentDetails, StringComparison.OrdinalIgnoreCase))
                    .Cast <EntityView>().ToList().ForEach(paymentView =>
                    {
                        heidelpayComponent = payments.FirstOrDefault(s => s.Id.Equals(paymentView.ItemId, StringComparison.OrdinalIgnoreCase)) as HeidelpayPaymentComponent;
                        if (heidelpayComponent != null)
                        {
                            PopulatePaymentDetails(paymentView, heidelpayComponent);
                        }
                    });
                }

                return(entityView);
            }

            heidelpayComponent = payments.FirstOrDefault(s => s.Id.Equals(request.ItemId, StringComparison.OrdinalIgnoreCase)) as HeidelpayPaymentComponent;
            if (heidelpayComponent != null)
            {
                PopulatePaymentDetails(entityView, heidelpayComponent);
            }
            return(entityView);
        }
        protected virtual void PopulateBillingParty(EntityView view, HeidelpayPaymentComponent heidelpayPayment)
        {
            Party billingParty = heidelpayPayment.BillingParty;

            view.Properties.Add(new ViewProperty
            {
                Name       = "FirstName",
                IsReadOnly = true,
                RawValue   = billingParty.FirstName
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "LastName",
                IsReadOnly = true,
                RawValue   = billingParty.LastName
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "Address1",
                IsReadOnly = true,
                RawValue   = billingParty.Address1
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "Address2",
                IsReadOnly = true,
                RawValue   = billingParty.Address2
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "City",
                IsReadOnly = true,
                RawValue   = billingParty.City
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "State",
                IsReadOnly = true,
                RawValue   = billingParty.State
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "CountryCode",
                IsReadOnly = true,
                RawValue   = billingParty.CountryCode,
                IsHidden   = true
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "ZipPostalCode",
                IsReadOnly = true,
                RawValue   = billingParty.ZipPostalCode
            });

            view.Properties.Add(new ViewProperty
            {
                Name       = "PhoneNumber",
                IsReadOnly = true,
                RawValue   = billingParty.PhoneNumber
            });
        }