Skip to content

Commit

Permalink
Merge pull request #4942 from rattrayalex-stripe/fix-57-rattrayalex-s…
Browse files Browse the repository at this point in the history
…tripe-set-stripe-app-info

Use stripe.setAppInfo to identify ReactionCommerce to Stripe (fixes #57)
  • Loading branch information
aldeed authored Jan 31, 2019
2 parents 997c3df + 4420554 commit f86050a
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactionError from "@reactioncommerce/reaction-error";
import stripeNpm from "stripe";
import getStripeInstance from "./getStripeInstance";

const PACKAGE_NAME = "reaction-marketplace";

Expand Down Expand Up @@ -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
};
}
Original file line number Diff line number Diff line change
@@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactionError from "@reactioncommerce/reaction-error";
import stripeNpm from "stripe";
import getStripeInstance from "./getStripeInstance";

const PACKAGE_NAME = "reaction-stripe";

Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit f86050a

Please sign in to comment.