This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
feat: Add ACH payment method #1083
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
058e85f
feat: Add stripe setupIntent api
suejung-sentry 0d80364
add end to end ach items
suejung-sentry 198f23e
move create-setup-intent to graphql api
suejung-sentry 5301ad6
cleanup and add tests
suejung-sentry c3528d6
Merge remote-tracking branch 'origin/main' into sshin/stripe-setup-in…
suejung-sentry 21a7792
Merge remote-tracking branch 'origin/main' into sshin/stripe-setup-in…
suejung-sentry 8ea30ac
incorporate PR comments
suejung-sentry 530136c
add logs
suejung-sentry 013e729
Merge remote-tracking branch 'origin/main' into sshin/stripe-setup-in…
suejung-sentry 3567fbb
fix lint
suejung-sentry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1228,6 +1228,46 @@ def test_update_email_address(self, modify_customer_mock, retrieve_mock): | |
| self.current_owner.stripe_customer_id, email=new_email | ||
| ) | ||
|
|
||
| @patch("services.billing.stripe.Subscription.retrieve") | ||
| @patch("services.billing.stripe.Customer.modify") | ||
| @patch("services.billing.stripe.PaymentMethod.modify") | ||
| @patch("services.billing.stripe.Customer.retrieve") | ||
| def test_update_email_address_with_propagate( | ||
| self, | ||
| customer_retrieve_mock, | ||
| payment_method_mock, | ||
| modify_customer_mock, | ||
| retrieve_mock, | ||
| ): | ||
| self.current_owner.stripe_customer_id = "flsoe" | ||
| self.current_owner.stripe_subscription_id = "djfos" | ||
| self.current_owner.save() | ||
|
|
||
| payment_method_id = "pm_123" | ||
| customer_retrieve_mock.return_value = { | ||
| "invoice_settings": {"default_payment_method": payment_method_id} | ||
| } | ||
|
|
||
| new_email = "[email protected]" | ||
| kwargs = { | ||
| "service": self.current_owner.service, | ||
| "owner_username": self.current_owner.username, | ||
| } | ||
| data = {"new_email": new_email, "should_propagate_to_payment_methods": True} | ||
| url = reverse("account_details-update-email", kwargs=kwargs) | ||
| response = self.client.patch(url, data=data, format="json") | ||
| assert response.status_code == status.HTTP_200_OK | ||
|
|
||
| modify_customer_mock.assert_called_once_with( | ||
| self.current_owner.stripe_customer_id, email=new_email | ||
| ) | ||
| customer_retrieve_mock.assert_called_once_with( | ||
| self.current_owner.stripe_customer_id | ||
| ) | ||
| payment_method_mock.assert_called_once_with( | ||
| payment_method_id, billing_details={"email": new_email} | ||
| ) | ||
|
|
||
| def test_update_billing_address_without_body(self): | ||
| kwargs = { | ||
| "service": self.current_owner.service, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
codecov_auth/commands/owner/interactors/create_stripe_setup_intent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import logging | ||
|
|
||
| import stripe | ||
|
|
||
| from codecov.commands.base import BaseInteractor | ||
| from codecov.commands.exceptions import Unauthenticated, Unauthorized, ValidationError | ||
| from codecov.db import sync_to_async | ||
| from codecov_auth.helpers import current_user_part_of_org | ||
| from codecov_auth.models import Owner | ||
| from services.billing import BillingService | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CreateStripeSetupIntentInteractor(BaseInteractor): | ||
| def validate(self, owner_obj: Owner) -> None: | ||
| if not self.current_user.is_authenticated: | ||
| raise Unauthenticated() | ||
| if not owner_obj: | ||
| raise ValidationError("Owner not found") | ||
| if not current_user_part_of_org(self.current_owner, owner_obj): | ||
| raise Unauthorized() | ||
|
|
||
| def create_setup_intent(self, owner_obj: Owner) -> stripe.SetupIntent: | ||
| try: | ||
| billing = BillingService(requesting_user=self.current_owner) | ||
| return billing.create_setup_intent(owner_obj) | ||
| except Exception as e: | ||
| log.error( | ||
| f"Error getting setup intent for owner {owner_obj.ownerid}", | ||
suejung-sentry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extra={"error": str(e)}, | ||
| ) | ||
| raise ValidationError("Unable to create setup intent") | ||
|
|
||
| @sync_to_async | ||
| def execute(self, owner: str) -> stripe.SetupIntent: | ||
| owner_obj = Owner.objects.filter(username=owner, service=self.service).first() | ||
suejung-sentry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.validate(owner_obj) | ||
| return self.create_setup_intent(owner_obj) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
graphql_api/tests/mutation/test_create_stripe_setup_intent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from django.test import TransactionTestCase | ||
| from shared.django_apps.core.tests.factories import OwnerFactory | ||
|
|
||
| from graphql_api.tests.helper import GraphQLTestHelper | ||
|
|
||
| query = """ | ||
| mutation($input: CreateStripeSetupIntentInput!) { | ||
| createStripeSetupIntent(input: $input) { | ||
| error { | ||
| __typename | ||
| } | ||
| clientSecret | ||
| } | ||
| } | ||
| """ | ||
|
|
||
|
|
||
| class CreateStripeSetupIntentTestCase(GraphQLTestHelper, TransactionTestCase): | ||
| def setUp(self): | ||
| self.owner = OwnerFactory(username="codecov-user") | ||
|
|
||
| def test_when_unauthenticated(self): | ||
| data = self.gql_request(query, variables={"input": {"owner": "somename"}}) | ||
| assert ( | ||
| data["createStripeSetupIntent"]["error"]["__typename"] | ||
| == "UnauthenticatedError" | ||
| ) | ||
|
|
||
| def test_when_unauthorized(self): | ||
| other_owner = OwnerFactory(username="other-user") | ||
| data = self.gql_request( | ||
| query, | ||
| owner=self.owner, | ||
| variables={"input": {"owner": other_owner.username}}, | ||
| ) | ||
| assert ( | ||
| data["createStripeSetupIntent"]["error"]["__typename"] | ||
| == "UnauthorizedError" | ||
| ) | ||
|
|
||
| @patch("services.billing.stripe.SetupIntent.create") | ||
| def test_when_validation_error(self, setup_intent_create_mock): | ||
| setup_intent_create_mock.side_effect = Exception("Some error") | ||
| data = self.gql_request( | ||
| query, owner=self.owner, variables={"input": {"owner": self.owner.username}} | ||
| ) | ||
| assert ( | ||
| data["createStripeSetupIntent"]["error"]["__typename"] == "ValidationError" | ||
| ) | ||
|
|
||
| def test_when_owner_not_found(self): | ||
| data = self.gql_request( | ||
| query, owner=self.owner, variables={"input": {"owner": "nonexistent-user"}} | ||
| ) | ||
| assert ( | ||
| data["createStripeSetupIntent"]["error"]["__typename"] == "ValidationError" | ||
| ) | ||
|
|
||
| @patch("services.billing.stripe.SetupIntent.create") | ||
| def test_success(self, setup_intent_create_mock): | ||
| setup_intent_create_mock.return_value = {"client_secret": "test-client-secret"} | ||
| data = self.gql_request( | ||
| query, owner=self.owner, variables={"input": {"owner": self.owner.username}} | ||
| ) | ||
| assert data["createStripeSetupIntent"]["clientSecret"] == "test-client-secret" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| input CreateStripeSetupIntentInput { | ||
| owner: String! | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
graphql_api/types/mutation/create_stripe_setup_intent/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from graphql_api.helpers.ariadne import ariadne_load_local_graphql | ||
|
|
||
| from .create_stripe_setup_intent import ( | ||
| error_create_stripe_setup_intent, | ||
| resolve_create_stripe_setup_intent, | ||
| ) | ||
|
|
||
| gql_create_stripe_setup_intent = ariadne_load_local_graphql( | ||
| __file__, "create_stripe_setup_intent.graphql" | ||
| ) | ||
|
|
||
| __all__ = ["error_create_stripe_setup_intent", "resolve_create_stripe_setup_intent"] |
6 changes: 6 additions & 0 deletions
6
graphql_api/types/mutation/create_stripe_setup_intent/create_stripe_setup_intent.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| union CreateStripeSetupIntentError = UnauthenticatedError | UnauthorizedError | ValidationError | ||
|
|
||
| type CreateStripeSetupIntentPayload { | ||
| error: CreateStripeSetupIntentError | ||
| clientSecret: String | ||
| } |
24 changes: 24 additions & 0 deletions
24
graphql_api/types/mutation/create_stripe_setup_intent/create_stripe_setup_intent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from typing import Any, Dict | ||
|
|
||
| from ariadne import UnionType | ||
| from ariadne.types import GraphQLResolveInfo | ||
|
|
||
| from graphql_api.helpers.mutation import ( | ||
| resolve_union_error_type, | ||
| wrap_error_handling_mutation, | ||
| ) | ||
|
|
||
|
|
||
| @wrap_error_handling_mutation | ||
| async def resolve_create_stripe_setup_intent( | ||
| _: Any, info: GraphQLResolveInfo, input: Dict[str, str] | ||
| ) -> Dict[str, str]: | ||
| command = info.context["executor"].get_command("owner") | ||
| resp = await command.create_stripe_setup_intent(input.get("owner")) | ||
| return { | ||
| "client_secret": resp["client_secret"], | ||
| } | ||
|
|
||
|
|
||
| error_create_stripe_setup_intent = UnionType("CreateStripeSetupIntentError") | ||
| error_create_stripe_setup_intent.type_resolver(resolve_union_error_type) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.