Exemplo n.º 1
0
        /// <summary>
        /// Handle admin tabstrip created event
        /// </summary>
        /// <param name="eventMessage">Event message</param>
        public async void HandleEvent(AdminTabStripCreated eventMessage)
        {
            if (eventMessage?.Helper == null)
            {
                return;
            }

            //we need customer details page
            var tabsElementId = "customer-edit";

            if (!eventMessage.TabStripName.Equals(tabsElementId))
            {
                return;
            }

            //check whether the payment plugin is installed and is active
            var worldpayPaymentMethod = _paymentService.LoadPaymentMethodBySystemName(WorldpayPaymentDefaults.SystemName);

            if (!(worldpayPaymentMethod?.PluginDescriptor?.Installed ?? false) || !_paymentService.IsPaymentMethodActive(worldpayPaymentMethod))
            {
                return;
            }

            //get the view model
            if (!(eventMessage.Helper.ViewData.Model is CustomerModel customerModel))
            {
                return;
            }

            //check whether a customer exists and isn't guest
            var customer = _customerService.GetCustomerById(customerModel.Id);

            if (customer == null || customer.IsGuest())
            {
                return;
            }

            //try to get stored in Vault customer
            var vaultCustomer = _worldpayPaymentManager.GetCustomer(_genericAttributeService.GetAttribute <string>(customer, WorldpayPaymentDefaults.CustomerIdAttribute));

            //prepare model
            var model = new WorldpayCustomerModel
            {
                Id                 = customerModel.Id,
                CustomerExists     = vaultCustomer != null,
                WorldpayCustomerId = vaultCustomer?.CustomerId
            };

            //create a new tab
            var tabName      = _localizationService.GetResource("Plugins.Payments.Worldpay.WorldpayCustomer");
            var url          = "~/Plugins/Payments.Worldpay/Views/Customer/_CreateOrUpdate.Worldpay.cshtml";
            var contentModel = (await eventMessage.Helper.PartialAsync(url, model)).RenderHtmlContent()
                               .Replace("</script>", "<\\/script>"); //we need escape a closing script tag to prevent terminating the script block early
            var worldpayCustomerTab = eventMessage.TabContentByModel("tab-worldpay", tabName, contentModel);

            //add this tab as a block to render on the customer details page
            eventMessage.BlocksToRender.Add(worldpayCustomerTab);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handle admin tabstrip created event
        /// </summary>
        /// <param name="eventMessage">Event message</param>
        public void HandleEvent(AdminTabStripCreated eventMessage)
        {
            if (eventMessage?.Helper == null)
            {
                return;
            }

            //we need customer details page
            var tabsElementId = "customer-edit";

            if (!eventMessage.TabStripName.Equals(tabsElementId))
            {
                return;
            }

            //check whether the payment plugin is installed and is active
            var worldpayPaymentMethod = _paymentService.LoadPaymentMethodBySystemName(WorldpayPaymentDefaults.SystemName);

            if (!(worldpayPaymentMethod?.PluginDescriptor?.Installed ?? false) || !worldpayPaymentMethod.IsPaymentMethodActive(_paymentSettings))
            {
                return;
            }

            //get the view model
            if (!(eventMessage.Helper.ViewData.Model is CustomerModel customerModel))
            {
                return;
            }

            //check whether a customer exists and isn't guest
            var customer = _customerService.GetCustomerById(customerModel.Id);

            if (customer == null || customer.IsGuest())
            {
                return;
            }

            //try to get stored in Vault customer
            var vaultCustomer = _worldpayPaymentManager.GetCustomer(customer.GetAttribute <string>(WorldpayPaymentDefaults.CustomerIdAttribute));

            //prepare model
            var model = new WorldpayCustomerModel
            {
                Id                 = customerModel.Id,
                CustomerExists     = vaultCustomer != null,
                WorldpayCustomerId = vaultCustomer?.CustomerId
            };

            //compose script to create a new tab
            var worldpayCustomerTabElementId = "tab-worldpay";
            var worldpayCustomerTab          = new HtmlString($@"
                <script>
                    $(document).ready(function() {{
                        $(`
                            <li>
                                <a data-tab-name='{worldpayCustomerTabElementId}' data-toggle='tab' href='#{worldpayCustomerTabElementId}'>
                                    {_localizationService.GetResource("Plugins.Payments.Worldpay.WorldpayCustomer")}
                                </a>
                            </li>
                        `).appendTo('#{tabsElementId} .nav-tabs:first');
                        $(`
                            <div class='tab-pane' id='{worldpayCustomerTabElementId}'>
                                {
                                    eventMessage.Helper.Partial("~/Plugins/Payments.Worldpay/Views/Customer/_CreateOrUpdate.Worldpay.cshtml", model).RenderHtmlContent()
                                        .Replace("</script>", "<\\/script>") //we need escape a closing script tag to prevent terminating the script block early
                                }
                            </div>
                        `).appendTo('#{tabsElementId} .tab-content:first');
                    }});
                </script>");

            //add this tab as a block to render on the customer details page
            eventMessage.BlocksToRender.Add(worldpayCustomerTab);
        }