X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/6d42bc478e3d1bd90eb294464748c21e4de0fc63..c7ff3149aa25aeda0a53ffa05761947a2e806484:/src/paypal/rest.py?ds=sidebyside diff --git a/src/paypal/rest.py b/src/paypal/rest.py index 92752b70f..11693cb8c 100644 --- a/src/paypal/rest.py +++ b/src/paypal/rest.py @@ -1,8 +1,8 @@ # 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 @@ -18,8 +18,8 @@ class PaypalError(Exception): pass -def absolute_url(url_name): - return "http://%s%s" % (Site.objects.get_current().domain, reverse(url_name)) +def absolute_url(url_name, kwargs=None): + return "http://%s%s" % (Site.objects.get_current().domain, reverse(url_name, kwargs=kwargs)) def create_plan(amount): @@ -28,7 +28,7 @@ def create_plan(amount): "description": "Cykliczna darowizna na wsparcie Wolnych Lektur", "merchant_preferences": { "auto_bill_amount": "yes", - "return_url": absolute_url('paypal_return'), + "return_url": absolute_url('paypal_return', {'key': '-'}), "cancel_url": absolute_url('paypal_cancel'), # "initial_fail_amount_action": "continue", "max_fail_attempts": "3", @@ -63,7 +63,7 @@ def get_link(links, rel): return link.href -def create_agreement(amount, app=False): +def create_agreement(amount, key, app=False): try: plan = BillingPlan.objects.get(amount=amount) except BillingPlan.DoesNotExist: @@ -84,8 +84,13 @@ def create_agreement(amount, app=False): }) if app: billing_agreement['override_merchant_preferences'] = { - 'return_url': absolute_url('paypal_app_return'), + 'return_url': absolute_url('paypal_app_return', {'key': key}), } + else: + billing_agreement['override_merchant_preferences'] = { + 'return_url': absolute_url('paypal_return', {'key': key}), + } + response = billing_agreement.create() if response: @@ -94,8 +99,8 @@ def create_agreement(amount, app=False): raise PaypalError(billing_agreement.error) -def agreement_approval_url(amount, app=False): - agreement = create_agreement(amount, app=app) +def agreement_approval_url(amount, key, app=False): + agreement = create_agreement(amount, key, app=app) return get_link(agreement.links, 'approval_url') @@ -112,10 +117,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