/// <summary> /// Process request to Payture server synchronously /// </summary> /// <returns>PaytureResponse - response from the Payture server. In case of exeption will be return PaytureResponse with exeption mesage in ErrCode field.</returns> public PaytureResponse ProcessOperation() { if (!_expanded) { return(PaytureResponse.ErrorResponse(this, "Params is not setted")); } if (Command == PaytureCommands.Init) { try { var operationResult = PostAsync(GetPath(), FormContent()).ContinueWith(r => ParseResponseInternal(r, Command, SessionType)).ContinueWith(r => FormRedirectURL(r)); operationResult.Wait(); return(operationResult.Result); } catch (Exception ex) { return(PaytureResponse.ErrorResponse(this, $"Error occurs{Environment.NewLine}Message: [{ex.Message}]{Environment.NewLine}StackTrace: {ex.StackTrace}")); } } else { try { var operationResult = PostAsync(GetPath(), FormContent()).ContinueWith(r => ParseResponseInternal(r, Command, SessionType)); operationResult.Wait(); return(operationResult.Result); } catch (Exception ex) { return(PaytureResponse.ErrorResponse(this, $"Error occurs{Environment.NewLine}Message: [{ex.Message}]{Environment.NewLine}StackTrace: {ex.StackTrace}")); } } }
/// <summary> /// Helper method for parsing received response (that in XML format) /// </summary> /// <param name="body">String representation of response body</param> /// <param name="command"></param> /// <returns>response object</returns> protected PaytureResponse ParseXMLResponse(string body, PaytureCommands command) { XElement xmlBody = XElement.Parse(body); var attributes = new Dictionary <string, string>(); var attrs = xmlBody.Attributes(); if (attrs != null) { foreach (var val in attrs.Select(n => new KeyValuePair <string, string>(n.Name.LocalName, n.Value))) { attributes.Add(val.Key, val.Value); } } var elems = ParseXMLElement(xmlBody); var paytureResponse = new PaytureResponse { Success = Boolean.Parse(attributes.Where(n => n.Key == "Success").FirstOrDefault().Value), ErrCode = attributes.Where(n => n.Key == "ErrCode").FirstOrDefault().Value, Attributes = attributes, InternalElements = elems, ResponseBodyXML = body }; if (command == PaytureCommands.GetList) { paytureResponse.ListCards = new List <CardInfo>(); var cards = xmlBody.Descendants().Where(n => n.Name == "Item").Select(n => new CardInfo(n.Attribute("CardName").Value, n.Attribute("CardId").Value, n.Attribute("CardHolder").Value, n.Attribute("Status").Value, Boolean.Parse(n.Attribute("Expired").Value), Boolean.Parse(n.Attribute("NoCVV").Value))); paytureResponse.ListCards.AddRange(cards); } // OnParseResponse( paytureResponse ); return(paytureResponse); }
/// <summary> /// Process request to Payture server synchronously /// </summary> /// <returns>PaytureResponse - response from the Payture server.</returns> public async Task <PaytureResponse> ProcessOperationAsync() { if (!_expanded) { return(PaytureResponse.ErrorResponse(this, "Params are not set")); } if (Command == PaytureCommands.Init) { return(await PostAsync(GetPath(), FormContent()).ContinueWith(r => ParseResponseInternal(r, Command, SessionType)).ContinueWith(r => FormRedirectURL(r))); } return(await PostAsync(GetPath(), FormContent()).ContinueWith(r => ParseResponseInternal(r, Command, SessionType))); }