Skip to content

Commit

Permalink
Merge pull request #4803 from reactioncommerce/refactor-ajporlante-us…
Browse files Browse the repository at this point in the history
…e-no-meteor-payment-methods

Use no-meteor functions for payment capture and refund methods
  • Loading branch information
aldeed authored Nov 26, 2018
2 parents 6ad1199 + 00f22d6 commit 1f6b5b3
Show file tree
Hide file tree
Showing 32 changed files with 743 additions and 934 deletions.
13 changes: 13 additions & 0 deletions imports/plugins/core/core/server/no-meteor/pluginRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ export const queries = {};
export const resolvers = {};
export const schemas = [];
export const paymentMethods = {};

/**
* @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;
}
15 changes: 8 additions & 7 deletions imports/plugins/core/orders/server/methods/capturePayments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import Logger from "@reactioncommerce/logger";
import { Meteor } from "meteor/meteor";
import { check } from "meteor/check";
import { Orders } from "/lib/collections";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import getGraphQLContextInMeteorMethod from "/imports/plugins/core/graphql/server/getGraphQLContextInMeteorMethod";
import { getPaymentMethodConfigByName } from "/imports/plugins/core/core/server/no-meteor/pluginRegistration";


/**
* @name orders/capturePayments
Expand Down Expand Up @@ -35,16 +38,14 @@ export default function capturePayments(orderId) {

// find the payment based on shopId
const { _id: groupId, payment } = order.shipping.find((group) => group.shopId === shopId);
const { mode, processor, status, transactionId } = payment;

if (mode === "capture" && status === "approved" && processor) {
// Grab the amount from the shipment, otherwise use the original amount
const processorLowercase = processor.toLowerCase();
const { mode, name, status, transactionId } = payment;

if (mode === "capture" && status === "approved" && name) {
let result;
let error;
try {
result = Meteor.call(`${processorLowercase}/payment/capture`, payment);
const context = Promise.await(getGraphQLContextInMeteorMethod(Reaction.getUserId()));
result = Promise.await(getPaymentMethodConfigByName(name).functions.capturePayment(context, payment));
} catch (err) {
error = err;
}
Expand Down
15 changes: 8 additions & 7 deletions imports/plugins/core/orders/server/methods/createRefund.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import _ from "lodash";
import { check, Match } from "meteor/check";
import Hooks from "@reactioncommerce/hooks";
import Logger from "@reactioncommerce/logger";
import { Meteor } from "meteor/meteor";
import { check, Match } from "meteor/check";
import ReactionError from "@reactioncommerce/reaction-error";
import { Orders, Packages } from "/lib/collections";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import getGraphQLContextInMeteorMethod from "/imports/plugins/core/graphql/server/getGraphQLContextInMeteorMethod";
import { getPaymentMethodConfigByName } from "/imports/plugins/core/core/server/no-meteor/pluginRegistration";
import sendOrderEmail from "../util/sendOrderEmail";

/**
Expand Down Expand Up @@ -34,16 +35,16 @@ export default function createRefund(orderId, paymentId, amount, sendEmail = tru
const fulfillmentGroup = order.shipping.find((group) => group.payment._id === paymentId);
const { _id: groupId, payment } = fulfillmentGroup;

const { mode: paymentMode, paymentPluginName, processor, transactionId } = payment;
const processorLowercase = processor.toLowerCase();
const { mode: paymentMode, paymentPluginName, name, transactionId } = payment;

const paymentPlugin = Packages.findOne({ name: paymentPluginName, shopId: order.shopId });

// check if payment provider supports de-authorize
let result;
let modifier = {};
const context = Promise.await(getGraphQLContextInMeteorMethod(Reaction.getUserId()));
if (_.get(paymentPlugin, "settings.support", []).indexOf("De-authorize") > -1) {
result = Meteor.call(`${processorLowercase}/payment/deAuthorize`, payment, amount);
result = Promise.await(getPaymentMethodConfigByName(name).functions.deAuthorizePayment(context, payment, amount));
modifier = {
$push: {
"shipping.$.payment.transactions": result
Expand All @@ -60,7 +61,7 @@ export default function createRefund(orderId, paymentId, amount, sendEmail = tru
throw new ReactionError("Attempt to de-authorize transaction failed", result.error);
}
} else if (paymentMode === "capture") {
result = Meteor.call(`${processorLowercase}/refund/create`, payment, amount);
result = Promise.await(getPaymentMethodConfigByName(name).functions.createRefund(context, payment, amount));
modifier = {
$push: {
"shipping.$.payment.transactions": result
Expand Down
7 changes: 4 additions & 3 deletions imports/plugins/core/orders/server/methods/listRefunds.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Meteor } from "meteor/meteor";
import { check } from "meteor/check";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import getGraphQLContextInMeteorMethod from "/imports/plugins/core/graphql/server/getGraphQLContextInMeteorMethod";
import { getPaymentMethodConfigByName } from "/imports/plugins/core/core/server/no-meteor/pluginRegistration";

/**
* @name orders/refund/list
Expand All @@ -22,8 +23,8 @@ export default function listRefunds(order) {
const refunds = [];
for (const group of order.shipping) {
const { payment } = group;
const processor = payment.processor.toLowerCase();
const shopRefunds = Meteor.call(`${processor}/refund/list`, payment);
const context = Promise.await(getGraphQLContextInMeteorMethod(Reaction.getUserId()));
const shopRefunds = Promise.await(getPaymentMethodConfigByName(payment.name).functions.listRefunds(context, payment));
refunds.push(...shopRefunds);
}
return refunds;
Expand Down
10 changes: 9 additions & 1 deletion imports/plugins/included/payments-example/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import Reaction from "/imports/plugins/core/core/server/Reaction";
import resolvers from "./server/no-meteor/resolvers";
import schemas from "./server/no-meteor/schemas";
import exampleCapturePayment from "./server/no-meteor/util/exampleCapturePayment";
import exampleCreateRefund from "./server/no-meteor/util/exampleCreateRefund";
import exampleListRefunds from "./server/no-meteor/util/exampleListRefunds";

Reaction.registerPackage({
label: "ExamplePayment",
Expand All @@ -14,7 +17,12 @@ Reaction.registerPackage({
},
paymentMethods: [{
name: "iou_example",
displayName: "IOU Example"
displayName: "IOU Example",
functions: {
capturePayment: exampleCapturePayment,
createRefund: exampleCreateRefund,
listRefunds: exampleListRefunds
}
}],
settings: {
mode: false,
Expand Down
1 change: 0 additions & 1 deletion imports/plugins/included/payments-example/server/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import "./methods/example";
import "./i18n";

This file was deleted.

Loading

0 comments on commit 1f6b5b3

Please sign in to comment.