Thursday, November 13, 2008

Implementing St George Payment Gateway using asp.net or c#


St.George Internet Payment Gateway
In order to implement internet payment gateway using St George api,using asp.net and c# you require followings things


Reliable $1 Web Hosting by 3iX

1) webpay.dll and Org.Mentalis.Security.dll
2) Client Id from the st george bank
3) digital certificate and its pwd from the bank

then in the pay button click paste the following code

Int32 pay1 = Convert.ToInt32(amount.Text);
if (pay1>=pay)
{
//Set the following values appropriately for your Bank/Service Provider
//String responseURL = "http://www.gwipg.stgeorge.com.au/";
String responseURL = "";

//To avoid processing duplicate payments, for this user, check the
//if ( Session.Contents[ "WEBPAYOBJECT" ] == null )
//{
webpay.client.Webpay w = new webpay.client.Webpay("10000xxx", "e:\\net.pfx", "yourpwd");
Session.Contents["WEBPAYOBJECT"] = w;
responseURL = processPayment(w);
String expdate = mm.SelectedValue + "/" + yy.SelectedValue;
obj.Insert(Convert.ToInt32(Session["MId"]), Convert.ToDecimal(amount.Text), ddlpaymentmethod.SelectedValue, cardName.Text, txtCardData.Text, cvc2.Text, expdate.ToString());
//}
Response.Redirect(responseURL);
}
else
{
lblpayment.Text = "Payment Should be greater than " + pay;
}



use the following function


private String processPayment(webpay.client.Webpay w)
{
System.Random randObj = new System.Random(5325);
w.setServers("www.gwipg.stgeorge.com.au");
w.setPort(3006); //use 3005 for testing
w.setValue("CLIENTID", "10000xxx");

w.setValue("INTERFACE", "CREDITCARD");
w.setValue("TRANSACTIONTYPE", "PURCHASE");
//Set the following values based on user input, or calculations from other parts of
//your application.
//Please consult your service provider's documentation for
//required and optional fields.
w.setValue("CARDHOLDER_NAME", cardName.Text);
w.setValue("CARDDATA", txtCardData.Text);
w.setValue("CARDEXPIRYDATE", mm.SelectedValue + yy.SelectedValue);
w.setValue("CVC2", cvc2.Text);

w.setValue("TOTALAMOUNT", amount.Text);
w.setValue("CLIENTREF", "" + randObj.Next());

//Store the Webpay Object in the session. So it can be retrieved easily by other pages (results/errors, etc)
Session.Contents["WEBPAYOBJECT"] = w;

System.Exception ex = null;
for (int i = 0; i < 2; i++)
{
try
{
w.execute();
ex = null;
break;
}
catch (webpay.client.ConnectException cex)
{
//Log the error, and try again.
ex = cex;
continue;
}
catch (System.Exception ioex)
{
//If a TXNREFERENCE is available, check the status of the transaction.
//The number of times to loop, and the sleep time may vary depending on the
//requirements of your application.
ex = ioex;
w.setValue("TRANSACTIONTYPE", "STATUS");
for (int j = 0; j < 2; j++)
{
try
{
//In case we the connection was dropped part way thru,
//give the transacton time to complete at the back end
System.Threading.Thread.Sleep(1500);
}
catch (Exception e)
{
}
try
{
w.execute();
//If the responsecode is still "IP", then wait and check the status again.
if (w.getValue("RESPONSECODE") != "IP")
{
//We have a result, continue
break;
}
}
catch (System.Exception e)
{
ex = e;
}
}
}
}
if (ex != null)
{
//An exception was encounteded which could not be recovered, possibly
//while attempting to retry or check status. Set the redirect to a
//page/process which can handle error conditions.
Session.Contents["ERROR"] = ex;
return "ErrorPage.aspx";
}
//Processing completed normally - redirect and display the results to the user.
//NOTE: Your applicaton will need also need to inspect the RESPONSECODE parameter and
//store the transaction result somewhere. This should be done here, and not in the
//Result display code, as this code will only execute once, while the display code has the potential
//to be re-executed if the user clicks "Refresh" or attempts to re-process the transaction
//using the "Back" button.
return "ResultsPage.aspx";
}

}
// if you face any problem please feel free to ask