在Azure上使用Apple Pay的JavaScript/NodeJS运行示例
Working Node.js Example for Apple Pay Transactions on Azure ASE
Hey everyone, I put together a working example for handling Apple Pay transactions with a Node.js server hosted on Azure App Service Environment (ASE). First off, you'll need an active Apple Developer account and to complete all the certificate setup steps Apple requires—this should help you avoid some common pitfalls I ran into. Let's dive in:
Prerequisites
- Active Apple Developer account
- Completed Apple Pay certificate configuration (merchant ID, payment processing certificate, merchant identity certificate, etc.)
- Azure App Service Environment (ASE) provisioned for your Node.js application
- Valid SSL certificate configured on your ASE (Apple Pay requires HTTPS exclusively)
Full Code Snippet
/* 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 { PaymentRequest } = require('@apple/apple-pay-api'); const app = express(); app.use(bodyParser.json()); // Load your Apple Pay credentials (store these securely in Azure Key Vault in production!) const merchantId = 'your-unique-merchant-id'; const privateKey = fs.readFileSync('./path/to/your/private-key.p8', 'utf8'); const paymentCert = fs.readFileSync('./path/to/your/payment-processing-cert.pem', 'utf8'); // Endpoint to initiate an Apple Pay session app.post('/api/apple-pay/init', async (req, res) => { try { const paymentRequest = new PaymentRequest({ merchantIdentifier: merchantId, displayName: 'Your Store Name', supportedNetworks: ['visa', 'masterCard', 'amex'], countryCode: 'US', // Update to your country code currencyCode: 'USD', // Update to your currency total: { label: 'Order Total', amount: '29.99' // Update to your transaction amount } }); const session = await paymentRequest.createSession(); res.json(session); } catch (error) { console.error('Failed to create Apple Pay session:', error); res.status(500).json({ error: 'Could not initialize Apple Pay session' }); } }); // Endpoint to process the encrypted payment token app.post('/api/apple-pay/process', async (req, res) => { const { paymentToken } = req.body; try { // Validate and decrypt the payment token using your certificate/key // Integrate with your payment processor (Stripe, Braintree, etc.) here console.log('Processing payment token:', paymentToken); // Simulate successful processing (replace with your actual logic) res.json({ success: true, message: 'Payment processed successfully' }); } catch (error) { console.error('Payment processing failed:', error); res.status(500).json({ error: 'Could not process payment' }); } }); // Start server (Azure ASE uses HTTPS by default; adjust port as needed) const port = process.env.PORT || 443; https.createServer({ key: fs.readFileSync('./path/to/your/azure-ssl-key.pem'), cert: fs.readFileSync('./path/to/your/azure-ssl-cert.pem') }, app).listen(port, () => { console.log(`Apple Pay server running on port ${port}`); });
Critical Configuration Tips
- Certificate Management:
- Convert your Apple-issued
.p12payment processing certificate to.pemformat for Node.js compatibility - Store all private keys and certificates in Azure Key Vault (never hardcode them or commit to version control)—use Azure App Service's Key Vault integration to fetch these at runtime
- Convert your Apple-issued
- Azure ASE Specifics:
- Verify your ASE has outbound network access to Apple's Pay servers (check network security groups if you have restricted traffic)
- Ensure your app's environment variables are set correctly (e.g.,
PORTis configured to match Azure's expected HTTPS port)
- Apple Pay Validation:
- Double-check that your merchant ID matches exactly what's configured in the Apple Developer Portal
- Test with Apple's sandbox environment first before going live to avoid blocking your production account
内容的提问来源于stack exchange,提问作者userara
相关产品推荐
相关产品推荐

