3

I want to integrate with Apple Pay on my website using the Apple Pay JS API, but my call to Apple is failing.

We are using Firebase Functions to run our server, and Firebase Hosting to host our website.

To do this, I need to request an Apple Pay Payments Session as described in https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session.

The way this seems to work is that I start a payments session client side on my website and configure this to call an endpoint on my server. This then calls Apple's servers with a merchant identifier certificate I've created, and I get the payments session back and return it to my website. I'm following these documentation pages: https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/providing_merchant_validation https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session

I am calling Apple like this:

const { validationUrl } = req.body

const axiosInstance = axios.create({
  httpsAgent: new https.Agent({
    cert: fs.readFileSync(`${__dirname}/merchantIdentity.crt.pem`),
    key: fs.readFileSync(`${__dirname}/merchantIdentity.key.pem`),
    rejectUnauthorized: false
  })
})

const appleUrl = `https://${validationUrl}/paymentSession`
const validateMerchantData = {
  merchantIdentifier: <my merchant id>,
  displayName: "Mercado",
  initiative: "web",
  initiativeContext: process.env.CLIENT_URL.replace("https://", "")
}

const appleRes = await axiosInstance.post(appleUrl, validateMerchantData)

CLIENT_URL is the base URL of my client website

I am getting the following error at this point:

Error: getaddrinfo EAI_AGAIN https
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
    at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17)

In https://developer.apple.com/documentation/apple_pay_on_the_web/configuring_your_environment, the documentation says "You must register and verify all top-level domains and subdomains where you will display the Apple Pay button." It's not clear to me whether this means the website domain (the hosting one) or our functions domain. I have successfully registered and verified our hosting domain, but I couldn't manage to do this with the functions domain, which is the one I'm actually using to call Apple (obviously wouldn't do this from the client, and Apple specifically says not to do this.

This is because Apple calls https://<domain_name>/.well-known/some_file.txt to do the verification, but firebase functions are always of the form https://-<project_id>.cloudfunctions.net/<function_name>, so it's impossible to have an endpoint for /.well-known. My solution has been to use rewrites on my Firebase Hosting URL to call my applePay firebase function.

What could be causing the error, and how could I resolve?

PS: not interested in any answers suggesting to use something like Stripe for Apple Pay.

mef27
  • 379
  • 4
  • 15

0 Answers0