protected T ApiCall <T>(PaypalOperation operation) where T : IPayPalApiModel, new() { // validating arguments if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.Credential == null) { throw new Exception("Must fill in the Credential property of operation."); } // informations to build request var url = this.EndPoints.GetApiEndPoint(operation.Credential.CredentialType, ApiProtocol.NVP); var nvp = operation.ToNameValueCollection(); byte[] byteArray = GetPostData(nvp); // building request with the above informations var webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = byteArray.Length; // sending data using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(byteArray, 0, byteArray.Length); } // receiving data using (WebResponse webResponse = webRequest.GetResponse()) using (Stream responseStream = webResponse.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { string responseFromServer = reader.ReadToEnd(); // creating result object from response var nvc = HttpUtility.ParseQueryString(responseFromServer); var result = new T(); result.Api = this; nvc.LoadToPayPalModelObject(result); return(result); } }
public void SetupCredential(PaypalOperation operation) { var user = this.configurations.PayPalUser; var password = this.configurations.PayPalPassword; var signature = this.configurations.PayPalSignature; if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(password) && !string.IsNullOrWhiteSpace(signature)) { operation.Credential = new PayPalSignatureCredential { ApiUserName = user, ApiPassword = password, ApiSignature = signature, } } ; if (operation.Credential == null) { throw new Exception("Unsupported credential configuration."); } }