5 posts / 0 new
Last post
mobeenkhan3895
Transaction successful but response getting null

My C# API is working fine and Transaction was completed successfully but problem is that, Response is always null, So that transactionresult is also coming null.


jitenderdhaliwal3952
Re: Transaction successful but response getting null

can you please share your code.

thanks


rohitrajagopal3402
Re: Transaction successful but response getting null

Hi Mobeen,

Please post your code.

Regards,

Payeezy Team


mobeenkhan3895
Re: Transaction successful but response getting null

My Controller Code is

public async Task CheckoutPayment()
{

var paymentService = new PaymentService("1.50", "0516", "ACB Cards",
"4111111111111111", "23".ToString()); //customer Id for Internal purpose to track which customer made the transaction...
TransactionResult transactionResult = await paymentService.PostAsync();

//The Problem arises when transactionresult is getting null. and program terminates from here.
if (transactionResult == null) return null;

if (transactionResult.TransactionApproved && !transactionResult.TransactionError
)
{
//transaction success....
TempData["Transaction_Approved"] = transactionResult.Message;
}
else
{
//transaction failed
var message = transactionResult.Message;
}
return View(transactionResult);

}

PaymentService.cs File are

public sealed class PaymentService
{
private const string HttpMethod = "POST";
private const string MessageType = "application/xml";
private const string TransactionVer = "/transaction/v13";

private readonly string GatewayId = string.Empty;
private readonly string GatewayPwd = string.Empty;
private readonly string TransactionType = string.Empty;
private readonly string Amount = string.Empty;
private readonly string CardExpiryDate = string.Empty;
private readonly string CardHolderName = string.Empty;
private readonly string CardNumber = string.Empty;
private readonly string HmacKey = string.Empty;
private readonly string KeyId = string.Empty;
private readonly string CustomerId = string.Empty;

private string ApiUri
{
get
{
return ConfigurationManager.AppSettings["GATEWAY_URL"] + TransactionVer;
}
}

private string RequestXml
{
get
{
var builder = new StringBuilder();
var writer = new StringWriter(builder);
using (var xml = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true, NewLineOnAttributes = true }))
{
xml.WriteStartElement("Transaction");
xml.WriteElementString("ExactID", GatewayId);//Gateway ID
xml.WriteElementString("Password", GatewayPwd);//Password
xml.WriteElementString("Transaction_Type", TransactionType);
xml.WriteElementString("DollarAmount", Amount);
xml.WriteElementString("Expiry_Date", CardExpiryDate);
xml.WriteElementString("CardHoldersName", CardHolderName);
xml.WriteElementString("Card_Number", CardNumber);
xml.WriteElementString("Customer_Ref", CustomerId);
xml.WriteEndElement();
writer.Flush();
}
writer = null;
return builder.ToString();
}
}

internal PaymentService(string amount, string cardExpiryDate, string cardHolderName, string cardNumber, string customerId)
{
GatewayId = ConfigurationManager.AppSettings["GATEWAY_ID"];
GatewayPwd = ConfigurationManager.AppSettings["GATEWAY_PWD"];
TransactionType = ConfigurationManager.AppSettings["TRANS_TYPE"];
HmacKey = ConfigurationManager.AppSettings["HMAC_KEY"];
KeyId = ConfigurationManager.AppSettings["KEY_ID"];
Amount = amount;
CardExpiryDate = cardExpiryDate;
CardHolderName = cardHolderName;
CardNumber = cardNumber;
CustomerId = customerId;
}

internal async Task PostAsync()
{
var xmlBytes = new ASCIIEncoding().GetBytes(RequestXml);
var sha1Crypto = new SHA1CryptoServiceProvider();
var hashContent = BitConverter.ToString(sha1Crypto.ComputeHash(xmlBytes)).Replace("-", "").ToLower();
var time = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
var hashData = string.Format("{0}\n{1}\n{2}\n{3}\n{4}", HttpMethod, MessageType, hashContent, time, TransactionVer);
var hmacSha = new HMACSHA1(Encoding.UTF8.GetBytes(HmacKey));
var hmacData = hmacSha.ComputeHash(Encoding.UTF8.GetBytes(hashData));
var base64Hash = Convert.ToBase64String(hmacData);
var responseXml = string.Empty;

try
{
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, ApiUri))
{
request.Headers.Add("x-gge4-date", time);
request.Headers.Add("x-gge4-content-sha1", hashContent);
request.Headers.Add("Authorization", "GGE4_API " + KeyId + ":" + base64Hash);
request.Content = new StringContent(RequestXml);
request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(MessageType);

using (var response = await client.SendAsync(request).ConfigureAwait(false))
{
if (response.StatusCode == System.Net.HttpStatusCode.Created || response.StatusCode == System.Net.HttpStatusCode.OK)
responseXml = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
else
LogStatusCode(response.StatusCode.ToString());
}
request.Content.Dispose();
}
}
return await ParseXml(responseXml);
}
catch (Exception)
{
/*log exception here*/
}
return await Task.FromResult(null);
}

private void LogStatusCode(string httpStatusCode)
{
//log your status code
}

//the Main problem begins from here When "responseXml" is also getting null.

///
/// convert string xml to object.
///
///
///
private async Task ParseXml(string responseXml)
{
TransactionResult model = null;
try
{
if (string.IsNullOrWhiteSpace(responseXml)) return await Task.FromResult(null);

var xml = XElement.Parse(responseXml);
model = (from result in xml.DescendantsAndSelf("TransactionResult")
select new TransactionResult
{
TransactionTag = result.Element("Transaction_Tag").Value,
AuthorizationNum = result.Element("Authorization_Num").Value,
CustomerRef = result.Element("Customer_Ref").Value,
ClientIP = result.Element("Client_IP").Value,
TransactionError = Boolean.Parse(result.Element("Transaction_Error").Value),
TransactionApproved = Boolean.Parse(result.Element("Transaction_Approved").Value),
BankMessage = result.Element("Bank_Message").Value,
CardType = result.Element("CardType").Value,

}).FirstOrDefault();

}
catch (Exception)
{
if (model == null)
model = new TransactionResult { CustomerRef = CustomerId, Message = responseXml };
}
return await Task.FromResult(model);
}
}

Please check the code and I mention the problem in commented form, between the code


rohitrajagopal3402
Re: Transaction successful but response getting null

Hi Mobeen,

Thank you for posting your code. It looks like you are calling the Payeezy gateway API (GGe4). Let me bring in the Payeezy gateway support team on this. Please look for my email.

Regards,

Rohit