mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Stripe refactor to lib (#8417)
* Moved stripe tests to folder * Abstracted stripe payments logic to lib * Added initial unit test for stripe payment * Added subscription tests * Added tests for regulare purchases * Added tests for edit subscription * Added cancel tests * Added integration tests * Fixed lint issues * Fixed lint issue
This commit is contained in:
@@ -11,20 +11,14 @@ import {
|
||||
model as Group,
|
||||
basicFields as basicGroupFields,
|
||||
} from '../models/group';
|
||||
import { model as Coupon } from '../models/coupon';
|
||||
import { model as User } from '../models/user';
|
||||
import {
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
} from './errors';
|
||||
import slack from './slack';
|
||||
import nconf from 'nconf';
|
||||
import stripeModule from 'stripe';
|
||||
import {
|
||||
BadRequest,
|
||||
} from './errors';
|
||||
import cc from 'coupon-code';
|
||||
|
||||
import stripeModule from 'stripe';
|
||||
const stripe = stripeModule(nconf.get('STRIPE_API_KEY'));
|
||||
|
||||
|
||||
@@ -382,106 +376,4 @@ api.buyGems = async function buyGems (data) {
|
||||
await data.user.save();
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows for purchasing a user subscription, group subscription or gems with Stripe
|
||||
*
|
||||
* @param options
|
||||
* @param options.token The stripe token generated on the front end
|
||||
* @param options.user The user object who is purchasing
|
||||
* @param options.gift The gift details if any
|
||||
* @param options.sub The subscription data to purchase
|
||||
* @param options.groupId The id of the group purchasing a subscription
|
||||
* @param options.email The email enter by the user on the Stripe form
|
||||
* @param options.headers The request headers to store on analytics
|
||||
* @return undefined
|
||||
*/
|
||||
api.payWithStripe = async function payWithStripe (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
|
||||
let stripeApi = stripe;
|
||||
|
||||
if (stripeInc) stripeApi = stripeInc;
|
||||
|
||||
if (!token) throw new BadRequest('Missing req.body.id');
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
amount = `${gift.gems.amount / 4 * 100}`;
|
||||
}
|
||||
}
|
||||
|
||||
response = await stripe.charges.create({
|
||||
amount,
|
||||
currency: 'usd',
|
||||
card: token,
|
||||
});
|
||||
}
|
||||
|
||||
if (sub) {
|
||||
await this.createSubscription({
|
||||
user,
|
||||
customerId: response.id,
|
||||
paymentMethod: 'Stripe',
|
||||
sub,
|
||||
headers,
|
||||
groupId,
|
||||
subscriptionId,
|
||||
});
|
||||
} else {
|
||||
let method = 'buyGems';
|
||||
let data = {
|
||||
user,
|
||||
customerId: response.id,
|
||||
paymentMethod: 'Stripe',
|
||||
gift,
|
||||
};
|
||||
|
||||
if (gift) {
|
||||
let member = await User.findById(gift.uuid).exec();
|
||||
gift.member = member;
|
||||
if (gift.type === 'subscription') method = 'createSubscription';
|
||||
data.paymentMethod = 'Gift';
|
||||
}
|
||||
|
||||
await this[method](data);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = api;
|
||||
|
||||
Reference in New Issue
Block a user