@task(queue='background_queue', ignore_result=True)
def send_autopay_failed(invoice, payment_method):
    subscription = invoice.subscription
    auto_payer = subscription.account.auto_pay_user
    payment_method = StripePaymentMethod.objects.get(web_user=auto_payer)
    autopay_card = payment_method.get_autopay_card(subscription.account)
    try:
        recipient = WebUser.get_by_username(auto_payer).get_email()
    except ResourceNotFound:
        recipient = auto_payer
    domain = invoice.get_domain()

    context = {
        'domain': domain,
        'subscription_plan': subscription.plan_version.plan.name,
        'billing_date': datetime.date.today(),
        'invoice_number': invoice.invoice_number,
        'autopay_card': autopay_card,
        'domain_url': absolute_reverse('dashboard_default', args=[domain]),
        'billing_info_url': absolute_reverse('domain_update_billing_info', args=[domain]),
        'support_email': settings.INVOICING_CONTACT_EMAIL,
    }

    template_html = 'accounting/autopay_failed_email.html'
    template_plaintext = 'accounting/autopay_failed_email.txt'

    send_HTML_email(
        subject=invoice.invoice_number % "Subscription Payment for CommCare Invoice %s was declined",
        recipient=recipient,
        html_content=render_to_string(template_html, context),
        text_content=render_to_string(template_plaintext, context),
        email_from=get_dimagi_from_email_by_product(subscription.plan_version.product_rate.product.product_type),
    )


# Email this out every Monday morning.
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/accounting/tasks.py/send_autopay_failed/swapped operands of `%`