diff --git a/fosspay/blueprints/html.py b/fosspay/blueprints/html.py index d6c03dd..329bd9d 100644 --- a/fosspay/blueprints/html.py +++ b/fosspay/blueprints/html.py @@ -110,6 +110,11 @@ def donate(): else: project_id = int(project_id) project = Project.query.filter(Project.id == project_id).first() + + if type == "once": + type = DonationType.one_time + else: + type = DonationType.monthly except: return { "success": False, "reason": "Invalid request" }, 400 @@ -120,9 +125,26 @@ def donate(): user = User(email, binascii.b2a_hex(os.urandom(20)).decode("utf-8")) user.passwordReset = binascii.b2a_hex(os.urandom(20)).decode("utf-8") user.passwordResetExpiry = datetime.now() + timedelta(days=1) - db.add(user) + print(stripe_token) customer = stripe.Customer.create(email=user.email, card=stripe_token) + print(customer) user.stripe_customer = customer.id + db.add(user) + + donation = Donation(user, type, amount, project) + db.add(donation) + + try: + charge = stripe.Charge.create( + amount=amount, + currency="usd", + customer=user.stripe_customer, + description="Donation to " + _cfg("your-name") + ) + except stripe.error.CardError as e: + db.rollback() + db.close() + return { "success": False, "reason": "Your card was declined." } db.commit() diff --git a/fosspay/objects.py b/fosspay/objects.py index 34eb9eb..5677895 100644 --- a/fosspay/objects.py +++ b/fosspay/objects.py @@ -61,12 +61,13 @@ class Donation(Base): created = Column(DateTime, nullable=False) emailed_about = Column(Boolean, nullable=False) - def __init__(self, user, type, amount): + def __init__(self, user, type, amount, project=None): self.user = user self.type = type self.amount = amount self.created = datetime.now() self.emailed_about = False + self.project = project def __repr__(self): return "".format( diff --git a/scripts/index.js b/scripts/index.js index 34cf9b7..dd19977 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -91,12 +91,20 @@ var xhr = new XMLHttpRequest(); xhr.open("POST", "donate"); xhr.onload = function() { - document.getElementById("donation-stuff").classList.add("hidden"); - document.getElementById("thanks").classList.remove("hidden"); var res = JSON.parse(this.responseText); - if (res.new_account) { - document.getElementById("new-donor-password").classList.remove("hidden"); - document.getElementById("reset-token").value = res.password_reset; + if (res.success) { + document.getElementById("donation-stuff").classList.add("hidden"); + document.getElementById("thanks").classList.remove("hidden"); + if (res.new_account) { + document.getElementById("new-donor-password").classList.remove("hidden"); + document.getElementById("reset-token").value = res.password_reset; + } + } else { + var errors = document.getElementById("errors"); + errors.classList.remove("hidden"); + errors.querySelector("p").textContent = res.reason; + e.target.removeAttribute("disabled"); + e.target.textContent = "Donate"; } }; xhr.send(data); diff --git a/templates/how-this-works.html b/templates/how-this-works.html deleted file mode 100644 index d50a8ed..0000000 --- a/templates/how-this-works.html +++ /dev/null @@ -1,10 +0,0 @@ -

-Donations accumulate until they reach enough to support a week of full -time work. Once they get to this amount, a week will be scheduled. The -amount of time each project receives is planned based on the amount of -donations received that specify that project in the drop-down. -

-

-If one project receives a million dollars and another project receives -one dollar, at least an hour will be spent on the second project. -

diff --git a/templates/index.html b/templates/index.html index 3cb61a3..99826a3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -129,6 +129,9 @@ window.default_type = "{{ _cfg("default-type") }}";
+
diff --git a/templates/summary.html b/templates/summary.html index 71df917..342bf9b 100644 --- a/templates/summary.html +++ b/templates/summary.html @@ -1,3 +1,9 @@ +{# + Try to keep this text short. Too long and it distracts from + the donation UI and will reduce the number of conversions you + actually get. +#} +

Donations accumulate until there's enough to fund one week of full time development. The project you specify influences which