/
stripe.js
510 lines (437 loc) · 17.9 KB
/
stripe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import Hooks from "@reactioncommerce/hooks";
import Logger from "@reactioncommerce/logger";
import accounting from "accounting-js";
import Random from "@reactioncommerce/random";
import stripeNpm from "stripe";
import { Meteor } from "meteor/meteor";
import { check, Match } from "meteor/check";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import { Cart, Shops, Accounts, Packages } from "/lib/collections";
import { PaymentMethodArgument } from "/lib/collections/schemas";
// Stripe uses a "Decimal-less" format so 10.00 becomes 1000
function formatForStripe(amount) {
return Math.round(amount * 100);
}
function unformatFromStripe(amount) {
return (amount / 100);
}
export const utils = {};
utils.getStripeApi = function (paymentPackageId) {
const stripePackage = Packages.findOne({ _id: paymentPackageId });
if (!stripePackage) throw new Error(`No package found with paymentPackageId ${paymentPackageId}`);
const stripeKey = stripePackage.settings.api_key || stripePackage.settings.connectAuth.access_token;
return stripeKey;
};
/**
* @summary Capture the results of a previous charge
* @param {object} paymentMethod - Object containing info about the previous transaction
* @returns {object} Object indicating the result, saved = true means success
* @private
*/
function stripeCaptureCharge(paymentMethod) {
let result;
const captureDetails = {
amount: formatForStripe(paymentMethod.amount)
};
const stripeKey = utils.getStripeApi(paymentMethod.paymentPackageId);
const stripe = stripeNpm(stripeKey);
try {
const capturePromise = stripe.charges.capture(paymentMethod.transactionId, captureDetails);
const captureResult = Promise.await(capturePromise);
if (captureResult.status === "succeeded") {
result = {
saved: true,
response: captureResult
};
} else {
result = {
saved: false,
response: captureResult
};
}
} catch (error) {
Logger.error(error);
result = {
saved: false,
error
};
return { error, result };
}
return result;
}
/**
* @summary normalizes the status of a transaction
* @param {object} transaction - The transaction that we need to normalize
* @return {string} normalized status string - either failed, settled, or created
* @private
*/
function normalizeStatus(transaction) {
if (!transaction) {
throw new ReactionError("invalid-parameter", "normalizeStatus requires a transaction");
}
// if this transaction failed, mode is "failed"
if (transaction.failure_code) {
return "failed";
}
// if this transaction was captured, status is "settled"
if (transaction.captured) { // Transaction was authorized but not captured
return "settled";
}
// Otherwise status is "created"
return "created";
}
/**
* @summary normalizes the mode of a transaction
* @param {object} transaction The transaction that we need to normalize
* @return {string} normalized status string - either failed, capture, or authorize
* @private
*/
function normalizeMode(transaction) {
if (!transaction) {
throw new ReactionError("invalid-parameter", "normalizeMode requires a transaction");
}
// if this transaction failed, mode is "failed"
if (transaction.failure_code) {
return "failed";
}
// If this transaction was captured, mode is "capture"
if (transaction.captured) {
return "capture";
}
// Anything else, mode is "authorize"
return "authorize";
}
/**
* @summary Normalizes the risk level response of a transaction to the values defined in paymentMethod schema
* @param {object} transaction - The transaction that we need to normalize
* @return {string} normalized status string - either elevated, high, or normal
* @private
*/
function normalizeRiskLevel(transaction) {
if (!transaction) {
throw new ReactionError("invalid-parameter", "normalizeRiskLevel requires a transaction");
}
const outcome = transaction.outcome && transaction.outcome.risk_level;
if (outcome === "elevated") {
return "elevated";
}
if (outcome === "highest") {
return "high";
}
// default to normal if no other flagged
return "normal";
}
function buildPaymentMethods(options) {
const { token, cartItemsByShop, transactionsByShopId } = options;
if (!transactionsByShopId) {
throw new ReactionError("invalid-parameter", "Creating a payment method log requries transaction data");
}
const shopIds = Object.keys(transactionsByShopId);
const storedCard = `${token.card.brand} ${token.card.last4}`;
const paymentMethods = [];
shopIds.forEach((shopId) => {
if (transactionsByShopId[shopId]) {
const cartItems = cartItemsByShop[shopId].map((item) => ({
_id: item._id,
productId: item.productId,
variantId: item.variantId,
shopId,
quantity: item.quantity
}));
// we need to grab this per shop to get the API key
const packageData = Packages.findOne({
name: "reaction-stripe",
shopId
});
const paymentMethod = {
processor: "Stripe",
storedCard,
method: "credit",
paymentPackageId: packageData._id,
// TODO: REVIEW WITH AARON - why is paymentSettings key important
// and why is it just defined on the client?
paymentSettingsKey: packageData.name.split("/").splice(-1)[0],
transactionId: transactionsByShopId[shopId].id,
amount: transactionsByShopId[shopId].amount * 0.01,
status: normalizeStatus(transactionsByShopId[shopId]),
mode: normalizeMode(transactionsByShopId[shopId]),
riskLevel: normalizeRiskLevel(transactionsByShopId[shopId]),
createdAt: new Date(transactionsByShopId[shopId].created),
transactions: [],
items: cartItems,
shopId
};
paymentMethod.transactions.push(transactionsByShopId[shopId]);
paymentMethods.push(paymentMethod);
}
});
return paymentMethods;
}
export const methods = {
async "stripe/payment/createCharges"(transactionType, token, cartId, cartToken) {
check(transactionType, String);
check(token, Object);
check(cartId, String);
check(cartToken, Match.Maybe(String));
const primaryShopId = Reaction.getPrimaryShopId();
const stripePkg = Reaction.getPackageSettingsWithOptions({
shopId: primaryShopId,
name: "reaction-stripe"
});
if (!stripePkg || !stripePkg.settings || !stripePkg.settings.api_key) {
// Fail if we can't find a Stripe API key
throw new ReactionError("not-configured", "Attempted to create multiple stripe charges, but stripe was not configured properly.");
}
const capture = transactionType === "capture";
// Must have an email
const cart = Cart.findOne({ _id: cartId });
let customerEmail = cart.email;
if (!customerEmail) {
const customerAccount = Accounts.findOne({ _id: cart.accountId });
if (customerAccount) {
const defaultEmail = (customerAccount.emails || []).find((email) => email.provides === "default");
if (defaultEmail) {
customerEmail = defaultEmail.address;
}
}
}
if (!customerEmail) {
throw new ReactionError("invalid-parameter", "No email associated with the cart");
}
// Initialize stripe api lib
const stripeApiKey = stripePkg.settings.api_key;
const stripe = stripeNpm(stripeApiKey);
// get array of shopIds that exist in this cart
const shopIds = cart.items.reduce((uniqueShopIds, item) => {
if (uniqueShopIds.indexOf(item.shopId) === -1) {
uniqueShopIds.push(item.shopId);
}
return uniqueShopIds;
}, []);
const transactionsByShopId = {};
// TODO: If there is only one transactionsByShopId and the shopId is primaryShopId -
// Create a standard charge and bypass creating a customer for this charge
const primaryShop = Shops.findOne({ _id: primaryShopId });
const { currency } = primaryShop;
try {
// Creates a customer object, adds a source via the card data
// and waits for the promise to resolve
const customer = Promise.await(stripe.customers.create({
email: customerEmail
}).then((cust) => {
const customerCard = stripe.customers.createSource(cust.id, { source: token.id });
return customerCard;
}));
// Get cart totals for each Shop
const cartTotals = cart.getTotalByShop();
const cartSubtotals = cart.getSubtotalByShop();
// Loop through all shopIds represented in cart
shopIds.forEach((shopId) => {
// TODO: If shopId is primaryShopId - create a non-connect charge with the
// stripe customer object
const isPrimaryShop = shopId === primaryShopId;
let merchantStripePkg;
// Initialize options - this is where idempotency_key
// and, if using connect, stripe_account go
const stripeOptions = {};
const stripeCharge = {
amount: formatForStripe(cartTotals[shopId]),
capture,
currency
// TODO: add product metadata to stripe charge
};
if (isPrimaryShop) {
// If this is the primary shop, we can make a direct charge to the
// customer object we just created.
stripeCharge.customer = customer.customer;
} else {
// If this is a merchant shop, we need to tokenize the customer
// and charge the token with the merchant id
merchantStripePkg = Reaction.getPackageSettingsWithOptions({
shopId,
name: "reaction-stripe"
});
// If this merchant doesn't have stripe setup, fail.
// We should _never_ get to this point, because
// this will not roll back the entire transaction
if (!merchantStripePkg ||
!merchantStripePkg.settings ||
!merchantStripePkg.settings.connectAuth ||
!merchantStripePkg.settings.connectAuth.stripe_user_id) {
throw new ReactionError("server-error", `Error processing payment for merchant with shopId ${shopId}`);
}
// get stripe account for this shop
const stripeUserId = merchantStripePkg.settings.connectAuth.stripe_user_id;
stripeOptions.stripe_account = stripeUserId; // eslint-disable-line camelcase
// Create token from our customer object to use with merchant shop
const customerToken = Promise.await(stripe.tokens.create({
customer: customer.customer
}, stripeOptions));
// TODO: Add description to charge in Stripe
stripeCharge.source = customerToken.id;
// Get the set application fee from the dashboard
const dashboardAppFee = stripePkg.settings.applicationFee || 0;
const percentAppFee = dashboardAppFee / 100; // Convert whole number app fee to percentage
// Initialize applicationFee - this can be adjusted by the onCalculateStripeApplicationFee event hook
const coreApplicationFee = formatForStripe(cartSubtotals[shopId] * percentAppFee);
/**
* Hook for affecting the application fee charged. Any hooks that `add` "onCalculateStripeApplicationFee" will
* run here
* @module hooks/payments/stripe
* @method onCalculateStripeApplicationFee
* @param {number} applicationFee the exact application fee in cents (must be returned by every hook)
* @param {object} options object containing properties passed into the hook
* @param {object} options.cart the cart object
* @param {object} options.shopId the shopId
* @todo Consider abstracting the application fee out of the Stripe implementation, into core payments
* @returns {number} the application fee after having been through all hooks (must be returned by ever hook)
* @private
*/
const applicationFee = Hooks.Events.run("onCalculateStripeApplicationFee", coreApplicationFee, {
cart, // The cart
shopId // currentShopId
});
// TODO: Consider discounts when determining application fee
// Charge the application fee created by hooks. If it doesn't exist, that means that a hook has fouled up
// the application fee. Review hooks and plugins.
// Fall back to the application fee that comes from the stripe dashboard when hook based app fee is undefined
// eslint-disable-next-line camelcase
stripeCharge.application_fee = applicationFee || coreApplicationFee;
}
// We should only do this once per shop per cart
stripeOptions.idempotency_key = `${shopId}${cart._id}${Random.id()}`; // eslint-disable-line camelcase
// Create a charge with the options set above
const charge = Promise.await(stripe.charges.create(stripeCharge, stripeOptions));
transactionsByShopId[shopId] = charge;
});
// get cartItemsByShop to build paymentMethods
const cartItemsByShop = cart.getItemsByShop();
// Build paymentMethods from transactions, card data and cart items
const paymentMethods = buildPaymentMethods({ token, cartItemsByShop, transactionsByShopId });
// If successful, call cart/submitPayment and return success back to client.
Meteor.call("cart/submitPayment", cartId, cartToken, paymentMethods);
return { success: true, transactions: transactionsByShopId };
} catch (error) {
// If unsuccessful
// return failure back to client if error is a standard stripe card error
if (error.rawType === "card_error") {
return {
success: false,
error: {
message: error.message,
code: error.code,
type: error.type,
rawType: error.rawType,
detail: error.detail
}
};
}
// If we get an unexpected error, log and return a censored error message
Logger.error(`Received unexpected error type: ${error.rawType}`);
Logger.error(error);
throw new ReactionError("server-error", "An unexpected error occurred while creating multiple stripe charges");
}
},
/**
* Capture a Stripe charge
* @see https://stripe.com/docs/api#capture_charge
* @param {Object} paymentMethod A PaymentMethod object
* @return {Object} results from Stripe normalized
*/
"stripe/payment/capture"(paymentMethod) {
// Call both check and validate because by calling `clean`, the audit pkg
// thinks that we haven't checked paymentMethod arg
check(paymentMethod, Object);
PaymentMethodArgument.validate(PaymentMethodArgument.clean(paymentMethod));
const captureDetails = {
amount: formatForStripe(paymentMethod.amount)
};
// 100% discounts are not valid when using Stripe
// If discount is 100%, capture 100% and then refund 100% of transaction
if (captureDetails.amount === accounting.unformat(0)) {
const voidedAmount = unformatFromStripe(paymentMethod.transactions[0].amount);
stripeCaptureCharge(paymentMethod);
return Meteor.call("stripe/refund/create", paymentMethod, voidedAmount);
}
return stripeCaptureCharge(paymentMethod);
},
/**
* Issue a refund against a previously captured transaction
* @see https://stripe.com/docs/api#refunds
* @param {Object} paymentMethod object
* @param {Number} amount to be refunded
* @param {String} reason refund was issued (currently unused by client)
* @return {Object} result
*/
"stripe/refund/create"(paymentMethod, amount, reason = "requested_by_customer") {
check(amount, Number);
check(reason, String);
// Call both check and validate because by calling `clean`, the audit pkg
// thinks that we haven't checked paymentMethod arg
check(paymentMethod, Object);
PaymentMethodArgument.validate(PaymentMethodArgument.clean(paymentMethod));
let result;
try {
const stripeKey = utils.getStripeApi(paymentMethod.paymentPackageId);
const stripe = stripeNpm(stripeKey);
const refundPromise = stripe.refunds.create({ charge: paymentMethod.transactionId, amount: formatForStripe(amount) });
const refundResult = Promise.await(refundPromise);
Logger.debug(refundResult);
if (refundResult && refundResult.object === "refund") {
result = {
saved: true,
response: refundResult
};
} else {
result = {
saved: false,
response: refundResult
};
Logger.warn("Stripe call succeeded but refund not issued");
}
} catch (error) {
Logger.error(error);
result = {
saved: false,
error: `Cannot issue refund: ${error.message}`
};
Logger.fatal("Stripe call failed, refund was not issued", error.message);
}
return result;
},
/**
* List refunds
* @param {Object} paymentMethod object
* @return {Object} result
*/
"stripe/refund/list"(paymentMethod) {
// Call both check and validate because by calling `clean`, the audit pkg
// thinks that we haven't checked paymentMethod arg
check(paymentMethod, Object);
PaymentMethodArgument.validate(PaymentMethodArgument.clean(paymentMethod));
const stripeKey = utils.getStripeApi(paymentMethod.paymentPackageId);
const stripe = stripeNpm(stripeKey);
let refundListResults;
try {
const refundListPromise = stripe.refunds.list({ charge: paymentMethod.transactionId });
refundListResults = Promise.await(refundListPromise);
} catch (error) {
Logger.error("Encountered an error when trying to list refunds", error.message);
}
const result = [];
if (refundListResults && refundListResults.data) {
for (const refund of refundListResults.data) {
result.push({
type: refund.object,
amount: refund.amount / 100,
created: refund.created * 1000,
currency: refund.currency,
raw: refund
});
}
}
return result;
}
};
Meteor.methods(methods);