Apple Pay Web

Hi Guys,


I'm struggling with an error on the request to start session with apple pay web, find below the error:


Error: error:0906D06C:PEM routines:PEM_read_bio:no start line


I've done the following process:


1. created the merchant-id

2. Generated the Apple Pay Payment Processing Certificate (apple_pay.cer) - installed on my mac

3. Verified my domain

4. Generated the Apple Pay Merchant Identity Certificate (merchant_id.cer) - installed on my mac

5. Opened the Keychain and found the merchant_id.cer

6. Right click on merchant_id.cer -> export -> merchant.com.xxxxx.pem

7. Moved the merchant.com.xxxxx.pem to the project cert folder


When the client-side call the API, I have the following request which is returing the error above:


  const options = {
    url: req.body.url,
    cert: applePayCert, //fs.readFileSync(APPLE_PAY_CERTIFICATE_PATH
    key: applePayCert,   //fs.readFileSync(APPLE_PAY_CERTIFICATE_PATH
    method: 'post',
    body: {
      merchantIdentifier: "merchant.com.xxxxx.pem",
      //domainName: MERCHANT_DOMAIN,
  displayName: 'My Store',
  initiative: 'web',
  initiativeContext: 'www.xxxxxx.com'
    },
    json: true,
  }
console.log(options);
  // Send the request to the Apple Pay server and return the response to the client
  request(options, function(err, response, body) {
    if (err) {
      console.log('Error generating Apple Pay session!');
      console.log(err, response, body);
      return res.status(400).send(body);
    }
    res.send(body);
  });

I'm trying to run on my localhost and also heroku server, but I get this error:


Does anyone had the same issue or know what I could be missing?

2018-08-10T15:21:01.939694+00:00 app[web.1]: Error generating Apple Pay session!
2018-08-10T15:21:01.945778+00:00 app[web.1]: Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
2018-08-10T15:21:01.945781+00:00 app[web.1]:     at Object.createSecureContext (_tls_common.js:104:17)
2018-08-10T15:21:01.945782+00:00 app[web.1]:     at Object.exports.connect (_tls_wrap.js:1049:48)
2018-08-10T15:21:01.945784+00:00 app[web.1]:     at Agent.createConnection (https.js:111:22)
2018-08-10T15:21:01.945785+00:00 app[web.1]:     at Agent.createSocket (_http_agent.js:227:26)
2018-08-10T15:21:01.945787+00:00 app[web.1]:     at Agent.addRequest (_http_agent.js:185:10)
2018-08-10T15:21:01.945806+00:00 app[web.1]:     at new ClientRequest (_http_client.js:258:16)
2018-08-10T15:21:01.945807+00:00 app[web.1]:     at Object.request (http.js:38:10)
2018-08-10T15:21:01.945809+00:00 app[web.1]:     at Object.request (https.js:239:15)
2018-08-10T15:21:01.945811+00:00 app[web.1]:     at Request.start (/app/node_modules/request/request.js:747:32)
2018-08-10T15:21:01.945819+00:00 app[web.1]:     at Request.write (/app/node_modules/request/request.js:1492:10) undefined undefined

If anyone runs across this, I had the same problem and it was due to a missing RSA key. Go to your keychain, click on certificates, and expand your Merchant Identity Cert. You should see a private key, click this and export it as PKCS #12 (.p12). You can then use openssl to convert this to a .pem file or you can use the .p12 directly (in node with request the agentOptions are {pfx: p12File, and passphrase: '***'}.

You have saved my life!

@alexmarion dude WTH. This should be in the documentation! I'm using node as backend to create the Apple Pay server as well. Also, I'm using node-fetch and custom https agent.

Here's two commands I've used to get the .key and .cert files https://stackoverflow.com/a/15144560

openssl pkcs12 -in certificado.p12 -out nuevo.crt.pem -clcerts -nokeys
openssl pkcs12 -in certificado.p12 -out nuevo.key.pem -nocerts -nodes

then it can be used like this

const httpsAgent = new https.Agent({
  key: fs.readFileSync('nuevo.key.pem'),
  cert: fs.readFileSync('nuevo.crt.pem'),
  rejectUnauthorized: false
});

const options= {
  merchantIdentifier: "merchant.prueba",
  displayName: "Prueba",
  initiative: "web",
  initiativeContext: "foo-bar-test.ngrok.io"
};
fetch('https://apple-pay-gateway-cert.apple.com/paymentservices/startSession', {
  method: 'POST',
  body: JSON.stringify(options),
  agent: httpsAgent
})
.then(function(response) {
  return response.json();
}).then(function () {
  // response
});
Apple Pay Web
 
 
Q