Beispiel #1
0
 def go(*args, **kw):
     try:
         ret = f(*args, **kw)
         db.commit()
         return ret
     except:
         db.rollback()
         db.close()
         raise
Beispiel #2
0
 def go(*args, **kw):
     try:
         ret = f(*args, **kw)
         db.commit()
         return ret
     except:
         db.rollback()
         db.close()
         raise
Beispiel #3
0
 def handle_500(e):
     # shit
     try:
         db.rollback()
         db.close()
     except:
         # shit shit
         print("We're very borked, letting init system kick us back up")
         sys.exit(1)
     return render_template("internal_error.html"), 500
Beispiel #4
0
 def handle_500(e):
     # shit
     try:
         db.rollback()
         db.close()
     except:
         # shit shit
         print("We're very borked, letting init system kick us back up")
         sys.exit(1)
     return render_template("internal_error.html"), 500
Beispiel #5
0
def donate():
    email = request.form.get("email")
    stripe_token = request.form.get("stripe_token")
    amount = request.form.get("amount")
    type = request.form.get("type")
    comment = request.form.get("comment")
    project_id = request.form.get("project")

    # validate and rejigger the form inputs
    if not email or not stripe_token or not amount or not type:
        return {"success": False, "reason": "Invalid request"}, 400
    try:
        if project_id is None or project_id == "null":
            project = None
        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

        amount = int(amount)
    except:
        return {"success": False, "reason": "Invalid request"}, 400

    new_account = False
    user = User.query.filter(User.email == email).first()
    if not user:
        new_account = True
        user = User(email, binascii.b2a_hex(os.urandom(20)).decode("utf-8"))
        user.password_reset = binascii.b2a_hex(os.urandom(20)).decode("utf-8")
        user.password_reset_expires = datetime.now() + timedelta(days=1)
        customer = stripe.Customer.create(email=user.email, card=stripe_token)
        user.stripe_customer = customer.id
        db.add(user)
    else:
        customer = stripe.Customer.retrieve(user.stripe_customer)
        new_source = customer.sources.create(source=stripe_token)
        customer.default_source = new_source.id
        customer.save()

    donation = Donation(user, type, amount, project, comment)
    db.add(donation)

    try:
        charge = stripe.Charge.create(amount=amount,
                                      currency=_cfg("currency"),
                                      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()

    send_thank_you(user, amount, type == DonationType.monthly)

    if new_account:
        return {
            "success": True,
            "new_account": new_account,
            "password_reset": user.password_reset
        }
    else:
        return {"success": True, "new_account": new_account}
Beispiel #6
0
def donate():
    email = request.form.get("email")
    stripe_token = request.form.get("stripe_token")
    amount = request.form.get("amount")
    type = request.form.get("type")
    comment = request.form.get("comment")
    project_id = request.form.get("project")

    # validate and rejigger the form inputs
    if not email or not stripe_token or not amount or not type:
        return {"success": False, "reason": "Invalid request"}, 400
    try:
        if project_id is None or project_id == "null":
            project = None
        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

        amount = int(amount)
    except:
        return {"success": False, "reason": "Invalid request"}, 400

    new_account = False
    user = User.query.filter(User.email == email).first()
    if not user:
        new_account = True
        user = User(email, binascii.b2a_hex(os.urandom(20)).decode("utf-8"))
        user.password_reset = binascii.b2a_hex(os.urandom(20)).decode("utf-8")
        user.password_reset_expires = datetime.now() + timedelta(days=1)
        customer = stripe.Customer.create(email=user.email, card=stripe_token)
        user.stripe_customer = customer.id
        db.add(user)
    else:
        customer = stripe.Customer.retrieve(user.stripe_customer)
        new_source = customer.sources.create(source=stripe_token)
        customer.default_source = new_source.id
        customer.save()

    donation = Donation(user, type, amount, project, comment)
    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()

    send_thank_you(user, amount, type == DonationType.monthly)

    if new_account:
        return {"success": True, "new_account": new_account, "password_reset": user.password_reset}
    else:
        return {"success": True, "new_account": new_account}