forked from SalesforceCommerceCloud/pwa-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone.js
More file actions
102 lines (90 loc) · 3.52 KB
/
standalone.js
File metadata and controls
102 lines (90 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/**
* Custom payment methods controller that doesn't require a basket
* This is specifically for "Buy Now" flows where we need to show Apple Pay
* before creating a basket
*/
// Helper function to get the Adyen PWA library version dynamically
function getAdyenPwaVersion() {
try {
// Try to read the version from the installed package
const packageJson = require('@adyen/adyen-salesforce-pwa/package.json')
return packageJson.version
} catch (error) {
console.error('Unable to determine Adyen PWA version', error)
}
}
export default async function handler(req, res) {
if (req.method !== 'GET') {
return res.status(405).json({error: 'Method not allowed'})
}
try {
const {siteId, locale} = req.query
if (!siteId) {
return res.status(400).json({error: 'siteId is required'})
}
// Get Adyen configuration from environment variables
// Environment variables are prefixed with the site ID
const apiKey = process.env[`${siteId}_ADYEN_API_KEY`]
const merchantAccount = process.env[`${siteId}_ADYEN_MERCHANT_ACCOUNT`]
const environment = process.env[`${siteId}_ADYEN_ENVIRONMENT`] || 'test'
const clientKey = process.env[`${siteId}_ADYEN_CLIENT_KEY`]
if (!apiKey || !merchantAccount || !clientKey) {
return res.status(500).json({
error: 'Missing Adyen configuration',
details: `Required environment variables: ${siteId}_ADYEN_API_KEY, ${siteId}_ADYEN_MERCHANT_ACCOUNT, ${siteId}_ADYEN_CLIENT_KEY`
})
}
// Construct Adyen API URL
const adyenBaseUrl =
environment === 'live'
? 'https://checkout-live.adyen.com/v70'
: 'https://checkout-test.adyen.com/v70'
// Call Adyen payment methods API without basket dependency
const adyenResponse = await fetch(`${adyenBaseUrl}/paymentMethods`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': apiKey
},
body: JSON.stringify({
merchantAccount,
countryCode: locale?.split('-')[1] || 'US',
channel: 'Web'
})
})
if (!adyenResponse.ok) {
const errorBody = await adyenResponse.text()
console.error('Adyen API error:', errorBody)
return res.status(adyenResponse.status).json({
error: 'Failed to fetch payment methods from Adyen'
})
}
const paymentMethods = await adyenResponse.json()
// Return the payment methods with environment configuration and application info
res.status(200).json({
...paymentMethods,
environment: {
ADYEN_ENVIRONMENT: environment,
ADYEN_CLIENT_KEY: clientKey
},
applicationInfo: {
adyenLibrary: {
name: 'adyen-salesforce-pwa',
version: getAdyenPwaVersion()
}
}
})
} catch (error) {
console.error('Payment methods API error:', error)
res.status(500).json({
error: 'Internal server error',
message: error.message
})
}
}