Пример #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // Set the Magento API Endpoint
                string strResult = getSessionID("http://www.moonclimbing.com", "NAVConnect", "NAVConnect");
                //Dim magev2 As New MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient("Mage_Api_Model_Server_Wsi_HandlerPort", MagentoURL.Text + "/index.php/api/v2_soap/index/")
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.Name          = "BasicHttpBinding_IService1";
                myBinding.Security.Mode = BasicHttpSecurityMode.None;
                myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                myBinding.Security.Transport.ProxyCredentialType  = HttpProxyCredentialType.None;
                myBinding.Security.Message.ClientCredentialType   = BasicHttpMessageCredentialType.UserName;
                myBinding.MaxReceivedMessageSize = 999999;
                EndpointAddress endPointAddress = new EndpointAddress("http://www.moonclimbing.com" + "/index.php/api/v2_soap/index/");
                //MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient magev2 = new MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient(myBinding, endPointAddress);
                MagentoAPIv2.PortTypeClient magev2 = new MagentoAPIv2.PortTypeClient(myBinding, endPointAddress);

                MagentoAPIv2.filters mf = new MagentoAPIv2.filters();
                MagentoAPIv2.catalogProductListRequest ProductListRequest = new MagentoAPIv2.catalogProductListRequest(strResult, mf, "");
                MagentoAPIv2.catalogProductEntity[]    ItemEntityList     = magev2.catalogProductList(strResult, mf, "");
                textBox1.Text = ItemEntityList.Length.ToString();
            }
            catch (Exception exc)
            {
                MagentoAPIv2.catalogProductEntity[] ItemEntityList = null;
            }

            /*
             * int ProductsUpdated = 0;
             *
             * string ConnString = string.Empty;
             * ConnString = "Data Source=" & SQLInstance.Text & ";Integrated Security=SSPI;Initial Catalog=" & NAVDBName.Text
             *
             * If ItemEntityList.Length > 0 Then
             *  For Each ItemEntity As MagentoAPIv2.catalogProductEntity In ItemEntityList
             *      Try
             *          ToolStripStatusLabel1.Text = "Updating.... " & ItemEntity.sku
             *          Me.StatusStrip1.Refresh()
             *
             *          Dim SQLcon As New SqlConnection(ConnString)
             *          Dim updateSql As String = "UPDATE [" & NAVCompanyName.Text & "$Item] set ecommerceID = '" & ItemEntity.product_id & "' where [No_] = '" & ItemEntity.sku & "'"
             *          SQLcon.Open()
             *          Dim UpdateCmd As New SqlCommand(updateSql, SQLcon)
             *          UpdateCmd.ExecuteNonQuery()
             *          SQLcon.Close()
             *          ProductsUpdated = ProductsUpdated + 1
             *      Catch ex As Exception
             *      End Try
             *  Next
             * End If
             *
             *
             * Me.Cursor = Cursors.Default
             *
             * ToolStripStatusLabel1.Text = "Product resync complete - " & ProductsUpdated & " products updated."
             */
        }
Пример #2
0
        public string getSessionID(string MagentoURL, string MagentoAPIUser, string MagentoAPIKey)
        {
            string mLogin = string.Empty;

            BasicHttpBinding myBinding = new BasicHttpBinding();

            myBinding.Name          = "Mage_Api_Model_Server_Wsi_HandlerPort";
            myBinding.Security.Mode = BasicHttpSecurityMode.None;
            myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            myBinding.Security.Transport.ProxyCredentialType  = HttpProxyCredentialType.None;
            myBinding.Security.Message.ClientCredentialType   = BasicHttpMessageCredentialType.UserName;
            myBinding.MaxReceivedMessageSize = 999999999;
            EndpointAddress endPointAddress = new EndpointAddress(MagentoURL + "/index.php/api/v2_soap/index/");

            //MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient magev2 = new MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient(myBinding, endPointAddress);
            MagentoAPIv2.PortTypeClient magev2 = new MagentoAPIv2.PortTypeClient(myBinding, endPointAddress);



            //MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient magev2 = new MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient("Mage_Api_Model_Server_Wsi_HandlerPort", MagentoURL.ToString() + "/index.php/api/v2_soap/index/");

            MagentoAPIv2.loginResponse loginResponse = new MagentoAPIv2.loginResponse();



            // 2.01d CS AMended so it uses the Magento API details stored in Registry
            //Dim loginRequest As New MagentoAPIv2.loginRequest("NAVConnect", "NAVConnect")
            MagentoAPIv2.loginRequest loginRequest = new MagentoAPIv2.loginRequest(MagentoAPIUser, MagentoAPIKey);

            try
            {
                mLogin = magev2.login(MagentoAPIUser, MagentoAPIKey);
            }
            catch (Exception exc)
            {
                mLogin = exc.InnerException.ToString();
            }
            return(mLogin);
        }
Пример #3
0
        public void UploadCustomersToolStripMenuItem(string strSessionID, string MagentoURL,
                                                     string CustomerNo, string CustomerName, string CustomerAddress, string CustomerAddress2, string CustomerCity,
                                                     string CustomerContact, string CustomerPhoneNo, string CustomerPostCode, string CustomerCounty,
                                                     string CustomerE_Mail, string CustomeFaxNo, string CustomerCountry, string CustomereCommerceID, string CustomereCommerceAddressID,
                                                     ref int intEcommerceID, ref int intEcommerceAddressID, ref string strError)
        {
            //  try
            // {   // Set the Magento API Endpoint
            BasicHttpBinding myBinding = new BasicHttpBinding();

            myBinding.Name          = "BasicHttpBinding_IService1";
            myBinding.Security.Mode = BasicHttpSecurityMode.None;
            myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            myBinding.Security.Transport.ProxyCredentialType  = HttpProxyCredentialType.None;
            myBinding.Security.Message.ClientCredentialType   = BasicHttpMessageCredentialType.UserName;
            myBinding.MaxReceivedMessageSize = 999999;
            EndpointAddress endPointAddress = new EndpointAddress(MagentoURL + "/index.php/api/v2_soap/index/");

            //MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient magev2 = new MagentoAPIv2.Mage_Api_Model_Server_Wsi_HandlerPortTypeClient(myBinding, endPointAddress);
            MagentoAPIv2.PortTypeClient magev2 = new MagentoAPIv2.PortTypeClient(myBinding, endPointAddress);

            // Define the new Magento Customer Record
            // StoreID for Moon is 2 but uses default if not specified.
            MagentoAPIv2.customerCustomerEntityToCreate CustomerRecord        = new MagentoAPIv2.customerCustomerEntityToCreate();
            MagentoAPIv2.customerAddressEntityCreate    CustomerAddressRecord = new MagentoAPIv2.customerAddressEntityCreate();

            string CustomerID, CustomerAddressID;
            int    X;
            string NewPassword;
            int    RecordsInserted = 0;
            int    RecordsUpdated  = 0;
            int    RecordsFailed   = 0;

            //Populate the new Magento Record
            if ((CustomereCommerceID != "") && !(string.IsNullOrEmpty(CustomereCommerceAddressID)))
            {
                CustomerID = CustomereCommerceID;
            }
            else
            {
                CustomerID = string.Empty;
            }

            CustomerAddressID    = CustomereCommerceAddressID; //CustomerAddressID = Val(sdr.GetString(13))
            CustomerRecord.email = CustomerE_Mail;             //email - can't be a duplicate otherwise createcustomer fails!!

            //2.02d error: "Index was outside the bounds of the array"
            //Dim Name() As String = Split((sdr.GetString(5)), " ")
            //CustomerRecord.firstname = Name(0)
            //CustomerRecord.lastname = Name(1)
            //2.02d that causes an error when the field GetString(5)=contact is blank or one word
            string[] lstName = CustomerContact.Split(new Char[] { ' ' });
            if (lstName.Length > 1)
            {
                CustomerRecord.firstname = lstName[0];
                CustomerRecord.lastname  = lstName[1];
            }
            else
            {
                CustomerRecord.firstname = CustomerContact;
            }


            //check the value in Magento to assig the right value at 'Associate to Website' by exporting a customer in csv
            // CustomerRecord.store_idSpecified = true;
            //  CustomerRecord.store_id = 1;
            // CustomerRecord.website_idSpecified = true;
            //  CustomerRecord.website_id = 1;
            //2.02d


            // V1.03d Replace old password details with random generated 10 character password
            if (string.IsNullOrEmpty(CustomerID))
            {
                //CustomerRecord.password = "******"
                //New code to generate a random password
                NewPassword = "";
                Random ran = new Random();
                for (int i = 1; i <= 2; i++)
                {
                    NewPassword = NewPassword + (ran.Next(1, 9999) * 10).ToString();
                }
                CustomerRecord.password = "******";     //NewPassword;
            }

            //CustomerRecord.store_id = 2
            //CustomerRecord.website_id =

            CustomerAddressRecord.city                         = CustomerCity;
            CustomerAddressRecord.company                      = CustomerName;
            CustomerAddressRecord.country_id                   = CustomerCountry; //Country code
            CustomerAddressRecord.fax                          = CustomeFaxNo;
            CustomerAddressRecord.firstname                    = CustomerRecord.firstname;
            CustomerAddressRecord.is_default_billing           = true;
            CustomerAddressRecord.is_default_billingSpecified  = true;
            CustomerAddressRecord.is_default_shipping          = true;
            CustomerAddressRecord.is_default_shippingSpecified = true;
            CustomerAddressRecord.lastname                     = CustomerRecord.lastname;
            CustomerAddressRecord.postcode                     = CustomerPostCode;
            CustomerAddressRecord.region                       = CustomerCounty;
            string[] lstStreet = new string[2];
            lstStreet[0] = CustomerAddress;
            lstStreet[1] = CustomerAddress2;
            CustomerAddressRecord.street    = lstStreet;
            CustomerAddressRecord.telephone = CustomerPhoneNo;

            if (CustomerAddressRecord.telephone == "")
            {
                CustomerAddressRecord.telephone = ".";
            }

            int intCustomerID;

            if (!Int32.TryParse(CustomerID, out intCustomerID))
            {
                intCustomerID = 0;
            }
            else
            {
                Int32.TryParse(CustomerID, out intCustomerID);
            }
            //CUSTOMER RECORD
            if (!string.IsNullOrEmpty(CustomerID))
            {
                //Update CUSTOMER
                try
                {       //magev2.customerCustomerUpdate(strSessionID, CustomerID, CustomerRecord);
                    magev2.customerCustomerUpdate(strSessionID, intCustomerID, CustomerRecord);
                    RecordsUpdated = RecordsUpdated + 1;
                }
                catch (Exception e)
                {
                    // If it fails, try again because Magento often returns a comms error
                    try
                    {
                        magev2.customerCustomerUpdate(strSessionID, intCustomerID, CustomerRecord);
                        RecordsUpdated = RecordsUpdated + 1;
                    }
                    catch (Exception ex2)
                    {
                        RecordsFailed = RecordsFailed + 1;
                        strError      = strError + "Failed: customer no: " + CustomerID + " ";
                    }
                }
            }
            else
            {
                // insert the record and update the NAV customer with the ID
                try
                {
                    //CustomerCreateResponse = magev2.customerCustomerCreate(CustomerCreateRequest)
                    intEcommerceID  = magev2.customerCustomerCreate(strSessionID, CustomerRecord);
                    RecordsInserted = RecordsInserted + 1;
                    intCustomerID   = intEcommerceID;
                }
                catch (Exception ex)
                {
                    // Magento API often returns a communication error so have a 2nd try if it fails
                    // try
                    //  {
                    intEcommerceID  = magev2.customerCustomerCreate(strSessionID, CustomerRecord);
                    RecordsInserted = RecordsInserted + 1;
                    intCustomerID   = intEcommerceID;
                    //  }
                    // catch (Exception exc2)
                    //{
                    // if it fails, try to update the record
                    //    RecordsFailed = RecordsFailed + 1;
                    //     strError = strError + exc2.InnerException.Message.ToString();//"Failed: customer no: " + CustomerID + " ";
                    // }
                }
            }     //end else

            //CUSTOMER ADDRESS RECORD  The Customer address ID is already set then update the record
            if (!string.IsNullOrEmpty(CustomerAddressID))
            {
                int intCustomerAddressID;
                if (Int32.TryParse(CustomerAddressID, out intCustomerAddressID))
                {
                    intCustomerAddressID = 0;
                }
                else
                {
                    Int32.TryParse(CustomerAddressID, out intCustomerAddressID);
                }

                //Update Customer address ID
                try
                {
                    magev2.customerAddressUpdate(strSessionID, intCustomerAddressID, CustomerAddressRecord);
                }
                catch (Exception e)
                {
                    // If it fails, try again because Magento often returns a comms error
                    try
                    {
                        magev2.customerAddressUpdate(strSessionID, intCustomerAddressID, CustomerAddressRecord);
                    }
                    catch (Exception ex2)
                    {
                        strError = strError + "Failed: customer address no: " + CustomerAddressID + " ";
                    }
                }
            }
            else
            {       //Create a new record and update the NAV Customer Record with the ID
                try
                {
                    intEcommerceAddressID = magev2.customerAddressCreate(strSessionID, intCustomerID, CustomerAddressRecord);
                }
                catch (Exception ex)
                {
                    // Magento API often returns a communication error so have a 2nd try if it fails
                    try
                    {
                        intEcommerceAddressID = magev2.customerAddressCreate(strSessionID, intCustomerID, CustomerAddressRecord);
                    }
                    catch (Exception exc2)
                    {
                        // if it fails, try to update the record
                        strError = strError + exc2.InnerException.Message.ToString();     //"Failed: customer no: " +  CustomerID + " ";
                    }
                }
            }     //end else

            //}
            //catch (Exception overallE)
            // {
            //    strError = strError + "issue with connection";
            //}
        }