Skip to content

Commit

Permalink
Remove explicit support for Source and SourceTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
jleclanche committed Apr 25, 2024
1 parent b05407f commit 3d31567
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 389 deletions.
25 changes: 0 additions & 25 deletions djstripe/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,31 +518,6 @@ def get_queryset(self, request):
return super().get_queryset(request).select_related("charge")


@admin.register(models.Source)
class SourceAdmin(StripeModelAdmin):
list_display = ("customer", "type", "status", "amount", "currency", "usage", "flow")
list_filter = ("type", "status", "usage", "flow")

def get_queryset(self, request):
return (
super()
.get_queryset(request)
.select_related("customer", "customer__subscriber")
)


@admin.register(models.SourceTransaction)
class SourceTransactionAdmin(StripeModelAdmin):
list_display = ("status", "amount", "currency")
list_filter = (
"status",
"source__id",
"source__customer",
"source__customer__subscriber",
)
list_select_related = ("source", "source__customer", "source__customer__subscriber")


@admin.register(models.PaymentMethod)
class PaymentMethodAdmin(StripeModelAdmin):
list_display = ("customer", "type", "billing_details")
Expand Down
68 changes: 3 additions & 65 deletions djstripe/event_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from djstripe.settings import djstripe_settings

from . import models
from .enums import PayoutType, SourceType
from .enums import PayoutType
from .signals import WEBHOOK_SIGNALS
from .utils import convert_tstamp

Expand Down Expand Up @@ -166,35 +166,6 @@ def handle_customer_discount_event(sender, event, **kwargs):
customer.save()


@djstripe_receiver("customer.source.created")
@djstripe_receiver("customer.source.deleted")
@djstripe_receiver("customer.source.expiring")
@djstripe_receiver("customer.source.updated")
def handle_customer_source_event(sender, event, **kwargs):
"""Handle updates to customer payment-source objects.
Docs: https://stripe.com/docs/api/sources
"""
customer_data = event.data.get("object", {})
source_type = customer_data.get("object", {})

# TODO: handle other types of sources
# (https://stripe.com/docs/api/sources)
if source_type == SourceType.card:
if event.verb.endswith("deleted") and customer_data:
# On customer.source.deleted, we do not delete the object,
# we merely unlink it.
# customer = Customer.objects.get(id=customer_data["id"])
# NOTE: for now, customer.sources still points to Card
# Also, https://github.com/dj-stripe/dj-stripe/issues/576
models.Card.objects.filter(id=customer_data.get("id", "")).delete()
models.DjstripePaymentMethod.objects.filter(
id=customer_data.get("id", "")
).delete()
else:
_handle_crud_like_event(target_cls=models.Card, event=event)


@djstripe_receiver("customer.subscription.created")
@djstripe_receiver("customer.subscription.deleted")
@djstripe_receiver("customer.subscription.paused")
Expand Down Expand Up @@ -350,37 +321,6 @@ def handle_charge_dispute_event(sender, event, **kwargs):
_handle_crud_like_event(target_cls=models.Dispute, event=event)


@djstripe_receiver("source.canceled")
@djstripe_receiver("source.chargeable")
@djstripe_receiver("source.failed")
@djstripe_receiver("source.mandate_notification")
@djstripe_receiver("source.refund_attributes_required")
def handle_source_event(sender, event, **kwargs):
"""Handle updates to Source objects
- charge: https://stripe.com/docs/api/charges
"""
# will recieve all events of the type source.X.Y so
# need to ensure the data object is related to Source Object
target_object_type = event.data.get("object", {}).get("object", {})

if target_object_type == "source":
_handle_crud_like_event(target_cls=models.Source, event=event)


@djstripe_receiver("source.transaction.created")
@djstripe_receiver("source.transaction.updated")
def handle_source_transaction_event(sender, event, **kwargs):
"""Handle updates to Source objects
- charge: https://stripe.com/docs/api/charges
"""
# will recieve all events of the type source.transaction.Y so
# need to ensure the data object is related to SourceTransaction Object
target_object_type = event.data.get("object", {}).get("object", {})

if target_object_type == "source_transaction":
_handle_crud_like_event(target_cls=models.SourceTransaction, event=event)


@djstripe_receiver("checkout.session.async_payment_failed")
@djstripe_receiver("checkout.session.async_payment_succeeded")
@djstripe_receiver("checkout.session.completed")
Expand Down Expand Up @@ -585,10 +525,8 @@ def _handle_crud_like_event(
if event.parts[:2] == ["account", "external_account"] and stripe_account:
kwargs["account"] = models.Account._get_or_retrieve(id=stripe_account)

# Stripe doesn't allow direct retrieval of Discount and SourceTransaction Objects
# indirect retrieval via Source will not work as SourceTransaction will not have a source attached in
# source.transaction.created event
if target_cls not in (models.Discount, models.SourceTransaction):
# Stripe doesn't allow direct retrieval of Discount Objects
if target_cls not in (models.Discount,):
data = target_cls(**kwargs).api_retrieve(
stripe_account=stripe_account, api_key=event.default_api_key
)
Expand Down
44 changes: 0 additions & 44 deletions djstripe/management/commands/djstripe_sync_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ def _should_sync_model(self, model):
if model in (
models.ApplicationFeeRefund,
models.LineItem,
models.Source,
models.SourceTransaction,
models.TransferReversal,
models.TaxId,
models.UsageRecordSummary,
Expand Down Expand Up @@ -352,46 +350,6 @@ def get_list_kwargs_pm(default_list_kwargs):

return all_list_kwargs

@staticmethod
def get_list_kwargs_src(default_list_kwargs):
"""Returns sequence of kwargs to sync Sources for
all Stripe Accounts"""

all_list_kwargs = []
for def_kwarg in default_list_kwargs:
stripe_account = def_kwarg.get("stripe_account")
api_key = def_kwarg.get("api_key")
for stripe_customer in models.Customer.api_list(
stripe_account=stripe_account, api_key=api_key
):
all_list_kwargs.append({"id": stripe_customer.id, **def_kwarg})

return all_list_kwargs

@staticmethod
def get_list_kwargs_srctxn(default_list_kwargs):
"""Returns sequence of kwargs to sync SourceTransactions for
all Stripe Accounts"""

all_list_kwargs = []
for def_kwarg in default_list_kwargs:
stripe_account = def_kwarg.get("stripe_account")
api_key = def_kwarg.get("api_key")
for stripe_customer in models.Customer.api_list(
stripe_account=stripe_account, api_key=api_key
):
all_list_kwargs.append({"id": stripe_customer.id, **def_kwarg})

# fetch all Sources associated with the current customer instance
for source in models.Customer.stripe_class.list_sources(
id=stripe_customer.id,
stripe_account=stripe_account,
object="source",
api_key=api_key,
).auto_paging_iter():
all_list_kwargs.append({"id": source.id, **def_kwarg})
return all_list_kwargs

@staticmethod
def get_list_kwargs_si(default_list_kwargs):
"""Returns sequence of kwargs to sync Subscription Items for
Expand Down Expand Up @@ -505,8 +463,6 @@ def get_list_kwargs(self, model, api_key: str):
list_kwarg_handlers_dict = {
"LineItem": self.get_list_kwargs_il,
"PaymentMethod": self.get_list_kwargs_pm,
"Source": self.get_list_kwargs_src,
"SourceTransaction": self.get_list_kwargs_srctxn,
"SubscriptionItem": self.get_list_kwargs_si,
"CountrySpec": self.get_list_kwargs_country_spec,
"TransferReversal": self.get_list_kwargs_trr,
Expand Down
11 changes: 1 addition & 10 deletions djstripe/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@
SetupIntent,
)
from .identity import VerificationReport, VerificationSession
from .payment_methods import (
BankAccount,
Card,
DjstripePaymentMethod,
PaymentMethod,
Source,
SourceTransaction,
)
from .payment_methods import BankAccount, Card, DjstripePaymentMethod, PaymentMethod
from .sigma import ScheduledQueryRun
from .webhooks import WebhookEndpoint, WebhookEventTrigger

Expand Down Expand Up @@ -91,8 +84,6 @@
"ScheduledQueryRun",
"SetupIntent",
"Session",
"Source",
"SourceTransaction",
"StripeModel",
"Subscription",
"SubscriptionItem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,6 @@ def update_fixture_obj(
created, obj = self.get_or_create_stripe_card(
old_obj=old_obj, readonly_fields=readonly_fields
)
elif issubclass(model_class, djstripe.models.Source):
created, obj = self.get_or_create_stripe_source(
old_obj=old_obj, readonly_fields=readonly_fields
)
elif issubclass(model_class, djstripe.models.Invoice):
created, obj = self.get_or_create_stripe_invoice(
old_obj=old_obj, writable_fields=["metadata"]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
Plan,
Price,
Product,
Source,
Subscription,
)
from djstripe.models.payment_methods import Source
from djstripe.settings import djstripe_settings

from . import (
Expand Down
123 changes: 0 additions & 123 deletions tests/test_source.py

This file was deleted.

Loading

0 comments on commit 3d31567

Please sign in to comment.