mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-25 02:31:59 -05:00
Added update subscription method to members api (#198)
refs TryGhost/Ghost#12127 - Adds new `updateSubscription` method to members-api which allows updating individual subscription for a member - New method only allows toggling of cancellation at period end for a subscription at the moment
This commit is contained in:
parent
1625bc94c4
commit
0dad6d147f
1 changed files with 27 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const debug = require('ghost-ignition').debug('users');
|
const debug = require('ghost-ignition').debug('users');
|
||||||
|
const common = require('../lib/common');
|
||||||
|
|
||||||
module.exports = function ({
|
module.exports = function ({
|
||||||
stripe,
|
stripe,
|
||||||
|
@ -86,12 +87,38 @@ module.exports = function ({
|
||||||
return stripe.setComplimentarySubscription(member);
|
return stripe.setComplimentarySubscription(member);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function updateSubscription(memberId, {cancelAtPeriodEnd, subscriptionId}) {
|
||||||
|
// Don't allow removing subscriptions that don't belong to the member
|
||||||
|
const member = await get({id: memberId});
|
||||||
|
const subscriptions = await stripe.getSubscriptions(member);
|
||||||
|
const subscription = subscriptions.find(sub => sub.id === subscriptionId);
|
||||||
|
if (!subscription) {
|
||||||
|
throw new common.errors.BadRequestError({
|
||||||
|
message: 'Updating subscription failed! Could not find subscription'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cancelAtPeriodEnd === undefined) {
|
||||||
|
throw new common.errors.BadRequestError({
|
||||||
|
message: 'Updating subscription failed!',
|
||||||
|
help: 'Request should contain "cancel" field.'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const subscriptionUpdate = {
|
||||||
|
id: subscription.id,
|
||||||
|
cancel_at_period_end: !!(cancelAtPeriodEnd)
|
||||||
|
};
|
||||||
|
|
||||||
|
await stripe.updateSubscriptionFromClient(subscriptionUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
create,
|
create,
|
||||||
update,
|
update,
|
||||||
list,
|
list,
|
||||||
get,
|
get,
|
||||||
destroy,
|
destroy,
|
||||||
|
updateSubscription,
|
||||||
setComplimentarySubscription: safeStripe('setComplimentarySubscription'),
|
setComplimentarySubscription: safeStripe('setComplimentarySubscription'),
|
||||||
setComplimentarySubscriptionById,
|
setComplimentarySubscriptionById,
|
||||||
cancelComplimentarySubscription: safeStripe('cancelComplimentarySubscription'),
|
cancelComplimentarySubscription: safeStripe('cancelComplimentarySubscription'),
|
||||||
|
|
Loading…
Add table
Reference in a new issue