Skip to content

Commit

Permalink
feat: added endpoint for e2e setup
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovski committed Nov 7, 2023
1 parent 71e2bdb commit bf3aacb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions adyen/controllers/e2e-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {ShopperLogin, ShopperBaskets} from 'commerce-sdk-isomorphic'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import AdyenCheckoutConfig from './checkout-config'

async function setupE2E(req, res) {
const checkout = AdyenCheckoutConfig.getInstance()

try {
const {app: appConfig} = getConfig()
const shopperBaskets = new ShopperBaskets({
...appConfig.commerceAPI,
headers: {authorization: req.headers.authorization}
})

const customerId = req.headers.customerid
const shopperLoginClientConfig = {
parameters: {
clientId: process.env.COMMERCE_API_CLIENT_ID,
organizationId: process.env.COMMERCE_API_ORG_ID,
shortCode: process.env.COMMERCE_API_SHORT_CODE,
siteId: process.env.COMMERCE_API_SITE_ID
}
}
const shopperLoginClient = new ShopperLogin(shopperLoginClientConfig)
const credentials = `${process.env.COMMERCE_API_CLIENT_ID_PRIVATE}:${process.env.COMMERCE_API_CLIENT_SECRET}`
const base64data = btoa(credentials)
const headers = {
authorization: `Basic ${base64data}`
}
const data = await shopperLoginClient.getAccessToken({
body: {
grant_type: 'client_credentials'
},
headers
})
const basket = await shopperBaskets.createBasket({
body: {
...req.body,
customerInfo: {
email: '[email protected]',
customerId: customerId || data.customer_id
}
},
headers: {
authorization: `Bearer ${data.access_token}`
}
})
res.json(basket)
} catch (err) {
res.status(err.statusCode || 500).json(err.message)
}
}

export default setupE2E
2 changes: 2 additions & 0 deletions overrides/app/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {isRemote} from '@salesforce/pwa-kit-runtime/utils/ssr-server'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import helmet from 'helmet'
import bodyParser from 'body-parser'
import EndToEndSetupController from '../../adyen/controllers/e2e-setup'
import PaymentMethodsController from '../../adyen/controllers/payment-methods'
import PaymentsDetailsController from '../../adyen/controllers/payments-details'
import PaymentsController from '../../adyen/controllers/payments'
Expand Down Expand Up @@ -97,6 +98,7 @@ const {handler} = runtime.createHandler(options, (app) => {
app.get('*', runtime.render)

// Routes
app.post('/api/adyen/e2e-setup', EndToEndSetupController)
app.post('/api/adyen/paymentMethods', PaymentMethodsController)
app.post('/api/adyen/payments/details', PaymentsDetailsController)
app.post('/api/adyen/payments', PaymentsController)
Expand Down

0 comments on commit bf3aacb

Please sign in to comment.