Problems with getting Apple Pay payment session

1.1k Views Asked by At

A year ago we implemented ApplePay on the web in our project and everything worked just fine. But now it's unstable and payment can be successful on the third try sometimes. We are facing an ECONNRESET error while requesting POST https://apple-pay-gateway.apple.com/paymentservices/startSession and an error message “Payment not completed” on the client side

Error: read ECONNRESET at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20) {
errno: -104,
code: 'ECONNRESET',
config: {
    url: 'https://apple-pay-gateway.apple.com/paymentservices/startSession',
    method: 'post',
    data: '{"merchantIdentifier":"merchant.com.xxxxxxxx","displayName":"xxxxxxxxxx","initiative":"web","initiativeContext":"xxxxxxxx.xxx","domainName":"xxxxxxxx.xxx"}',
    headers: {
        Accept: 'application/json, text/plain, */*',
            'Content-Type': 'application/json',
            'User-Agent': 'axios/0.19.2',
            'Content-Length': 191
    }
},
...
response: undefined,
isAxiosError: true,

Client side code:

applePaySession.onvalidatemerchant = async (event) => {
  try {
    const data = {
      url: event.validationURL,
      method: 'post',
      body: {
        merchantIdentifier,
        displayName,
        initiative: "web",
        initiativeContext: window.location.hostname
      },
      json: true,
    }
    const merchantSession = await this.$axios.$post('/apple_pay_session', data);
    if (merchantSession && merchantSession.merchantSessionIdentifier) {
      applePaySession.completeMerchantValidation(merchantSession)
    } else {
      applePaySession.abort();
    }
  } catch (error) {
    logReqError(error)
    applePaySession.abort();
  }
};

Server side:

    const httpsAgent = new https.Agent({
        rejectUnauthorized: false,
        keepAlive: true,
        cert: fs.readFileSync(`uploads/apple_pay/${id}/certificate.pem`),
        key: fs.readFileSync(`uploads/apple_pay/${id}/private.key`)
    });

    const sessionRes = await axios({
        url: request.body.url,
        method: request.body.method.toLowerCase(),
        data: request.body.body,
        headers: {'Content-Type': 'application/json'}, httpsAgent
    })

We checked the status of Merchant Domain - it's verified, Apple Pay Payment Processing Certificate and Apple Pay Merchant Identity Certificate are valid. I also checked project code with the official documentation, as well as with other sources. I hope anyone has had a similar problem. Any code samples or guesses will be helpful anyway

0

There are 0 best solutions below