public async Task <IActionResult> Validate([FromBody] ValidateMerchantSessionModel model, CancellationToken cancellationToken = default) { // You may wish to additionally validate that the URI specified for merchant validation in the // request body is a documented Apple Pay JS hostname. The IP addresses and DNS hostnames of // these servers are available here: https://developer.apple.com/documentation/applepayjs/setting_up_server_requirements if (!ModelState.IsValid || string.IsNullOrWhiteSpace(model?.ValidationUrl) || !Uri.TryCreate(model.ValidationUrl, UriKind.Absolute, out Uri? requestUri)) { return(BadRequest()); } // Create the JSON payload to POST to the Apple Pay merchant validation URL. var request = new MerchantSessionRequest() { DisplayName = _options.StoreName, Initiative = "web", InitiativeContext = Request.GetTypedHeaders().Host.Value, MerchantIdentifier = _certificate.GetMerchantIdentifier(), }; JsonDocument merchantSession = await _client.GetMerchantSessionAsync(requestUri, request, cancellationToken); // Return the merchant session as-is to the JavaScript as JSON. return(Json(merchantSession.RootElement)); }
public async Task <string> GetMerchantSessionAsync( Uri requestUri, MerchantSessionRequest request, CancellationToken cancellationToken = default(CancellationToken)) { // POST the data to create a valid Apple Pay merchant session. string json = JsonConvert.SerializeObject(request); using (var content = new StringContent(json, Encoding.UTF8, "application/json")) { var clientHandler = GetHttpClientHandler(); HttpClient client = new HttpClient(clientHandler); try { var response = client.PostAsync(requestUri, content).Result; response.EnsureSuccessStatusCode(); // Read the opaque merchant session JSON from the response body. using (var stream = await response.Content.ReadAsStreamAsync()) { return(stream.ReadToEnd()); } } catch (Exception e) { return(e.ToString()); } } }
public async Task <MerchantSessionResponose> ValidateAsync(string validationUrl, string initiativeContext) { var identifider = _certificateProvider.GetMerchantIdentifier(); // Create the JSON payload to POST to the Apple Pay merchant validation URL. var request = new MerchantSessionRequest() { DisplayName = "Bin Shop", Initiative = "web", InitiativeContext = initiativeContext, MerchantIdentifier = identifider }; var client = _clientFactory.CreateClient(Constants.NamedHttpClientApplePay); var jsonContent = JsonSerializer.Serialize(request, _jsonOptions); var requestMessage = new HttpRequestMessage() { RequestUri = new Uri(validationUrl), Method = HttpMethod.Post, Content = new StringContent(jsonContent, Encoding.UTF8, MediaTypeNames.Application.Json) }; _logger.LogInformation("start to merchant validation."); var response = await client.SendAsync(requestMessage); _logger.LogInformation("merchant validation responsed from Apple."); response.EnsureSuccessStatusCode(); var json = await response.Content.ReadAsStringAsync(); var merchantSession = JsonSerializer.Deserialize <MerchantSessionResponose>(json, _jsonOptions); return(merchantSession); }
public async Task <string> Get(string url)//GetAsync([FromBody]AppleReq urlreq) { _logger.Debug("===Apple Pay STAR==="); //string url = urlreq.url; _logger.Debug("url = " + url); //_logger.Debug(Path.GetFullPath(cerpath)); #region Load certificate //var certificate = new X509Certificate2(cerpath); //var certificate = new X509Certificate2(cerpath); ////var certificate = new X509Certificate2("D:\\Dropbox\\GMP\\merchant.asia.gomypay.applepay.cer"); //_logger.Debug(certificate.ToString()); #endregion try { #region Load certificate var certificate = new X509Certificate2(_cerpath); _logger.Debug(certificate.ToString()); #endregion HttpClientHandler handler = new HttpClientHandler(); handler.ClientCertificates.Add(certificate); //handler.SslProtocols = SslProtocols.Tls12; //handler.SslProtocols = SslProtocols.Tls12; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //request = WebRequest.Create(url) as HttpWebRequest; //request.ProtocolVersion = HttpVersion.Version10; } else { //request = WebRequest.Create(url) as HttpWebRequest; } /* * if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) * { * ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; * ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); * request = WebRequest.Create(url) as HttpWebRequest; * request.ProtocolVersion = HttpVersion.Version10; * } * else * { * request = WebRequest.Create(url) as HttpWebRequest; * } */ var http = new HttpClient(handler); http.DefaultRequestHeaders.Add("Accept", "application/json"); _logger.Debug(http.ToString()); var options = new MerchantSessionRequest() { MerchantIdentifier = "merchant.asia.gomypay.applepay", DisplayName = "GomyPay", Initiative = "web", InitiativeContext = "cathay.gomytw.com" }; var json = JsonConvert.SerializeObject(options); var content = new StringContent(json, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = content }; _logger.Debug(request.ToString()); HttpResponseMessage response = await http.SendAsync(request); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); return(result); } catch (Exception ex) { _logger.Error(ex); return(ex.ToString()); } }