Skip to content

Commit

Permalink
(refactor): unify payments-stripe to use getStripeInstanceForShop, an…
Browse files Browse the repository at this point in the history
…d pull out a STRIPE_PACKAGE_NAME constant

Signed-off-by: Alex Rattray <[email protected]>
  • Loading branch information
rattrayalex-stripe committed Jan 31, 2019
1 parent f86050a commit bc5bb2c
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { unstoreAnonymousCart } from "/imports/plugins/core/cart/client/util/ano
import getCart from "/imports/plugins/core/cart/client/util/getCart";
import buildOrderInputFromCart from "/imports/plugins/core/cart/client/util/buildOrderInputFromCart";
import simpleGraphQLClient from "/imports/plugins/core/graphql/lib/helpers/simpleClient";
import { STRIPE_PACKAGE_NAME } from "/imports/plugins/included/payments-stripe/lib/constants";
import InjectedCardForm from "../components/injectedCardForm";

class StripePaymentFormContainer extends Component {
Expand Down Expand Up @@ -77,7 +78,7 @@ function getSubmitHandler(billingAddress, cart, cartToken) {
function composer(props, onData) {
const subscription = Reaction.Subscriptions.Packages;
const stripePackage = Packages.findOne({
name: "reaction-stripe",
name: STRIPE_PACKAGE_NAME,
shopId: Reaction.getPrimaryShopId()
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { AutoForm } from "meteor/aldeed:autoform";
import { Reaction, i18next } from "/client/api";
import { Packages } from "/lib/collections";
import { StripePackageConfig } from "../../lib/collections/schemas";
import { STRIPE_PACKAGE_NAME } from "../../lib/constants";

/**
* @summary get Stripe Package record
* @returns {Object} Package data
*/
function packageData() {
return Packages.findOne({
name: "reaction-stripe",
name: STRIPE_PACKAGE_NAME,
shopId: Reaction.getShopId()
});
}
Expand Down
2 changes: 2 additions & 0 deletions imports/plugins/included/payments-stripe/lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export const STRIPE_PACKAGE_NAME = "reaction-stripe";
3 changes: 2 additions & 1 deletion imports/plugins/included/payments-stripe/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import stripeCapturePayment from "./server/no-meteor/util/stripeCapturePayment";
import stripeCreateAuthorizedPayment from "./server/no-meteor/util/stripeCreateAuthorizedPayment";
import stripeCreateRefund from "./server/no-meteor/util/stripeCreateRefund";
import stripeListRefunds from "./server/no-meteor/util/stripeListRefunds";
import { STRIPE_PACKAGE_NAME } from "./lib/constants";

Reaction.registerPackage({
label: "Stripe",
name: "reaction-stripe",
name: STRIPE_PACKAGE_NAME,
icon: "fa fa-cc-stripe",
autoEnable: true,
graphQL: {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import ReactionError from "@reactioncommerce/reaction-error";
import Logger from "@reactioncommerce/logger";
import getStripeInstance from "./getStripeInstance";
import getStripePackageForShop from "./getStripePackageForShop";

const PACKAGE_NAME = "reaction-stripe";

/**
* @summary Given a shop ID, gets the Stripe package data for that shop and an instance
* of the Stripe API configured with that shop's API key.
* @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 { collections } = context;
const { Packages } = collections;
const stripePkg = await getStripePackageForShop(context, shopId);
const stripePkgSettings = (stripePkg || {}).settings || {};

const stripePkg = await Packages.findOne({
name: PACKAGE_NAME,
shopId
});

const stripeApiKey = stripePkg && stripePkg.settings && stripePkg.settings.api_key;
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(stripeAccessToken);
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PACKAGE_NAME } from "/imports/plugins/included/payments-stripe/lib/constants";

/**
* @summary Given a shop ID, gets the Stripe package data for that shop.
* @param {Object} context The context object, with `collections.Packages` on it
* @param {String} shopId The shop ID
* @returns {Object} The Stripe package data.
*/
export default async function getStripePackageForShop(context, shopId) {
const { Packages } = context.collections;

return Packages.findOne({
name: PACKAGE_NAME,
shopId
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Logger from "@reactioncommerce/logger";
import formatForStripe from "./formatForStripe";
import getStripeApiKey from "./getStripeApiKey";
import getStripeInstance from "./getStripeInstance";
import getStripeInstanceForShop from "./getStripeInstanceForShop";

/**
* @summary Capture the results of a previous charge
Expand All @@ -16,8 +15,7 @@ export default async function stripeCaptureCharge(context, payment) {
amount: formatForStripe(payment.amount)
};

const stripeKey = await getStripeApiKey(context, payment.paymentPluginName, payment.shopId);
const stripe = getStripeInstance(stripeKey);
const stripe = await getStripeInstanceForShop(context, payment.shopId);

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("./getStripeApiKey", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY"));
jest.mock("./getStripeInstanceForShop", () => jest.fn().mockImplementation(() => require("stripe")("STRIPE_API_KEY")));

const stripeCaptureResult = {
id: "ch_17hZ4wBXXkbZQs3xL5JhlSgS",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Random from "@reactioncommerce/random";
import { STRIPE_PACKAGE_NAME } from "../lib/constants";
import getStripeInstanceForShop from "../util/getStripeInstanceForShop";

const METHOD = "credit";
const PACKAGE_NAME = "reaction-stripe";
const PAYMENT_METHOD_NAME = "stripe_card";

// NOTE: The "processor" value is lowercased and then prefixed to various payment Meteor method names,
Expand Down Expand Up @@ -95,7 +95,7 @@ export default async function stripeCreateAuthorizedPayment(context, input) {
method: METHOD,
mode: "authorize",
name: PAYMENT_METHOD_NAME,
paymentPluginName: PACKAGE_NAME,
paymentPluginName: STRIPE_PACKAGE_NAME,
processor: PROCESSOR,
riskLevel: riskLevelMap[charge.outcome && charge.outcome.risk_level] || "normal",
shopId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Logger from "@reactioncommerce/logger";
import getStripeApiKey from "./getStripeApiKey";
import getStripeInstance from "./getStripeInstance";
import getStripeInstanceForShop from "./getStripeInstanceForShop";
import formatForStripe from "./formatForStripe";

/**
Expand All @@ -16,8 +15,7 @@ import formatForStripe from "./formatForStripe";
export default async function stripeCreateRefund(context, paymentMethod, amount) {
let result;
try {
const stripeKey = await getStripeApiKey(context, paymentMethod.paymentPluginName, paymentMethod.shopId);
const stripe = getStripeInstance(stripeKey);
const stripe = await getStripeInstanceForShop(context, paymentMethod.shopId);

const refundResult = await stripe.refunds.create({ charge: paymentMethod.transactionId, amount: formatForStripe(amount) });
Logger.debug(refundResult);
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("./getStripeApiKey", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY"));
jest.mock("./getStripeInstanceForShop", () => jest.fn().mockImplementation(() => require("stripe")("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,5 @@
import Logger from "@reactioncommerce/logger";
import getStripeApiKey from "./getStripeApiKey";
import getStripeInstance from "./getStripeInstance";
import getStripeInstanceForShop from "./getStripeInstanceForShop";

/**
* @name stripeListRefunds
Expand All @@ -12,8 +11,7 @@ import getStripeInstance from "./getStripeInstance";
* @private
*/
export default async function stripeListRefunds(context, paymentMethod) {
const stripeKey = await getStripeApiKey(context, paymentMethod.paymentPluginName, paymentMethod.shopId);
const stripe = getStripeInstance(stripeKey);
const stripe = await getStripeInstanceForShop(context, paymentMethod.shopId);

let refundListResults;
try {
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("./getStripeApiKey", () => jest.fn().mockImplementation(() => "STRIPE_API_KEY"));
jest.mock("./getStripeInstanceForShop", () => jest.fn().mockImplementation(() => require("stripe")("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 bc5bb2c

Please sign in to comment.