Switch invoice download to API endpoint
This starts using the recently added Go API endpoint for invoice downloads in the front-end. This removes all usage of weasyprint from meta.sr.ht.
This commit is contained in:
parent
835f74056f
commit
68fc1966da
|
@ -1,3 +1,4 @@
|
||||||
|
import requests
|
||||||
import stripe
|
import stripe
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from flask import Blueprint, render_template, request, redirect
|
from flask import Blueprint, render_template, request, redirect
|
||||||
|
@ -7,7 +8,8 @@ from metasrht.billing import charge_user
|
||||||
from metasrht.types import User, UserType, PaymentInterval, Invoice
|
from metasrht.types import User, UserType, PaymentInterval, Invoice
|
||||||
from metasrht.webhooks import deliver_profile_update
|
from metasrht.webhooks import deliver_profile_update
|
||||||
from sqlalchemy import and_
|
from sqlalchemy import and_
|
||||||
from srht.config import cfg
|
from srht.config import cfg, get_origin
|
||||||
|
from srht.crypto import encrypt_request_authorization
|
||||||
from srht.database import db
|
from srht.database import db
|
||||||
from srht.flask import session
|
from srht.flask import session
|
||||||
from srht.oauth import current_user, loginrequired, freshen_user
|
from srht.oauth import current_user, loginrequired, freshen_user
|
||||||
|
@ -213,38 +215,13 @@ def invoice_GET(invoice_id):
|
||||||
@billing.route("/billing/invoice/<int:invoice_id>", methods=["POST"])
|
@billing.route("/billing/invoice/<int:invoice_id>", methods=["POST"])
|
||||||
@loginrequired
|
@loginrequired
|
||||||
def invoice_POST(invoice_id):
|
def invoice_POST(invoice_id):
|
||||||
invoice = Invoice.query.filter(Invoice.id == invoice_id).one_or_none()
|
origin = cfg("meta.sr.ht", "api-origin", default=get_origin("meta.sr.ht"))
|
||||||
if not invoice:
|
headers = {
|
||||||
abort(404)
|
"X-Forwarded-For": ", ".join(request.access_route),
|
||||||
if (invoice.user_id != current_user.id
|
**encrypt_request_authorization(user=current_user),
|
||||||
and current_user.user_type != UserType.admin):
|
}
|
||||||
abort(401)
|
r = requests.post(f"{origin}/query/invoice/{invoice_id}",
|
||||||
valid = Validation(request)
|
headers=headers, data=request.get_data())
|
||||||
bill_to = valid.optional("address-to")
|
filename = f"invoice_{invoice_id}.pdf"
|
||||||
if not bill_to:
|
|
||||||
bill_to = "~" + invoice.user.username
|
|
||||||
bill_from = [l for l in [
|
|
||||||
cfg("meta.sr.ht::billing", "address-line1", default=None),
|
|
||||||
cfg("meta.sr.ht::billing", "address-line2", default=None),
|
|
||||||
cfg("meta.sr.ht::billing", "address-line3", default=None),
|
|
||||||
cfg("meta.sr.ht::billing", "address-line4", default=None)
|
|
||||||
] if l]
|
|
||||||
|
|
||||||
# Split bill_to to first row (rendered as heading) and others
|
|
||||||
[bill_from_head, *bill_from_tail] = bill_from or [None]
|
|
||||||
|
|
||||||
html = render_template("billing-invoice-pdf.html",
|
|
||||||
invoice=invoice,
|
|
||||||
amount=f"${invoice.cents / 100:.2f}",
|
|
||||||
source=invoice.source,
|
|
||||||
created=invoice.created.strftime("%Y-%m-%d"),
|
|
||||||
valid_thru=invoice.valid_thru.strftime("%Y-%m-%d"),
|
|
||||||
bill_to=bill_to,
|
|
||||||
bill_from_head=bill_from_head,
|
|
||||||
bill_from_tail=bill_from_tail)
|
|
||||||
|
|
||||||
pdf = HTML(string=html).write_pdf()
|
|
||||||
|
|
||||||
filename = f"invoice_{invoice.id}.pdf"
|
|
||||||
headers = [('Content-Disposition', f'attachment; filename="{filename}"')]
|
headers = [('Content-Disposition', f'attachment; filename="{filename}"')]
|
||||||
return Response(pdf, mimetype="application/pdf", headers=headers)
|
return Response(r.content, mimetype="application/pdf", headers=headers)
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>sr.ht subscription invoice</title>
|
|
||||||
<style type="text/css">
|
|
||||||
* {
|
|
||||||
font-family: "Courier New", Courier, monospace;
|
|
||||||
font-size: 14px;
|
|
||||||
}
|
|
||||||
h1 {
|
|
||||||
font-size: 1.6rem;
|
|
||||||
margin-bottom: .5rem;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
{% if bill_from_head %}
|
|
||||||
<h1>{{ bill_from_head }}</h1>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% for line in bill_from_tail %}
|
|
||||||
{{ line }}
|
|
||||||
{% if not loop.last %}<br />{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
<div style="float: right">
|
|
||||||
<strong>
|
|
||||||
Invoice #{{ invoice.id }}
|
|
||||||
</strong><br />
|
|
||||||
Issued {{ created }}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
|
|
||||||
<hr />
|
|
||||||
|
|
||||||
<dl>
|
|
||||||
<dt>Service</dt>
|
|
||||||
<dd>sr.ht subscription fee</dd>
|
|
||||||
<dt>Amount</dt>
|
|
||||||
<dd>{{ amount }}</dd>
|
|
||||||
<dt>Paid with</dt>
|
|
||||||
<dd>{{ source }}</dd>
|
|
||||||
<dt>Paid on</dt>
|
|
||||||
<dd>{{ created }}</dd>
|
|
||||||
<dt>Valid for service thru</dt>
|
|
||||||
<dd>{{ valid_thru }}</dd>
|
|
||||||
</dl>
|
|
||||||
|
|
||||||
<hr />
|
|
||||||
|
|
||||||
<p><strong>Invoice to</strong>:</p>
|
|
||||||
<pre>{{ bill_to }}</pre>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
Loading…
Reference in New Issue