Jump to content

Webservice & C# - Création d'un client


Recommended Posts

Bonjour à tous,

 

Je développe actuellement un logiciel faisant la liaison entre un site Prestashop et une solution de Gestion Commerciale.

 

Je désire, via les webservice, intégrer un nouveau client sur le site.

 

J'ai donc développé le code suivant:

public void CreateCustomer()
{
     try
     {
                RestClient client = new RestClient();
                client.BaseUrl = this.mo.PrestaShopURL;
                client.Authenticator = new HttpBasicAuthenticator(this.mo.PrestaShopToken, "");

                RestRequest request = new RestRequest();
                request.Resource = "api/customers?schema=synopsis";
                request.Method = Method.GET;

                RestResponse response = client.Execute(request) as RestResponse;

                XElement customerXMLFull = XElement.Parse(response.Content);
                XElement customerXML = customerXMLFull.Descendants().FirstOrDefault();
                XElement customerXML2;

                customerXML2 = customerXML.Element("id_default_group");
                customerXML2.Value = "<![CDATA[" + this.mc.IDDefaultGroup.ToString() + "]]>";
                customerXML2 = customerXML.Element("id_lang");
                customerXML2.Value = "<![CDATA[" + this.mc.IDLang.ToString() + "]]>";
                customerXML2 = customerXML.Element("newsletter_date_add");
                customerXML2.Value = "<![CDATA[" + this.mc.NewsletterDateAdd.ToString() + "]]>";
                customerXML2 = customerXML.Element("ip_registration_newsletter");
                customerXML2.Value = "<![CDATA[" + this.mc.IPRegistrationNewsletter + "]]>";
                customerXML2 = customerXML.Element("last_passwd_gen");
                customerXML2.Value = "<![CDATA[" + this.mc.LastPassWordGen.ToString() + "]]>";
                customerXML2 = customerXML.Element("secure_key");
                customerXML2.Value = "<![CDATA[" + this.mc.SecureKey + "]]>";
                customerXML2 = customerXML.Element("deleted");
                customerXML2.Value = "<![CDATA[" + this.mc.Deleted.ToString() + "]]>";
                customerXML2 = customerXML.Element("passwd");
                customerXML2.Value = "<![CDATA[" + this.mc.Password + "]]>";
                customerXML2 = customerXML.Element("lastname");
                customerXML2.Value = "<![CDATA[" + this.mc.Lastname + "]]>";
                customerXML2 = customerXML.Element("firstname");
                customerXML2.Value = "<![CDATA[" + this.mc.Firstname + "]]>";
                customerXML2 = customerXML.Element("email");
                customerXML2.Value = "<![CDATA[" + this.mc.Email + "]]>";
                customerXML2 = customerXML.Element("id_gender");
                customerXML2.Value = "<![CDATA[" + this.mc.IDGender.ToString() + "]]>";
                customerXML2 = customerXML.Element("birthday");
                customerXML2.Value = "<![CDATA[" + this.mc.Birthday.ToString() + "]]>";
                customerXML2 = customerXML.Element("newsletter");
                customerXML2.Value = "<![CDATA[" + this.mc.Newsletter.ToString() + "]]>";
                customerXML2 = customerXML.Element("optin");
                customerXML2.Value = "<![CDATA[" + this.mc.Optin.ToString() + "]]>";
                customerXML2 = customerXML.Element("website");
                customerXML2.Value = "<![CDATA[" + this.mc.Website + "]]>";
                customerXML2 = customerXML.Element("company");
                customerXML2.Value = "<![CDATA[" + this.mc.Company + "]]>";
                customerXML2 = customerXML.Element("siret");
                customerXML2.Value = "<![CDATA[" + this.mc.Siret + "]]>";
                customerXML2 = customerXML.Element("ape");
                customerXML2.Value = "<![CDATA[" + this.mc.APE + "]]>";
                customerXML2 = customerXML.Element("outstanding_allow_amount");
                customerXML2.Value = "<![CDATA[" + this.mc.OutstandingAllowAmount.ToString() + "]]>";
                customerXML2 = customerXML.Element("show_public_prices");
                customerXML2.Value = "<![CDATA[" + this.mc.ShowPublicPrices.ToString() + "]]>";
                customerXML2 = customerXML.Element("id_risk");
                customerXML2.Value = "<![CDATA[" + this.mc.IDRisk.ToString() + "]]>";
                customerXML2 = customerXML.Element("max_payment_days");
                customerXML2.Value = "<![CDATA[" + this.mc.MaxPaymentDays.ToString() + "]]>";
                customerXML2 = customerXML.Element("active");
                customerXML2.Value = "<![CDATA[" + this.mc.Active.ToString() + "]]>";
                customerXML2 = customerXML.Element("note");
                customerXML2.Value = "<![CDATA[" + this.mc.Note;
                customerXML2 = customerXML.Element("is_guest");
                customerXML2.Value = "<![CDATA[" + this.mc.IsGuest.ToString() + "]]>";
                customerXML2 = customerXML.Element("id_shop");
                customerXML2.Value = "<![CDATA[" + this.mc.IDShop.ToString() + "]]>";
                customerXML2 = customerXML.Element("id_shop_group");
                customerXML2.Value = "<![CDATA[" + this.mc.IDShopGroup.ToString() + "]]>";
                customerXML2 = customerXML.Element("date_add");
                customerXML2.Value = "<![CDATA[" + this.mc.DateAdd.ToString() + "]]>";
                customerXML2 = customerXML.Element("date_upd");
                customerXML2.Value = "<![CDATA[" + this.mc.DateUpd.ToString() + "]]>";
                customerXML2 = customerXML.Element("associations").Descendants("groups").Descendants("group").First();
                customerXML2.Value = "<![CDATA[" + this.mc.Group.ToString() + "]]>";

                request = new RestRequest();
                request.Resource = "customers";
                request.Method = Method.POST;
                request.RequestFormat = DataFormat.Xml;
                request.AddParameter("xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + customerXMLFull);

                client = new RestClient();
                client.BaseUrl = this.mo.PrestaShopURL;
                client.Authenticator = new HttpBasicAuthenticator(this.mo.PrestaShopToken, "");
                request.AddParameter("Account", this.mo.PrestaShopToken, ParameterType.UrlSegment);

                response = client.Execute(request) as RestResponse;
     }
     catch (Exception ex)
     {
                MessageBox.Show(ex.Message);
     }
}

Après exécution du code, le client n'est pas créé et aucune erreur n'est remontée.

 

Toute aide est la bienvenue.

 

Cordialement.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...