Reputation: 662
I have an internal form service that collects payments via Stripe. I'm curious how I can toggle between test mode and live mode with their SDK based on a query param?
Here's an example route:
router.post("/:formId", function(req, res, next) {
let isTest = false
var stripeSdk = require("stripe")(process.env.stripe_client_secret);
if (req.query.test === "true") {
stripeSdk = require("stripe")(process.env.stripe_client_test_secret);
isTest = true;
}
//use the sdk with either test or live mode depending on the call
});
The code above works, but I'm curious if there's a better way to change an npm package import based on the request.
Upvotes: 0
Views: 26
Reputation: 2424
You could write:
const isTest = (req.query.test === "true");
const stripeSecret = isTest ? process.env.stripe_client_test_secret : process.env.stripe_client_secret;
const stripeSdk = require('stripe')(stripeSecret);
But I guess it is more a matter of taste than anything else.
I don't think it gets more fancy than that.
Upvotes: 1