From ae9870993a4fad667ec37e2c7f2ba8293b881c56 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Thu, 18 Jun 2020 18:01:04 +0200 Subject: [PATCH] Added hasActiveStripeSubscriptions method (#169) no-issue This can be used by Ghost to determine if the Stripe keys can be deleted --- ghost/members-api/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index ae050b670b..b22cd88419 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -39,6 +39,26 @@ module.exports = function MembersApi({ const {encodeIdentityToken, decodeToken} = Tokens({privateKey, publicKey, issuer}); const metadata = Metadata({memberStripeCustomerModel, stripeCustomerSubscriptionModel}); + async function hasActiveStripeSubscriptions() { + const firstActiveSubscription = await stripeCustomerSubscriptionModel.findOne({ + status: 'active' + }); + + if (firstActiveSubscription) { + return true; + } + + const firstTrialingSubscription = await stripeCustomerSubscriptionModel.findOne({ + status: 'trialing' + }); + + if (firstTrialingSubscription) { + return true; + } + + return false; + } + const stripeStorage = { async get(member) { return metadata.getMetadata('stripe', member); @@ -450,6 +470,7 @@ module.exports = function MembersApi({ bus, sendEmailWithMagicLink, getMagicLink, + hasActiveStripeSubscriptions, members: users }; };