I am getting error while await applePayClient.PostAsJsonAsync(validationUrl, validationPayload)
I am testing it on local machine. Am I even can test this on local machine or not?
Error: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
validationUrl: https://apple-pay-gateway.apple.com/paymentservices/startSession
JS
<script> $("#pay-cc-button").click(function () { $.ajax({ url: "../Payment/ProcessCCPayment", data: {
},
type: "POST",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
});
$(document).ready(function () {
if (window.ApplePaySession && ApplePaySession.canMakePayments()) {
$('#apple-pay-button').show();
$('#apple-pay-button').on('click', function () {
const paymentRequest = {
countryCode: 'US',
currencyCode: 'USD',
total: {
label: 'xxxxx',
amount: '1.10',
},
supportedNetworks: ['visa', 'masterCard', 'amex', 'discover'],
merchantCapabilities: ['supports3DS']//, 'supportsCredit', 'supportsDebit']
};
const session = new ApplePaySession(3, paymentRequest);
session.onvalidatemerchant = function (event) {
const validationURL = event.validationURL;
validateMerchant(validationURL).then(function (merchantSession) {
session.completeMerchantValidation(merchantSession);
});
};
session.onpaymentauthorized = function (event) {
const payment = event.payment;
processPayment(payment).then(function (success) {
if (success) {
session.completePayment(ApplePaySession.STATUS_SUCCESS);
} else {
session.completePayment(ApplePaySession.STATUS_FAILURE);
}
});
};
session.begin();
});
}
function validateMerchant(validationURL) {
return $.ajax({
url: '/Payment/ValidateMerchant',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ validationURL: validationURL }),
success: function (data) {
$("#processStatus").append("success validate-merchant = " + data + "<br />");
},
error: function (data) {
$("#processStatus").append("error validate-merchant = " + data + "<br />");
},
});
}
function processPayment(payment) {
console.log("payment" + payment);
return $.ajax({
url: '/Payment/ProcessPayment',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(payment),
success: function (data) {
$("#processStatus").append("success process-payment = " + data + "<br />");
},
error: function (data) {
$("#processStatus").append("error validate-merchant = " + data + "<br />");
},
});
}
});
</script>
C# code:
var applePayClientHandler = new HttpClientHandler { SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13 };
var applePayClient = new HttpClient(applePayClientHandler); var merchantId = "merchant.com.xxxxxx.sandbox"; var _displayName = "Sandbox"; var domainName = "xxxxxx.co";
var validationUrl = request.ValidationURL;
var validationPayload = new { MerchantIdentifier = merchantId, DisplayName = _displayName, Initiative = "web", InitiativeContext = domainName }; try { var response = await applePayClient.PostAsJsonAsync(validationUrl, validationPayload); var merchantSession = await response.Content.ReadAsStringAsync();
return merchantSession;
} catch (HttpRequestException httpEx) { // Log detailed HTTP request/response information Console.WriteLine($"HttpRequestException: {httpEx.Message}"); if (httpEx.InnerException != null) { Console.WriteLine($"InnerException: {httpEx.InnerException.Message}"); } throw; }
Hi @rk25, @sjscheider,
This .NET error is likely related to transport layer security (TLS), and not Apple Pay directly. Please ensure your server meets our requirements below:
Setting Up Your Server
https://developer.apple.com/documentation/apple_pay_on_the_web/setting_up_your_server
And then ensure your .NET implementation has the proper security protocol configurations. I'd expect you to find more details about this configuration in .NET on one of their support channels.
Cheers,
Paris X Pinkney | WWDR | DTS Engineer