Esempio n. 1
0
        public GatewayApiRequest(GatewayApiConfig gatewayApiConfig, customerBilling billingAddress, string receiptNumber, string orderNumber)
        {
            GatewayApiConfig = gatewayApiConfig;

            if (billingAddress != null)
            {
                if (!string.IsNullOrEmpty(billingAddress.Name))
                {
                    CustomerName = billingAddress.Name;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingUnit))
                {
                    BillingUnit = billingAddress.BillingUnit;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingFloor))
                {
                    BillingFloor = billingAddress.BillingFloor;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingStreetName))
                {
                    BillingStreetName = billingAddress.BillingStreetName;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingBuildingNumber))
                {
                    BillingBuildingNumber = billingAddress.BillingBuildingNumber;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingBuildingName))
                {
                    BillingBuildingName = billingAddress.BillingBuildingName;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingContactNumber))
                {
                    BillingContactNumber = billingAddress.BillingContactNumber;
                }

                if (!string.IsNullOrEmpty(billingAddress.BillingPostCode))
                {
                    BillingPostCode = billingAddress.BillingPostCode;
                }
            }

            if (!string.IsNullOrEmpty(receiptNumber))
            {
                ReceiptNumber = receiptNumber;
            }

            if (!string.IsNullOrEmpty(orderNumber))
            {
                OrderNumber = orderNumber;
            }
        }
Esempio n. 2
0
        public GatewayApiRequest(GatewayApiConfig gatewayApiConfig, string receiptNumber, string orderNumber)
        {
            GatewayApiConfig = gatewayApiConfig;

            if (!string.IsNullOrEmpty(receiptNumber))
            {
                ReceiptNumber = receiptNumber;
            }

            if (!string.IsNullOrEmpty(orderNumber))
            {
                OrderNumber = orderNumber;
            }
        }
Esempio n. 3
0
 public GatewayApiClient(GatewayApiConfig gatewayApiConfig)
 {
     GatewayApiConfig = gatewayApiConfig;
 }
Esempio n. 4
0
 public GatewayApiRequest(GatewayApiConfig gatewayApiConfig)
 {
     GatewayApiConfig = gatewayApiConfig;
 }
Esempio n. 5
0
        public String executeHTTPMethod(GatewayApiRequest gatewayApiRequest)
        {
            string body = String.Empty;

            GatewayApiConfig GatewayApiConfig = gatewayApiRequest.GatewayApiConfig;


            //proxy settings
            if (GatewayApiConfig.UseProxy)
            {
                WebProxy proxy = new WebProxy(GatewayApiConfig.ProxyHost, true);
                if (!String.IsNullOrEmpty(GatewayApiConfig.ProxyUser))
                {
                    if (String.IsNullOrEmpty(GatewayApiConfig.ProxyDomain))
                    {
                        proxy.Credentials = new NetworkCredential(GatewayApiConfig.ProxyUser, GatewayApiConfig.ProxyPassword);
                    }
                    else
                    {
                        proxy.Credentials = new NetworkCredential(GatewayApiConfig.ProxyUser, GatewayApiConfig.ProxyPassword, GatewayApiConfig.ProxyDomain);
                    }
                }
                WebRequest.DefaultWebProxy = proxy;
            }

            // Create the web request
            HttpWebRequest request = WebRequest.Create(gatewayApiRequest.RequestUrl) as HttpWebRequest;

            //http verb
            request.Method = gatewayApiRequest.ApiMethod;

            //content type, json, form, etc
            request.ContentType = gatewayApiRequest.ContentType;

            LogInfo.Information($@"HttpWebRequest url {gatewayApiRequest.RequestUrl} method {request.Method}");


            //Authentication setting
            if (GatewayApiConfig.AuthenticationByCertificate)
            {
                //custom implementation fo SSL certificate validation callback
                request.ServerCertificateValidationCallback +=
                    (sender, cert, chain, error) =>
                {
                    return(error == SslPolicyErrors.None || (error != SslPolicyErrors.None && GatewayApiConfig.IgnoreSslErrors));
                };

                //create a new certificate collection
                X509Certificate2Collection certificates = new X509Certificate2Collection();

                //load and add a new certificate loaded from file (p12)
                certificates.Add(new X509Certificate2(new X509Certificate(GatewayApiConfig.CertificateLocation, GatewayApiConfig.CertificatePassword)));

                //attach certificate to request
                request.ClientCertificates = certificates;
            }
            else
            {
                string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(GatewayApiConfig.Username + ":" + GatewayApiConfig.Password));
                request.Headers.Add("Authorization", "Basic " + credentials);
            }

            //buuld the request
            try
            {
                if ((gatewayApiRequest.ApiMethod == "PUT" || gatewayApiRequest.ApiMethod == "POST") &&
                    !String.IsNullOrEmpty(gatewayApiRequest.Payload))
                {
                    LogInfo.Information($@"HttpWebRequest payload {gatewayApiRequest.Payload}");

                    // Create a byte array of the data we want to send
                    byte[] utf8bytes    = Encoding.UTF8.GetBytes(gatewayApiRequest.Payload);
                    byte[] iso8859bytes = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), utf8bytes);

                    // Set the content length in the request headers
                    request.ContentLength = iso8859bytes.Length;

                    // Write request data
                    using (Stream postStream = request.GetRequestStream())
                    {
                        postStream.Write(iso8859bytes, 0, iso8859bytes.Length);
                    }
                }

                // Get response
                try
                {
                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        // Get the response stream
                        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("iso-8859-1"));
                        body = reader.ReadToEnd();
                    }
                }
                catch (WebException wex)
                {
                    LogInfo.Debug($@"Response debug : {wex.Response.Headers}");
                    StreamReader reader = new StreamReader(wex.Response.GetResponseStream(), Encoding.GetEncoding("iso-8859-1"));
                    body = reader.ReadToEnd();
                }

                LogInfo.Information($@"HttpWebResponse response {body}");

                return(body);
            }
            catch (Exception ex)
            {
                return(ex.Message + "\n\naddress:\n" + request.Address.ToString() + "\n\nheader:\n" + request.Headers.ToString() + "data submitted:\n" + gatewayApiRequest.Payload);
            }
        }