Flagsmith / flagsmith

Open Source Feature Flagging and Remote Config Service. Host on-prem or use our hosted version at https://flagsmith.com/
https://flagsmith.com/
BSD 3-Clause "New" or "Revised" License
4.84k stars 369 forks source link

Chargebee upgrades can cause a second subscription to be created #312

Closed dabeeeenster closed 2 years ago

dabeeeenster commented 3 years ago

We need to implement subscription upgrades via the API.

To Do

gagantrivedi commented 3 years ago

The backend already supports this. front-end just needs to add update subscription(currently it's new subscription) modal on the front-end.

kyle-ssg commented 3 years ago

Unsure of what's required here, the frontend currently calls api/v1/organisations/:id/update-subscription/ when chargebee calls back. Assign back to me when this is fleshed out a bit better.

dabeeeenster commented 3 years ago

OK i think this is just fundamentally not following the same steps described here. We need an endpoint similar in that page to generate_checkout_existing_url which AFAICT we don't have, and the FE is not attempting to call when upgrading the sub.

matthewelwell commented 3 years ago

This PR ads a new endpoint to get the URL to the hosted page for an existing subscription, the FE should call this and direct the user there to upgrade instead of generating a new hosted page and then call the 'update-subscription' endpoint with the id of the hosted page as normal.

The issue we have at the moment (as I understand it) is that the FE is generating a brand new hosted page unassociated with an existing subscription id and hence a new subscription is being created rather than the existing one being amended.

Note that this flow is only applicable for organisations with existing subscriptions of course. Any new organisations / orgs without a subscription id will need to follow the current workflow.

kyle-ssg commented 2 years ago

image

Frontend will be live under the flag "upgrade_subscription", it looks like the hosted URL that comes from the API needs to be slightly different as it always shows the existing plan without the ability to edit. If possible this should preselect a plan that is passed to the API.

matthewelwell commented 2 years ago

Ok, a bit of digging and looking through chargeee's unusually poor documentation, it turns out we just need to add the plan id of the plan the user wants to switch to when getting the hosted page.

To achieve this, I've updated the endpoint slightly so that it better describes what's happening.

The endpoint now looks like:

POST /api/organisations/:id/get-hosted-page-url-for-subscription-upgrade

 + request 
{
    "plan_id": "startup"
}

 + response
{
    "url": "https://some.chargebee.url/page/id"
}

@kyle-ssg this is currently in a PR on the API here so once that's merged you can test it out on staging.

dabeeeenster commented 2 years ago

The plan IDs are:

kyle-ssg commented 2 years ago

Looks good against staging, though it doesn't work against production (maybe that's known but raising here just in case, get-hosted-page-url-for-subscription-upgrade returns subscription_id: ["This field may not be null."])

kyle-ssg commented 2 years ago

Frontend's live under https://github.com/Flagsmith/flagsmith/pull/544 under flag upgrade_subscription

matthewelwell commented 2 years ago

I'll look at handling that better but the above just means that the organisation you're trying to request the page for doesn't have a subscription, or their subscription doesn't have an id (maybe it's some manual subscription or something).