Skip to content

[usage] Implement CreateStripeSubscription #14044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2022
Merged

Conversation

laushinka
Copy link
Contributor

@laushinka laushinka commented Oct 20, 2022

Description

Implements CreateStripeSubscription.
Follow-up: Plug it into server with a feature flag.

Related Issue(s)

Depends on #14124
Fixes #13198

How to test

  1. Go to the preview env.
  2. Create a team with the name Gitpod in it (make sure to use the capital 'G').
  3. Go to the team's Billing page.
  4. Upgrade plan.

Release Notes

NONE

Documentation

Werft options:

  • /werft with-local-preview
    If enabled this will build install/preview
  • /werft with-preview
  • /werft with-integration-tests=all
    Valid options are all, workspace, webapp, ide

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-lau-createstripesub-impl.9 because the annotations in the pull request description changed
(with .werft/ from main)

@laushinka laushinka force-pushed the lau/createstripesub-impl branch 6 times, most recently from 5ba024f to a774cce Compare October 24, 2022 06:55
@laushinka
Copy link
Contributor Author

laushinka commented Oct 24, 2022

Pipeline has been acting weird.

/werft run

👍 started the job as gitpod-build-lau-createstripesub-impl.24
(with .werft/ from main)

@laushinka laushinka requested a review from easyCZ October 24, 2022 07:30
@laushinka laushinka force-pushed the lau/createstripesub-impl branch 3 times, most recently from 132c297 to f4acfbf Compare October 24, 2022 12:28
@laushinka laushinka marked this pull request as ready for review October 24, 2022 12:39
@laushinka laushinka requested a review from a team October 24, 2022 12:39
@laushinka laushinka requested a review from easyCZ October 24, 2022 12:39
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Oct 24, 2022
@easyCZ
Copy link
Member

easyCZ commented Oct 24, 2022

Moving this back to draft, we need to clean up a bit and rebase on #14124 before we can land this.

@laushinka laushinka force-pushed the lau/createstripesub-impl branch 2 times, most recently from 8271404 to e482b76 Compare October 27, 2022 11:01
@laushinka laushinka requested a review from easyCZ October 27, 2022 11:10
@laushinka laushinka force-pushed the lau/createstripesub-impl branch 2 times, most recently from 66b8ad5 to 7359f0b Compare October 28, 2022 08:45
@laushinka laushinka requested a review from jankeromnes October 28, 2022 08:47
@laushinka
Copy link
Contributor Author

laushinka commented Oct 28, 2022

Ready for the next round of review, but holding to comment out this test line.

/hold

@laushinka laushinka force-pushed the lau/createstripesub-impl branch 3 times, most recently from 6106a43 to f979de3 Compare October 31, 2022 11:04
@laushinka laushinka requested a review from easyCZ October 31, 2022 11:04
@laushinka
Copy link
Contributor Author

laushinka commented Oct 31, 2022

@easyCZ Changes incorporated. If you don't need the preview anymore, I'll remove the call in the server[1] and put back the original. ✅

@easyCZ
Copy link
Member

easyCZ commented Oct 31, 2022

@easyCZ Changes incorporated. If you don't need the preview anymore, I'll remove the call in the server[1] and put back the original.

I think it might be easier to add the feature flag in this PR such that you don't have to keep test commits around.

@laushinka
Copy link
Contributor Author

laushinka commented Oct 31, 2022

I think it might be easier to add the feature flag in this PR such that you don't have to keep test commits around.

I can understand and will keep that in mind for next tasks, but for this one I would like to have a separate feature flag PR[1] to avoid adding more things into this already big PR which has taken quite some time.

@laushinka laushinka force-pushed the lau/createstripesub-impl branch from f979de3 to b7a62fe Compare October 31, 2022 13:04
Copy link
Member

@easyCZ easyCZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great, almost there.

Comment on lines 191 to 198
priceIdentifier := getPriceIdentifier(attributionID, stripeCustomer, s)
if priceIdentifier == "" {
return nil, status.Errorf(codes.InvalidArgument, "Invalid currency %s for customer ID %s", stripeCustomer.Metadata["preferredCurrency"], stripeCustomer.ID)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because here you only check the empty string, and return an error. You can increase cohesion of this handling by pushing that error into the getPriceIdentifier. It would look like this:

Suggested change
priceIdentifier := getPriceIdentifier(attributionID, stripeCustomer, s)
if priceIdentifier == "" {
return nil, status.Errorf(codes.InvalidArgument, "Invalid currency %s for customer ID %s", stripeCustomer.Metadata["preferredCurrency"], stripeCustomer.ID)
}
priceID, err := getPriceIdentifier(attributionID, stripeCustomer, s)
if err != nil {
// note we're not wrapping the error, as the `getPriceIdentifier` can do that for us, with more context on the actual failure.
return nil, err
}

The getPriceIdentifier then becomes:

func getPriceIdentifier(attributionID db.AttributionID, stripeCustomer *stripe_api.Customer, s *BillingService) (string, error) {
	preferredCurrency := stripeCustomer.Metadata["preferredCurrency"]
	if stripeCustomer.Metadata["preferredCurrency"] == "" {
		log.
			WithField("stripe_customer_id", stripeCustomer.ID).
			Warn("No preferred currency set. Defaulting to USD")
	}

	entity, _ := attributionID.Values()

	switch entity {
		case db.AttributionEntity_User:
			switch preferredCurrency {
				case "EUR":
					return s.stripePrices.IndividualUsagePriceIDs.EUR, nil
				default:
					return s.stripePrices.IndividualUsagePriceIDs.USD, nil
			}

		case AttributionEntity_Team:
			switch preferredCurrency {
				case "EUR":
					return s.stripePrices.TeamUsagePriceIDs.EUR, nil
				default:
					return s.stripePrices.TeamUsagePriceIDs.USD, nil
			}

		default:
			return "", status.Errorf(codes.InvalidArgument, "Invalid currency %s for customer ID %s", stripeCustomer.Metadata["preferredCurrency"], stripeCustomer.ID)
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this is much better, and good point about returning the error here instead.

@laushinka laushinka force-pushed the lau/createstripesub-impl branch from 70cd0b7 to f6d0b91 Compare November 1, 2022 10:30
@laushinka laushinka requested a review from easyCZ November 1, 2022 11:04
Copy link
Member

@easyCZ easyCZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @laushinka.

@laushinka
Copy link
Contributor Author

@laushinka
Copy link
Contributor Author

Thanks a lot, @easyCZ! Will squash and remove hold.

@laushinka laushinka force-pushed the lau/createstripesub-impl branch from f6d0b91 to cdb2d9e Compare November 1, 2022 14:18
@roboquat roboquat merged commit ee8354a into main Nov 1, 2022
@roboquat roboquat deleted the lau/createstripesub-impl branch November 1, 2022 15:15
@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production release-note-none size/L team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[usage] Move Stripe handling from server to usage
4 participants