API
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PaygateRepository.cs 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using RestSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnivateProperties_API.Context;
  9. using UnivateProperties_API.Model.Financial;
  10. namespace UnivateProperties_API.Repository.Financial
  11. {
  12. public interface IPaygateRepository
  13. {
  14. string GoToPaymentGateway(Payment payment);
  15. string PaymentQuery(string id, Payment payment);
  16. }
  17. public class PaygateRepository: IPaygateRepository
  18. {
  19. private readonly DataContext _dbContext;
  20. public PaygateRepository(DataContext db)
  21. {
  22. _dbContext = db;
  23. }
  24. public string GoToPaymentGateway(Payment payment)
  25. {
  26. string utcDate = DateTime.UtcNow.ToString("yyyy-MM-dd H:mm:ss");
  27. var client = new RestClient("https://secure.paygate.co.za/payweb3/initiate.trans");
  28. client.Timeout = -1;
  29. var request = new RestRequest(Method.POST);
  30. var total = payment.Amount;
  31. string paygateId = "10011072130";
  32. string reff = "";
  33. Payment paymentObj = new Payment();
  34. if (payment.TimeshareWeekId != 0)
  35. {
  36. reff = payment.TimeshareWeekId.ToString();
  37. paymentObj.TimeshareWeekId = payment.TimeshareWeekId;
  38. }
  39. else
  40. {
  41. reff = payment.PropertyId.ToString();
  42. paymentObj.PropertyId = payment.PropertyId;
  43. }
  44. string amm = Math.Round((total * 100)).ToString();
  45. string currenc = "ZAR";
  46. string retUrl = "http://training.provision-sa.com:82/api/redirect";
  47. string transDate = utcDate;
  48. string loc = "en-za";
  49. string count = "ZAF";
  50. string mail = "jlouw365@gmail.com";
  51. request.AlwaysMultipartFormData = true;
  52. request.AddParameter("PAYGATE_ID", paygateId);
  53. request.AddParameter("REFERENCE", reff);
  54. request.AddParameter("AMOUNT", amm);
  55. request.AddParameter("CURRENCY", currenc);
  56. request.AddParameter("RETURN_URL", retUrl);
  57. request.AddParameter("TRANSACTION_DATE", transDate);
  58. request.AddParameter("LOCALE", loc);
  59. request.AddParameter("COUNTRY", count);
  60. request.AddParameter("EMAIL", mail);
  61. string checksum = Checksum(
  62. paygateId +
  63. reff +
  64. amm +
  65. currenc +
  66. retUrl +
  67. transDate +
  68. loc +
  69. count +
  70. mail +
  71. "secret");
  72. request.AddParameter("CHECKSUM", checksum);
  73. string gatewayReturn = client.Execute(request).Content.ToString();
  74. List<string> vs = gatewayReturn.Split('&').ToList();
  75. string payReqId = vs[1].Split('=')[1].ToString();
  76. string resultString = client.Execute(request).Content.ToString();
  77. var resultArr = resultString.Split('&');
  78. List<string> valueArr = new List<string>();
  79. foreach (var item in resultArr)
  80. {
  81. valueArr.Add(item.Split('=')[1]);
  82. }
  83. paymentObj.Amount = total;
  84. paymentObj.PayRequestId = valueArr[1];
  85. paymentObj.PaymentToken = valueArr[2];
  86. paymentObj.Checksum = valueArr[3];
  87. //PaymentQuery(paygateId, paymentObj);
  88. _dbContext.Payments.Add(paymentObj);
  89. _dbContext.SaveChanges();
  90. return resultString;
  91. }
  92. public string PaymentQuery(string paygateId, Payment payment)
  93. {
  94. var client = new RestClient("https://secure.paygate.co.za/payweb3/query.trans");
  95. client.Timeout = -1;
  96. var request = new RestRequest(Method.POST);
  97. request.AddParameter("PAYGATE_ID", paygateId);
  98. request.AddParameter("PAY_REQUEST_ID", payment.PayRequestId);
  99. request.AddParameter("REFERENCE", payment.PaymentToken);
  100. string checksum = Checksum(
  101. paygateId +
  102. payment.PayRequestId +
  103. payment.PaymentToken +
  104. "secret");
  105. request.AddParameter("CHECKSUM", checksum);
  106. string resultString = client.Execute(request).Content.ToString();
  107. var resultArr = resultString.Split('&');
  108. List<string> valueArr = new List<string>();
  109. string transactionResult = "";
  110. foreach (var item in resultArr)
  111. {
  112. valueArr.Add(item.Split('=')[1]);
  113. }
  114. switch (valueArr[4])
  115. {
  116. case "900001":
  117. transactionResult = "Call for Approval";
  118. break;
  119. case "900002":
  120. transactionResult = "Card Expired";
  121. break;
  122. case "900003":
  123. transactionResult = "Insufficient Funds";
  124. break;
  125. case "900004":
  126. transactionResult = "Invalid Card Number";
  127. break;
  128. case "900005": //Indicates a communications failure between the banks systems.
  129. transactionResult = "Bank Interface Timeout";
  130. break;
  131. case "900006":
  132. transactionResult = "Invalid Card";
  133. break;
  134. case "900007":
  135. transactionResult = "Declined";
  136. break;
  137. case "900009":
  138. transactionResult = "Lost Card";
  139. break;
  140. case "900010":
  141. transactionResult = "Invalid Card Length";
  142. break;
  143. case "900011":
  144. transactionResult = "Suspected Fraud";
  145. break;
  146. case "900012":
  147. transactionResult = "Card Reported as Stolen";
  148. break;
  149. case "900013":
  150. transactionResult = "Restricted Card";
  151. break;
  152. case "900014":
  153. transactionResult = "Excessive Card Usage";
  154. break;
  155. case "900015":
  156. transactionResult = "Card Blacklisted";
  157. break;
  158. case "990017":
  159. transactionResult = "Auth Done";
  160. break;
  161. case "900207": //Indicates the cardholder did not enter their MasterCard SecureCode / Verified by Visa password correctly.
  162. transactionResult = "Declined; authentication failed";
  163. break;
  164. case "990020":
  165. transactionResult = "Auth Declined";
  166. break;
  167. case "900210": //Indicates that the MasterCard SecureCode / Verified-by-Visa transaction has already been completed. Most likely caused by a customer clicking the refresh button.
  168. transactionResult = "3D Secure Lookup Timeout";
  169. break;
  170. case "991001":
  171. transactionResult = "Invalid expiry date";
  172. break;
  173. case "991002":
  174. transactionResult = "Invalid Amount";
  175. break;
  176. case "900205":
  177. transactionResult = "Unexpected authentication result (phase 1)";
  178. break;
  179. case "900206":
  180. transactionResult = "Unexpected authentication result (phase 2)";
  181. break;
  182. case "990001":
  183. transactionResult = "Could not insert into Database";
  184. break;
  185. case "990022":
  186. transactionResult = "Bank not available";
  187. break;
  188. case "990053":
  189. transactionResult = "Error processing transaction";
  190. break;
  191. case "900209": //Indicates the verification data returned from MasterCard SecureCode / Verified-by-Visa has been altered.
  192. transactionResult = "Transaction verification failed (phase 2)";
  193. break;
  194. case "900019":
  195. transactionResult = "Invalid PayVault Scope";
  196. break;
  197. case "990024":
  198. transactionResult = "Duplicate Transaction Detected. Please check before submitting";
  199. break;
  200. case "990028": //Customer clicks the ‘Cancel’ button on the payment page.
  201. transactionResult = "Transaction cancelled";
  202. break;
  203. }
  204. payment.PaymentStatus = transactionResult;
  205. _dbContext.Payments.Update(payment);
  206. _dbContext.SaveChanges();
  207. return resultString;
  208. }
  209. private string Checksum(string data)
  210. {
  211. using (var md5 = MD5.Create())
  212. {
  213. return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(data)))
  214. .Replace("-", string.Empty).ToLower();
  215. }
  216. }
  217. }
  218. }