Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: source Stripe API keys from ENV vars #5771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STRIPE_API_KEY=YOUR_PRIVATE_STRIPE_API_KEY
10 changes: 10 additions & 0 deletions src/plugins/payments-stripe/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import envalid from "envalid";

const { str, testOnly } = envalid;

export default envalid.cleanEnv(process.env, {
STRIPE_API_KEY: str({
desc: "A private Stripe API key",
devDefault: testOnly("YOUR_PRIVATE_STRIPE_API_KEY")
})
});
9 changes: 0 additions & 9 deletions src/plugins/payments-stripe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ export default async function register(app) {
listRefunds: stripeListRefunds
}
}],
settings: {
mode: false,
api_key: "",
public: {
publishable_key: "",
client_id: ""
},
connectAuth: {}
},
registry: [
// Settings panel
{
Expand Down
23 changes: 4 additions & 19 deletions src/plugins/payments-stripe/util/getStripeInstanceForShop.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import ReactionError from "@reactioncommerce/reaction-error";
import Logger from "@reactioncommerce/logger";
import config from "../config.js";
import getStripeInstance from "./getStripeInstance.js";
import getStripePackageForShop from "./getStripePackageForShop.js";

/**
* @summary Given a shop ID, gets an instance of the Stripe API configured with that shop's API key.
* @param {Object} context The context object, with `collections.Packages` on it
* @param {String} shopId The shop ID
* @returns {Object} The Stripe SDK object
*/
export default async function getStripeInstanceForShop(context, shopId) {
const stripePkg = await getStripePackageForShop(context, shopId);
const stripePkgSettings = (stripePkg || {}).settings || {};
export default async function getStripeInstanceForShop(context) {
const { STRIPE_API_KEY } = config;

const stripeApiKey = stripePkgSettings.api_key;
if (!stripeApiKey) {
const stripeAccessToken = (stripePkgSettings.connectAuth || {}).access_token;
if (stripeAccessToken) {
Logger.warn("Using a Stripe access_token instead of an api_key is deprecated. Please set an API Key.");
return getStripeInstance(context, stripeAccessToken);
}

throw new ReactionError("not-configured", "Stripe is not configured properly. Please set an API Key.");
}

return getStripeInstance(context, stripeApiKey);
return getStripeInstance(context, STRIPE_API_KEY);
}
16 changes: 0 additions & 16 deletions src/plugins/payments-stripe/util/getStripePackageForShop.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/payments-stripe/util/stripeCaptureCharge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function stripeCaptureCharge(context, payment) {
amount: formatForStripe(payment.amount)
};

const stripe = await getStripeInstanceForShop(context, payment.shopId);
const stripe = await getStripeInstanceForShop(context);

try {
const captureResult = await stripe.charges.capture(payment.transactionId, captureDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default async function stripeCreateAuthorizedPayment(context, input) {
}
} = input;

const stripe = await getStripeInstanceForShop(context, shopId);
const stripe = await getStripeInstanceForShop(context);

// For orders with only a single fulfillment group, we could create a charge directly from the card token, and skip
// creating a customer. However, to help make future charging easier and because we always have an email address and tracking
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/payments-stripe/util/stripeCreateRefund.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import formatForStripe from "./formatForStripe.js";
export default async function stripeCreateRefund(context, paymentMethod, amount, reason) {
let result;
try {
const stripe = await getStripeInstanceForShop(context, paymentMethod.shopId);
const stripe = await getStripeInstanceForShop(context);

const refundResult = await stripe.refunds.create({ charge: paymentMethod.transactionId, amount: formatForStripe(amount), reason });
Logger.debug(refundResult);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/payments-stripe/util/stripeListRefunds.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import getStripeInstanceForShop from "./getStripeInstanceForShop.js";
* @private
*/
export default async function stripeListRefunds(context, paymentMethod) {
const stripe = await getStripeInstanceForShop(context, paymentMethod.shopId);
const stripe = await getStripeInstanceForShop(context);

let refundListResults;
try {
Expand Down