コード例 #1
0
        public ActionResult Index()
        {
            var gateway = new Gateway.Gateway();
            var req     = System.Web.HttpContext.Current.Request;

            // After the customer as responded to the ACS server, it returns the user
            // to the URL we provide, which includes acs=1 as a GET variable.
            // This code renames all the submitted form inputs, and also breaks out of the frame.
            // (In dynamic languages, this is effectively `responseCode = POST`. In C# we can
            // use string manipulation instead.
            if (req.QueryString.AllKeys.Contains("acs"))
            {
                var fields = new Dictionary <string, string>();

                foreach (var formItem in req.Form.AllKeys)
                {
                    fields.Add("threeDSResponse[" + formItem + "]", req.Form[formItem]);
                }

                ViewBag.GatewayHtml = SilentPost(req.Url.AbsoluteUri.Replace("acs=1", ""), fields, "_parent");

                return(View());
            }

            if (req.HttpMethod == "GET")
            {
                // Only the first request to this page/controller is a GET, that means this
                // must be the first request made.
                // The first step is to collect the customer's browser data. The SDK provides
                // a helper form for this purpose.
                ViewBag.GatewayHtml = gateway.CollectBrowserInfo(null, GetEnvironmentData());
            }
            else
            {
                // If any of the POST form keys start with threeDSResponse we have had a
                // response from the ACS server (after it has been wrapped by the section
                // of code above.)
                if (AnyKeyStartsWith(req.Form, "threeDSResponse"))
                {
                    var fields = new Dictionary <string, string>
                    {
                        { "action", "SALE" }
                    };

                    foreach (string item in req.Form)
                    {
                        if (item.StartsWith("threeDSResponse"))
                        {
                            fields.Add(item, req.Form[item]);
                        }
                    }

                    fields.Add("threeDSRef", threeDSRef);

                    // Send the threeDSresponse to the server, along with threeDSRef previously stored.
                    // The request will generally look like:
                    // threeDSResponse[AnyKeyFromACS] => value from the ACS
                    // threeDSRef => threeDSRef previously sent by the gateway.
                    // action => SALE
                    // MerchantID => 100856
                    ViewBag.GatewayHtml = ProcessResponseFields(gateway.DirectRequest(fields, null));
                }
                else // We don't have a threeDSResponse, but this is a POST request.
                {    // This is the first request send to the Gateway, which may then request the user
                     // be redirected to the ACS server.

                    var remoteAddress = req.UserHostAddress;

                    if (remoteAddress == "::1")
                    {
                        // In development environments we often see the IPv6 localhost address.
                        // The Gateway requires an IPv4 address.
                        remoteAddress = "8.8.8.8";
                    }

                    // Please note that this value MUST be HTTPS.
                    // In development, this may be achieved using Apache as a reverse proxy and
                    // changing this value.
                    var thisUrl = req.Url.AbsoluteUri;

                    var requestFields = GetInitialForm(thisUrl, remoteAddress);

                    foreach (string item in req.Form)
                    {
                        if (item.StartsWith("browserInfo["))
                        {
                            // remove the browserInfo[ and the trailing ]
                            requestFields.Add(item.Substring(12, item.Length - 13), req.Form[item]);
                        }
                    }

                    ViewBag.GatewayHtml = ProcessResponseFields(gateway.DirectRequest(requestFields, null));
                }
            }


            return(View());
        }
コード例 #2
0
 public void Setup()
 {
     gateway = new Gateway("1", "pass");
 }