mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
add stripe webhook to handle cancelled subscriptions
This commit is contained in:
@@ -3,35 +3,47 @@ import each from 'lodash/each';
|
||||
|
||||
let subscriptionBlocks = {
|
||||
basic_earned: {
|
||||
target: 'user',
|
||||
canSubscribe: true,
|
||||
months: 1,
|
||||
price: 5,
|
||||
},
|
||||
basic_3mo: {
|
||||
target: 'user',
|
||||
canSubscribe: true,
|
||||
months: 3,
|
||||
price: 15,
|
||||
},
|
||||
basic_6mo: {
|
||||
target: 'user',
|
||||
canSubscribe: true,
|
||||
months: 6,
|
||||
price: 30,
|
||||
},
|
||||
google_6mo: {
|
||||
target: 'user',
|
||||
canSubscribe: true,
|
||||
months: 6,
|
||||
price: 24,
|
||||
discount: true,
|
||||
original: 30,
|
||||
},
|
||||
basic_12mo: {
|
||||
target: 'user',
|
||||
canSubscribe: true,
|
||||
months: 12,
|
||||
price: 48,
|
||||
},
|
||||
group_monthly: {
|
||||
type: 'group',
|
||||
target: 'group',
|
||||
canSubscribe: true,
|
||||
months: 1,
|
||||
price: 9,
|
||||
quantity: 3, // Default quantity for Stripe - The same as having 3 user subscriptions
|
||||
},
|
||||
group_plan_auto: {
|
||||
type: 'group',
|
||||
target: 'user',
|
||||
canSubscribe: false,
|
||||
months: 0,
|
||||
price: 0,
|
||||
quantity: 1,
|
||||
|
||||
@@ -87,4 +87,14 @@ api.subscribeCancel = {
|
||||
},
|
||||
};
|
||||
|
||||
api.handleWebhooks = {
|
||||
method: 'POST',
|
||||
url: '/stripe/webhook',
|
||||
async handler (req, res) {
|
||||
await stripePayments.handleWebhooks({requestBody: req.body});
|
||||
|
||||
return res.respond(200, {});
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = api;
|
||||
|
||||
@@ -2,7 +2,7 @@ import stripeModule from 'stripe';
|
||||
import nconf from 'nconf';
|
||||
import cc from 'coupon-code';
|
||||
import moment from 'moment';
|
||||
|
||||
import logger from './logger';
|
||||
import {
|
||||
BadRequest,
|
||||
NotAuthorized,
|
||||
@@ -270,4 +270,74 @@ api.chargeForAdditionalGroupMember = async function chargeForAdditionalGroupMemb
|
||||
group.purchased.plan.quantity = group.memberCount + plan.quantity - 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle webhooks from stripes
|
||||
*
|
||||
* @param options
|
||||
* @param options.user The user object who is purchasing
|
||||
* @param options.groupId The id of the group purchasing a subscription
|
||||
*
|
||||
* @return undefined
|
||||
*/
|
||||
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;
|
||||
if (stripeInc) stripeApi = stripeInc;
|
||||
|
||||
const eventJSON = JSON.parse(requestBody);
|
||||
|
||||
// Verify the event by fetching it from Stripe
|
||||
const event = await stripeApi.events.retrieve(eventJSON.id);
|
||||
|
||||
switch (event.type) {
|
||||
case 'customer.subscription.deleted': {
|
||||
// event.request === null means that the user itself cancelled the subscrioption,
|
||||
// the cancellation on our side has been already handled
|
||||
if (event.request === null) break;
|
||||
|
||||
const subscription = event.data.object;
|
||||
const customerId = subscription.customer;
|
||||
const isGroupSub = shared.content.subscriptionBlocks[subscription.plan.id].target === 'group';
|
||||
|
||||
let user;
|
||||
let groupId;
|
||||
|
||||
if (isGroupSub) {
|
||||
let groupFields = basicGroupFields.concat(' purchased');
|
||||
let group = await Group.findOne({
|
||||
'purchased.plan.customerId': customerId,
|
||||
paymentMethod: this.constants.PAYMENT_METHOD,
|
||||
}).select(groupFields).exec();
|
||||
|
||||
if (!group) throw new NotFound(i18n.t('groupNotFound'));
|
||||
groupId = group._id;
|
||||
|
||||
user = await User.findById(group.leader).exec();
|
||||
} else {
|
||||
user = await User.findOne({
|
||||
'purchased.plan.customerId': customerId,
|
||||
paymentMethod: this.constants.PAYMENT_METHOD,
|
||||
}).exec();
|
||||
}
|
||||
|
||||
if (!user) throw new NotFound(i18n.t('groupNotFound'));
|
||||
|
||||
await stripeApi.customers.del(customerId);
|
||||
|
||||
await payments.cancelSubscription({
|
||||
user,
|
||||
groupId,
|
||||
paymentMethod: this.constants.PAYMENT_METHOD,
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
logger.error(new Error(`Missing handler for Stripe webhook ${event.type}`), {eventJSON});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
module.exports = api;
|
||||
|
||||
@@ -31,7 +31,7 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template',
|
||||
div(ng-if='!user.purchased.plan.customerId || (user.purchased.plan.customerId && user.purchased.plan.dateTerminated)')
|
||||
h4(ng-if='(user.purchased.plan.customerId && user.purchased.plan.dateTerminated)')= env.t("resubscribe")
|
||||
.form-group.reduce-top-margin
|
||||
.radio(ng-repeat='block in Content.subscriptionBlocks | toArray | omit: "discount==true" | orderBy:"months"', ng-if="block.type !== 'group'")
|
||||
.radio(ng-repeat='block in Content.subscriptionBlocks | toArray | omit: "discount==true" | orderBy:"months"', ng-if="block.target !== 'group' && block.canSubscribe === true")
|
||||
label
|
||||
input(type="radio", name="subRadio", ng-value="block.key", ng-model='_subscription.key')
|
||||
span(ng-show='block.original')
|
||||
|
||||
Reference in New Issue
Block a user