using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using
System.Net; // needed for HttpWebRequest and
HttpWebResponse
using
System.IO; // needed for StreamReader and StreamWriter
namespace PaymentProcessor
{
public partial class PaymentDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["OrderTotal"] == null)
{
Response.Redirect("PizzaOrder.aspx");
}
else
{
Decimal total = (Decimal)Session["OrderTotal"];
lblDisplay.Text
= "You order's total is " + total.ToString("C2") + "<br />" +
"Enter your credit card information to complete the
order.";
}
}
}
protected void btnProcessPurchase_Click(object sender, EventArgs e)
{
String ccNumber = txtCreditCardNumber.Text;
String ccExpirationDate = ddlMonth.SelectedValue + ddlYear.SelectedValue;
// By default, this sample code is designed to post to the test
server for
//
developer accounts: https://test.authorize.net/gateway/transact.dll
// for
real accounts (even in test mode), please make sure that you are
//
posting to: https://secure.authorize.net/gateway/transact.dll
String post_url = "https://test.authorize.net/gateway/transact.dll";
Dictionary<String, String> post_values = new Dictionary<String,String>();
// The
API LoginID and Transaction Key must be replaced with
valid values for your account.
post_values.Add("x_login", " YOUR_API_LOGIN_ID_FROM_AUTHORIZE.NET
");
post_values.Add("x_tran_key", "YOUR_TRANSACTION_KEY_FROM_AUTHORIZE.NET");
post_values.Add("x_delim_data", "TRUE");
post_values.Add("x_delim_char", "|");
post_values.Add("x_relay_response", "FALSE");
post_values.Add("x_type", "AUTH_CAPTURE");
post_values.Add("x_method", "CC");
//post_values.Add("x_card_num", "4111111111111111");
post_values.Add("x_card_num", ccNumber);
post_values.Add("x_exp_date", ccExpirationDate); //0115
is needed
post_values.Add("x_amount", Session["OrderTotal"].ToString());
post_values.Add("x_description", "Pascucci's
Pizza Palace Order (" + DateTime.Now.Date.ToShortDateString() + " " + DateTime.Now.TimeOfDay.ToString() + ")");
post_values.Add("x_first_name", txtFName.Text);
post_values.Add("x_last_name", txtLName.Text);
post_values.Add("x_address", txtAddress.Text);
post_values.Add("x_state", ddlState.SelectedValue);
post_values.Add("x_zip", txtZipcode.Text);
//
Additional fields can be added here as outlined in the AIM integration
// guide
at: http://developer.authorize.net
// This section takes the input fields and their values from
the Dictionary
// and
converts them to the proper format for an HTTP Post Request.
// For
example: "x_login=username&x_tran_key=a1B2c3D4"
String post_string = "";
foreach (KeyValuePair<String, String> field in post_values)
{
post_string
= post_string + field.Key +
"=" + HttpUtility.UrlEncode(field.Value) + "&";
}
//
Remove the & character from the end of the string.
post_string
= post_string.Substring(0, post_string.Length - 1);
// Create an HttpWebRequest object to
communicate with the Authorize.net payment gateway
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(post_url);
objRequest.Method
= "POST";
objRequest.ContentLength
= post_string.Length;
objRequest.ContentType
= "application/x-www-form-urlencoded";
// Send
the Request with the POST data using a StreamWriter.
StreamWriter requestStream = new StreamWriter(objRequest.GetRequestStream());
requestStream.Write(post_string);
requestStream.Close();
// Get
the Response and convert it to a string
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
StreamReader responseStream = new StreamReader(objResponse.GetResponseStream());
String responseData = responseStream.ReadToEnd();
responseStream.Close();
// Break
the response data into separate strings
String[] responseArray = responseData.Split(post_values["x_delim_char"].ToCharArray());
String responseCode = responseArray[0];
// Check
the response code returned from processing the transaction
if (responseCode == "1") //
Transaction Approved
{
ccNumber = ccNumber.Substring(ccNumber.Length - 4);
Decimal orderTotal = (Decimal)Session["OrderTotal"];
lblTransactionResults.Text
= "Thank you for your business. The
order was completed successfully. <br/><br/>" +
"Order Information: <br/>" +
Session["Name"] + "<br/>" +
Session["Address"] + "<br/>" +
Session["Phone"] +"<br/>" +
Session["Method"] + "<br/>" +
"Total: " + orderTotal.ToString("C2") + "<br/>" +
"------------------------------- <br/>" +
"Billing Information: <br/>" +
txtFName.Text + "
" + txtLName.Text
+ "<br/>" +
txtAddress.Text + "<br/>" +
txtCity.Text + "<br/>" +
ddlState.SelectedValue + "<br/>" +
txtZipcode.Text
+ "<br/>" +
ddlCardType.SelectedValue + "<br/>" +
"Card #: **** **** **** " + ccNumber +
"<br/> <br/>";
}
else if (responseCode
== "2") // Transaction Declined
{
lblTransactionResults.Text
= "The credit card transaction was
declined.<br />" +
"Please enter another credit card to complete this
order.";
}
else if (responseCode
== "3") // Transaction Error
{
lblTransactionResults.Text
= "There was an error with the
transaction.<br
/>" +
"Please review the credit card information you
entered, <br />" +
"or enter another credit
card to complete this order.";
}
else if (responseCode
== "4") // Transaction Held For Review
{
lblTransactionResults.Text
= "The credit card transaction is
being held for review. <br />" +
"It may take some time to complete this transaction.";
}
else
{
lblTransactionResults.Text
= "There was a problem with the credit
card transaction. <br />An unknown response
code was returned.";
}
}
}
}