Jordan Trajkov

Jordan Trajkov

  • 1.8k
  • 88
  • 2.1k

Create new product in paypal for subscription with REST API

Sep 20 2019 2:04 PM
Hello, i'm trying to crate new product, plan and subscription using paypal REST API. I'm using c# code in .net. Everything works find but the response is with status: 200 and it means OK but in the documentation i see that i have to receive a response with status: 201 CREATED. Also in my response there is not info about the created product. I'm not sure if i'm missing somethings because if the product is created i have to receive back product's information. This is the code i use:
 
  1. public void CreateProduct()  
  2.         {  
  3.             try  
  4.             {  
  5.                 string URL = "https://api.sandbox.paypal.com/v1/catalogs/products";  
  6.                 string urlParameters = "?name=testName";  
  7.   
  8.                 // using System.Net;  
  9.                 ServicePointManager.Expect100Continue = true;  
  10.                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;  
  11.                 // Use SecurityProtocolType.Ssl3 if needed for compatibility reasons  
  12.   
  13.                 HttpClient client = new HttpClient();  
  14.                 client.BaseAddress = new Uri(URL);  
  15.   
  16.                 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer""Access-Token-Is-Here-I-Remove-It-For-Security-Purpose");  
  17.                 client.DefaultRequestHeaders.Add("PayPal-Request-Id""ERRCAT001");   
  18.                 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  19.   
  20.                 // List data response.  
  21.                 HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.  
  22.                 if (response.IsSuccessStatusCode)  
  23.                 {  
  24.                     // Parse the response body.  
  25.                     //var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;  //Make sure to add a reference to System.Net.Http.Formatting.dll  
  26.                     //foreach (var d in dataObjects)  
  27.                     //{  
  28.                     //    Console.WriteLine("{0}", d.Name);  
  29.                     //}  
  30.                 }  
  31.                 else  
  32.                 {  
  33.                     Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);  
  34.                 }  
  35.   
  36.                 //Make any other calls using HttpClient here.  
  37.   
  38.                 //Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.  
  39.                 client.Dispose();  
  40.             }  
  41.             catch (Exception err)  
  42.             {  
  43.                 string error = err.Message;  
  44.             }  
  45.         }