123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using RestSharp;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Model.Financial;
-
- namespace UnivateProperties_API.Repository.Financial
- {
- public interface IPaygateRepository
- {
- string GoToPaymentGateway(Payment payment);
- string PaymentQuery(string id, Payment payment);
- }
-
- public class PaygateRepository: IPaygateRepository
- {
- private readonly DataContext _dbContext;
-
- public PaygateRepository(DataContext db)
- {
- _dbContext = db;
- }
-
- public string GoToPaymentGateway(Payment payment)
- {
- string utcDate = DateTime.UtcNow.ToString("yyyy-MM-dd H:mm:ss");
- var client = new RestClient("https://secure.paygate.co.za/payweb3/initiate.trans");
- client.Timeout = -1;
- var request = new RestRequest(Method.POST);
- var total = payment.Amount;
- string paygateId = "10011072130";
- string reff = "";
- Payment paymentObj = new Payment();
- if (payment.TimeshareWeekId != 0)
- {
- reff = payment.TimeshareWeekId.ToString();
- paymentObj.TimeshareWeekId = payment.TimeshareWeekId;
- }
- else
- {
- reff = payment.PropertyId.ToString();
- paymentObj.PropertyId = payment.PropertyId;
- }
-
- string amm = Math.Round((total * 100)).ToString();
- string currenc = "ZAR";
- string retUrl = "http://training.provision-sa.com:82/api/redirect";
- string transDate = utcDate;
- string loc = "en-za";
- string count = "ZAF";
- string mail = "jlouw365@gmail.com";
- request.AlwaysMultipartFormData = true;
- request.AddParameter("PAYGATE_ID", paygateId);
- request.AddParameter("REFERENCE", reff);
- request.AddParameter("AMOUNT", amm);
- request.AddParameter("CURRENCY", currenc);
- request.AddParameter("RETURN_URL", retUrl);
- request.AddParameter("TRANSACTION_DATE", transDate);
- request.AddParameter("LOCALE", loc);
- request.AddParameter("COUNTRY", count);
- request.AddParameter("EMAIL", mail);
- string checksum = Checksum(
- paygateId +
- reff +
- amm +
- currenc +
- retUrl +
- transDate +
- loc +
- count +
- mail +
- "secret");
- request.AddParameter("CHECKSUM", checksum);
- string gatewayReturn = client.Execute(request).Content.ToString();
- List<string> vs = gatewayReturn.Split('&').ToList();
- string payReqId = vs[1].Split('=')[1].ToString();
- string resultString = client.Execute(request).Content.ToString();
- var resultArr = resultString.Split('&');
- List<string> valueArr = new List<string>();
- foreach (var item in resultArr)
- {
- valueArr.Add(item.Split('=')[1]);
- }
-
- paymentObj.Amount = total;
- paymentObj.PayRequestId = valueArr[1];
- paymentObj.PaymentToken = valueArr[2];
- paymentObj.Checksum = valueArr[3];
-
- //PaymentQuery(paygateId, paymentObj);
-
-
- _dbContext.Payments.Add(paymentObj);
- _dbContext.SaveChanges();
-
- return resultString;
- }
-
- public string PaymentQuery(string paygateId, Payment payment)
- {
- var client = new RestClient("https://secure.paygate.co.za/payweb3/query.trans");
- client.Timeout = -1;
- var request = new RestRequest(Method.POST);
- request.AddParameter("PAYGATE_ID", paygateId);
- request.AddParameter("PAY_REQUEST_ID", payment.PayRequestId);
- request.AddParameter("REFERENCE", payment.PaymentToken);
- string checksum = Checksum(
- paygateId +
- payment.PayRequestId +
- payment.PaymentToken +
- "secret");
- request.AddParameter("CHECKSUM", checksum);
- string resultString = client.Execute(request).Content.ToString();
- var resultArr = resultString.Split('&');
- List<string> valueArr = new List<string>();
- string transactionResult = "";
- foreach (var item in resultArr)
- {
- valueArr.Add(item.Split('=')[1]);
- }
-
- switch (valueArr[4])
- {
- case "900001":
- transactionResult = "Call for Approval";
- break;
- case "900002":
- transactionResult = "Card Expired";
- break;
- case "900003":
- transactionResult = "Insufficient Funds";
- break;
- case "900004":
- transactionResult = "Invalid Card Number";
- break;
- case "900005": //Indicates a communications failure between the banks systems.
- transactionResult = "Bank Interface Timeout";
- break;
- case "900006":
- transactionResult = "Invalid Card";
- break;
- case "900007":
- transactionResult = "Declined";
- break;
- case "900009":
- transactionResult = "Lost Card";
- break;
- case "900010":
- transactionResult = "Invalid Card Length";
- break;
- case "900011":
- transactionResult = "Suspected Fraud";
- break;
- case "900012":
- transactionResult = "Card Reported as Stolen";
- break;
- case "900013":
- transactionResult = "Restricted Card";
- break;
- case "900014":
- transactionResult = "Excessive Card Usage";
- break;
- case "900015":
- transactionResult = "Card Blacklisted";
- break;
- case "990017":
- transactionResult = "Auth Done";
- break;
- case "900207": //Indicates the cardholder did not enter their MasterCard SecureCode / Verified by Visa password correctly.
- transactionResult = "Declined; authentication failed";
- break;
- case "990020":
- transactionResult = "Auth Declined";
- break;
- 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.
- transactionResult = "3D Secure Lookup Timeout";
- break;
- case "991001":
- transactionResult = "Invalid expiry date";
- break;
- case "991002":
- transactionResult = "Invalid Amount";
- break;
- case "900205":
- transactionResult = "Unexpected authentication result (phase 1)";
- break;
- case "900206":
- transactionResult = "Unexpected authentication result (phase 2)";
- break;
- case "990001":
- transactionResult = "Could not insert into Database";
- break;
- case "990022":
- transactionResult = "Bank not available";
- break;
- case "990053":
- transactionResult = "Error processing transaction";
- break;
- case "900209": //Indicates the verification data returned from MasterCard SecureCode / Verified-by-Visa has been altered.
- transactionResult = "Transaction verification failed (phase 2)";
- break;
- case "900019":
- transactionResult = "Invalid PayVault Scope";
- break;
- case "990024":
- transactionResult = "Duplicate Transaction Detected. Please check before submitting";
- break;
- case "990028": //Customer clicks the ‘Cancel’ button on the payment page.
- transactionResult = "Transaction cancelled";
- break;
- }
-
- payment.PaymentStatus = transactionResult;
- _dbContext.Payments.Update(payment);
- _dbContext.SaveChanges();
-
- return resultString;
- }
-
- private string Checksum(string data)
- {
- using (var md5 = MD5.Create())
- {
- return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(data)))
- .Replace("-", string.Empty).ToLower();
- }
- }
- }
- }
|