I am trying to access Adyen test API that requires basic authentication credentials. https://docs.adyen.com/developers/ecommerce-integration
My credentials work when accessing the API page through browser.
But I get an 401 Unauthorized response when trying to access the API with XMLHttpRequest POST request.
Javascript Code
var url = "https://pal-test.adyen.com/pal/servlet/Payment/v25/authorise";
var username = "ws@Company.CompanyName";
var password = "J}5fJ6+?e6&lh/Zb0>r5y2W5t";
var base64Credentials = btoa(username+":"+password);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", url, true);
xhttp.setRequestHeader("content-type", "application/json");
xhttp.setRequestHeader("Authorization", "Basic " + base64Credentials);
var requestParams = XXXXXXXX;
xhttp.send(requestParams);
Result
That screenshot shows “Request Method: OPTIONS”, which indicates the details displayed are for a CORS preflight OPTIONS request automatically made by your browser—not for your
POST
.Your browser doesn’t (and can’t) send the
Authorization
header when it makes thatOPTIONS
request, and that causes the preflight to fail, so the browser never moves on to trying yourPOST
.As long as
https://pal-test.adyen.com/pal/servlet/Payment/v25/authorise
requires authentication forOPTIONS
requests, there’s no way you can make a successfulPOST
to it.The reason is because what’s happening here is this:
Authorization
header.Authorization
header require me to do a CORS preflightOPTIONS
to make sure the server allows requests with that header.OPTIONS
request to the server without theAuthorization
header—because the whole purpose of theOPTIONS
check is to see if it’s OK to send that.OPTIONS
request but instead of responding to it in a way that indicates it allowsAuthorization
in requests, it rejects it with a 401 since it lacks that header.POST
request from your code.The PAL is a Payment Authorisation API. You never want to call it from a browser. You only want to expose your username and password to send in payments in your backend code.
In Client-side encryption, the encryption is done in the browser. You then send the encrypted data to your own server. On your server you then create a payment authorization request (of which the encrypted data is one of the elements, along side payment amount, etc).
If you would be able to manage to make this run from your browser, your end solution will allow your shoppers to change amounts, currency's, payment meta data etc from the JavaScript layer. This should never be the case.
The authorization is for that reason part of the "Server side" integration part of documentation: https://docs.adyen.com/developers/ecommerce-integration?ecommerce=ecommerce-integration#serverside
Depending on your server side landscape the CURL implementation in your favorite language differs, but most of the time are easy to find.
Kind regards,
Arnoud