iosapplepayapplepayjspayment-request-api

Unable to get any response from the apple pay - This resource came from a local override


I am integrating the apple pay and following the payment request API! According to the documentation (https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session)

cert: merchIdentityCert and key: merchIdentityCert both are the same. I append my merchantIdentityCertificate.pem to both cert and key.

But, Unable to get any response from the apple pay servers. After my request it is throwing an error and safari is displaying a message as “This resource came from a local override”

Code:

const merchIdentityCert = fs.readFileSync("./merchIdentityCert.pem")
const httpsAgent = new https.Agent({
  cert: merchIdentityCert,
  key: merchIdentityCert,
  maxVersion: "TLSv1.2",
  minVersion: "TLSv1.2"
})
const post = (url, body) => {
logger.info({ message: "apple pay START", url, body })
fetch(url, {
  body: JSON.stringify(body),
  method: "POST",
  agent: httpsAgent
}).then(resp => {
logger.info({ message: "apple pay SUCCESS", resp })
return resp
}).catch((error) => {
logger.info({ message: "apple pay ERROR", error })
return error
})
}

Logs:

message: "apple pay START", url: "https://-pay-gateway-cert.apple.com/paymentservices/startSession", body: {"merchantIdentifier":"..","displayName":"Test Pay","initiative":"web","initiativeContext":"--**.***."}

message: "apple pay ERROR", error: {}*

I am using a node-fetch library. My web app and node app deployed in AWS servers. I have fulfilled server setup and environment setup requirements with certificates. [https://developer.apple.com/documentation/apple_pay_on_the_web/setting_up_your_server]

Does anyone have an idea about this?


Solution

  • Able to resolve the {"size":0,"timeout":0} issue and it is related to node fetch library. I fixed it from my code level. (Node-fetch problems with POST requests, https://www.npmjs.com/package/node-fetch)

    Apart from that, I used the below request to debug using the remote server, and It was giving the session object to me.

    curl -XPOST -H "Content-type: application/json" -d '{"merchantIdentifier":"merchant.***.xxxxx","displayName":"TestPay","initiative":"web","initiativeContext":"***-***-xxxxxx.xxxxxxxxxx.xx"}' --cert cert.pem:cert.pem 'https://apple-pay-gateway-cert.apple.com/paymentservices/startSession'
    

    my code:

    import fetch from "node-fetch"
    import fs from "fs"
    import { promiseReject } from "../utils/misc"
    import { Logger } from "../services/Logger"
    import https from "https"
    const logger = new Logger("Apple Pay Client")
    const checkStatusAndGetJSON = (fetchResponse) =>
        fetchResponse.ok
            ? fetchResponse.json()
            : fetchResponse.json().then(promiseReject)
    const merchIdentityCert = fs.readFileSync("./merchIdentityCert.pem")
    const httpsAgent = new https.Agent({
        cert: merchIdentityCert,
        key: merchIdentityCert,
        maxVersion: "TLSv1.2",
        minVersion: "TLSv1.2"
    })
    const basicHeaders = {
        "Accept": "application/json",
        "lang": "en",
        "Content-Type": "application/json"
    }
    const post = (url, body) => {
        const start = Date.now()
        return fetch(url, {
            body: JSON.stringify(body),
            headers: basicHeaders,
            method: "POST",
            agent: httpsAgent
        }).then(resp => {
            const duration = Date.now() - start
            logger.debug(`apple pay call took ${duration} millis.`, { endpoint: url, method: "POST", duration })
            return resp
        }).then(checkStatusAndGetJSON)
    }
    /** Apple Pay */
    export const performValidation = (url, body) => post(url, body)