diff --git a/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstance.js b/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstance.js new file mode 100644 index 00000000000..4bf660a5b70 --- /dev/null +++ b/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstance.js @@ -0,0 +1,20 @@ +import stripeNpm from "stripe"; +import packageJson from "/package.json"; + +// This should not be customized per application. +const APP_INFO = { + name: "ReactionCommerceMarketplace", + version: packageJson.version, + url: packageJson.url +}; + +/** + * @name getStripeInstance + * @param {String} stripeApiKey Stripe API Key, see https://stripe.com/docs/keys + * @returns {Object} The Stripe SDK object + */ +export default function getStripeInstance(stripeApiKey) { + const stripe = stripeNpm(stripeApiKey); + stripe.setAppInfo(APP_INFO); + return stripe; +} diff --git a/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstanceForShop.js b/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstanceForShop.js index 3ed0649c509..398ca62ab6a 100644 --- a/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstanceForShop.js +++ b/imports/plugins/included/marketplace/server/no-meteor/util/getStripeInstanceForShop.js @@ -1,5 +1,5 @@ import ReactionError from "@reactioncommerce/reaction-error"; -import stripeNpm from "stripe"; +import getStripeInstance from "./getStripeInstance"; const PACKAGE_NAME = "reaction-marketplace"; @@ -43,11 +43,13 @@ export default async function getStripeInstanceForShop(context, shopId) { throw new ReactionError("server-error", "Stripe is not configured properly. Please set an API Key."); } + const stripe = getStripeInstance(stripeApiKey); + const applicationFee = primaryStripePkg.settings.applicationFee || 0; return { applicationFee, merchantStripeUserId, - stripe: stripeNpm(stripeApiKey) + stripe }; } diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeApi.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeApiKey.js similarity index 83% rename from imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeApi.js rename to imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeApiKey.js index 117ccaded39..c3a454c40db 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeApi.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeApiKey.js @@ -1,11 +1,11 @@ /** - * @name getStripeApi + * @name getStripeApiKey * @param {Object} context - an object containing the per-request state * @param {String} paymentPluginName - plugin name * @param {String} shopId Shop ID * @returns {String} Stripe key */ -export default async function getStripeApi(context, paymentPluginName, shopId) { +export default async function getStripeApiKey(context, paymentPluginName, shopId) { const { collections: { Packages } } = context; const stripePackage = await Packages.findOne({ name: paymentPluginName, shopId }); if (!stripePackage) throw new Error(`No package found with name ${paymentPluginName}`); diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstance.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstance.js new file mode 100644 index 00000000000..d391f59b863 --- /dev/null +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstance.js @@ -0,0 +1,20 @@ +import stripeNpm from "stripe"; +import packageJson from "/package.json"; + +// This should not be customized per application. +const APP_INFO = { + name: "ReactionCommerce", + version: packageJson.version, + url: packageJson.url +}; + +/** + * @name getStripeInstance + * @param {String} stripeApiKey Stripe API Key, see https://stripe.com/docs/keys + * @returns {Object} The Stripe SDK object + */ +export default function getStripeInstance(stripeApiKey) { + const stripe = stripeNpm(stripeApiKey); + stripe.setAppInfo(APP_INFO); + return stripe; +} diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstanceForShop.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstanceForShop.js index c79e581ebe3..398de449238 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstanceForShop.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/getStripeInstanceForShop.js @@ -1,5 +1,5 @@ import ReactionError from "@reactioncommerce/reaction-error"; -import stripeNpm from "stripe"; +import getStripeInstance from "./getStripeInstance"; const PACKAGE_NAME = "reaction-stripe"; @@ -24,5 +24,5 @@ export default async function getStripeInstanceForShop(context, shopId) { throw new ReactionError("not-configured", "Stripe is not configured properly. Please set an API Key."); } - return stripeNpm(stripeApiKey); + return getStripeInstance(stripeApiKey); } diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCaptureCharge.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCaptureCharge.js index f3d17d94871..a1970f8f9f1 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCaptureCharge.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCaptureCharge.js @@ -1,7 +1,7 @@ -import stripeNpm from "stripe"; import Logger from "@reactioncommerce/logger"; import formatForStripe from "./formatForStripe"; -import getStripeApi from "./getStripeApi"; +import getStripeApiKey from "./getStripeApiKey"; +import getStripeInstance from "./getStripeInstance"; /** * @summary Capture the results of a previous charge @@ -16,8 +16,8 @@ export default async function stripeCaptureCharge(context, payment) { amount: formatForStripe(payment.amount) }; - const stripeKey = await getStripeApi(context, payment.paymentPluginName, payment.shopId); - const stripe = stripeNpm(stripeKey); + const stripeKey = await getStripeApiKey(context, payment.paymentPluginName, payment.shopId); + const stripe = getStripeInstance(stripeKey); try { const captureResult = await stripe.charges.capture(payment.transactionId, captureDetails); diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCapturePayment.test.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCapturePayment.test.js index 257a980ef75..d58407f35d3 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCapturePayment.test.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCapturePayment.test.js @@ -3,7 +3,7 @@ import nock from "nock"; import mockContext from "/imports/test-utils/helpers/mockContext"; import stripeCapturePayment from "./stripeCapturePayment"; -jest.mock("./getStripeApi", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY")); +jest.mock("./getStripeApiKey", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY")); const stripeCaptureResult = { id: "ch_17hZ4wBXXkbZQs3xL5JhlSgS", diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.js index 19556c92f31..248451a1f21 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.js @@ -1,6 +1,6 @@ -import stripeNpm from "stripe"; import Logger from "@reactioncommerce/logger"; -import getStripeApi from "./getStripeApi"; +import getStripeApiKey from "./getStripeApiKey"; +import getStripeInstance from "./getStripeInstance"; import formatForStripe from "./formatForStripe"; /** @@ -16,8 +16,9 @@ import formatForStripe from "./formatForStripe"; export default async function stripeCreateRefund(context, paymentMethod, amount) { let result; try { - const stripeKey = await getStripeApi(context, paymentMethod.paymentPluginName, paymentMethod.shopId); - const stripe = stripeNpm(stripeKey); + const stripeKey = await getStripeApiKey(context, paymentMethod.paymentPluginName, paymentMethod.shopId); + const stripe = getStripeInstance(stripeKey); + const refundResult = await stripe.refunds.create({ charge: paymentMethod.transactionId, amount: formatForStripe(amount) }); Logger.debug(refundResult); if (refundResult && refundResult.object === "refund") { diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.test.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.test.js index 55da569f38c..1d84934f14e 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.test.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeCreateRefund.test.js @@ -3,7 +3,7 @@ import nock from "nock"; import mockContext from "/imports/test-utils/helpers/mockContext"; import stripeCreateRefund from "./stripeCreateRefund"; -jest.mock("./getStripeApi", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY")); +jest.mock("./getStripeApiKey", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY")); test("should call StripeApi.methods.createRefund with the proper parameters and return saved = true", async () => { const paymentMethod = { diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.js index 1ef425972aa..4af9312ff6f 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.js @@ -1,6 +1,6 @@ -import stripeNpm from "stripe"; import Logger from "@reactioncommerce/logger"; -import getStripeApi from "./getStripeApi"; +import getStripeApiKey from "./getStripeApiKey"; +import getStripeInstance from "./getStripeInstance"; /** * @name stripeListRefunds @@ -12,8 +12,9 @@ import getStripeApi from "./getStripeApi"; * @private */ export default async function stripeListRefunds(context, paymentMethod) { - const stripeKey = await getStripeApi(context, paymentMethod.paymentPluginName, paymentMethod.shopId); - const stripe = stripeNpm(stripeKey); + const stripeKey = await getStripeApiKey(context, paymentMethod.paymentPluginName, paymentMethod.shopId); + const stripe = getStripeInstance(stripeKey); + let refundListResults; try { refundListResults = await stripe.refunds.list({ charge: paymentMethod.transactionId }); diff --git a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.test.js b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.test.js index 2a9c0a8c544..24d9b9489d4 100644 --- a/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.test.js +++ b/imports/plugins/included/payments-stripe/server/no-meteor/util/stripeListRefunds.test.js @@ -3,7 +3,7 @@ import nock from "nock"; import mockContext from "/imports/test-utils/helpers/mockContext"; import stripeListRefunds from "./stripeListRefunds"; -jest.mock("./getStripeApi", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY")); +jest.mock("./getStripeApiKey", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY")); test("should call StripeApi.methods.listRefunds with the proper parameters and return a properly" + "formatted list of refunds", async () => {