Skip to content

Commit

Permalink
Fix #1343 error handle SubscribieStripeNotConnectedYet if offline str…
Browse files Browse the repository at this point in the history
…ipe / stripe not connected
  • Loading branch information
chrisjsimpson committed Jun 4, 2024
1 parent ae67112 commit a3e26e3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 46 deletions.
95 changes: 55 additions & 40 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jinja2
import requests
from subscribie.utils import (
SubscribieStripeNotConnectedYet,
get_stripe_secret_key,
get_stripe_connect_account,
create_stripe_connect_account,
Expand Down Expand Up @@ -488,9 +489,15 @@ def dashboard():
num_signups = get_number_of_signups()
num_one_off_purchases = get_number_of_one_off_purchases()
num_donations = get_number_of_transactions_with_donations()
num_recent_subscription_cancellations = (
get_number_of_recent_subscription_cancellations()
)

try:
num_recent_subscription_cancellations = (
get_number_of_recent_subscription_cancellations()
)
except stripe._error.APIConnectionError as e:
log.error(f"stripe._error.APIConnectionError: {e}")
num_recent_subscription_cancellations = "unknown"

shop_default_country_code = get_shop_default_country_code()
saas_url = current_app.config.get("SAAS_URL")

Expand Down Expand Up @@ -991,8 +998,9 @@ def stripe_connect():
account = get_stripe_connect_account()
if account is not None and account.charges_enabled and account.payouts_enabled:
payment_provider.stripe_active = True
else:
except SubscribieStripeNotConnectedYet as e:
payment_provider.stripe_active = False

except (
stripe.error.PermissionError,
stripe.error.InvalidRequestError,
Expand Down Expand Up @@ -1132,6 +1140,7 @@ def stripe_onboarding():
shop_is_changing_default_currency = True
raise AttributeError
except (
SubscribieStripeNotConnectedYet,
stripe.error.PermissionError,
stripe.error.InvalidRequestError,
AttributeError,
Expand Down Expand Up @@ -1307,49 +1316,55 @@ def show_recent_subscription_cancellations():
see StripeInvoice for possible improvements
"""
stripe.api_key = get_stripe_secret_key()
connect_account = get_stripe_connect_account()

subscription_cancellations = stripe.Event.list(
stripe_account=connect_account.id,
limit=100,
types=["customer.subscription.deleted"],
)

cancellations = []
# subscription id
for index, value in enumerate(subscription_cancellations):
# Get Person
person = (
Person.query.execution_options(include_archived=True)
.filter_by(uuid=value.data.object.metadata.person_uuid)
.one_or_none()
try:
connect_account = get_stripe_connect_account()
stripe_connected = True
subscription_cancellations = stripe.Event.list(
stripe_account=connect_account.id,
limit=100,
types=["customer.subscription.deleted"],
)
if person is None:
log.info(
f"""Person query retruned None- probably archived.\n
Skipping Person with uuid {value.data.object.metadata.person_uuid}"""

# subscription id
for index, value in enumerate(subscription_cancellations):
# Get Person
person = (
Person.query.execution_options(include_archived=True)
.filter_by(uuid=value.data.object.metadata.person_uuid)
.one_or_none()
)
continue
if person is None:
log.info(
f"""Person query retruned None- probably archived.\n
Skipping Person with uuid {value.data.object.metadata.person_uuid}"""
)
continue

# Get Subscription
subscription = (
Subscription.query.execution_options(include_archived=True)
.filter_by(stripe_subscription_id=value.data.object.id)
.one()
)
cancellation_date = value.data.object.canceled_at
cancellation_reason = value.data.object.cancellation_details.reason
cancellations.append(
{
"subscription": subscription,
"person": person,
"cancellation_date": cancellation_date,
"cancellation_reason": cancellation_reason,
}
)
except SubscribieStripeNotConnectedYet as e:
log.error(f"SubscribieStripeNotConnectedYet: {e}")
stripe_connected = False

# Get Subscription
subscription = (
Subscription.query.execution_options(include_archived=True)
.filter_by(stripe_subscription_id=value.data.object.id)
.one()
)
cancellation_date = value.data.object.canceled_at
cancellation_reason = value.data.object.cancellation_details.reason
cancellations.append(
{
"subscription": subscription,
"person": person,
"cancellation_date": cancellation_date,
"cancellation_reason": cancellation_reason,
}
)
return render_template(
"admin/recent_subscription_cancellations.html",
cancellations=cancellations,
stripe_connected=stripe_connected
)


Expand Down
14 changes: 9 additions & 5 deletions subscribie/blueprints/admin/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ def get_number_of_recent_subscription_cancellations():
stripe.api_key = get_stripe_secret_key()
connect_account_id = get_stripe_connect_account_id()

subscription_cancellations = stripe.Event.list(
stripe_account=connect_account_id,
limit=100,
types=["customer.subscription.deleted"],
)
try:
subscription_cancellations = stripe.Event.list(
stripe_account=connect_account_id,
limit=100,
types=["customer.subscription.deleted"],
)
except stripe._error.APIConnectionError as e:
log.error(f"stripe._error.APIConnectionError: {e}")
raise

return len(subscription_cancellations)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ <h2 class="text-center text-dark mb-3">Recent Subscription Cancellations</h2>
<div class="section">
<div class="container">

{% if stripe_connected is sameas False %}
Stripe connection is not yet complete.
<a class="btn btn-warning"
href="{{ url_for('admin.stripe_connect') }}">
Review Stripe
</a>
{% else %}
<p>
Below is the list of recent subscription cancellations (if any) within the last 30 days.
</p>
Expand Down Expand Up @@ -76,6 +83,7 @@ <h3>Reason Code</h3>
{% endfor %}
</tbody>
</table>
{% endif %}
</div> <!-- end .container -->
</div><!-- end .section -->
</main>
Expand Down
11 changes: 10 additions & 1 deletion subscribie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
}


class SubscribieStripeNotConnectedYet(Exception):
"""
Exception to raise/inform when Subscribie shop is not
yet connected to Stripe but client code attempts
to get information from Stripe apis.
"""
pass


def get_geo_currency_code():
"""Return currency code based on current detected (or selected)
country code.
Expand Down Expand Up @@ -181,7 +190,7 @@ def get_stripe_connect_account():
account_id = payment_provider.stripe_test_connect_account_id

if account_id is None or account_id == "":
return None
raise SubscribieStripeNotConnectedYet()

try:
account = stripe.Account.retrieve(account_id)
Expand Down

0 comments on commit a3e26e3

Please sign in to comment.