Пример #1
0
        public async Task <JsonResult> GetRequest(Guid?id)
        {
            if (id == null)
            {
                throw new NullReferenceException();
            }
            var        contract = db.Contract.Find(id);
            HttpClient http     = new HttpClient();
            Dictionary <string, string> request = new Dictionary <string, string>();
            string paymentAmount = (double.Parse(contract.ProjectCost) * 100).ToString("00"); // amount int cents e.i 50 rands is 5000 cents

            request.Add("PAYGATE_ID", PayGateID);
            request.Add("REFERENCE", contract.QuotationReference); // Payment ref e.g ORDER NUMBER
            request.Add("AMOUNT", paymentAmount);
            request.Add("CURRENCY", "ZAR");                        // South Africa
            request.Add("RETURN_URL", $"{Request.Url.Scheme}://{Request.Url.Authority}/pay/completepayment");
            request.Add("TRANSACTION_DATE", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            request.Add("LOCALE", "en-za");
            request.Add("COUNTRY", "ZAF");

            // get authenticated user's email
            // use a valid email, paygate will send a transaction confirmation to it
            var email = db.Client.Where(c => c.UserId == contract.ClientId).FirstOrDefault().Email;

            request.Add("EMAIL", email);

            request.Add("CHECKSUM", _payment.GetMd5Hash(request, PayGateKey));

            string              requestString = _payment.ToUrlEncodedString(request);
            StringContent       content       = new StringContent(requestString, Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response      = await http.PostAsync("https://secure.paygate.co.za/payweb3/initiate.trans", content);

            // if the request did not succeed, this line will make the program crash
            response.EnsureSuccessStatusCode();

            string responseContent = await response.Content.ReadAsStringAsync();

            Dictionary <string, string> results = _payment.ToDictionary(responseContent);

            if (results.Keys.Contains("ERROR"))
            {
                return(Json(new
                {
                    success = false,
                    message = "An error occured while initiating your request"
                }, JsonRequestBehavior.AllowGet));
            }

            if (!_payment.VerifyMd5Hash(results, PayGateKey, results["CHECKSUM"]))
            {
                return(Json(new
                {
                    success = false,
                    message = "MD5 verification failed"
                }, JsonRequestBehavior.AllowGet));
            }

            bool IsRecorded = _payment.AddTransaction(request, results["PAY_REQUEST_ID"]);

            if (IsRecorded)
            {
                return(Json(new
                {
                    success = true,
                    message = "Request completed successfully",
                    results
                }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new
            {
                success = false,
                message = "Failed to record a transaction"
            }, JsonRequestBehavior.AllowGet));
        }
        public async Task <JsonResult> GetRequest()
        {
            //Sample Order
            Order orderDb = new Order();

            //Self entered value, hard coded.
            orderDb = db.Orders.Find(getOrderIDD);
            int patientId = Convert.ToInt32(Session["id"]);

            HttpClient http = new HttpClient();
            Dictionary <string, string> request = new Dictionary <string, string>();
            string paymentAmount = (orderDb.TotalPrice * 100).ToString("00"); // amount int cents e.i 50 rands is 5000 cents

            request.Add("PAYGATE_ID", PayGateID);
            request.Add("REFERENCE", orderDb.OrderID.ToString()); // Payment ref e.g ORDER NUMBER
            request.Add("AMOUNT", paymentAmount);
            request.Add("CURRENCY", "ZAR");                       // South Africa
            request.Add("RETURN_URL", $"{Request.Url.Scheme}://{Request.Url.Authority}/pay/completepayment");
            request.Add("TRANSACTION_DATE", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            request.Add("LOCALE", "en-za");
            request.Add("COUNTRY", "ZAF");

            // get authenticated user's email
            // use a valid email, paygate will send a transaction confirmation to it
            //if (HttpContext.User.Identity.IsAuthenticated)
            //{
            //    //request.Add("EMAIL", _payment.GetAuthenticatedUser().Email);
            //    request.Add("EMAIL", "*****@*****.**");
            //} else
            if (Session["Title"] == "Patient")
            {
                //request.Add("EMAIL", _payment.GetAuthenticatedUser().Email);
                Patient patient = db.Patients.Find(patientId);

                request.Add("EMAIL", patient.Email);
            }
            else
            {
                // put your own email address for the payment confirmation (developer only)
                request.Add("EMAIL", "*****@*****.**");
            }
            request.Add("CHECKSUM", _payment.GetMd5Hash(request, PayGateKey));
            //This place all request key values above to a string encoded in HTTP protocol
            string        requestString = _payment.ToUrlEncodedString(request);
            StringContent content       = new StringContent(requestString, Encoding.UTF8, "application/x-www-form-urlencoded");
            //HTTP Response from PAYGATE based on the Request sent to its website from GetRequest actionMethod from Pay Controller.
            HttpResponseMessage response = await http.PostAsync("https://secure.paygate.co.za/payweb3/initiate.trans", content);

            // if the request did not succeed, this line will make the program crash
            response.EnsureSuccessStatusCode();
            //Get the Content information from HTTP Responce and convert it a string
            string responseContent = await response.Content.ReadAsStringAsync();

            //Top 4 imperative information from Response (PAYGATE SERVER) to return to user website
            Dictionary <string, string> results = _payment.ToDictionary(responseContent);

            if (results.Keys.Contains("ERROR"))
            {
                return(Json(new
                {
                    success = false,
                    message = "An error occured while initiating your request"
                }, JsonRequestBehavior.AllowGet));
            }
            //if MD5 is not true or equal produce the error
            if (!_payment.VerifyMd5Hash(results, PayGateKey, results["CHECKSUM"]))
            {
                return(Json(new
                {
                    success = false,
                    message = "MD5 verification failed"
                }, JsonRequestBehavior.AllowGet));
            }

            bool IsRecorded = _payment.AddTransaction(request, results["PAY_REQUEST_ID"]);

            if (IsRecorded)
            {
                return(Json(new
                {
                    success = true,
                    message = "Request completed successfully",
                    results
                }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new
            {
                success = false,
                message = "Failed to record a transaction"
            }, JsonRequestBehavior.AllowGet));
        }
Пример #3
0
        public async Task <JsonResult> GetRequest(string reference)
        {
            decimal amount = 0;
            string  email  = User.Identity.Name;

            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                foreach (var item in context.Carts.Where(c => c.Reference == reference &&
                                                         !c.IsDeleted && !c.IsComplete).ToList())
                {
                    amount += item.Price * (decimal)item.Quantity;
                }
            }
            HttpClient http = new HttpClient();
            Dictionary <string, string> request = new Dictionary <string, string>();
            string paymentAmount = (amount * 100).ToString("00"); // amount int cents e.i 50 rands is 5000 cents

            request.Add("PAYGATE_ID", _payGateID);
            request.Add("REFERENCE", reference); // Payment ref e.g ORDER NUMBER
            request.Add("AMOUNT", paymentAmount);
            request.Add("CURRENCY", "ZAR");      // South Africa
            request.Add("RETURN_URL", $"{Request.Url.Scheme}://{Request.Url.Authority}/pay/completepayment");
            request.Add("TRANSACTION_DATE", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            request.Add("LOCALE", "en-za");
            request.Add("COUNTRY", "ZAF");

            request.Add("EMAIL", email);

            request.Add("CHECKSUM", _payment.GetMd5Hash(request, _payGateKey));

            string              requestString = _payment.ToUrlEncodedString(request);
            StringContent       content       = new StringContent(requestString, Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response      = await http.PostAsync("https://secure.paygate.co.za/payweb3/initiate.trans", content);

            // if the request did not succeed, this line will make the program crash
            response.EnsureSuccessStatusCode();

            string responseContent = await response.Content.ReadAsStringAsync();

            Dictionary <string, string> results = _payment.ToDictionary(responseContent);

            if (results.Keys.Contains("ERROR"))
            {
                return(Json(new
                {
                    success = false,
                    message = "An error occured while initiating your request"
                }, JsonRequestBehavior.AllowGet));
            }

            if (!_payment.VerifyMd5Hash(results, _payGateKey, results["CHECKSUM"]))
            {
                return(Json(new
                {
                    success = false,
                    message = "MD5 verification failed"
                }, JsonRequestBehavior.AllowGet));
            }

            bool IsRecorded = _payment.AddTransaction(request, results["PAY_REQUEST_ID"]);

            if (IsRecorded)
            {
                return(Json(new
                {
                    success = true,
                    message = "Request completed successfully",
                    results
                }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new
            {
                success = false,
                message = "Failed to record a transaction"
            }, JsonRequestBehavior.AllowGet));
        }