PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsData/AuthorizeNet.cs

https://bitbucket.org/mahalowe/bvcms
C# | 414 lines | 407 code | 7 blank | 0 comment | 21 complexity | c304a072654583265a42145eba0d6781 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using UtilityExtensions;
  6. using System.Net;
  7. using System.Text;
  8. using System.Xml.Linq;
  9. using System.IO;
  10. using System.Xml;
  11. using System.Xml.XPath;
  12. namespace CmsData
  13. {
  14. public class AuthorizeNet : IDisposable
  15. {
  16. XNamespace ns = "AnetApi/xml/v1/schema/AnetApiSchema.xsd";
  17. const string produrl = "https://api.authorize.net/xml/v1/request.api";
  18. const string testurl = "https://apitest.authorize.net/xml/v1/request.api";
  19. string testMode = "";
  20. string url;
  21. string login;
  22. string key;
  23. CMSDataContext Db;
  24. public AuthorizeNet(CMSDataContext Db, bool testing)
  25. {
  26. #if DEBUG
  27. testing = true;
  28. #endif
  29. this.Db = Db;
  30. if (testing)
  31. {
  32. login = "9t8Pqzs4CW3S";
  33. key = "9j33v58nuZB865WR";
  34. url = testurl;
  35. testMode = "testMode";
  36. }
  37. else
  38. {
  39. login = Db.Setting("x_login", "");
  40. key = Db.Setting("x_tran_key", "");
  41. url = produrl;
  42. testMode = "liveMode";
  43. }
  44. }
  45. private XDocument getResponse(string request)
  46. {
  47. var wc = new WebClient();
  48. wc.Headers.Add("Content-Type", "text/xml");
  49. var bits = Encoding.UTF8.GetBytes(request);
  50. var ret = wc.UploadData(url, "POST", bits);
  51. using (var xmlStream = new MemoryStream(ret))
  52. using (var xmlReader = new XmlTextReader(xmlStream))
  53. {
  54. var x = XDocument.Load(xmlReader);
  55. var result = x.Descendants(ns + "resultCode").First().Value;
  56. if (result == "Error")
  57. {
  58. var message = x.Descendants(ns + "text").First().Value;
  59. throw new Exception(message);
  60. }
  61. return x;
  62. }
  63. }
  64. public void AddUpdateCustomerProfile(int PeopleId,
  65. string type,
  66. string cardnumber,
  67. string expires,
  68. string cardcode,
  69. string routing,
  70. string account)
  71. {
  72. var exp = expires;
  73. if (exp.HasValue())
  74. exp = "20" + expires.Substring(2, 2) + "-" + expires.Substring(0, 2);
  75. var p = Db.LoadPersonById(PeopleId);
  76. var pi = p.PaymentInfo();
  77. if (pi == null)
  78. {
  79. pi = new PaymentInfo();
  80. p.PaymentInfos.Add(pi);
  81. }
  82. if (pi.AuNetCustId == null) // create a new profilein Authorize.NET CIM
  83. {
  84. XDocument request = null;
  85. if (type == "B")
  86. {
  87. request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  88. Element("createCustomerProfileRequest",
  89. Element("merchantAuthentication",
  90. Element("name", login),
  91. Element("transactionKey", key)
  92. ),
  93. Element("profile",
  94. Element("merchantCustomerId", PeopleId),
  95. Element("email", p.EmailAddress),
  96. Element("paymentProfiles",
  97. Element("billTo",
  98. Element("firstName", p.FirstName),
  99. Element("lastName", p.LastName),
  100. Element("address", p.PrimaryAddress),
  101. Element("city", p.PrimaryCity),
  102. Element("state", p.PrimaryState),
  103. Element("zip", p.PrimaryZip),
  104. Element("phoneNumber", p.HomePhone)
  105. ),
  106. Element("payment",
  107. Element("bankAccount",
  108. Element("routingNumber", routing),
  109. Element("accountNumber", account),
  110. Element("nameOnAccount", p.Name)
  111. )
  112. )
  113. )
  114. )
  115. )
  116. );
  117. }
  118. else
  119. {
  120. request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  121. Element("createCustomerProfileRequest",
  122. Element("merchantAuthentication",
  123. Element("name", login),
  124. Element("transactionKey", key)
  125. ),
  126. Element("profile",
  127. Element("merchantCustomerId", PeopleId),
  128. Element("email", p.EmailAddress),
  129. Element("paymentProfiles",
  130. Element("billTo",
  131. Element("firstName", p.FirstName),
  132. Element("lastName", p.LastName),
  133. Element("address", p.PrimaryAddress),
  134. Element("city", p.PrimaryCity),
  135. Element("state", p.PrimaryState),
  136. Element("zip", p.PrimaryZip),
  137. Element("phoneNumber", p.HomePhone)
  138. ),
  139. Element("payment",
  140. Element("creditCard",
  141. Element("cardNumber", cardnumber),
  142. Element("expirationDate", exp),
  143. Element("cardCode", cardcode)
  144. )
  145. )
  146. )
  147. )
  148. )
  149. );
  150. }
  151. var s = request.ToString();
  152. var x = getResponse(s);
  153. var id = x.Descendants(ns + "customerProfileId").First().Value.ToInt();
  154. var pid = x.Descendants(ns + "customerPaymentProfileIdList")
  155. .Descendants(ns + "numericString").First().Value.ToInt();
  156. pi.AuNetCustId = id;
  157. pi.AuNetCustPayId = pid;
  158. }
  159. else
  160. {
  161. if (account.HasValue() && account.StartsWith("X"))
  162. {
  163. var xe = getCustomerPaymentProfile(PeopleId);
  164. var xba = xe.Descendants(ns + "bankAccount").Single();
  165. routing = xba.Element(ns + "routingNumber").Value;
  166. account = xba.Element(ns + "accountNumber").Value;
  167. }
  168. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  169. Element("updateCustomerProfileRequest",
  170. Element("merchantAuthentication",
  171. Element("name", login),
  172. Element("transactionKey", key)
  173. ),
  174. Element("profile",
  175. Element("merchantCustomerId", PeopleId),
  176. Element("email", p.EmailAddress),
  177. Element("customerProfileId", pi.AuNetCustId)
  178. )
  179. )
  180. );
  181. var x = getResponse(request.ToString());
  182. if (type == "B")
  183. request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  184. Element("updateCustomerPaymentProfileRequest",
  185. Element("merchantAuthentication",
  186. Element("name", login),
  187. Element("transactionKey", key)
  188. ),
  189. Element("customerProfileId", pi.AuNetCustId),
  190. Element("paymentProfile",
  191. Element("billTo",
  192. Element("firstName", p.FirstName),
  193. Element("lastName", p.LastName),
  194. Element("address", p.PrimaryAddress),
  195. Element("city", p.PrimaryCity),
  196. Element("state", p.PrimaryState),
  197. Element("zip", p.PrimaryZip),
  198. Element("phoneNumber", p.HomePhone)
  199. ),
  200. Element("payment",
  201. Element("bankAccount",
  202. Element("routingNumber", routing),
  203. Element("accountNumber", account),
  204. Element("nameOnAccount", p.Name)
  205. )
  206. ),
  207. Element("customerPaymentProfileId", pi.AuNetCustPayId)
  208. )
  209. )
  210. );
  211. else
  212. request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  213. Element("updateCustomerPaymentProfileRequest",
  214. Element("merchantAuthentication",
  215. Element("name", login),
  216. Element("transactionKey", key)
  217. ),
  218. Element("customerProfileId", pi.AuNetCustId),
  219. Element("paymentProfile",
  220. Element("billTo",
  221. Element("firstName", p.FirstName),
  222. Element("lastName", p.LastName),
  223. Element("address", p.PrimaryAddress),
  224. Element("city", p.PrimaryCity),
  225. Element("state", p.PrimaryState),
  226. Element("zip", p.PrimaryZip),
  227. Element("phoneNumber", p.HomePhone)
  228. ),
  229. Element("payment",
  230. Element("creditCard",
  231. Element("cardNumber", cardnumber),
  232. Element("expirationDate", exp),
  233. Element("cardCode", cardcode)
  234. )
  235. ),
  236. Element("customerPaymentProfileId", pi.AuNetCustPayId)
  237. )
  238. )
  239. );
  240. x = getResponse(request.ToString());
  241. }
  242. pi.MaskedAccount = Util.MaskAccount(account);
  243. pi.MaskedCard = Util.MaskCC(cardnumber);
  244. pi.Ccv = cardcode;
  245. pi.Expires = expires;
  246. Db.SubmitChanges();
  247. }
  248. public string deleteCustomerProfile(int custid)
  249. {
  250. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  251. Element("deleteCustomerProfileRequest",
  252. Element("merchantAuthentication",
  253. Element("name", login),
  254. Element("transactionKey", key)
  255. ),
  256. Element("customerProfileId", custid)
  257. )
  258. );
  259. var x = getResponse(request.ToString());
  260. return x.ToString();
  261. }
  262. public string getCustomerProfileIds()
  263. {
  264. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  265. Element("getCustomerProfileIdsRequest",
  266. Element("merchantAuthentication",
  267. Element("name", login),
  268. Element("transactionKey", key)
  269. )
  270. )
  271. );
  272. var x = getResponse(request.ToString());
  273. return x.ToString();
  274. }
  275. public XDocument getCustomerPaymentProfile(int PeopleId)
  276. {
  277. var rg = Db.PaymentInfos.Single(pp => pp.PeopleId == PeopleId);
  278. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  279. Element("getCustomerPaymentProfileRequest",
  280. Element("merchantAuthentication",
  281. Element("name", login),
  282. Element("transactionKey", key)
  283. ),
  284. Element("customerProfileId", rg.AuNetCustId),
  285. Element("customerPaymentProfileId", rg.AuNetCustPayId)
  286. )
  287. );
  288. var x = getResponse(request.ToString());
  289. return x;
  290. }
  291. public string getCustomerProfile(int PeopleId)
  292. {
  293. var au = Db.PaymentInfos.Single(pp => pp.PeopleId == PeopleId);
  294. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  295. Element("getCustomerProfileRequest",
  296. Element("merchantAuthentication",
  297. Element("name", login),
  298. Element("transactionKey", key)
  299. ),
  300. Element("customerProfileId", au.AuNetCustId)
  301. )
  302. );
  303. var x = getResponse(request.ToString());
  304. return x.ToString();
  305. }
  306. public TransactionResponse createCustomerProfileTransactionRequest(int PeopleId, decimal amt, string description, int tranid)
  307. {
  308. var pi = Db.PaymentInfos.Single(pp => pp.PeopleId == PeopleId);
  309. if (pi == null)
  310. return new TransactionResponse
  311. {
  312. Approved = false,
  313. Message = "missing payment info",
  314. };
  315. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  316. Element("createCustomerProfileTransactionRequest",
  317. Element("merchantAuthentication",
  318. Element("name", login),
  319. Element("transactionKey", key)
  320. ),
  321. Element("refId", PeopleId),
  322. Element("transaction",
  323. Element("profileTransAuthCapture",
  324. Element("amount", amt),
  325. Element("customerProfileId", pi.AuNetCustId),
  326. Element("customerPaymentProfileId", pi.AuNetCustPayId),
  327. Element("order",
  328. Element("invoiceNumber", tranid),
  329. Element("description", description)
  330. ),
  331. Element("cardCode", pi.Ccv)
  332. )
  333. )
  334. )
  335. );
  336. var x = getResponse(request.ToString());
  337. var resp = x.Descendants(ns + "directResponse").First().Value;
  338. var a = resp.Split('|');
  339. var tr = new TransactionResponse
  340. {
  341. Approved = a[0] == "1",
  342. Message = a[3],
  343. AuthCode = a[4],
  344. TransactionId = a[6]
  345. };
  346. return tr;
  347. }
  348. private XElement Element(string name, params object[] content)
  349. {
  350. return new XElement(ns + name, content);
  351. }
  352. public TransactionResponse createTransactionRequest(int PeopleId, decimal amt, string cardnumber, string expires, string description, int tranid, string cardcode)
  353. {
  354. var p = Db.LoadPersonById(PeopleId);
  355. var request = new XDocument(new XDeclaration("1.0", "utf-8", null),
  356. Element("createTransactionRequest",
  357. Element("merchantAuthentication",
  358. Element("name", login),
  359. Element("transactionKey", key)
  360. ),
  361. Element("transactionRequest",
  362. Element("transactionType", "authCaptureTransaction"), // or refundTransaction or voidTransaction
  363. Element("amount", amt),
  364. Element("payment",
  365. Element("creditCard",
  366. Element("cardNumber", cardnumber),
  367. Element("expirationDate", expires),
  368. Element("cardCode", cardcode)
  369. )
  370. ),
  371. Element("order",
  372. Element("invoiceNumber", tranid),
  373. Element("description", description)
  374. ),
  375. Element("customer",
  376. Element("id", PeopleId),
  377. Element("email", p.EmailAddress)
  378. ),
  379. Element("billTo",
  380. Element("firstName", p.FirstName),
  381. Element("lastName", p.LastName),
  382. Element("address", p.PrimaryAddress),
  383. Element("city", p.PrimaryCity),
  384. Element("state", p.PrimaryState),
  385. Element("zip", p.PrimaryZip),
  386. Element("phoneNumber", p.HomePhone)
  387. ),
  388. Element("customerIP", Util.GetIPAddress())
  389. )
  390. )
  391. );
  392. var x = getResponse(request.ToString());
  393. var resp = x.Descendants(ns + "transactionResponse").First();
  394. var tr = new TransactionResponse
  395. {
  396. Approved = resp.Element(ns + "responseCode").Value == "1",
  397. AuthCode = resp.Element(ns + "authCode").Value,
  398. Message = resp.Descendants(ns + "message").First().Element(ns + "description").Value,
  399. TransactionId = resp.Element(ns + "transId").Value
  400. };
  401. return tr;
  402. }
  403. public void Dispose()
  404. {
  405. }
  406. }
  407. }