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 <IActionResult> Validate([FromBody] ValidateMerchantSessionModel model) { // 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()); } // Load the merchant certificate for two-way TLS authentication with the Apple Pay server. var certificate = LoadMerchantCertificate(); // Get the merchant identifier from the certificate to send in the validation payload. var merchantIdentifier = GetMerchantIdentifier(certificate); // Create the JSON payload to POST to the Apple Pay merchant validation URL. var payload = new { merchantIdentifier = merchantIdentifier, domainName = Request.GetTypedHeaders().Host.Value, displayName = _options.StoreName }; JObject merchantSession; // Create an HTTP client with the merchant certificate // for two-way TLS authentication over HTTPS. using (var httpClient = CreateHttpClient(certificate)) { var jsonPayload = JsonConvert.SerializeObject(payload); using (var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json")) { // POST the data to create a valid Apple Pay merchant session. using (var response = await httpClient.PostAsync(requestUri, content)) { response.EnsureSuccessStatusCode(); // Read the opaque merchant session JSON from the response body. var merchantSessionJson = await response.Content.ReadAsStringAsync(); merchantSession = JObject.Parse(merchantSessionJson); } } } // Return the merchant session as-is to the JavaScript as JSON. return(Json(merchantSession)); }
public async Task <ActionResult> Validate([FromBody] ValidateMerchantSessionModel model) { Uri requestUri; if (!ModelState.IsValid || string.IsNullOrWhiteSpace(model?.ValidationUrl) || !Uri.TryCreate(model.ValidationUrl, UriKind.Absolute, out requestUri)) { return(Json("0"));;//BadRequest(); } // Load the merchant certificate for two-way TLS authentication with the Apple Pay server. var certificate = LoadMerchantCertificate(); // Get the merchant identifier from the certificate to send in the validation payload. var merchantIdentifier = GetMerchantIdentifier(certificate); // Create the JSON payload to POST to the Apple Pay merchant validation URL. var payload = new { merchantIdentifier = merchantIdentifier, //domainName = Request.GetTypedHeaders().Host.Value, displayName = _options.StoreName }; JObject merchantSession; // Create an HTTP client with the merchant certificate // for two-way TLS authentication over HTTPS. using (var httpClient = CreateHttpClient(certificate)) { var jsonPayload = JsonConvert.SerializeObject(payload); using (var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json")) { // POST the data to create a valid Apple Pay merchant session. using (var response = await httpClient.PostAsync(requestUri, content)) { response.EnsureSuccessStatusCode(); // Read the opaque merchant session JSON from the response body. var merchantSessionJson = await response.Content.ReadAsStringAsync(); merchantSession = JObject.Parse(merchantSessionJson); } } } // Return the merchant session as-is to the JavaScript as JSON. return(Json(merchantSession)); }
public ActionResult StartApplePaySession([FromBody] ValidateMerchantSessionModel model, CancellationToken cancellationToken) { Uri requestUri; if (!ModelState.IsValid || string.IsNullOrWhiteSpace(model?.ValidationUrl) || !Uri.TryCreate(model.ValidationUrl, UriKind.Absolute, out requestUri)) { return(Json(false)); } string result = GetMerchantSessionAsync(new Uri(model.ValidationUrl), new MerchantSessionRequest { DisplayName = _applePayConfiguration.DisplayName, Initiative = "web", InitiativeContext = _applePayConfiguration.InitiativeContext, MerchantIdentifier = CertificateService.GetMerchantIdentifier( CertificateService.LoadMerchantCertificate(_applePayConfiguration.CertificateThumbprint)) }).Result; return(Json(result)); }