Issue charges through stripe on donation

This commit is contained in:
Drew DeVault 2015-09-06 13:17:04 -04:00
parent 2fb788316b
commit 04f0a617c2
6 changed files with 47 additions and 17 deletions

View File

@ -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()

View File

@ -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 "<Donation {} from {}: ${} ({})>".format(

View File

@ -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);

View File

@ -1,10 +0,0 @@
<p>
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.
</p>
<p>
If one project receives a million dollars and another project receives
one dollar, at least an hour will be spent on the second project.
</p>

View File

@ -129,6 +129,9 @@ window.default_type = "{{ _cfg("default-type") }}";
</div>
<div class="row" style="margin-top: 50px">
<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="donate-button">Donate</button>
</div>
</div>

View File

@ -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.
#}
<p>
Donations accumulate until there's enough to fund one week of
full time development. The project you specify influences which