-
Notifications
You must be signed in to change notification settings - Fork 15
/
cronjob.py
executable file
·48 lines (39 loc) · 1.4 KB
/
cronjob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
from fosspay.objects import *
from fosspay.database import db
from fosspay.config import _cfg
from fosspay.email import send_thank_you, send_declined
from datetime import datetime, timedelta
import stripe
stripe.api_key = _cfg("stripe-secret")
print("Processing monthly donations")
donations = Donation.query \
.filter(Donation.type == DonationType.monthly) \
.filter(Donation.active) \
.all()
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)))