Skip to content

Commit

Permalink
#1295 show customer balanse alongside recent cancellations
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Feb 10, 2024
1 parent e7efe0c commit 1cddf86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
9 changes: 9 additions & 0 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,15 @@ def show_recent_subscription_cancellations():
limit=100,
types=["customer.subscription.deleted"],
)
for event in subscription_cancellations.auto_paging_iter():
log.info("appending event")
log.info(
f"Length of subscription_cancellations.data is {len(subscription_cancellations.data)}" # noqa: E501
)
if len(subscription_cancellations.data) > 100:
break
subscription_cancellations.data.append(event)

cancellations = []
# subscription id
for index, value in enumerate(subscription_cancellations):
Expand Down
26 changes: 16 additions & 10 deletions subscribie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ def get_subscriptions(self, include_archived=True):

return subscriptions

def balance(self):
def balance(self, skipFetchDeclineCode=False):
"""Return the customer balance
Customer balance is the total charged - total collected over the lifetime
of their account.
"""
total_charged = 0
total_collected = 0
customer_balance = 0
invoices = self.invoices()
invoices = self.invoices(skipFetchDeclineCode=skipFetchDeclineCode)
for invoice in invoices:
total_charged += invoice.amount_due

Expand All @@ -192,7 +192,7 @@ def balance(self):
"customer_balance": customer_balance,
}

def invoices(self, refetchCachedStripeInvoices=False):
def invoices(self, refetchCachedStripeInvoices=False, skipFetchDeclineCode=False):
"""Get all cached Stripe invoices for a given person
NOTE: This is a **cached** view of Stripe invoices,
Expand All @@ -217,7 +217,9 @@ def invoices(self, refetchCachedStripeInvoices=False):

stripe.api_key = get_stripe_secret_key()
stripe_account_id = get_stripe_connect_account_id()
query = database.session.query(StripeInvoice).execution_options(include_archived=True)
query = database.session.query(StripeInvoice).execution_options(
include_archived=True
)
query = query.join(
Subscription,
StripeInvoice.subscribie_subscription_id == Subscription.id, # noqa: E501
Expand All @@ -230,12 +232,16 @@ def invoices(self, refetchCachedStripeInvoices=False):
setattr(invoice, "created", stripeRawInvoice["created"])
# Get stripe_decline_code if possible
try:
payment_intent_id = stripeRawInvoice["payment_intent"]
stripe_decline_code = stripe.PaymentIntent.retrieve(
payment_intent_id,
stripe_account=stripe_account_id,
).last_payment_error.decline_code
setattr(invoice, "stripe_decline_code", stripe_decline_code)
if skipFetchDeclineCode is not True:
payment_intent_id = stripeRawInvoice["payment_intent"]
stripe_decline_code = stripe.PaymentIntent.retrieve(
payment_intent_id,
stripe_account=stripe_account_id,
).last_payment_error.decline_code
setattr(invoice, "stripe_decline_code", stripe_decline_code)
else:
setattr(invoice, "stripe_decline_code", "unknown")

except Exception as e:
log.debug(
f"Failed to get stripe_decline_code for invoice {invoice.id}. Exeption: {e}" # noqa: E501
Expand Down

0 comments on commit 1cddf86

Please sign in to comment.