Интеграция Node.js с Braintree
Я пытаюсь интегрировать Braintree для своего проекта Node.js + React.
Код ниже — это код моей серверной стороны (node.js). Файл шлюз.js получит конфигурации из БД и создаст новый шлюз Braintree для других файлов (функций) для обработки платежей и возврата средств.
Но мне любопытно, что это хороший подход. В моем коде шлюз Braintree обернут функцией (createBraintreeGateway.). Чего я боюсь, так это проблем с производительностью. Если каждый раз, когда клиент осуществляет оплату или осуществляет возврат средств, эта функция будет вызываться, и это будет огромной потерей.
Если мой код неверен, каков наилучший подход? Заранее спасибо.
// gateway.js
const braintree = require('braintree')
const logger = require('../../../infrastructure/logger')
const { decryptWithAES } = require('../../some-folder/helper/encrypt/aes')
const createBraintreeGateway = async () => {
try {
const configs = await somefunction.findAllConfigs()
const getConfig = async name => {
const checker = configs[name]
if (!checker) { return null }
const decoded = await decryptWithAES(checker.value)
return decoded
}
const [ liveId, livePublicKey, livePrivateKey, testId, testPublicKey, testPrivateKey ] = await Promise.all([
getConfig('live_id'),
getConfig('live_public_key'),
getConfig('live_private_key'),
getConfig('test_id'),
getConfig('test_public_key'),
getConfig('test_private_key')
])
const environment = configs.mode === 'live' ? braintree.Environment.Production : braintree.Environment.Sandbox
return new braintree.BraintreeGateway({
environment,
merchantId: configs.mode === 'live' ? liveId : testId,
publicKey: configs.mode === 'live' ? livePublicKey : testPublicKey,
privateKey: configs.mode === 'live' ? livePrivateKey : testPrivateKey,
})
} catch (error) {
logger.error(`[BrainTree] Error while initializing the gateway: ${error}`)
throw error
}
}
module.exports = createBraintreeGateway
Вот еще одна функция, которая вызывает шлюз Braintree.
const createBraintreeGateway = require('./gateway')
const someOtherFunction = async option => {
const gateway = await createBraintreeGateway()
gateway.clientToken.generate(customerId).then(response => {
token = response.clientToken
})