Refactored stripe checkout (#10345)

* Refactored stripe checkout

* Fixed dependency injection cache
This commit is contained in:
Keith Holliday
2018-05-19 10:31:26 -05:00
committed by GitHub
parent 25d07ac0ce
commit 04d7ff13de
6 changed files with 203 additions and 135 deletions

View File

@@ -1,7 +1,5 @@
import stripeModule from 'stripe';
import nconf from 'nconf';
import cc from 'coupon-code';
import moment from 'moment';
import logger from '../logger';
import {
BadRequest,
@@ -10,38 +8,22 @@ import {
} from '../errors';
import payments from './payments';
import { model as User } from '../../models/user';
import { model as Coupon } from '../../models/coupon';
import {
model as Group,
basicFields as basicGroupFields,
} from '../../models/group';
import shared from '../../../common';
import stripeConstants from './stripe/constants';
import { checkout } from './stripe/checkout';
import { getStripeApi, setStripeApi } from './stripe/api';
let stripe = stripeModule(nconf.get('STRIPE_API_KEY'));
const i18n = shared.i18n;
let api = {};
api.constants = {
// CURRENCY_CODE: 'USD',
// SELLER_NOTE: 'Habitica Payment',
// SELLER_NOTE_SUBSCRIPTION: 'Habitica Subscription',
// SELLER_NOTE_ATHORIZATION_SUBSCRIPTION: 'Habitica Subscription Payment',
// STORE_NAME: 'Habitica',
//
// GIFT_TYPE_GEMS: 'gems',
// GIFT_TYPE_SUBSCRIPTION: 'subscription',
//
// METHOD_BUY_GEMS: 'buyGems',
// METHOD_CREATE_SUBSCRIPTION: 'createSubscription',
PAYMENT_METHOD: 'Stripe',
// PAYMENT_METHOD_GIFT: 'Amazon Payments (Gift)',
};
api.setStripeApi = function setStripeApi (stripeInc) {
stripe = stripeInc;
};
api.constants = Object.assign({}, stripeConstants);
api.setStripeApi = setStripeApi;
/**
* Allows for purchasing a user subscription, group subscription or gems with Stripe
@@ -56,110 +38,7 @@ api.setStripeApi = function setStripeApi (stripeInc) {
* @param options.headers The request headers to store on analytics
* @return undefined
*/
api.checkout = async function checkout (options, stripeInc) {
let {
token,
user,
gift,
sub,
groupId,
email,
headers,
coupon,
} = options;
let response;
let subscriptionId;
// @TODO: We need to mock this, but curently we don't have correct Dependency Injection. And the Stripe Api doesn't seem to be a singleton?
let stripeApi = stripe;
if (stripeInc) stripeApi = stripeInc;
if (!token) throw new BadRequest('Missing req.body.id');
if (gift) {
const member = await User.findById(gift.uuid).exec();
gift.member = member;
}
if (sub) {
if (sub.discount) {
if (!coupon) throw new BadRequest(shared.i18n.t('couponCodeRequired'));
coupon = await Coupon.findOne({_id: cc.validate(coupon), event: sub.key}).exec();
if (!coupon) throw new BadRequest(shared.i18n.t('invalidCoupon'));
}
let customerObject = {
email,
metadata: { uuid: user._id },
card: token,
plan: sub.key,
};
if (groupId) {
customerObject.quantity = sub.quantity;
const groupFields = basicGroupFields.concat(' purchased');
const group = await Group.getGroup({user, groupId, populateLeader: false, groupFields});
const membersCount = await group.getMemberCount();
customerObject.quantity = membersCount + sub.quantity - 1;
}
response = await stripeApi.customers.create(customerObject);
if (groupId) subscriptionId = response.subscriptions.data[0].id;
} else {
let amount = 500; // $5
if (gift) {
if (gift.type === 'subscription') {
amount = `${shared.content.subscriptionBlocks[gift.subscription.key].price * 100}`;
} else {
if (gift.gems.amount <= 0) {
throw new BadRequest(shared.i18n.t('badAmountOfGemsToPurchase'));
}
amount = `${gift.gems.amount / 4 * 100}`;
}
}
if (!gift || gift.type === 'gems') {
const receiver = gift ? gift.member : user;
const receiverCanGetGems = await receiver.canGetGems();
if (!receiverCanGetGems) throw new NotAuthorized(shared.i18n.t('groupPolicyCannotGetGems', receiver.preferences.language));
}
response = await stripeApi.charges.create({
amount,
currency: 'usd',
card: token,
});
}
if (sub) {
await payments.createSubscription({
user,
customerId: response.id,
paymentMethod: this.constants.PAYMENT_METHOD,
sub,
headers,
groupId,
subscriptionId,
});
} else {
let method = 'buyGems';
let data = {
user,
customerId: response.id,
paymentMethod: this.constants.PAYMENT_METHOD,
gift,
};
if (gift) {
if (gift.type === 'subscription') method = 'createSubscription';
data.paymentMethod = 'Gift';
}
await payments[method](data);
}
};
api.checkout = checkout;
/**
* Edits a subscription created by Stripe
@@ -176,7 +55,7 @@ api.editSubscription = async function editSubscription (options, stripeInc) {
let customerId;
// @TODO: We need to mock this, but curently we don't have correct Dependency Injection. And the Stripe Api doesn't seem to be a singleton?
let stripeApi = stripe;
let stripeApi = getStripeApi();
if (stripeInc) stripeApi = stripeInc;
if (groupId) {
@@ -220,7 +99,7 @@ api.cancelSubscription = async function cancelSubscription (options, stripeInc)
let customerId;
// @TODO: We need to mock this, but curently we don't have correct Dependency Injection. And the Stripe Api doesn't seem to be a singleton?
let stripeApi = stripe;
let stripeApi = getStripeApi();
if (stripeInc) stripeApi = stripeInc;
if (groupId) {
@@ -271,7 +150,7 @@ api.cancelSubscription = async function cancelSubscription (options, stripeInc)
};
api.chargeForAdditionalGroupMember = async function chargeForAdditionalGroupMember (group) {
let stripeApi = stripe;
let stripeApi = getStripeApi();
let plan = shared.content.subscriptionBlocks.group_monthly;
await stripeApi.subscriptions.update(
@@ -298,7 +177,7 @@ api.handleWebhooks = async function handleWebhooks (options, stripeInc) {
let {requestBody} = options;
// @TODO: We need to mock this, but curently we don't have correct Dependency Injection. And the Stripe Api doesn't seem to be a singleton?
let stripeApi = stripe;
let stripeApi = getStripeApi();
if (stripeInc) stripeApi = stripeInc;
// Verify the event by fetching it from Stripe