updates/fixes related to tildeclub.

This commit is contained in:
fosspay service
2026-06-11 15:43:00 -06:00
parent 907bf2206f
commit ccdad46377
7 changed files with 219 additions and 88 deletions

1
.gitignore vendored
View File

@@ -12,3 +12,4 @@ storage/
pip-selfcheck.json pip-selfcheck.json
.sass-cache/ .sass-cache/
overrides/ overrides/
venv/

View File

@@ -5,29 +5,37 @@ from fosspay.config import _cfg
from fosspay.email import send_thank_you, send_declined from fosspay.email import send_thank_you, send_declined
from datetime import datetime, timedelta from datetime import datetime, timedelta
import requests import requests
import stripe import stripe
import subprocess import subprocess
import sys # ← required for sys.exit below ❱❱ ADDED
stripe.api_key = _cfg("stripe-secret") stripe.api_key = _cfg("stripe-secret")
currency = _cfg("currency") currency = _cfg("currency")
print("Processing monthly donations at " + str(datetime.utcnow())) print("Processing monthly donations at " + str(datetime.utcnow()))
donations = Donation.query \ donations = (Donation.query
.filter(Donation.type == DonationType.monthly) \ .filter(Donation.type == DonationType.monthly)
.filter(Donation.active) \ .filter(Donation.active)
.all() .all())
limit = datetime.now() - timedelta(days=30) limit = datetime.now() - timedelta(days=30)
for donation in donations: for donation in donations:
if donation.updated < limit: if donation.updated < limit:
print("Charging {}".format(donation)) print(f"Charging {donation}")
user = donation.user user = donation.user
customer = stripe.Customer.retrieve(user.stripe_customer)
# ── guard against missing customer IDs ─────────────────────────
if not user.stripe_customer:
print(" • no stripe_customer on record → skipped")
continue
customer = stripe.Customer.retrieve( # ❱❱ FIXED
user.stripe_customer,
expand=["sources"] # ensure .sources exists
)
try: try:
charge = stripe.Charge.create( charge = stripe.Charge.create(
amount=donation.amount, amount=donation.amount,
@@ -35,31 +43,34 @@ for donation in donations:
customer=user.stripe_customer, customer=user.stripe_customer,
description="Donation to " + _cfg("your-name") description="Donation to " + _cfg("your-name")
) )
except stripe.error.CardError as e: except stripe.error.CardError:
donation.active = False donation.active = False
db.commit() db.commit()
send_declined(user, donation.amount) send_declined(user, donation.amount)
print("Declined") print(" • declined")
continue continue
send_thank_you(user, donation.amount, donation.type == DonationType.monthly) send_thank_you(user, donation.amount, True)
donation.updated = datetime.now() donation.updated = datetime.now()
donation.payments += 1 donation.payments += 1
db.commit() db.commit()
else: else:
print("Skipping {}".format(donation)) print(f"Skipping {donation}")
print("{} records processed.".format(len(donations))) print(f"{len(donations)} records processed.")
# ───────────────────────── Patreon token refresh ──────────────────────
if _cfg("patreon-refresh-token"): if _cfg("patreon-refresh-token"):
print("Updating Patreon API token") print("Updating Patreon API token")
r = requests.post(
r = requests.post('https://www.patreon.com/api/oauth2/token', params={ 'https://www.patreon.com/api/oauth2/token',
params={
'grant_type': 'refresh_token', 'grant_type': 'refresh_token',
'refresh_token': _cfg("patreon-refresh-token"), 'refresh_token': _cfg("patreon-refresh-token"),
'client_id': _cfg("patreon-client-id"), 'client_id': _cfg("patreon-client-id"),
'client_secret': _cfg("patreon-client-secret") 'client_secret': _cfg("patreon-client-secret")
}) }
)
if r.status_code != 200: if r.status_code != 200:
print("Failed to update Patreon API token") print("Failed to update Patreon API token")
sys.exit(1) sys.exit(1)
@@ -71,6 +82,7 @@ if _cfg("patreon-refresh-token"):
with open("config.ini", "w") as f: with open("config.ini", "w") as f:
f.write(config) f.write(config)
print("Refreshed Patreon API token") print("Refreshed Patreon API token")
reload_cmd = _cfg("reload-command") reload_cmd = _cfg("reload-command")
if not reload_cmd: if not reload_cmd:
print("Cannot reload application, add reload-command to config.ini") print("Cannot reload application, add reload-command to config.ini")

View File

@@ -71,8 +71,11 @@ def index():
lp_count = 0 lp_count = 0
lp_sum = 0 lp_sum = 0
# ── GitHub Sponsors totals ────────────────────────────────────────
github_token = _cfg("github-token") github_token = _cfg("github-token")
if github_token: if github_token:
try:
query = """ query = """
{ {
viewer { viewer {
@@ -92,22 +95,31 @@ def index():
} }
} }
""" """
r = requests.post("https://api.github.com/graphql", json={ r = requests.post(
"query": query "https://api.github.com/graphql",
}, headers={ json={"query": query},
"Authorization": f"bearer {github_token}" headers={"Authorization": f"bearer {github_token}"}
}) )
result = r.json() result = r.json()
nodes = result["data"]["viewer"]["sponsorsListing"]["tiers"]["nodes"] viewer = (result.get("data") or {}).get("viewer") or {}
gh_user = viewer.get("login")
listing = viewer.get("sponsorsListing") or {}
tiers = (listing.get("tiers") or {}).get("nodes") or []
cnt = lambda n: n["adminInfo"]["sponsorships"]["totalCount"] cnt = lambda n: n["adminInfo"]["sponsorships"]["totalCount"]
gh_count = sum(cnt(n) for n in nodes) gh_count = sum(cnt(n) for n in tiers)
gh_sum = sum(n["monthlyPriceInCents"] * cnt(n) for n in nodes) gh_sum = sum(n["monthlyPriceInCents"] * cnt(n) for n in tiers)
gh_user = result["data"]["viewer"]["login"]
except Exception:
gh_count = 0
gh_sum = 0
gh_user = None
else: else:
gh_count = 0 gh_count = 0
gh_sum = 0 gh_sum = 0
gh_user = None gh_user = None
return render_template("index.html", projects=projects, return render_template("index.html", projects=projects,
avatar=avatar, selected_project=selected_project, avatar=avatar, selected_project=selected_project,
recurring_count=recurring_count, recurring_sum=recurring_sum, recurring_count=recurring_count, recurring_sum=recurring_sum,
@@ -236,7 +248,7 @@ def donate():
db.add(user) db.add(user)
else: else:
customer = stripe.Customer.retrieve(user.stripe_customer) customer = stripe.Customer.retrieve(user.stripe_customer)
new_source = customer.sources.create(source=stripe_token) new_source = customer.create_source(user.stripe_customer, source=stripe_token)
customer.default_source = new_source.id customer.default_source = new_source.id
customer.save() customer.save()

View File

@@ -1,10 +1,12 @@
import logging import logging
import os
from pathlib import Path
try: try:
from configparser import ConfigParser from configparser import ConfigParser
except ImportError: except ImportError:
# Python 2 support # Python 2 support
from ConfigParser import ConfigParser from ConfigParser import ConfigParser # type: ignore
logger = logging.getLogger("fosspay") logger = logging.getLogger("fosspay")
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
@@ -19,13 +21,49 @@ logger.addHandler(sh)
# scss logger # scss logger
logging.getLogger("scss").addHandler(sh) logging.getLogger("scss").addHandler(sh)
env = 'dev' env = "dev"
config = None config = None
def _read_config_file(cfg, path: Path) -> None:
"""
Read config.ini using the best available API.
Python 3.2+ prefers read_file(); Python 2 uses readfp().
"""
with path.open("r", encoding="utf-8") as f:
if hasattr(cfg, "read_file"):
cfg.read_file(f)
else:
# Python 2
cfg.readfp(f) # type: ignore[attr-defined]
def load_config(): def load_config():
global config global config
config = ConfigParser() config = ConfigParser()
config.readfp(open('config.ini'))
# Keep existing behavior: look in current working directory first.
candidates = [Path(os.getcwd()) / "config.ini"]
# Fallback: look in project root (parent of the fosspay package dir).
# /home/fosspay/app/fosspay/config.py -> /home/fosspay/app/config.ini
candidates.append(Path(__file__).resolve().parent.parent / "config.ini")
# Optional override, doesn't break defaults:
# export FOSSPAY_CONFIG=/path/to/config.ini
override = os.environ.get("FOSSPAY_CONFIG")
if override:
candidates.insert(0, Path(override))
for p in candidates:
if p.is_file():
_read_config_file(config, p)
logger.debug("Loaded config.ini from %s", str(p))
return
logger.error("Could not find config.ini. Tried: %s", ", ".join(str(p) for p in candidates))
raise FileNotFoundError("config.ini not found")
load_config() load_config()

View File

@@ -1,3 +1,8 @@
# fosspay/email.py
# ───────────────────────────────────────────────────────────────
# Only change: smarter SMTP opener that works with either SMTPS(465)
# or STARTTLS(587/25). All calling functions now use it.
# ───────────────────────────────────────────────────────────────
import smtplib import smtplib
import os import os
import html.parser import html.parser
@@ -12,12 +17,28 @@ from fosspay.objects import User, DonationType
from fosspay.config import _cfg, _cfgi from fosspay.config import _cfg, _cfgi
from fosspay.currency import currency from fosspay.currency import currency
def send_thank_you(user, amount, monthly):
# helper: open & log in using the right TLS mode
def _open_smtp():
if _cfg("smtp-host") == "": if _cfg("smtp-host") == "":
return return None
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port")) host = _cfg("smtp-host")
port = _cfgi("smtp-port")
if port == 465: # implicit TLS
smtp = smtplib.SMTP_SSL(host, port)
else: # STARTTLS pathway
smtp = smtplib.SMTP(host, port)
smtp.ehlo()
smtp.starttls()
smtp.ehlo() smtp.ehlo()
smtp.login(_cfg("smtp-user"), _cfg("smtp-password")) smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
return smtp
def send_thank_you(user, amount, monthly):
smtp = _open_smtp()
if smtp is None:
return
with open("emails/thank-you") as f: with open("emails/thank-you") as f:
tmpl = Template(f.read()) tmpl = Template(f.read())
message = MIMEText(tmpl.substitute(**{ message = MIMEText(tmpl.substitute(**{
@@ -34,12 +55,11 @@ def send_thank_you(user, amount, monthly):
smtp.sendmail(_cfg("smtp-from"), [user.email], message.as_string()) smtp.sendmail(_cfg("smtp-from"), [user.email], message.as_string())
smtp.quit() smtp.quit()
def send_password_reset(user): def send_password_reset(user):
if _cfg("smtp-host") == "": smtp = _open_smtp()
if smtp is None:
return return
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
smtp.ehlo()
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
with open("emails/reset-password") as f: with open("emails/reset-password") as f:
tmpl = Template(f.read()) tmpl = Template(f.read())
message = MIMEText(tmpl.substitute(**{ message = MIMEText(tmpl.substitute(**{
@@ -55,12 +75,11 @@ def send_password_reset(user):
smtp.sendmail(_cfg("smtp-from"), [user.email], message.as_string()) smtp.sendmail(_cfg("smtp-from"), [user.email], message.as_string())
smtp.quit() smtp.quit()
def send_declined(user, amount): def send_declined(user, amount):
if _cfg("smtp-host") == "": smtp = _open_smtp()
if smtp is None:
return return
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
smtp.ehlo()
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
with open("emails/declined") as f: with open("emails/declined") as f:
tmpl = Template(f.read()) tmpl = Template(f.read())
message = MIMEText(tmpl.substitute(**{ message = MIMEText(tmpl.substitute(**{
@@ -75,12 +94,11 @@ def send_declined(user, amount):
smtp.sendmail(_cfg("smtp-from"), [user.email], message.as_string()) smtp.sendmail(_cfg("smtp-from"), [user.email], message.as_string())
smtp.quit() smtp.quit()
def send_new_donation(user, donation): def send_new_donation(user, donation):
if _cfg("smtp-host") == "": smtp = _open_smtp()
if smtp is None:
return return
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
smtp.ehlo()
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
with open("emails/new_donation") as f: with open("emails/new_donation") as f:
tmpl = Template(f.read()) tmpl = Template(f.read())
message = MIMEText(tmpl.substitute(**{ message = MIMEText(tmpl.substitute(**{
@@ -99,12 +117,11 @@ def send_new_donation(user, donation):
smtp.sendmail(_cfg("smtp-from"), [_cfg('your-email')], message.as_string()) smtp.sendmail(_cfg("smtp-from"), [_cfg('your-email')], message.as_string())
smtp.quit() smtp.quit()
def send_cancellation_notice(user, donation): def send_cancellation_notice(user, donation):
if _cfg("smtp-host") == "": smtp = _open_smtp()
if smtp is None:
return return
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
smtp.ehlo()
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
with open("emails/cancelled") as f: with open("emails/cancelled") as f:
tmpl = Template(f.read()) tmpl = Template(f.read())
message = MIMEText(tmpl.substitute(**{ message = MIMEText(tmpl.substitute(**{

View File

@@ -20,33 +20,41 @@
{% set gh_progress = gh_sum / adjusted_goal %} {% set gh_progress = gh_sum / adjusted_goal %}
{% set progress = total_sum / goal %} {% set progress = total_sum / goal %}
<div class="progress" style="height: 3rem"> <div class="progress" style="height: 3rem">
{% if recurring_sum %}
<div <div
class="progress-bar progress-bar-primary" class="progress-bar progress-bar-primary"
style="width: {{ recurring_progress * 100 }}%; line-height: 2.5" style="width: {{ recurring_progress * 100 }}%; min-width: 0; line-height: 2.5"
> >
<span>{{ currency.amount("{:.0f}".format(recurring_sum / 100)) }}</span> <span>{{ currency.amount("{:.0f}".format(recurring_sum / 100)) }}</span>
</div> </div>
{% endif %}
{% if patreon_sum %}
<div <div
class="progress-bar progress-bar-info" class="progress-bar progress-bar-info"
style="width: {{ patreon_progress * 100 }}%; line-height: 2.5" style="width: {{ patreon_progress * 100 }}%; min-width: 0; line-height: 2.5"
> >
<span>{{ currency.amount("{:.0f}".format(patreon_sum / 100)) }}</span> <span>{{ currency.amount("{:.0f}".format(patreon_sum / 100)) }}</span>
</div> </div>
{% endif %}
{% if lp_sum %}
<div <div
class="progress-bar progress-bar-warning" class="progress-bar progress-bar-warning"
style="width: {{ lp_progress * 100 }}%; line-height: 2.5" style="width: {{ lp_progress * 100 }}%; min-width: 0; line-height: 2.5"
> >
<span>{{ currency.amount("{:.0f}".format(lp_sum / 100)) }}</span> <span>{{ currency.amount("{:.0f}".format(lp_sum / 100)) }}</span>
</div> </div>
{% endif %}
{% if gh_sum %}
<div <div
class="progress-bar progress-bar-primary" class="progress-bar progress-bar-primary"
style="width: {{ gh_progress * 100 }}%; line-height: 2.5" style="width: {{ gh_progress * 100 }}%; min-width: 0; line-height: 2.5"
> >
<span>{{ currency.amount("{:.0f}".format(gh_sum / 100)) }}</span> <span>{{ currency.amount("{:.0f}".format(gh_sum / 100)) }}</span>
</div> </div>
{% endif %}
</div> </div>
{% endif %} {% endif %}
</div> </div>
@@ -89,7 +97,7 @@
class="text-primary" class="text-primary"
href="https://github.com/{{gh_user}}"> href="https://github.com/{{gh_user}}">
GitHub <i class="glyphicon glyphicon-share"></i> GitHub <i class="glyphicon glyphicon-share"></i>
</a></strong> ({{ gh_count }} supporter{{ "s" if lp_count != 1 else "" }}) </a></strong> ({{ gh_count }} supporter{{ "s" if gh_count != 1 else "" }})
</p> </p>
{% endif %} {% endif %}
{% endif %} {% endif %}

View File

@@ -1,13 +1,56 @@
{# {# templates/summary.html — combined quickpitch + full donate info #}
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>Your donation here supports <a href="https://tilde.club">tilde.club</a>, and any other services that are run by tilde.club.</p>
<p> I pay for hosting and domain costs out of pocket and spend far more time than I should running these services. Anything you can pitch in helps keep everything up and running.</p> <p>Your donation keeps our community servers, IRC nodes, and hosted
services online. Current costs run about <strong>$200/month</strong> for
hosting plus <strong>$150/year</strong> in domains.</p>
<p>My hosting costs are around $200/month and domains are around $150/year.</p> <p class="small">
Payments are processed securely by Stripe; card details never touch our
servers. Stripe charges 2.9% +30¢ per transaction.
</p>
<p>Donations are securely processed through Stripe, and your payment information is not stored on my servers. I am charged a 2.9%+30c fee per transaction.</p> <p><strong><em>NOTICE: Tilde Club is not a registered legal entity
or charity.Donations are <u>not</u> taxdeductible.</em></strong></p>
<details class="donate-more">
<summary class="btn btn-default btn-lg btn-block donate-summary">More ways to contribute&nbsp;</summary>
<h3 id="methods-you-can-donate-to-tilde.club.">Alternative donation
methods</h3>
<p>All funds offset hosting (servers, domains).</p>
<p>
<a href="https://ko-fi.com/tildeclub">
<img src="https://shields.io/badge/kofi-Support_Us-ff5f5f?logo=ko-fi&style=for-the-badge"
alt="Donate via Kofi">
</a>
</p>
<p>
<a href="https://www.paypal.com/donate?hosted_button_id=DWHSADKJ26HZ8">
<img src="https://img.shields.io/badge/PayPal-Support_Us-003087?logo=paypal&logoColor=fff"
alt="Donate via PayPal">
</a>
</p>
<p>
<a href="https://github.com/sponsors/tildeclub">
<img src="https://img.shields.io/badge/GitHub%20Sponsors-EA4AAA?logo=githubsponsors&logoColor=fff"
alt="GitHub Sponsors">
</a>
</p>
<h4>Affiliate links</h4>
<ul>
<li><a href="https://clients.whc.ca/aff.php?aff=7560">Web Hosting Canada</a> domains &amp; hosting</li>
</ul>
<p><strong>Note:</strong> Email <code>root@tilde.club</code> after donating so we can add you to the GoldStar Supporters list.</p>
<h3 id="be-involved">Be involved!</h3>
<p>The best way to support us is to join the community: open pull
requests on GitHub, chat on IRC, build cool web pages, or develop
software with the tools on tilde.club.</p>
<p>Ask on IRC if you have questions!</p>
</details>