|  | @@ -0,0 +1,85 @@
 | 
		
	
		
			
			|  | 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.Model.Financial;
 | 
		
	
		
			
			|  | 9 | +
 | 
		
	
		
			
			|  | 10 | +namespace UnivateProperties_API.Repository.Financial
 | 
		
	
		
			
			|  | 11 | +{
 | 
		
	
		
			
			|  | 12 | +    public interface IPaygateRepository
 | 
		
	
		
			
			|  | 13 | +    {
 | 
		
	
		
			
			|  | 14 | +        string GoToPaymentGateway(Payment payment);
 | 
		
	
		
			
			|  | 15 | +    }
 | 
		
	
		
			
			|  | 16 | +
 | 
		
	
		
			
			|  | 17 | +    public class PaygateRepository: IPaygateRepository
 | 
		
	
		
			
			|  | 18 | +    {
 | 
		
	
		
			
			|  | 19 | +        public string GoToPaymentGateway(Payment payment)
 | 
		
	
		
			
			|  | 20 | +        {
 | 
		
	
		
			
			|  | 21 | +            string utcDate = DateTime.UtcNow.ToString("yyyy-MM-dd H:mm:ss");
 | 
		
	
		
			
			|  | 22 | +            var client = new RestClient("https://secure.paygate.co.za/payweb3/initiate.trans");
 | 
		
	
		
			
			|  | 23 | +            client.Timeout = -1;
 | 
		
	
		
			
			|  | 24 | +            var request = new RestRequest(Method.POST);
 | 
		
	
		
			
			|  | 25 | +            var total = payment.Amount;
 | 
		
	
		
			
			|  | 26 | +            string paygateId = "10011072130";
 | 
		
	
		
			
			|  | 27 | +            string reff = "";
 | 
		
	
		
			
			|  | 28 | +            if (payment.TimeshareWeekId != 0)
 | 
		
	
		
			
			|  | 29 | +            {
 | 
		
	
		
			
			|  | 30 | +                reff = payment.TimeshareWeekId.ToString();
 | 
		
	
		
			
			|  | 31 | +            }
 | 
		
	
		
			
			|  | 32 | +            else
 | 
		
	
		
			
			|  | 33 | +            {
 | 
		
	
		
			
			|  | 34 | +                reff = payment.PropertyId.ToString();
 | 
		
	
		
			
			|  | 35 | +            }
 | 
		
	
		
			
			|  | 36 | +
 | 
		
	
		
			
			|  | 37 | +            string amm = Math.Round((total * 100)).ToString();
 | 
		
	
		
			
			|  | 38 | +            string currenc = "ZAR";
 | 
		
	
		
			
			|  | 39 | +            string retUrl = "http://localhost:57260/api/values";
 | 
		
	
		
			
			|  | 40 | +            string transDate = utcDate;
 | 
		
	
		
			
			|  | 41 | +            string loc = "en-za";
 | 
		
	
		
			
			|  | 42 | +            string count = "ZAF";
 | 
		
	
		
			
			|  | 43 | +            string mail = "jlouw365@gmail.com";
 | 
		
	
		
			
			|  | 44 | +            request.AlwaysMultipartFormData = true;
 | 
		
	
		
			
			|  | 45 | +            request.AddParameter("PAYGATE_ID", paygateId);
 | 
		
	
		
			
			|  | 46 | +            request.AddParameter("REFERENCE", reff);
 | 
		
	
		
			
			|  | 47 | +            request.AddParameter("AMOUNT", amm);
 | 
		
	
		
			
			|  | 48 | +            request.AddParameter("CURRENCY", currenc);
 | 
		
	
		
			
			|  | 49 | +            request.AddParameter("RETURN_URL", retUrl);
 | 
		
	
		
			
			|  | 50 | +            request.AddParameter("TRANSACTION_DATE", transDate);
 | 
		
	
		
			
			|  | 51 | +            request.AddParameter("LOCALE", loc);
 | 
		
	
		
			
			|  | 52 | +            request.AddParameter("COUNTRY", count);
 | 
		
	
		
			
			|  | 53 | +            request.AddParameter("EMAIL", mail);
 | 
		
	
		
			
			|  | 54 | +            string checksum = Checksum(
 | 
		
	
		
			
			|  | 55 | +                paygateId +
 | 
		
	
		
			
			|  | 56 | +                reff +
 | 
		
	
		
			
			|  | 57 | +                amm +
 | 
		
	
		
			
			|  | 58 | +                currenc +
 | 
		
	
		
			
			|  | 59 | +                retUrl +
 | 
		
	
		
			
			|  | 60 | +                transDate +
 | 
		
	
		
			
			|  | 61 | +                loc +
 | 
		
	
		
			
			|  | 62 | +                count +
 | 
		
	
		
			
			|  | 63 | +                mail +
 | 
		
	
		
			
			|  | 64 | +                "secret");
 | 
		
	
		
			
			|  | 65 | +            request.AddParameter("CHECKSUM", checksum);
 | 
		
	
		
			
			|  | 66 | +            string gatewayReturn = client.Execute(request).Content.ToString();
 | 
		
	
		
			
			|  | 67 | +            List<string> vs = gatewayReturn.Split('&').ToList();
 | 
		
	
		
			
			|  | 68 | +            string payReqId = vs[1].Split('=')[1].ToString();
 | 
		
	
		
			
			|  | 69 | +            //var updatedOrder = _dbContext.Payments.OrderByDescending(x => x.Id).FirstOrDefault();
 | 
		
	
		
			
			|  | 70 | +            //updatedOrder.PaymentToken = payReqId;
 | 
		
	
		
			
			|  | 71 | +            //_dbContext.Payments.Update(updatedOrder);
 | 
		
	
		
			
			|  | 72 | +            //_dbContext.SaveChanges();
 | 
		
	
		
			
			|  | 73 | +            return client.Execute(request).Content.ToString();
 | 
		
	
		
			
			|  | 74 | +        }
 | 
		
	
		
			
			|  | 75 | +
 | 
		
	
		
			
			|  | 76 | +        private string Checksum(string data)
 | 
		
	
		
			
			|  | 77 | +        {
 | 
		
	
		
			
			|  | 78 | +            using (var md5 = MD5.Create())
 | 
		
	
		
			
			|  | 79 | +            {
 | 
		
	
		
			
			|  | 80 | +                return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(data)))
 | 
		
	
		
			
			|  | 81 | +                    .Replace("-", string.Empty).ToLower();
 | 
		
	
		
			
			|  | 82 | +            }
 | 
		
	
		
			
			|  | 83 | +        }
 | 
		
	
		
			
			|  | 84 | +    }
 | 
		
	
		
			
			|  | 85 | +}
 |