Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import SimpleSchema from "simpl-schema";
import accounting from "accounting-js";
import Logger from "@reactioncommerce/logger";
import ReactionError from "@reactioncommerce/reaction-error";
import { getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import sendOrderEmail from "../util/sendOrderEmail.js";

const inputSchema = new SimpleSchema({
Expand Down Expand Up @@ -60,7 +59,7 @@ export default async function createRefund(context, input) {

// Verify that payment method will accept refunds
const { name } = payment;
const { canRefund, functions } = getPaymentMethodConfigByName(name);
const { canRefund, functions } = context.queries.getPaymentMethodConfigByName(name);
if (!canRefund) throw new ReactionError("invalid", "Refunding not supported");

// Find total of any previous refunds, and determine how much balance is left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Logger from "@reactioncommerce/logger";
import Random from "@reactioncommerce/random";
import ReactionError from "@reactioncommerce/reaction-error";
import getAnonymousAccessToken from "@reactioncommerce/api-utils/getAnonymousAccessToken.js";
import { getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import buildOrderFulfillmentGroupFromInput from "../util/buildOrderFulfillmentGroupFromInput.js";
import verifyPaymentsMatchOrderTotal from "../util/verifyPaymentsMatchOrderTotal.js";
import { Order as OrderSchema, orderInputSchema, Payment as PaymentSchema, paymentInputSchema } from "../simpleSchemas";
Expand Down Expand Up @@ -109,7 +108,7 @@ async function createPayments({
// Grab config for this payment method
let paymentMethodConfig;
try {
paymentMethodConfig = getPaymentMethodConfigByName(methodName);
paymentMethodConfig = context.queries.getPaymentMethodConfigByName(methodName);
} catch (error) {
Logger.error(error);
throw new ReactionError("payment-failed", `Invalid payment method name: ${methodName}`);
Expand Down
3 changes: 1 addition & 2 deletions imports/node-app/core-services/orders/queries/refunds.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ReactionError from "@reactioncommerce/reaction-error";
import { getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import { getOrderQuery } from "../util/getOrderQuery.js";

/**
Expand Down Expand Up @@ -30,7 +29,7 @@ export default async function refunds(context, { orderId, shopId, token } = {})

if (Array.isArray(order.payments)) {
const shopRefundsWithPaymentPromises = order.payments.map((payment) =>
getPaymentMethodConfigByName(payment.name)
context.queries.getPaymentMethodConfigByName(payment.name)
.functions.listRefunds(context, payment)
.then((shopRefunds) => ({ shopRefunds, payment })));

Expand Down
25 changes: 11 additions & 14 deletions imports/node-app/core-services/orders/queries/refunds.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/* eslint camelcase: 0 */
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import { rewire$getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import refunds from "./refunds.js";

beforeAll(() => {
rewire$getPaymentMethodConfigByName(() => ({
functions: {
listRefunds: async () => [{
_id: "refundId",
type: "refund",
amount: 19.99,
currency: "usd"
}]
}
}));
});
mockContext.queries.getPaymentMethodConfigByName = jest.fn().mockName("getPaymentMethodConfigByName").mockImplementation(() => ({
functions: {
listRefunds: async () => [{
_id: "refundId",
type: "refund",
amount: 19.99,
currency: "usd"
}]
}
}));

beforeEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

const order = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ReactionError from "@reactioncommerce/reaction-error";
import { getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import { getOrderQuery } from "../util/getOrderQuery.js";

/**
Expand Down Expand Up @@ -39,7 +38,7 @@ export default async function refundsByPaymentId(context, { orderId, paymentId,
throw new ReactionError("not-found", "Payment not found");
}

const shopRefunds = await getPaymentMethodConfigByName(payment.name).functions.listRefunds(context, payment);
const shopRefunds = await context.queries.getPaymentMethodConfigByName(payment.name).functions.listRefunds(context, payment);
const shopRefundsWithPaymentId = shopRefunds.map((shopRefund) => ({
...shopRefund,
paymentId: payment._id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/* eslint camelcase: 0 */
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import { rewire$getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import refundsByPaymentId from "./refundsByPaymentId.js";

beforeAll(() => {
rewire$getPaymentMethodConfigByName(() => ({
functions: {
listRefunds: () => [{
_id: "refundId",
type: "refund",
amount: 19.99,
currency: "usd"
}]
}
}));
});
mockContext.queries.getPaymentMethodConfigByName = jest.fn().mockName("getPaymentMethodConfigByName").mockImplementation(() => ({
functions: {
listRefunds: async () => [{
_id: "refundId",
type: "refund",
amount: 19.99,
currency: "usd"
}]
}
}));

beforeEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

const order = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from "lodash";
import formatMoney from "@reactioncommerce/api-utils/formatMoney";
import formatMoney from "@reactioncommerce/api-utils/formatMoney.js";
import { xformOrderItems } from "@reactioncommerce/reaction-graphql-xforms/order";
import { getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import { addAnonymousOrderToken } from "./anonymousToken.js";

/**
Expand Down Expand Up @@ -78,7 +77,7 @@ export default async function getDataForOrderEmail(context, { order }) {

if (Array.isArray(order.payments)) {
const promises = order.payments.map(async (payment) => {
const shopRefunds = await getPaymentMethodConfigByName(payment.name).functions.listRefunds(context, payment);
const shopRefunds = await context.queries.getPaymentMethodConfigByName(payment.name).functions.listRefunds(context, payment);
const shopRefundsWithPaymentId = shopRefunds.map((shopRefund) => ({ ...shopRefund, paymentId: payment._id }));
refunds.push(...shopRefundsWithPaymentId);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/* eslint-disable require-jsdoc */
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import Factory from "/imports/test-utils/helpers/factory"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import { restore, rewire$getPaymentMethodConfigByName } from "/imports/node-app/core-services/payments/registration.js"; // TODO: remove cross-plugin import (https://github.com/reactioncommerce/reaction/issues/5653)
import getDataForOrderEmail from "./getDataForOrderEmail.js";

beforeAll(() => {
rewire$getPaymentMethodConfigByName(() => ({
functions: {
listRefunds: () => []
}
}));
});
mockContext.queries.getPaymentMethodConfigByName = jest.fn().mockName("getPaymentMethodConfigByName").mockImplementation(() => ({
functions: {
listRefunds: async () => [{
_id: "refundId",
type: "refund",
amount: 19.99,
currency: "usd"
}]
}
}));

afterAll(() => {
restore();
beforeEach(() => {
jest.clearAllMocks();
});

function setupMocks(mockShop, mockCatalogItem) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ReactionError from "@reactioncommerce/reaction-error";
import SimpleSchema from "simpl-schema";
import { getPaymentMethodConfigByName } from "../registration.js";

const inputSchema = new SimpleSchema({
orderId: String,
Expand Down Expand Up @@ -53,7 +52,7 @@ export default async function captureOrderPayments(context, input = {}) {
const capturePromises = orderPaymentsToCapture.map(async (payment) => {
let result = { saved: false };
try {
result = await getPaymentMethodConfigByName(payment.name).functions.capturePayment(context, payment);
result = await context.queries.getPaymentMethodConfigByName(payment.name).functions.capturePayment(context, payment);
} catch (error) {
result.error = error;
result.errorCode = "uncaught_plugin_error";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { paymentMethods } from "../registration.js";

/**
* @name getPaymentMethodConfigByName
* @param {String} name payment method name, e.g. example, stripe_card
* @returns {Object} payment method configuration
*/
export default function getPaymentMethodConfigByName(name) {
const config = paymentMethods[name];
if (!config) {
throw new Error(`Configuration not found for ${name} payment method. Did you remove the plugin that provides this payment method?`);
}
return config;
}
2 changes: 2 additions & 0 deletions imports/node-app/core-services/payments/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import availablePaymentMethods from "./availablePaymentMethods.js";
import getPaymentMethodConfigByName from "./getPaymentMethodConfigByName.js";
import paymentMethods from "./paymentMethods.js";

export default {
availablePaymentMethods,
getPaymentMethodConfigByName,
paymentMethods
};
13 changes: 0 additions & 13 deletions imports/node-app/core-services/payments/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,3 @@ export function registerPluginHandler({ name: pluginName, paymentMethods: plugin
}
}
}

/**
* @name getPaymentMethodConfigByName
* @param {String} name payment method name, e.g. example, stripe_card
* @returns {Object} payment method configuration
*/
export function getPaymentMethodConfigByName(name) {
const config = paymentMethods[name];
if (!config) {
throw new Error(`Configuration not found for ${name} payment method. Did you remove the plugin that provides this payment method?`);
}
return config;
}
2 changes: 1 addition & 1 deletion imports/node-app/core-services/product/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Product,
ProductVariant,
VariantMedia
} from "./simpleSchemas.js"; // TODO: update simpleschemas
} from "./simpleSchemas.js";

/**
* @summary Import and call this function to add this plugin to your API.
Expand Down