Exemplo n.º 1
0
        private int CallWebService(Payment payment, byte transactionType)
        {
            int    ret      = 10;
            string url      = payment.ProviderURL;
            string tranDesc = "";

            if (Tools.NullToString(url).Length == 0)
            {
                if (Tools.LiveTestOrDev() == Constants.SystemMode.Live)
                {
                    url = "https://www.paygenius.co.za";
                }
                else
                {
                    url = "https://developer.paygenius.co.za";
                }
            }

            ret = 20;
            if (url.EndsWith("/"))
            {
                url = url.Substring(0, url.Length - 1);
            }

            ret = 30;
            if (transactionType == (byte)Constants.TransactionType.GetToken)
            {
                url      = url + "/pg/api/v2/card/register";
                tranDesc = "Get Token";
            }
            else if (transactionType == (byte)Constants.TransactionType.TokenPayment)
            {
                url      = url + "/pg/api/v2/payment/create";
                tranDesc = "Process Payment";
            }
            else
            {
            }

//			if ( Tools.NullToString(urlDetail).Length > 0 )
//			{
//				ret = 30;
//				if ( url.EndsWith("/") )
//					url = url.Substring(0,url.Length-1);
//				ret = 40;
//				if ( urlDetail.StartsWith("/") )
//					urlDetail = urlDetail.Substring(1);
//				ret = 50;
//				url = url + "/" + urlDetail;
//			}

            ret        = 60;
            strResult  = "";
            resultCode = "99";
            resultMsg  = "Internal error connecting to " + url;
            ret        = 70;

            try
            {
                string         sig;
                byte[]         page       = Encoding.UTF8.GetBytes(xmlSent);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.ContentType = "application/json";
                webRequest.Accept      = "application/json";
                webRequest.Method      = "POST";
                ret = 60;
                webRequest.Headers["X-Token"] = payment.ProviderKey;
                ret = 90;
                sig = GetSignature(payment.ProviderPassword, url, xmlSent);
                webRequest.Headers["X-Signature"] = sig;
                ret = 100;

                Tools.LogInfo("TransactionPayGenius.CallWebService/20",
                              "Transaction Type=" + tranDesc +
                              ", URL=" + url +
                              ", Token=" + payment.ProviderKey +
                              ", Key=" + payment.ProviderPassword +
                              ", Signature=" + sig +
                              ", JSON Sent=" + xmlSent, 10);

                using (Stream stream = webRequest.GetRequestStream())
                {
                    ret = 110;
                    stream.Write(page, 0, page.Length);
                    stream.Flush();
                    stream.Close();
                }

                ret = 120;
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    ret = 130;
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        ret       = 140;
                        strResult = rd.ReadToEnd();
                    }
                    if (strResult.Length == 0)
                    {
                        ret       = 150;
                        resultMsg = "No data returned from " + url;
                        Tools.LogInfo("TransactionPayGenius.CallWebService/30", "Failed, JSON Rec=(empty)", 199);
                    }
                    else
                    {
                        ret        = 160;
                        resultCode = Tools.JSONValue(strResult, "code");
                        resultMsg  = Tools.JSONValue(strResult, "message");

                        if (Successful)
                        {
                            ret        = 170;
                            resultCode = "00";
                            Tools.LogInfo("TransactionPayGenius.CallWebService/40", "Successful, JSON Rec=" + strResult, 255);
                        }
                        else
                        {
                            ret = 180;
                            Tools.LogInfo("TransactionPayGenius.CallWebService/50", "Failed, JSON Rec=" + strResult, 199);
                            if (Tools.StringToInt(resultCode) == 0)
                            {
                                resultCode = "99";
                            }
                        }
                    }
                }
                ret = 0;
            }
            catch (Exception ex)
            {
                Tools.LogInfo("TransactionPayGenius.CallWebService/298", "ret=" + ret.ToString(), 220);
                Tools.LogException("TransactionPayGenius.CallWebService/299", "ret=" + ret.ToString(), ex);
            }
            return(ret);
        }
Exemplo n.º 2
0
        private int CallWebService(Payment payment)
        {
            int    ret = 10;
            string url = payment.ProviderURL;

            if (Tools.NullToString(url).Length == 0)
            {
                url = BureauURL;
            }

            if (Tools.NullToString(url).Length == 0)
            {
                return(20);
            }

            ret        = 30;
            acsUrl     = "";
            txnStatus  = "";
            strResult  = "";
            resultCode = "99999";
            resultMsg  = "(99999) Internal error connecting to " + url;
            ret        = 50;

            try
            {
                string         sig;
                byte[]         page       = Encoding.UTF8.GetBytes(xmlSent);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                ret = 60;
                webRequest.ContentType = "application/json";
                webRequest.Accept      = "application/json";
                webRequest.Method      = "POST";
                ret = 70;
                webRequest.Headers["keyId"] = payment.ProviderKey;
                ret = 80;
                sig = GetSignature(xmlSent, payment.ProviderPassword);
                webRequest.Headers["hmac"] = sig;
                ret = 100;

                Tools.LogInfo("TransactionENets.CallWebService/10",
                              "Transaction=" + Tools.TransactionTypeName(payment.TransactionType) +
                              ", URL=" + url +
                              ", MID=" + payment.ProviderAccount +
                              ", KeyId=" + payment.ProviderKey +
//				            ", SecretKey=" + payment.ProviderPassword +
                              ", Signature=" + sig +
                              ", JSON Sent=" + xmlSent, 10);

                using (Stream stream = webRequest.GetRequestStream())
                {
                    ret = 110;
                    stream.Write(page, 0, page.Length);
                    stream.Flush();
                    stream.Close();
                }

                ret = 120;
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    ret = 130;
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        ret       = 140;
                        strResult = rd.ReadToEnd();
                    }
                }

                if (strResult.Trim().Length == 0)
                {
                    ret       = 150;
                    resultMsg = "No data returned from " + url;
                    Tools.LogInfo("TransactionENets.CallWebService/20", Tools.TransactionTypeName(payment.TransactionType) + ", JSON Rec=(blank)", 199);
                }
                else
                {
                    Tools.LogInfo("TransactionENets.CallWebService/30", Tools.TransactionTypeName(payment.TransactionType) + ", JSON Rec=" + strResult, 255);

                    ret        = 160;
                    txnStatus  = Tools.JSONValue(strResult, "netsTxnStatus");
                    resultMsg  = Tools.JSONValue(strResult, "netsTxnMsg");
                    resultCode = Tools.JSONValue(strResult, "stageRespCode");

                    if (resultCode.Length > 0)
                    {
                        try
                        {
                            ret = 170;
                            string rex = resultCode.Trim().ToUpper();
                            int    k   = rex.IndexOf("-");
                            if (k >= 0 && k < rex.Length - 1)
                            {
                                rex = rex.Substring(k + 1);
                            }
                            else if (k >= 0)
                            {
                                rex = rex.Substring(0, k);
                            }
                            ret        = 180;
                            resultCode = rex;
                        }
                        catch
                        { }
                    }

                    ret = 190;
                    if (!Successful || resultMsg.Length > 0)
                    {
                        resultMsg = resultMsg + " (netsTxnStatus=" + txnStatus + ")";
                    }

                    if (payment.TransactionType == (byte)Constants.TransactionType.ManualPayment)
                    {
                        if (txnStatus == "5")                           // 3d Secure
                        {
                            eci     = Tools.JSONValue(strResult, "eci");
                            paReq   = Tools.JSONValue(strResult, "pareq");
                            termUrl = Tools.JSONValue(strResult, "termUrl");
                            md      = Tools.JSONValue(strResult, "md");
                            acsUrl  = Tools.JSONValue(strResult, "acsUrl");
                        }
                    }
                }
                ret = 0;
            }
            catch (WebException ex1)
            {
                Tools.DecodeWebException(ex1, "TransactionENets.CallWebService/297", "ret=" + ret.ToString());
            }
            catch (Exception ex2)
            {
                Tools.LogInfo("TransactionENets.CallWebService/298", "ret=" + ret.ToString(), 220);
                Tools.LogException("TransactionENets.CallWebService/299", "ret=" + ret.ToString(), ex2);
            }
            return(ret);
        }