session.completeMerchantValidation returns an error in applepay js

We are trying to implement apple pay on the web in our project. When trying to implement apple pay the session.completeMerchantValidation returns the error InvalidAccessError : The object does not support the operation or argument . Below is the code snippet of my code. I am passing an object to session.completeMerchantValidation(as mentioned in other stackoverflow posts) but still getting the error. The domain name in the merchantSession object matches with the host name in the request url also. Not sure what is the issue here.Any help to fix this error is highly appreciated



session.onvalidatemerchant = (event) => {
var promise = validateMerchant2(event.validationURL);
promise.then(function (merchantSession) {
try{
session.completeMerchantValidation(merchantSession);
}
catch(err){alert("error while completing merchant validation : "+err);}
});
};
function validateMerchant2(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
var sessionURL='/getAppleSession2?url='+url;
alert('sessionURL is'+sessionURL);
xhr.open('GET',sessionURL);
xhr.onload = function () {
alert("status " + this.status);
if (this.status >= 200 && this.status < 300) {
alert('xhr.response is : '+typeof(xhr.response)+': '+JSON.stringify(xhr.response));
try{
resolve(JSON.parse(xhr.response));
}
catch(e){
alert('Error while doing JSON parse on xhr.response'+e);
}
} else {
alert('in reject of validateMerchant2 '+this.status+' ' +xhr.statusText);
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}

Replies

There could be a few different things happening here. First thing I would do to try and narrow this down is try and defensively evaluate the object you are passing into session.completeMerchantValidation(). Are you able to determine that this is not the object structure you are expecting before using this object?


The second thing you could do is make sure you are getting valid merchant session on the server before using this object on the client. For example, are you seeing anything on the server that looks off when communicating with the Apple Pay servers?


Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com