From: Radek Czajka Date: Fri, 18 Feb 2022 09:42:45 +0000 (+0100) Subject: Include paypal in receipts. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/d5a5399ebf7c51aae90dd4df2a803faaa08807d9 Include paypal in receipts. --- diff --git a/src/club/models.py b/src/club/models.py index 089bbbd33..a0722d38e 100644 --- a/src/club/models.py +++ b/src/club/models.py @@ -299,6 +299,7 @@ class PayUOrder(payu_models.Order): def send_receipt(cls, email, year): Contact = apps.get_model('messaging', 'Contact') Funding = apps.get_model('funding', 'Funding') + BillingAgreement = apps.get_model('paypal', 'BillingAgreement') payments = [] try: @@ -325,6 +326,9 @@ class PayUOrder(payu_models.Order): 'amount': order.get_amount(), }) + for ba in BillingAgreement.objects.filter(schedule__email=email): + payments.extend(ba.get_donations(year)) + fundings = Funding.objects.filter( email=email, payed_at__year=year diff --git a/src/paypal/models.py b/src/paypal/models.py index 3fc012ba7..3baf99eba 100644 --- a/src/paypal/models.py +++ b/src/paypal/models.py @@ -21,3 +21,7 @@ class BillingAgreement(models.Model): def check_agreement(self): from .rest import check_agreement return check_agreement(self.agreement_id) + + def get_donations(self, year): + from .rest import get_donations + return get_donations(self.agreement_id, year) diff --git a/src/paypal/rest.py b/src/paypal/rest.py index ff8c85115..25bb21ae9 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 datetime, timedelta +from decimal import Decimal import paypalrestsdk import pytz from django.contrib.sites.models import Site @@ -90,7 +90,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: @@ -124,3 +124,25 @@ def user_is_subscribed(user): 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