The code I want to refactor look as following:
public bool CheckPay(Int64 orderID)
{
vpc_OrderInfo = orderID;
RequestParams =
string.Format(
"vpc_Version={0}&vpc_Command={1}&vpc_AccessCode={2}&vpc_MerchTxnRef={3}&vpc_Merchant={4}&vpc_User={5}&vpc_Password={6}",
vpc_Version, vpc_Command, vpc_AccessCode, vpc_MerchTxnRef, vpc_Merchant, vpc_User, vpc_Password);
string[] response = SendRequest(vpc_VirtualPaymentClientURL, RequestParams).Split('&');
bool ResponseCode = false;
bool exist = false;
Total = 0;
TransactionDate = DateTime.Now;
TransactionId = CardNo = vpc_Message = string.Empty;
var query = new StringBuilder();
try
{
foreach (string s in response)
{
if (string.IsNullOrWhiteSpace(s))
throw new Exception(string.Format("Message='{0}' ", s));
if (s.IndexOf("vpc_SecureHash") == -1)
{
string vpcMessage = s.Split('=')[1];
query.AppendFormat("{0}-{1}|", s.Split('=')[0], vpcMessage);
if (s.IndexOf("vpc_DRExists") > -1)
{
exist = vpcMessage == "Y";
}
if (s.IndexOf("vpc_TxnResponseCode") > -1)
{
ResponseCode = vpcMessage == "0";
}
if (s.IndexOf("vpc_Amount") > -1)
{
Total =
double.Parse(vpcMessage.Length > 2
? vpcMessage.Insert(vpcMessage.Length - 2, ",")
: "0," + vpcMessage);
}
if (s.IndexOf("vpc_BatchNo") > -1)
{
TransactionDate = DateTime.Parse(vpcMessage.Insert(6, "-").Insert(4, "-"));
}
if (s.IndexOf("vpc_TransactionNo") > -1)
{
TransactionId = vpcMessage;
}
if (s.IndexOf("vpc_CardNum") > -1)
{
CardNo = vpcMessage;
}
if (s.IndexOf("vpc_Message") > -1)
{
vpc_Message = vpcMessage;
}
}
serviceLog.InfoFormat("Message: " + s);
}
}
catch (Exception ex)
{
serviceLog.Error("fatal error", ex);
}
return exist && ResponseCode;
}
I'm not sure if I can get rid of these multiple if statements.
The "exist" and "ResponseCode" are boolean values and I need them to return bool type from method. Other values are class properties.
IndexOf()
? Shouldn't you just checks.Split('=')[0]
? \$\endgroup\$