Commit Graph

172 Commits

Author SHA1 Message Date
Fabien O'Carroll
a4dcb66080 Updated cancelSubscription to take member id
refs https://github.com/TryGhost/Team/issues/775

Exposing this to the Admin API means that we want to be able to cancel
by id, not just email. The same pattern from editSubscription has been
used.
2021-06-10 17:10:58 +01:00
Rishabh
daeacfbd89 Removed setting existing legacy prices to inactive
refs fd40e04105

The current implementation of setting legacy prices as inactive has a bug where it also sets the active price to inactive if only one of monthly/yearly price is edited while the other is not changed. This is a temporary reversal till we can narrow down the issue and fix to correctly set legacy plans to inactive.
2021-06-04 12:20:22 +05:30
Rishabh
7d8e18c802 Added migration to revert portal_plans setting to named values
closes https://github.com/TryGhost/Team/issues/753

Currently, the portal_plans setting is storing price ids for active monthly/yearly prices for the default product, which was done to allow multiple prices in Portal. Since we only want to limit the prices for a Product to monthly/yearly, we are reverting the earlier migration and only store the available prices as monthly / yearly in portal setting instead of ids. Its also in sync with the approach in theme/API where we use named prices for monthly/yearly instead of price id list.
2021-06-04 12:14:57 +05:30
Fabien O'Carroll
fd40e04105 Handled monthly and yearly prices in product repo
refs https://github.com/TryGhost/Team/issues/712

This allows prices to be created and assigned to a product as the
default monthly or yearly price.
2021-06-03 14:45:36 +01:00
Fabien O'Carroll
f660405937 Populated {monthly,yearly}_price_id from settings
refs https://github.com/TryGhost/Team/issues/711

As this migration relies on the settings being populated, it cannot be a
standard migration in Ghost core, as the settings are populated by these
code migrations.
2021-06-02 09:04:54 +01:00
Fabien O'Carroll
a126764dcd Updated error message for Stripe not connected
refs https://github.com/TryGhost/Team/issues/704

This improves the error messaging by explaining exactly what the problem
is and including a link to our documentation
2021-05-24 17:41:49 +01:00
Fabien O'Carroll
40012160ed Errored when Stripe connection is missing and used
refs https://github.com/TryGhost/Team/issues/704

Currently when attempting to create stripe_prices without a Stripe
connection, it will fail silently. This is an issue when initially
configuring Members as the Stripe connection can take some time to be
established. By erroring we allow the client to be notifed that the
connection does not yet exist, so that it can be retried later.
2021-05-24 10:19:55 +01:00
Fabien O'Carroll
98fe7785d6 Handled week & day interval for calculating MRR
refs https://github.com/TryGhost/Team/issues/635

It's possible that we have subscriptions in the system which have been
created externally, and so using an interval of week or day. This change
ensures that we handle the mrr_delta for these subscriptions correctly.
2021-05-20 16:56:14 +01:00
Fabien O'Carroll
f3ecb44e08 Updated migration to use getPrice method
refs https://github.com/TryGhost/Team/issues/657

Since we have removed the getPlan method we need to update the migration
which is the last piece of code which used it. Prices are backwards
compatible with Plans - so this will continue to work as expected.
2021-05-20 15:35:58 +01:00
Fabien O'Carroll
1b628a1637 Refactored Token Service to use async/await
refs https://github.com/TryGhost/Team/issues/657

- Removes the use of "plans" from token service - method wasn't used
- Updates to use async/await so the code is clearer
- Install types for the node-jose module
- Install types for the jsonwebtoken module
2021-05-20 15:18:46 +01:00
Fabien O'Carroll
10b40dfd16 Added comment for deprecation of plan_from_trial
refs https://github.com/TryGhost/Team/issues/657

We must still use the "plan" approach of creating Checkout Sessions so
that we are able to maintain our current functionality of setting the
free trial from the one associated with the Plan/Price used for the
Checkout Session.

In future we would handle free trials internally in Ghost and apply them
on a subscription-by-subscription basis.
2021-05-20 15:15:33 +01:00
Fabien O'Carroll
16b984e2f6 Fixed types for StripeAPIService
no-issue
2021-05-20 15:15:24 +01:00
Fabien O'Carroll
162722b9d3 Removed unused plan related code
refs https://github.com/TryGhost/Team/issues/657

This code is no longer used since we got rid of the StripePlans service
2021-05-20 15:14:24 +01:00
Fabien O'Carroll
26e2cb98e9 Removed environment checks from webhook service
refs https://github.com/TryGhost/Team/issues/598
refs https://github.com/TryGhost/Ghost/commit/5cdf910e

Since we have included these checks in the Ghost codebase we do not need
to run them here.
2021-05-20 12:11:43 +01:00
Fabien O'Carroll
ee32528006 Populated members_{monthly,yearly}_price_id settings
refs https://github.com/TryGhost/Team/issues/698

As this migration relies on the `stripe_prices` table being populated,
it can not be in a standard versioned migration in Ghost core.
2021-05-19 15:16:15 +01:00
Fabien O'Carroll
25d3d42427 Removed use of 'comped' status for Members
refs https://github.com/TryGhost/Team/issues/693

With the new system of Custom Products, the concept of Complimentary is
not longer a thing, and will instead be handled by explicitly creating
prices with no amount. This means that the 'comped' status for members
will be replaced with 'paid'.
2021-05-17 13:06:41 +01:00
Fabien O'Carroll
d5269d8a9a Fixed finding newly created zero-amount price
no-issue

The condition in the find statement was incorrecly referring to the
subscription rather than the price for the subscription.
2021-05-11 10:58:41 +01:00
Fabien O'Carroll
d32e44c73b Mapped Stripe Product name to Product name
closes https://github.com/TryGhost/Team/issues/682

This ensures that the Stripe Product name is updated during the
migrations of an existing site and any future updates to the Product
name.
2021-05-10 19:21:41 +01:00
Fabien O'Carroll
15b535fd45 Removed unused stripe-plans service
no-issue

This module is no longer used!
2021-05-10 19:21:41 +01:00
Fabien O'Carroll
45d338730c Updated webhook service to work with multiple products
no-issue

Since we do not necessarily have a single stripe product anymore, we
should be checking if an invoice webhook is for a stripe product which
we know about. We use the Products repository to search our database for
one.
2021-05-10 19:21:41 +01:00
Fabien O'Carroll
a3f7f3d1a0 Updated setComplimentary to work with new system
closes https://github.com/TryGhost/Team/issues/650

Despite the fact we're getting rid of the concept of Complimentary, we
must maintain backwards compatibility for the `comped` flag in the Admin
API & the importer. This flag is handled via the `setComplimentary`
method, which has been updated to work with the new system.
2021-05-10 19:21:32 +01:00
Fabien O'Carroll
f2123d07db Added support for un/archiving Prices
https://github.com/TryGhost/Team/issues/665

We update both Stripe and our database based on the `active` flag for
existing stripe prices.
2021-05-07 17:01:00 +01:00
Rishabh
aa640ada5e Added migration for stripe plans to custom prices
refs https://github.com/TryGhost/Team/issues/637

- Adds one-off migration that reads from current `stripe_plans data` for a price, and ensures that the corresponding price is present in `stripe_prices` table at start.
- Currently, the portal_plans setting is used to determine the prices available to Portal for showing on Signup or Subscription change screen. The values allowed in portal_plans currently only allow [free, monthly, yearly] , which needs to be updated now to store price ids of available prices instead. Uses above migration to populate `portal_plans` with ids instead of names.
2021-05-04 21:52:51 +05:30
Rishabh
460dd09f8b Added description to product repository
refs https://github.com/TryGhost/Team/issues/586
refs https://github.com/TryGhost/Ghost/commit/b4d9ee0b

The `products` and `stripe_prices` were missing a description
column which will be used by Portal to display information about the
products and prices
2021-05-04 21:52:51 +05:30
Rishabh
3a27d1bd0c Updated APIs to use price ids
refs https://github.com/TryGhost/Team/issues/637

All the APIs that currently work with price names needs to be updated to work with price ids instead to work with custom prices/products. This change updates APIs to work with Price IDs in `checkout` , `updateSubscription` and other APIs/methods.
2021-05-04 21:52:51 +05:30
Fabien O'Carroll
4e98c62b71 Fixed update method for Products repository
no-issue

When updating a Product we can pass existing Stripe Prices, these will
either be adding to the database, or updated if they already exist. When
updating them we were attemping to use the `id` passed in the update,
which is not necessarily included. Instead we should use the `id` of the
StripePrice which we have already retrieved from the database.
2021-04-26 17:15:17 +01:00
Fabien O'Carroll
20e5dcc91d Added createSubscription method to member repo
refs https://github.com/TryGhost/Team/issues/616

This is a generic method for adding a subscription to a member for a
particular price/product pair. This will be used in the Admin for e.g.
giving complimentary subscriptions.
2021-04-23 17:34:04 +01:00
Rishabh Garg
a08690363e Updated product repo to allow editing price names (#265)
refs https://github.com/TryGhost/Team/issues/616

For existing prices linked to a Product, we only allow site owners/admins to edit their nickname and nothing else. This change handles the update in Ghost, but needs extension to update the name in Stripe as well.
2021-04-23 21:46:07 +05:30
Rishabh Garg
42ade8fd12 Updated customer fetch from Stripe to include subscriptions (#264)
closes https://github.com/TryGhost/Team/issues/628
refs 9010a62d54

Following up on last commit, this moves up the expansion of Stripe customer fetch to always include `subscriptions` by default in api service so we don't accidentally miss it.
2021-04-22 21:51:01 +05:30
Rish
9010a62d54 🐛 Fixed members importer failing to link paid subscriptions
no refs

The latest version of Stripe doesn't return the `subscriptions` object on `Customer` resource by default and needs an extra param to do so. As we recently updated Members to use the latest Stripe version, the importer tries to fetch all subscriptions of a customer to map in Ghost but was failing due to missing `subscriptions` data. Fix updates the Stripe API to include `subscriptions` by default in response.
2021-04-22 21:22:23 +05:30
Rish
c612b4d194 Ignored missing plans on Stripe for update
refs https://github.com/TryGhost/Team/issues/591

It's possible to have sites which still have subscriptions in their DB from old Stripe accounts, most likely added when we allowed Stripe Direct, as those subscriptions were not cleaned up. While populating plans and products for existing subscriptions, we want to ignore these old subscriptions which are not part of current Stripe account.
2021-04-22 11:49:08 +05:30
Rishabh Garg
bee619c123 Updated link subscription to handle missing stripe data (#262)
refs https://github.com/TryGhost/Team/issues/619

On linking a stripe subscription to a member, this change -

- Adds missing stripe price or stripe product from subscription to DB
  - Missing Stripe price is attached to the first Ghost Product if no matching Product exists
- Updates usage from plan to price in the `linkSubscription` method
- Updates products associated with a member based on active subscriptions
2021-04-20 17:21:16 +05:30
Fabien 'egg' O'Carroll
dc0e5b0ec8 Wired up ProductRepository to members-api
refs https://github.com/TryGhost/Team/issues/616

Working with ProductRepository as a separate package was more trouble
than it was worth, so it's been moved into members-api. We expose the
product repository so that Ghost Admin API can access it.
2021-04-19 15:09:28 +01:00
Rishabh Garg
836b7f235e Populate stripe prices and products for existing customers (#258)
refs https://github.com/TryGhost/Team/issues/586

On Ghost Boot, as part of configuring Stripe, this populates stripe products and prices for existing stripe customers in the newly created `stripe_prices` and `stripe_products` table, which allows us to map existing customers to default Ghost product and on current prices. The population script on boot is only run if we find -

- A Ghost Product
- No rows in `stripe_products`
- No rows in `stripe_prices`
- One or more rows in `members_stripe_customers_subscriptions`
2021-04-12 20:38:01 +05:30
Rishabh Garg
72e097cdc2 Updated Stripe major version and types (#256)
refs https://github.com/TryGhost/Team/issues/593

- Bumps `stripe` node library major version to v8 - 8.142
- Bumps default Stripe version to latest - '2020-08-27'
- Updated webhook Stripe version to latest - '2020-08-27'
- Removes `@types/stripe` in favor of first-class types support in `stripe` lib directly
- Updates types across files
2021-04-09 21:11:16 +05:30
Rish
d4488b5e59 🐛 Fixed incorrect mrr delta calculation
refs https://github.com/TryGhost/Team/issues/595

For a canceled subscription, the desired MRR delta is to reduce by negative of original amount, but our logic was incorrectly reducing it by double which led to big gap between real MRR and one shown on Dashboard.

- Fixes calculation for MRR change for canceled subscriptions
2021-04-06 18:42:55 +05:30
Fabien O'Carroll
999acc60d7 Added support for updating subscription by id
no-issue

The Admin API uses a Member id rather than email to update
subscriptions, this ensures that we provide an interface that will
continue to work with the Admin API
2021-03-30 11:00:41 +01:00
Fabien O'Carroll
e2a46863d8 Updated MemberRepostitory with subscription methods
https://github.com/TryGhost/Team/issues/530

These are required by the smart_cancel functionality
2021-03-30 10:36:49 +01:00
Fabien O'Carroll
a302ff1597 Added support for smart_cancel options
https://github.com/TryGhost/Team/issues/530

This option will check to see if a subscription is in an unpaid or past
due state, and if so, will cancel the subscription immediately, rather
than cancelling at the period end.
2021-03-30 10:36:45 +01:00
Fabien 'egg' O'Carroll
3ed10ecdf6 Added tests for updateSubscription
refs https://github.com/TryGhost/Team/issues/530

The RouterController was a grab bag of all controller methods, making it
difficult to mock & test. This adds a MemberController with a smaller
API - making it easier to test.
2021-03-25 12:19:01 +00:00
Rish
cac4ca14ff Added check for plan nickname to exist
no-issue

When seeding the database with fake members & stripe data, it's possible
to create stripe plans without a nickname. Similarly some other services
do not have a nickname on their plans. This ensures that we do not error
when working with these plans.
2021-03-10 17:15:16 +00:00
Fabien O'Carroll
907ccd9f34 Fixed creating events within transaction
no-issue

If we are to perform the `linkSubscription` method inside of a
transaction, the addition of the paid subscription events would happen
outside of the transaction, and cause errors. This ensures that we pass
the options object (containing the transaction) to the models calls to
add paid subscription events
2021-03-10 17:15:16 +00:00
Fabien O'Carroll
9be1d2de4f Updated invoice webhook handling for payment events
no-issue

1. We do not want to store payment events for payments of 0 value
2. Stripe webhooks can arrive and be processed "out of order", which can
   result in us attempting to add a payment event for a member which
   does not yet exist. The change here will 404 in such (edge) cases, so
   that Stripe will retry the webhook at a later point, when the Member
   has been created, allowing us to store the payment event.
2021-03-10 17:15:16 +00:00
Fabien O'Carroll
d662003fdb Updated status events to happen before subscribed events
no-issue

Given that status events are used to determine signup events,
they should be the first event for a member.
2021-03-05 12:59:05 +00:00
Fabien O'Carroll
58c9c1c649 Cleaned newsletter subscription events from timeline
refs https://github.com/TryGhost/Team/issues/469

In order to reduce noise, we want to only display newsletter
subscription events which are not likely to be the result of a member
signup. The approach we've taken is to remove any newsletter
subscription (not unsubscription) event, if when sorted in chronological
order, it is to reside next to a signup event for the same member.

An improvement to this approach might be to add some kind of transaction
id  to events which would allow us to group together events which should
be considered to have happened simultaneously.
2021-03-05 12:59:05 +00:00
Fabien O'Carroll
e003c10e8b Added signup_events to the event timeline
refs https://github.com/TryGhost/Team/issues/469

Signup events are captured by status changes with no `from_status`, this
means that the member did not have a status (did not exist) before this
change.
2021-03-05 12:59:05 +00:00
Fabien O'Carroll
c8eb50bf57 Fixed ordering of event timelime
refs https://github.com/TryGhost/Team/issues/469

We order the set of all events by created_at, but were not fetching the
individual events with the same order applies, this resulted in
incorrect results.
2021-03-05 12:57:50 +00:00
Fabien O'Carroll
095c624172 🐛 Fixed cancelling subscriptions when destroying
refs https://github.com/TryGhost/Ghost/issues/12711

We must wait for the stripeSubscriptions relation to be loaded before
attempting to loop through them. As well as this we should use `upsert`
so that we can edit a subscription record by `subscription_id`, rather
than the (internal) `id`
2021-03-03 13:15:13 +00:00
Fabien O'Carroll
17175c87cc Fixed bug with calculating MRR and Volume
no-issue

This was not accessing the correct data when summing the deltas
2021-02-23 17:45:52 +00:00
Fabien 'egg' O'Carroll
e1b3b38bcd Added initial support for event timelines
refs https://github.com/TryGhost/Ghost/issues/12602

This adds initial support for sitewide event timelines
2021-02-23 11:21:48 +00:00