Add support for invoices

This commit is contained in:
Drew DeVault 2019-02-06 09:05:20 -05:00
parent b667230570
commit 85ce9c5327
4 changed files with 100 additions and 0 deletions

View File

@ -295,3 +295,10 @@ def cancel(id):
db.commit()
send_cancellation_notice(user, donation)
return redirect("/panel")
@html.route("/invoice/<id>")
def invoice(id):
invoice = Invoice.query.filter(Invoice.external_id == id).first()
if not invoice:
abort(404)
return render_template("invoice.html", invoice=invoice)

View File

@ -8,6 +8,7 @@ from .database import Base
from datetime import datetime
from enum import Enum
import bcrypt
import binascii
import os
import hashlib
@ -99,3 +100,15 @@ class Project(Base):
def __repr__(self):
return "<Project {} {}>".format(self.id, self.name)
class Invoice(Base):
__tablename__ = 'invoices'
id = Column(Integer, primary_key=True)
created = Column(DateTime, nullable=False)
external_id = Column(String(16), index=True)
amount = Column(Integer, nullable=False)
comment = Column(String(512), nullable=False)
def __init__(self):
self.external_id = binascii.hexlify(os.urandom(8)).decode()
self.created = datetime.now()

20
invoice Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python3
from fosspay.database import db
from fosspay.objects import Invoice
from fosspay.config import _cfg
import sys
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <amount in cents> <comment>")
sys.exit(1)
amount = int(sys.argv[1])
comment = sys.argv[2]
invoice = Invoice()
invoice.amount = amount
invoice.comment = comment
db.add(invoice)
db.commit()
print(f"{_cfg('protocol')}://{_cfg('domain')}/invoice/{invoice.external_id}")

60
templates/invoice.html Normal file
View File

@ -0,0 +1,60 @@
{% extends "layout.html" %}
{% block scripts %}
<script>
window.stripe_key = "{{ _cfg("stripe-publish") }}";
window.your_name = "{{ _cfg("your-name") }}";
window.amount = {{invoice.amount}};
window.invoice = "{{invoice.external_id}}";
window.comment = "{{invoice.comment}}";
const currency = "{{ _cfg("currency") }}";
</script>
<script src="//checkout.stripe.com/checkout.js"></script>
<script src="../static/invoice.js"></script>
{% endblock %}
{% block body %}
<div class="well">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Invoice to {{ _cfg("your-name") }}</h1>
</div>
</div>
</div>
</div>
<noscript>
<div class="container">
<div class="alert alert-danger">
<p>This page requires Javascript. It's necessary to send your credit card number to
<a href="https://stripe.com/">Stripe</a> directly.</p>
</div>
</div>
</noscript>
<div class="container text-center hidden" id="thanks">
<p>Thank you for your payment - it has been processed successfully.</p>
</div>
<div class="container text-center" id="donation-stuff">
<h3>Invoice for ${{"{:.2f}".format(invoice.amount / 100)}}</h3>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<p>
{{invoice.comment}}
</p>
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="alert alert-danger hidden" id="errors"><p></p></div>
<button class="btn btn-block btn-success" id="submit">
Submit Payment
</button>
</div>
</div>
</div>
<div class="container text-center">
<p>
<small class="text-muted">
Powered by <a href="https://github.com/SirCmpwn/fosspay">fosspay</a>.
</small>
</p>
</div>
{% endblock %}