X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/74f7584b18b4386433b4c02336f5adafcae530c5..bde2a16ae2669bea94ec0f23c60213a26d4881f4:/src/paypal/rest.py diff --git a/src/paypal/rest.py b/src/paypal/rest.py index ff8c85115..e70c67bd4 100644 --- a/src/paypal/rest.py +++ b/src/paypal/rest.py @@ -1,10 +1,9 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from datetime import timedelta - +from datetime import date, datetime, timedelta +from decimal import Decimal import paypalrestsdk -import pytz from django.contrib.sites.models import Site from django.urls import reverse from django.utils import timezone @@ -70,7 +69,7 @@ def create_agreement(amount, key, app=False): plan_id = create_plan(amount) else: plan_id = plan.plan_id - start = (timezone.now() + timedelta(0, 3600*24)).astimezone(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ') + start = (timezone.now() + timedelta(0, 3600*24)).astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') billing_agreement = paypalrestsdk.BillingAgreement({ "name": "Subskrypcja klubu WL", "description": "Stałe wsparcie Wolnych Lektur kwotą %s złotych" % amount, @@ -90,7 +89,7 @@ def create_agreement(amount, key, app=False): billing_agreement['override_merchant_preferences'] = { 'return_url': absolute_url('paypal_return', {'key': key}), } - + response = billing_agreement.create() if response: @@ -117,10 +116,27 @@ def check_agreement(agreement_id): return a.state == 'Active' -def user_is_subscribed(user): - agreements = BillingAgreement.objects.filter(user=user) - return any(agreement.check_agreement() for agreement in agreements) - - def execute_agreement(token): return paypalrestsdk.BillingAgreement.execute(token) + + +def get_donations(agreement_id, year): + a = get_agreement(agreement_id) + transactions = [] + for transaction in a.search_transactions( + date(year - 1, 12, 31), + date(year + 1, 1, 1))['agreement_transaction_list']: + if transaction['status'] != 'Completed': + continue + dt = datetime.strptime( + transaction['time_stamp'], + '%Y-%m-%dT%H:%M:%S%z' + ).astimezone() + if dt.year != year: + continue + assert transaction['amount']['currency'] == 'PLN' + transactions.append({ + 'timestamp': dt, + 'amount': Decimal(transaction['amount']['value']) + }) + return transactions