X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/f9b76b3071f108b29a086c14e745da2f3921d82c..74f7584b18b4386433b4c02336f5adafcae530c5:/src/paypal/rest.py?ds=sidebyside diff --git a/src/paypal/rest.py b/src/paypal/rest.py index 8927cabbf..ff8c85115 100644 --- a/src/paypal/rest.py +++ b/src/paypal/rest.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # @@ -7,7 +6,7 @@ from datetime import timedelta import paypalrestsdk import pytz from django.contrib.sites.models import Site -from django.core.urlresolvers import reverse +from django.urls import reverse from django.utils import timezone from django.conf import settings from .models import BillingPlan, BillingAgreement @@ -19,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): @@ -29,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", @@ -64,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: @@ -73,8 +72,8 @@ def create_agreement(amount, app=False): plan_id = plan.plan_id start = (timezone.now() + timedelta(0, 3600*24)).astimezone(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ') billing_agreement = paypalrestsdk.BillingAgreement({ - "name": u"Subskrypcja klubu WL", - "description": u"Stałe wsparcie Wolnych Lektur kwotą %s złotych" % amount, + "name": "Subskrypcja klubu WL", + "description": "Stałe wsparcie Wolnych Lektur kwotą %s złotych" % amount, "start_date": start, "plan": { "id": plan_id, @@ -85,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: @@ -95,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')