public static ANetApiResponse PostData <TQ, TS>(AuthorizeNET.Environment env, TQ request) where TQ : ANetApiRequest where TS : ANetApiResponse { ANetApiResponse response = null; if (null == request) { throw new ArgumentNullException("request"); } Logger.LogDebug("MerchantInfo->LoginId/TransactionKey: '{0}':'{1}'->{2}", request.merchantAuthentication.name, request.merchantAuthentication.ItemElementName, request.merchantAuthentication.Item); var postUrl = GetPostUrl(env); var requestType = typeof(TQ); string responseAsString = null; using (var clientHandler = new HttpClientHandler()) { //TODO: clientHandler.Proxy = SetProxyIfRequested(clientHandler.Proxy); using (var client = new HttpClient(clientHandler)) { //set the http connection timeout var httpConnectionTimeout = AuthorizeNET.Environment.getIntProperty(Constants.HttpConnectionTimeout); client.Timeout = TimeSpan.FromMilliseconds(httpConnectionTimeout != 0 ? httpConnectionTimeout : Constants.HttpConnectionDefaultTimeout); //set the time out to read/write from stream //var httpReadWriteTimeout = AuthorizeNET.Environment.getIntProperty(Constants.HttpReadWriteTimeout); //client.ReadWriteTimeout = (httpReadWriteTimeout != 0 ? httpReadWriteTimeout : Constants.HttpReadWriteDefaultTimeout); var content = new StringContent(XmlUtility.Serialize(request), Encoding.UTF8, "text/xml"); var webResponse = client.PostAsync(postUrl, content).Result; Logger.LogDebug("Retrieving Response from Url: '{0}'", postUrl); // Get the response Logger.LogDebug("Received Response: '{0}'", webResponse); responseAsString = webResponse.Content.ReadAsStringAsync().Result; Logger.LogDebug("Response from Stream: '{0}'", responseAsString); } } if (null != responseAsString) { try { // try deserializing to the expected response type response = XmlUtility.Deserialize <TS>(responseAsString); } catch (Exception) { // probably a bad response, try if this is an error response response = XmlUtility.Deserialize <ANetApiResponse>(responseAsString); } //if error response if (response is ErrorResponse) { response = response as ErrorResponse; } } return(response); }
private static Uri GetPostUrl(AuthorizeNET.Environment env) { var postUrl = new Uri(env.XmlBaseUrl + "/xml/v1/request.api"); Logger.LogDebug("Creating PostRequest Url: '{0}'", postUrl); return(postUrl); }
public void Execute(AuthorizeNET.Environment environment = null) { BeforeExecute(); //Logger.debug(string.Format(CultureInfo.InvariantCulture, "Executing Request:'{0}'", XmlUtility.GetXml(GetApiRequest()))); if (null == environment) { environment = ApiOperationBase <ANetApiRequest, ANetApiResponse> .RunEnvironment; } if (null == environment) { throw new ArgumentException(NullEnvironmentErrorMessage); } var httpApiResponse = HttpUtility.PostData <TQ, TS>(environment, GetApiRequest()); if (null != httpApiResponse) { Logger.LogDebug("Received Response:'{0}' for request:'{1}'", httpApiResponse, GetApiRequest()); if (httpApiResponse.GetType() == _responseClass) { var response = (TS)httpApiResponse; SetApiResponse(response); Logger.LogDebug("Setting response: '{0}'", response); } else if (httpApiResponse.GetType() == typeof(ErrorResponse)) { SetErrorResponse(httpApiResponse); Logger.LogDebug("Received ErrorResponse:'{0}'", httpApiResponse); } else { SetErrorResponse(httpApiResponse); Logger.LogError("Invalid response:'{0}'", httpApiResponse); } Logger.LogDebug("Response obtained: {0}", GetApiResponse()); SetResultStatus(); } else { Logger.LogDebug("Got a 'null' Response for request:'{0}'\n", GetApiRequest()); } AfterExecute(); }
public TS ExecuteWithApiResponse(AuthorizeNET.Environment environment = null) { Execute(environment); return(GetApiResponse()); }