在 Azure 上使用 Apple Pay JavaScript/NodeJS

Posted

技术标签:

【中文标题】在 Azure 上使用 Apple Pay JavaScript/NodeJS【英文标题】:Using Apple Pay JavaScript/NodeJS on Azure 【发布时间】:2018-04-17 17:25:47 【问题描述】:

我使用以下示例作为工作示例,通过 Azure 上的 NodeJS 服务器接收 javascript Apple Pay 交易。拥有开发者帐户后,您必须经历一个完整的过程才能获得证书,但希望它会对某人有所帮助。

这在 Azure 应用程序服务环境 (ASE) 上运行。

/*
Using Apple Pay JavaScript/NodeJS on Azure  

*/

const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const https = require("https");
const http = require("http");
const request = require("request");
var port = process.env.PORT || 1337;


console.log("port: "+port);
/**
* IMPORTANT
* Change these paths to your own SSL and Apple Pay certificates,
* with the appropriate merchant identifier and domain
* See the README for more information.
*/
const APPLE_PAY_CERTIFICATE_PATH = "./certificates/apple_pay.pem";
const SSL_CERTIFICATE_PATH = "./certificates/merchant_id.pem";
const SSL_KEY_PATH = "./certificates/merchant_id_local.pem";
const MERCHANT_IDENTIFIER = "merchant.com.yourcompany";
const MERCHANT_DOMAIN = "yourdomain.azurewebsites.net";

console.log("==>> server starting.........");

try 
  fs.accessSync(APPLE_PAY_CERTIFICATE_PATH);
  fs.accessSync(SSL_CERTIFICATE_PATH);
  fs.accessSync(SSL_KEY_PATH);
 catch (e) 
    console.log("==>> error:.."+e);
  throw new Error('You must generate your SSL and Apple Pay certificates before running this example.');


  console.log("==>> getting certs:..");
const sslKey = fs.readFileSync(SSL_KEY_PATH);
const sslCert = fs.readFileSync(SSL_CERTIFICATE_PATH);
const applePayCert = fs.readFileSync(APPLE_PAY_CERTIFICATE_PATH);


/**
* Set up our server and static page hosting
*/
console.log("==>> setting up server ");
const app = express();
app.use(express.static('public', dotfiles: 'allow' ));
app.use(bodyParser.json());


console.log("==>> setting up post ");

app.post('/postToServer', function (req, res) 
    
    console.log("==>> inside  postToServer");
    console.log(req.body.toCS);
    	if (!req.body.url) return res.sendStatus(400);
);

app.post('/getApplePaySession', function (req, res) 
console.log("==>> inside  getApplePaySession post req:");
	// We need a URL from the client to call
	if (!req.body.url) return res.sendStatus(400);

	// We must provide our Apple Pay certificate, merchant ID, domain name, and display name
	const options = 
		url: req.body.url,
		cert: sslKey,
		key: sslKey,
		method: 'post',
		body: 
			merchantIdentifier: MERCHANT_IDENTIFIER,
			domainName: MERCHANT_DOMAIN,
			displayName: 'My Store',
		,
		json: true,
	

	// Send the request to the Apple Pay server and return the response to the client
	request(options, function(err, response, body) 
	    console.log("==>> sending to apple pay server body: ");
	   console.log(body);
	   console.log(response);
		if (err) 
			console.log('Error generating Apple Pay session!');
			console.log(err, response, body);
			res.status(500).send(body);
		
		res.send(body);
	);
);

process.on('uncaughtException', function(err)
	console.log("error: "+err.stack);
);

app.get('/.well-known/apple-developer-merchantid-domain-association', function(req, res) 
    console.log("==>>1 __dirname"+__dirname);
  res.sendfile(__dirname + '/.well-known/apple-developer-merchantid-domain-association');
  
);

app.get('/.well-known/apple-app-site-association', function(req, res) 
    console.log("==>>2 __dirname"+__dirname);
  res.sendfile(__dirname + '/.well-known/apple-app-site-association.file');
);

http.createServer(app).listen(port);

【问题讨论】:

超级好用!只是好奇,我注意到在这段代码中,变量 sslCert 和 applePayCert 根本没有使用。这是故意的吗? 【参考方案1】:

你的那些是错误的:

cert: sslKey,
key: sslKey

cer:certificate_sandbox.pem 密钥:certificate_sandbox.key

【讨论】:

以上是关于在 Azure 上使用 Apple Pay JavaScript/NodeJS的主要内容,如果未能解决你的问题,请参考以下文章

Apple Pay 网页版商户验证

iOS开发 Apple Pay

Samsung Pay/Apple Pay/Android Pay支付技术大比拼

Apple Pay(转)

如何在 Chrome 上模拟 iOS 用户代理,以启用 Apple Pay 显示?

Apple Pay 可以与使用企业配置文件签名的应用程序一起使用吗?