Commit Graph

130 Commits

Author SHA1 Message Date
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
Rish
65cf639603 Fixed incorrect member id in status event
no refs

The member id assigned when creating a new status event on member creation was incorrectly using `data.id` instead of `member.id`, which was undefined causing a validation error.
2021-02-23 11:21:48 +00:00
Fabien 'egg' O'Carroll
9081298517 Added initial support for Member events (#241)
refs https://github.com/TryGhost/Ghost/issues/12602

* Added Event Repository
** Added method for MRR over time
** Added method for newsletter subscriptions over time
** Added method for gross volume over time
** Added method for status segment size over time
* Captured login events
* Captured newsletter subscription/unsubscription
* Captured email address change events
* Captured paid subscription events
* Captured payment events
* Captured status events
2021-02-23 11:21:48 +00:00
Rish
b8a17f2be0 Revert "Removed unused upsertCustomer method"
This reverts commit b261c1d61e06e35657741de888fd651329101d69.
upsertCustomer method is used to overwrite customer when checkout session is completed.
2021-02-23 11:21:48 +00:00
Fabien 'egg' O'Carroll
02685e3d3c Threw "fatal" errors for non-prod usage of live Stripe data (#236)
refs https://github.com/TryGhost/Ghost/issues/12448

The `fatal` property will allow Ghost to determine whether or not it should kill the process
2021-02-23 11:21:11 +00:00
Fabien O'Carroll
1932d56890 Updated linkSubscription to ensure flag is synced 2021-02-23 11:21:11 +00:00
Fabien O'Carroll
f9dbeafbbb Added helper for determining active subscriptions 2021-02-23 11:21:11 +00:00
Fabien O'Carroll
02990ce7e5 Removed unused upsertCustomer method 2021-02-23 11:21:11 +00:00
Fabien 'egg' O'Carroll
2fb6708944 🐛 Fixed issues when updating payment method (#247)
refs https://github.com/TryGhost/Team/issues/479

* Fixed updating payment method for canceled subscriptions

Stripe considers canceled subscriptions as non-existent, so any attempts
to update them will fail with a 404 not found. Prior to this change we
were attempting to update *all* subscriptions for a customer, including
those which were canceled. This would cause an error and the loop to
break.

* 🐛 Fixed errors for members with multiple active customers

Members with multiple active customers would have their first active
customer found updated with the new payment method. We would then
iterate through *all* active subscription, and attempt to update their
payment method. If a subscription was not owned by the customer that was
just updated, it would error and cause the loop to break out.

* Added ability to update a specific subscription's payment method

In order to remove ambiguity we add the ability to update the payment
method for a specific subscription. This will remove room for errors as
we will not have to worry about if a subscription belong to the customer
or not.
2021-02-23 11:19:21 +00:00
Fabien O'Carroll
8f333cc38b 🐛 Fixed comp plans for members w/ active subscriptions
refs https://github.com/TryGhost/Team/issues/475

The subscription object here is a database model rather than an
ISubscription from the stripe library, and we need to 1) use the `get`
method to read attributes and 2) read the `subscription_id` attribute,
as the `id` is our internal one.
2021-02-22 14:00:24 +00:00
Fabien 'egg' O'Carroll
274412b051 Fixed webhook handling for Checkout Sessions (#245)
no-issue

Fetching relations via the model returns a promise, and this was missing
the `await` keyword. We also need to `get` the subscription_id attribute
as we're working with models rather than subscription objects.
2021-02-17 17:12:45 +00:00
Fabien 'egg' O'Carroll
991b6e92a3 Ensured latest subscription data is always stored (#240)
no-issue

If we receive webhooks out of order, e.g. a
`customer.subscription.updated` with a status of 'active', followed by a
`customer.subscription.created` with a status of 'incomplete'. We would
overwrite the correct value with data from the "older" webhook. This
ensures that we always fetch the latest data from the Stripe API before
storing in the database.
2021-02-11 17:43:36 +00:00
Rish
4b4a990e1d Fixed default urls for checkout session
no-issue
related to ef9cb0862c

In the last patch which fixes the bug for not passing custom redirect urls when a checkout session is created, we missed updating the case where a logged in member tries to update the subscription.
2021-02-01 10:24:16 +05:30
Fabien 'egg' O'Carroll
ef9cb0862c Passed default URLs when creating checkout session (#235)
* Passed default URLs when creating checkout session

no-issue

This fixes a bug when a checkout session is created without passing
custom redirect URLs
2021-01-27 15:19:09 +00:00
Fabien O'Carroll
0c6b92be45 Fixed Stripe Checkout for non-members
no-issue

We needed to check for  being
2021-01-26 12:14:56 +00:00
Rish
0d7c06b5a8 Fixed customer not fetched for billing update
no issue

The logic to fetch member for a checkout session was incorrectly creating a new customer instead of finding an existing one, so the member's billing details was not getting updated.
2021-01-26 17:25:40 +05:30
Fabien O'Carroll
a8782054ba Fixed Stripe Checkout displaying member email
no-issue
2021-01-26 11:26:57 +00:00
Fabien O'Carroll
0268bee13e Fixed checks for if Stripe is configured
no-issue
2021-01-26 11:26:28 +00:00
Fabien O'Carroll
77b328bf11 Fixed bug with reapplying complimentary subscriptions
no-issue

We should have been checking for the existence of active subscriptions
in order to update them
2021-01-25 13:20:31 +00:00
Fabien O'Carroll
693cc53138 Ensured transactions are passed correctly
no-issue
2021-01-22 15:15:31 +00:00
Fabien O'Carroll
6412cfe3c6 Fixed error responses when missing data
no-issue

We should error early when recieving a request that does not contain the
required data.
2021-01-19 10:42:41 +00:00
Fabien 'egg' O'Carroll
e3ef01932f Refactor members-api (#231)
no-issue

This refactors the members-api module so that it is easier to test going forward,
as well as easier to understand & navigate. The Stripe API no longer contains
storage code, this is all handled via the member repository. And we have dedicated
services for webhooks, and stripe plans initialisation.
2021-01-18 13:55:40 +00:00
Fabien O'Carroll
3abb0c543b Updated setComplimentarySubscription to error without a Stripe connection
no-issue

This allows the consumer (e.g. the importer) to surfaces errors when importing comped members
2020-12-04 10:10:59 +00:00
Fabien O'Carroll
d70aab83f6 Updated linkStripeCustomer to error rather than silently fail
no-issue

This allows the importer to surface errors when linking members to customers
2020-12-04 10:10:59 +00:00
Fabien O'Carroll
05d5310343 Updated members-api to allow passing options to models
no-issue

This allows us to do multiple operations within a db transaction, which
will be used for the importer to ensure atomic inserts
2020-12-04 10:10:59 +00:00
Fabien 'egg' O'Carroll
ecd5bb2c01 Stored cancellation reason in local database (#222)
refs https://github.com/TryGhost/Ghost/issues/12403
refs https://github.com/TryGhost/Ghost/pull/12405

This smol change means that we keep our local cancellation_reason column in sync with what we set in Stripe metadata
2020-11-23 16:50:03 +00:00
Fabien 'egg' O'Carroll
da00444961 Added support for cancellation_reason (#221)
refs https://github.com/TryGhost/Ghost/issues/12403

Adds support for sending a cancellation_reason when cancelling a plan and store the reason on the Subscription metadata
2020-11-23 16:28:35 +00:00
Rishabh Garg
c996c7b576 Added unpaid and past_due subscription status as paid member (#211)
refs https://github.com/TryGhost/Ghost/issues/12256 , https://github.com/TryGhost/Ghost/issues/12255

Currently when listing subscriptions for Members, we were only showing the subscriptions which have a status of trialing or active.

Based on discussion, the `unpaid` and `past_due` states on Stripe also represent owner's intention of considering a subscription as active instead of `cancelled`, so we allow any subscriptions under these 2 states to be also listed for a member and consider them as `paid`.

- Subscriptions will go into a past_due state if the payment is missed, this should be considered a grace period where the member still has access.

- After this the subscriptions will either go to the unpaid or the cancelled state - this can be configured on an account by account basis in the Stripe dashboard. `unpaid` is considered as an intention to keep the subscription to allow for re-activation later.
2020-10-27 15:15:23 +05:30
Matt Hanley
4359517e75 Added Stripe webhook listener for subscription created event (#208)
no-issue

Subscription created events are required for migrating Stripe subscriptions from
alternative platforms, which involves creating a new subscription for a customer
(outside of Ghost) before cancelling the original subscription.
2020-10-15 16:25:36 +05:30
Fabien 'egg' O'Carroll
8efc4c7016 Removed old webhook cleanup code (#207)
no-issue

This is no longer necessary anymore, it would delete all webhooks which
matched the current webhook handler URL, which is undesirable
2020-10-05 09:21:20 +01:00
Fabien O'Carroll
f41f366b5a Updated customer when member email is changed
refs: https://github.com/TryGhost/Ghost/issues/12055

This ensures that newsletters and billing related emails are all sent to
the same address
2020-09-28 16:57:51 +01:00
Fabien O'Carroll
b189584f98 Added method to to update customer email address
refs: https://github.com/TryGhost/Ghost/issues/12055

This will be used by the users module when updating a members email
address to keep the Stripe Customer email in sync.
2020-09-28 16:57:51 +01:00
Kristian Freeman
6ec7eeae33 Added support for promo codes in Stripe Checkout (#194)
no-issue

This commit adds support for Stripe's newly-added promotional code
parameter when creating a new Stripe Checkout session.

ref: https://stripe.com/docs/payments/checkout/set-up-a-subscription#coupons
2020-09-21 11:53:36 +01:00
Nazar Gargol
30f758e297 🐛 Fixed create and update user methods to account for created_at and subscribed fields
refs https://github.com/TryGhost/Ghost/issues/12156

- During the refactor - 117309b4e8 (diff-3daeef67d07a2a0f94c89a86cafcede9R44), `subscribed` and `created_at` fields have been overlooked. All fields accepted by Ghost's `POST /members` and `PUT /members/:id` should be supported
2020-08-24 18:29:03 +12:00
Rish
1fe75532e5 🐛 Fixed incorrect stripe method for cancelling subscriptions
refs https://github.com/TryGhost/Ghost/issues/12150

- `destroy` method was using incorrect cancel subscriptions method - stripe.cancelStripeSubscriptions - which doesn't exist
- Fixes call with intended method - `stripe.cancelAllSubscriptions` - to cancel all subscriptions
2020-08-21 16:11:24 +05:30
Rishabh Garg
0dad6d147f 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
2020-08-20 14:24:29 +05:30
Fabien O'Carroll
66b099222f Fixed throttling of Stripe API requests
no-issue

This ensures any requests during exponential backoff are correctly rate
limited too
2020-08-18 11:28:15 +01:00
Fabien O'Carroll
2d347cd5fd Fixed LeakyBucket params for test and live mode
no-issue

These were the wrong way round initially, and not caught when testing
with live api keys
2020-08-18 10:38:26 +01:00
Fabien 'egg' O'Carroll
c7ea226d9e Updated stripe module for the bulk importer (#196)
no-issue

* Added LeakyBucket rate limiting for all Stripe requests
* Added createCustomer method
* Added createComplimentarySubscription method
* Replaced getStripeCustomer with getCustomer
* Exported createStripeCustomer & createComplimentarySubscription
2020-08-17 17:35:18 +01:00
Fabien 'egg' O'Carroll
117309b4e8 Used models internally and for exported API (#195)
no-issue

Using models internally and in the exported API means that we avoid expensive
`toJSON` calls, which affects performance when looping through large lists of
members. It also allows us to take advantage of the new relations used in the
models.

The addition of "ByID" methods for linking stripe customers and setting
complimentary subscriptions allows bulk imports to avoid the overhead of creating
a model for each members, instead passing an id string. n.b. currently the impl
_does_ still create models, but it makes it easier to optimise and refactor in the 
future.
2020-08-12 12:57:28 +01:00
Fabien 'egg' O'Carroll
e7484638e3 Ensured that we do not insert orphaned rows (#190)
no-issue

Previously we would blindly put subscriptions into the database when we
received a webhook, which could result in orphaned rows that were not
linked to a customer (and by extension a member)

This updates the logic so that we will only add subscriptions if we have
a record of their customer.

Customers are only added during a checkout.session.completed webhook, at
which point a member is guarunteed, but for formailty and safety against
changes in the flow, the logic has been applied to inserting customers
too.
2020-07-24 15:39:01 +02:00
Fabien O'Carroll
d63484e99a Handled subscription deletion errors with logging
refs https://github.com/TryGhost/Ghost/issues/11557

If a subscription failed to delete, we would error and bailout of the
process, this updates it to log the error so that site owners have a
record of the error in the logs, but also to continue through the rest
of the subscriptions.
2020-07-24 13:46:38 +02:00
Fabien O'Carroll
b435d6a8c1 Renamed destroyStripeSubscriptions to cancelStripeSubscriptions
no-issue

Destroy is terminology we usually use for the model layer and was a
little confusing without context, this method is used in one place so
it's a low effort cleanup with minimal repercussions
2020-07-24 13:46:38 +02:00