Skip to content
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

Fix #1256 Shop owner failed payment email shows plan title #1259

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ wheel
Flask>=2,<3
flask_cors
Flask-Reuploaded==0.3.2
Flask-WTF==1.0.0
Flask-WTF==1.2.1
email-validator==1.1.3
Flask-Mail>=0.9.1
requests==2.31.0
Expand Down
37 changes: 36 additions & 1 deletion subscribie/blueprints/checkout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ def stripe_webhook():

See https://github.com/Subscribie/subscribie/issues/352
"""
connect_account_id = get_stripe_connect_account_id()
event = request.json
is_donation = False

Expand All @@ -824,22 +825,56 @@ def stripe_webhook():
# Handle the payment_intent.payment_failed
if event["type"] == "payment_intent.payment_failed":
log.info("Stripe webhook event: payment_intent.payment_failed")
plan_title = None
try:
# Get plan metadata by retrieving the stripe invoice id
# and then the metadata (stripe ultimatly puts plan metadata
# on the invoice->lines->data[0] object. Metadata is not available
# directly on the payment_intent.payment_failed event, hence the need
# to extract and fetch the stripe invoice id -> invoice and work backwards
# to identify the associated Subscribie plan.
eventObj = event["data"]["object"]
stripe_invoice_id = eventObj["invoice"]
try:
stripe_invoice = stripe.Invoice.retrieve(
stripe_invoice_id, stripe_account=connect_account_id
) # noqa: E501
stripe_invoice_metadata = stripe_invoice.lines.data[0]["metadata"]
subscribie_plan_uuid = stripe_invoice_metadata.plan_uuid
plan = (
Plan.query.filter_by(uuid=subscribie_plan_uuid)
.execution_options(include_archived=True)
.first()
) # noqa: E501
if plan is None:
log.warning(
f"Plan not found from invoice metadata. Stripe invoice_id: {stripe_invoice_id}" # noqa: E501
)
else:
plan_title = plan.title
except Exception as e:
log.error(
f"Error whilst extracting invoice from payment_intent.payment_failed event. {e}." # noqa: E501
)
stripe_invoice_id = eventObj["id"]

log.info(eventObj)
personName = eventObj["charges"]["data"][0]["billing_details"]["name"]
personEmail = eventObj["charges"]["data"][0]["billing_details"]["email"]
# Notify Shop owner if payment_failed event was related to a Subscription charge # noqa: E501
if eventObj["charges"]["data"][0]["description"] == "Subscription update":
emailBody = f"""A recent subscription charge failed to be collected from Subscriber:\n\n{personName}\n\nEmail: {personEmail}\n\n
Plan title: {plan_title}\n\n
The failure code was: {eventObj['charges']['data'][0]['failure_code']}\n\n
The failure message was: {eventObj['charges']['data'][0]['failure_message']}\n\n
Please note, payments are automatically retried and no action is required unless you wish to pause or stop the subscription from your admin dashboard.""" # noqa: E501
log.info(emailBody)
shop_admins = [user.email for user in User.query.all()]
company = Company.query.first()
msg = EmailMessageQueue()
msg["Subject"] = company.name + " " + "A payment collection failed"
msg["Subject"] = (
company.name + " " + f"A payment collection failed. {plan_title}"
) # noqa: E501
msg["FROM"] = current_app.config["EMAIL_LOGIN_FROM"]
msg["TO"] = shop_admins
msg.set_content(emailBody)
Expand Down
9 changes: 8 additions & 1 deletion subscribie/themes/theme-builder/builder/faq.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ <h3>What is my shop URL?</h3>
</div>
</div>

<div class="row">
<div class="col-sm-12">
<h3>Will I receive a domain when I sign up?</h3>
<p>After signing up and creating your shop on Subscribie, a shop will be created for you with an automated domain containing the name of your business.</p>
</div>
</div>

<div class="row">
<div class="col-sm-12">
<h3>Can I use Subscribie with my existing website?</h3>
Expand Down Expand Up @@ -85,7 +92,7 @@ <h3>When I get orders, how will I see them and track them?</h3>
<h3>Can I take order notes from customers?</h3>
<p>
Yes. For each subscription plan you sell, you can choose to request customers
leave a note alongside their order. You can then see these on you shop
leave a note alongside their order. You can then see these on your shop
dashboard under 'order notes'.
</p>
</div>
Expand Down
Loading