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}
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}
user = donation.user customer = stripe.Customer.retrieve(user.stripe_customer) try: charge = stripe.Charge.create(amount=donation.amount, currency="usd", customer=user.stripe_customer, description="Donation to " + _cfg("your-name")) except stripe.error.CardError as e: donation.active = False db.commit() send_declined(user, donation.amount) print("Declined") continue send_thank_you(user, donation.amount, donation.type == DonationType.monthly) donation.updated = datetime.now() donation.payments += 1 db.commit() else: print("Skipping {}".format(donation)) print("{} records processed.".format(len(donations))) if _cfg("patreon-refresh-token"): print("Updating Patreon API token") r = requests.post('https://www.patreon.com/api/oauth2/token', params={ 'grant_type': 'refresh_token', 'refresh_token': _cfg("patreon-refresh-token"),
limit = datetime.now() - timedelta(days=30) for donation in donations: if donation.updated < limit: print("Charging {}".format(donation)) user = donation.user customer = stripe.Customer.retrieve(user.stripe_customer) try: charge = stripe.Charge.create( amount=donation.amount, currency="usd", customer=user.stripe_customer, description="Donation to " + _cfg("your-name") ) except stripe.error.CardError as e: donation.active = False db.commit() send_declined(user, donation.amount) print("Declined") continue send_thank_you(user, donation.amount, donation.type == DonationType.monthly) donation.updated = datetime.now() donation.payments += 1 db.commit() else: print("Skipping {}".format(donation)) print("Done. {} records processed.".format(len(donations)))