Skip to content

Commit 2804415

Browse files
committed
first commit
1 parent 15cc7d7 commit 2804415

File tree

67 files changed

+27594
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+27594
-0
lines changed

Authorize.NET/AIM/Gateway.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Configuration;
6+
using System.Net;
7+
using System.IO;
8+
using System.Diagnostics;
9+
using System.Security.Cryptography.X509Certificates;
10+
using System.Collections.Specialized;
11+
using System.Xml.Serialization;
12+
using System.Xml;
13+
14+
namespace AuthorizeNet {
15+
16+
public enum ServiceMode {
17+
Test,
18+
Live
19+
}
20+
21+
public class Gateway : AuthorizeNet.IGateway {
22+
23+
24+
public const string TEST_URL = "https://test.authorize.net/gateway/transact.dll";
25+
public const string LIVE_URL = "https://secure.authorize.net/gateway/transact.dll";
26+
27+
28+
public string ApiLogin { get; set;}
29+
public string TransactionKey { get; set;}
30+
public bool TestMode { get; set;}
31+
public Gateway (string apiLogin, string transactionKey, bool testMode)
32+
{
33+
ApiLogin = apiLogin;
34+
TransactionKey = transactionKey;
35+
TestMode = testMode;
36+
}
37+
38+
public Gateway(string apiLogin, string transactionKey):this(apiLogin,transactionKey,true){}
39+
40+
public IGatewayResponse Send(IGatewayRequest request) {
41+
return Send(request, null);
42+
}
43+
44+
protected void LoadAuthorization(IGatewayRequest request) {
45+
request.Queue(ApiFields.ApiLogin, ApiLogin);
46+
request.Queue(ApiFields.TransactionKey, TransactionKey);
47+
}
48+
49+
protected string SendRequest(string serviceUrl, IGatewayRequest request) {
50+
51+
var postData = request.ToPostString();
52+
var result = "";
53+
54+
//override the local cert policy - this is for Mono ONLY
55+
//ServicePointManager.CertificatePolicy = new PolicyOverride();
56+
57+
var webRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
58+
webRequest.Method = "POST";
59+
webRequest.ContentLength = postData.Length;
60+
webRequest.ContentType = "application/x-www-form-urlencoded";
61+
62+
// post data is sent as a stream
63+
StreamWriter myWriter = null;
64+
myWriter = new StreamWriter(webRequest.GetRequestStream());
65+
myWriter.Write(postData);
66+
myWriter.Close();
67+
68+
// returned values are returned as a stream, then read into a string
69+
var response = (HttpWebResponse)webRequest.GetResponse();
70+
using (StreamReader responseStream = new StreamReader(response.GetResponseStream())) {
71+
result = responseStream.ReadToEnd();
72+
responseStream.Close();
73+
}
74+
75+
// the response string is broken into an array
76+
// The split character specified here must match the delimiting character specified above
77+
return result;
78+
}
79+
80+
public virtual IGatewayResponse Send (IGatewayRequest request, string description)
81+
{
82+
83+
var serviceUrl = TEST_URL;
84+
if (!TestMode)
85+
serviceUrl = LIVE_URL;
86+
87+
LoadAuthorization(request);
88+
if(String.IsNullOrEmpty(request.Description))
89+
request.Queue(ApiFields.Description, description);
90+
#if debug
91+
request.DuplicateWindow = "0";
92+
#endif
93+
var response = SendRequest(serviceUrl, request);
94+
95+
return DecideResponse(response.Split('|'));
96+
}
97+
98+
99+
/// <summary>
100+
/// Decides the response.
101+
/// </summary>
102+
/// <param name="rawResponse">The raw response.</param>
103+
/// <returns></returns>
104+
public IGatewayResponse DecideResponse (string[] rawResponse)
105+
{
106+
107+
if (rawResponse.Length == 1)
108+
throw new InvalidDataException ("There was an error returned from AuthorizeNet: " + rawResponse[0] + "; this usually means your data sent along was incorrect. Please recheck that all dates and amounts are formatted correctly");
109+
110+
return new GatewayResponse (rawResponse);
111+
}
112+
113+
114+
class PolicyOverride : ICertificatePolicy
115+
{
116+
117+
bool ICertificatePolicy.CheckValidationResult (ServicePoint srvPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest request, int certificateProblem)
118+
{
119+
return true;
120+
}
121+
}
122+
123+
124+
125+
}
126+
}

Authorize.NET/AIM/IGateway.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Specialized;
3+
using System.Xml;
4+
namespace AuthorizeNet {
5+
public interface IGateway {
6+
string ApiLogin { get; set; }
7+
string TransactionKey { get; set; }
8+
IGatewayResponse Send (IGatewayRequest request);
9+
IGatewayResponse Send(IGatewayRequest request, string description);
10+
}
11+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Collections.Specialized;
6+
7+
namespace AuthorizeNet {
8+
9+
/// <summary>
10+
/// A request that authorizes a transaction, no capture
11+
/// </summary>
12+
public class AuthorizationRequest:GatewayRequest {
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="AuthorizationRequest"/> class.
16+
/// </summary>
17+
/// <param name="cardNumber">The card number.</param>
18+
/// <param name="expirationMonthAndYear">The expiration month and year.</param>
19+
/// <param name="amount">The amount.</param>
20+
/// <param name="description">The description.</param>
21+
public AuthorizationRequest(string cardNumber, string expirationMonthAndYear, decimal amount, string description) {
22+
this.SetApiAction(RequestAction.AuthorizeAndCapture);
23+
SetQueue(cardNumber, expirationMonthAndYear, amount, description);
24+
}
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="AuthorizationRequest"/> class.
27+
/// </summary>
28+
/// <param name="cardNumber">The card number.</param>
29+
/// <param name="expirationMonthAndYear">The expiration month and year.</param>
30+
/// <param name="amount">The amount.</param>
31+
/// <param name="description">The description.</param>
32+
/// <param name="includeCapture">if set to <c>true</c> [include capture].</param>
33+
public AuthorizationRequest(string cardNumber, string expirationMonthAndYear, decimal amount, string description, bool includeCapture)
34+
{
35+
if (includeCapture) {
36+
this.SetApiAction(RequestAction.AuthorizeAndCapture);
37+
} else {
38+
this.SetApiAction(RequestAction.Authorize);
39+
}
40+
SetQueue(cardNumber, expirationMonthAndYear, amount, description);
41+
}
42+
43+
/// <summary>
44+
/// Loader for use with form POSTS from web
45+
/// </summary>
46+
/// <param name="post"></param>
47+
public AuthorizationRequest(NameValueCollection post) {
48+
this.SetApiAction(RequestAction.AuthorizeAndCapture);
49+
this.Queue(ApiFields.CreditCardNumber, post[ApiFields.CreditCardNumber]);
50+
this.Queue(ApiFields.CreditCardExpiration, post[ApiFields.CreditCardExpiration]);
51+
this.Queue(ApiFields.Amount, post[ApiFields.Amount]);
52+
}
53+
54+
protected virtual void SetQueue(string cardNumber, string expirationMonthAndYear, decimal amount, string description) {
55+
this.Queue(ApiFields.CreditCardNumber, cardNumber);
56+
this.Queue(ApiFields.CreditCardExpiration, expirationMonthAndYear);
57+
this.Queue(ApiFields.Amount, amount.ToString());
58+
this.Queue(ApiFields.Description, description);
59+
}
60+
61+
62+
63+
64+
65+
66+
}
67+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace AuthorizeNet {
7+
/// <summary>
8+
/// A request representing a Capture - the final transfer of funds that happens after an auth.
9+
/// </summary>
10+
public class CaptureRequest:GatewayRequest {
11+
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="CaptureRequest"/> class.
14+
/// </summary>
15+
/// <param name="amount">The amount.</param>
16+
/// <param name="transactionId">The transaction id.</param>
17+
/// <param name="authCode">The auth code.</param>
18+
public CaptureRequest(decimal amount, string transactionId, string authCode) {
19+
this.SetApiAction(RequestAction.Capture);
20+
this.Queue(ApiFields.Amount, amount.ToString());
21+
this.Queue(ApiFields.TransactionID, transactionId);
22+
this.Queue(ApiFields.AuthorizationCode, authCode);
23+
}
24+
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace AuthorizeNet {
7+
/// <summary>
8+
/// Credits, or refunds, the amount back to the user
9+
/// </summary>
10+
public class CreditRequest:GatewayRequest {
11+
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="CreditRequest"/> class.
14+
/// </summary>
15+
/// <param name="transactionId">The transaction id.</param>
16+
/// <param name="amount">The amount.</param>
17+
/// <param name="cardNumber">The card number.</param>
18+
public CreditRequest(string transactionId, decimal amount, string cardNumber) {
19+
20+
this.SetApiAction(RequestAction.Credit);
21+
22+
this.Queue(ApiFields.TransactionID, transactionId);
23+
this.Queue(ApiFields.Amount, amount.ToString());
24+
this.Queue(ApiFields.CreditCardNumber, cardNumber);
25+
26+
}
27+
}
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace AuthorizeNet {
7+
public class EcheckRequest:GatewayRequest {
8+
9+
/// <summary>
10+
/// Creates an ECheck transaction (defaulted to WEB) request for use with the AIM gateway
11+
/// </summary>
12+
/// <param name="amount"></param>
13+
/// <param name="bankABACode">The valid routing number of the customer’s bank</param>
14+
/// <param name="bankAccountNumber">The customer’s valid bank account number</param>
15+
/// <param name="acctType">CHECKING, BUSINESSCHECKING, SAVINGS</param>
16+
/// <param name="bankName">The name of the bank that holds the customer’s account</param>
17+
/// <param name="acctName">The name associated with the bank account</param>
18+
/// <param name="bankCheckNumber">The check number on the customer’s paper check</param>
19+
public EcheckRequest(decimal amount, string bankABACode, string bankAccountNumber,
20+
BankAccountType acctType, string bankName, string acctName, string bankCheckNumber) :
21+
this(EcheckType.WEB, amount, bankABACode, bankAccountNumber, acctType, bankName, acctName, bankCheckNumber) { }
22+
23+
24+
/// <summary>
25+
/// Creates an ECheck transaction request for use with the AIM gateway
26+
/// </summary>
27+
/// <param name="type">The Echeck Transaction type: ARC, BOC, CCD, PPD, TEL, WEB</param>
28+
/// <param name="amount"></param>
29+
/// <param name="bankABACode">The valid routing number of the customer’s bank</param>
30+
/// <param name="bankAccountNumber">The customer’s valid bank account number</param>
31+
/// <param name="acctType">CHECKING, BUSINESSCHECKING, SAVINGS</param>
32+
/// <param name="bankName">The name of the bank that holds the customer’s account</param>
33+
/// <param name="acctName">The name associated with the bank account</param>
34+
/// <param name="bankCheckNumber">The check number on the customer’s paper check</param>
35+
public EcheckRequest(EcheckType type, decimal amount, string bankABACode, string bankAccountNumber,
36+
BankAccountType acctType, string bankName, string acctName, string bankCheckNumber) {
37+
38+
Queue(ApiFields.Method, "ECHECK");
39+
this.BankABACode = bankABACode;
40+
this.BankAccountName = acctName;
41+
this.BankAccountNumber = bankAccountNumber;
42+
this.BankAccountType = acctType;
43+
this.BankCheckNumber = bankCheckNumber;
44+
this.Amount = amount.ToString();
45+
46+
}
47+
48+
/// <summary>
49+
/// Sets the eCheck request as a recurring payment
50+
/// </summary>
51+
/// <returns></returns>
52+
public EcheckRequest AsRecurring() {
53+
this.RecurringBilling = "Y";
54+
return this;
55+
}
56+
57+
}
58+
}

0 commit comments

Comments
 (0)