我使用以下示例作为工作示例,通过 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);