我已经在nodeJS中集成了BrainTree嵌入式用户界面,用于结帐支付。这只是我需要创建的一个演示。我没有遇到任何问题,它足够简单。我唯一想做的是从代码中隐藏merchantId、publicKey和privateKey。我想直接在Heroku Config Vars中添加它们。我知道如何在Python中完成所有这些操作,但是我不知道如何在JavaScript中实现它。2我给你看了改变键的代码:
const express = require('express');
const router = express.Router();
const braintree = require('braintree');
router.post('/', (req, res, next) => {
const gateway = new braintree.BraintreeGateway({
environment: braintree.Environment.Sandbox,
// Use your own credentials from the sandbox Control Panel here
merchantId: 'h43jgh5g3gl4543',
publicKey: 'hj45j4h5lh45hl4h5l',
privateKey: 'b5hbhbb45bhbh4kfndnfdkkfnd'
});
// Use the payment method nonce here
const nonceFromTheClient = req.body.paymentMethodNonce;
// Create a new transaction for $10
const newTransaction = gateway.transaction.sale({
amount: '10.00',
paymentMethodNonce: nonceFromTheClient,
options: {
// This option requests the funds from the transaction
// once it has been authorized successfully
submitForSettlement: true
}
}, (error, result) => {
if (result) {
res.send(result);
} else {
res.status(500).send(error);
}
});
});
module.exports = router;
如何将这些值分配给变量,然后传递给Heroku Config Vars?
非常感谢您的帮助!
编辑初始帖子:
抱歉,我需要添加更多关于这篇文章的信息。我按照用户的建议,以如下方式更改了Node.js中的代码:
router.post('/', (req, res, next) => {
const gateway = new braintree.BraintreeGateway({
environment: braintree.Environment.Sandbox,
// Use your own credentials from the sandbox Control Panel here
merchantId: process.env.MERCHANT_ID,
publicKey: process.env.PUBLIC_KEY,
privateKey: process.env.PRIVATE_KEY
});
我将这些值添加到Heroku中,它看起来工作正常,但我还更改了index.hbs中的沙箱值:
<script>
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: process.env.SANDBOX_KEY,
container: '#dropin-container',
paypal: {
flow: 'checkout',
buttonStyle: {
color: 'blue',
shape: 'rect',
size: 'medium'
},
amount: '10.00',
currency: 'USD'
}
},
我用process.env.SANDBOX_KEY替换了值“sanbox_34920hfjh34i23h4oi3”,并在Heroku中添加了该值。
但现在接口不工作了,为什么?
非常感谢您的宝贵帮助!
1条答案
按热度按时间s2j5cfk01#
您可以在Heroku Jmeter 板中添加Config变量,或使用CLI在此处添加更多信息Configuration and Config Vars。完成后,您可以在Node应用程序中以
process.env.NAME_OF_VARIABLE
的身份访问它们。