/*
  * Get default signature version for this API Type
  *
  * @return int
  */
 public int getDefaultSignatureVersion()
 {
     if (Paymentwall_Widget.getApiType() != Paymentwall_Widget.API_CART)
     {
         return(Paymentwall_Widget.DEFAULT_SIGNATURE_VERSION);
     }
     else
     {
         return(Paymentwall_Widget.SIGNATURE_VERSION_2);
     }
 }
 /**
  * Build controller URL depending on API type
  *
  * @param string widget code of the widget
  * @param bool flexibleCall
  * @return string
  */
 protected string buildController(string widget, bool flexibleCall = false)
 {
     if (Paymentwall_Widget.getApiType() == Paymentwall_Widget.API_VC)
     {
         if (!Regex.IsMatch(widget, @"^w|s|mw"))
         {
             return(Paymentwall_Widget.CONTROLLER_PAYMENT_VIRTUAL_CURRENCY);
         }
         else
         {
             return("");
         }
     }
     else if (Paymentwall_Widget.getApiType() == Paymentwall_Widget.API_GOODS)
     {
         if (!flexibleCall)
         {
             if (!Regex.IsMatch(widget, @"^w|s|mw"))
             {
                 return(Paymentwall_Widget.CONTROLELR_PAYMENT_DIGITAL_GOODS);
             }
             else
             {
                 return("");
             }
         }
         else
         {
             return(Paymentwall_Widget.CONTROLELR_PAYMENT_DIGITAL_GOODS);
         }
     }
     else
     {
         return(Paymentwall_Widget.CONTROLLER_PAYMENT_CART);
     }
 }
        /*
         * Return URL for the widget
         *
         * @return string
         */
        public string getUrl()
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters["key"]    = Paymentwall_Widget.getAppKey();
            parameters["uid"]    = this.userId;
            parameters["widget"] = this.widgetCode;

            int productsNumber = this.products.Count;

            if (Paymentwall_Widget.getApiType() == Paymentwall_Widget.API_GOODS)
            {
                if (productsNumber > 0)
                {
                    if (productsNumber == 1)
                    {
                        Paymentwall_Product product          = this.products[0];
                        Paymentwall_Product postTrialProduct = null;
                        if (product.getTrialProduct() is Paymentwall_Product)
                        {
                            postTrialProduct = product;
                            product          = product.getTrialProduct();
                        }
                        parameters.Add("amount", product.getAmount().ToString());
                        parameters.Add("currencyCode", product.getCurrencyCode());
                        parameters.Add("ag_name", product.getName());
                        parameters.Add("ag_external_id", product.getId());
                        parameters.Add("ag_type", product.getType());

                        if (product.getType() == Paymentwall_Product.TYPE_SUBSCRIPTION)
                        {
                            parameters.Add("ag_period_length", product.getPeriodLength().ToString());
                            parameters.Add("ag_period_type", product.getPeriodType());

                            if (product.isRecurring())
                            {
                                parameters.Add("ag_recurring", (Convert.ToInt32(product.isRecurring())).ToString());

                                if (postTrialProduct != null)
                                {
                                    parameters.Add("ag_trial", "1");
                                    parameters.Add("ag_post_trial_external_id", postTrialProduct.getId());
                                    parameters.Add("ag_post_trial_period_length", postTrialProduct.getPeriodLength().ToString());
                                    parameters.Add("ag_post_trial_period_type", postTrialProduct.getPeriodType());
                                    parameters.Add("ag_post_trial_name", postTrialProduct.getName());
                                    parameters.Add("post_trial_amount", postTrialProduct.getAmount().ToString());
                                    parameters.Add("post_trial_currencyCode", postTrialProduct.getCurrencyCode().ToString());
                                }
                            }
                        }
                    } //end if (productNumber == 1)

                    else
                    {
                        //TODO: Paymentwall_Widget.appendToErrors('Only 1 product is allowed in flexible widget call');
                    }
                } //end if (productNumber > 0)
            }
            else if (Paymentwall_Widget.getApiType() == Paymentwall_Widget.API_CART)
            {
                int index = 0;

                foreach (Paymentwall_Product product in this.products)
                {
                    parameters.Add("external_ids[" + index.ToString() + "]", product.getId());

                    if (product.getAmount() != -1f)
                    {
                        parameters.Add("prices[" + index.ToString() + "]", product.getAmount().ToString());
                    }

                    if (product.getCurrencyCode() != null)
                    {
                        parameters.Add("currencies[" + index.ToString() + "]", product.getCurrencyCode());
                    }

                    index++;
                }

                index = 0;
            }

            int signatureVersion = this.getDefaultSignatureVersion();

            parameters.Add("sign_version", Convert.ToString(signatureVersion));

            if (this.extraParams.ContainsKey("sign_version"))
            {
                parameters["sign_version"] = this.extraParams["sign_version"];
                signatureVersion           = Convert.ToInt32(this.extraParams["sign_version"]);
            }
            parameters = mergeDictionaries(parameters, extraParams);

            parameters["sign"] = Paymentwall_Widget.calculateSignature(parameters, Paymentwall_Widget.getSecretKey(), signatureVersion);

            return(Paymentwall_Widget.BASE_URL + "/" + this.buildController(this.widgetCode) + "?" + this.buildQueryString(parameters, "&"));
        }