Collect paypal transactions
[wolnelektury.git] / src / paypal / models.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 # from django.contrib.auth import get_user_model
5 from django.contrib.auth.models import User
6 from django.db import models
7
8
9 class BillingPlan(models.Model):
10     plan_id = models.CharField(max_length=32)
11     amount = models.IntegerField(db_index=True, unique=True)
12
13
14 class BillingAgreement(models.Model):
15     agreement_id = models.CharField(max_length=32)
16     schedule = models.ForeignKey('club.Schedule', models.PROTECT)
17     plan = models.ForeignKey(BillingPlan, models.PROTECT)
18     active = models.BooleanField(max_length=32)
19     token = models.CharField(max_length=32)
20
21     def check_agreement(self):
22         from .rest import check_agreement
23         return check_agreement(self.agreement_id)
24
25     def get_donations(self, year):
26         from .rest import get_donations
27         return get_donations(self.agreement_id, year)
28
29     def update_donations(self, year):
30         from .rest import get_donations
31         for donation in get_donations(self.agreement_id, year):
32             Donation.objects.get_or_create(
33                 transaction_id=donation['transaction_id'],
34                 defaults={
35                     'timestamp': donation['timestamp'],
36                     'amount': donation['amount'],
37                 }
38             )
39
40
41 class Donation(models.Model):
42     billing_agreement = models.ForeignKey(BillingAgreement, models.CASCADE)
43     transaction_id = models.CharField(max_length=255, db_index=True)
44     timestamp = models.DateTimeField()
45     amount = models.DecimalField(decimal_places=2, max_digits=10)