X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/468c2e650c854ffa691afc73aa1f41ce61283cdb..f39c00ef647a7f625d9bd61c570b89d7b9762c61:/src/paypal/rest.py diff --git a/src/paypal/rest.py b/src/paypal/rest.py index 68ac30ff5..92752b70f 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,11 +6,10 @@ 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 paypalrestsdk import BillingPlan, BillingAgreement, ResourceNotFound from django.conf import settings -from .models import BillingPlan as BillingPlanModel +from .models import BillingPlan, BillingAgreement paypalrestsdk.configure(settings.PAYPAL_CONFIG) @@ -25,7 +23,7 @@ def absolute_url(url_name): def create_plan(amount): - billing_plan = BillingPlan({ + billing_plan = paypalrestsdk.BillingPlan({ "name": "Cykliczna darowizna na Wolne Lektury: %s zł" % amount, "description": "Cykliczna darowizna na wsparcie Wolnych Lektur", "merchant_preferences": { @@ -55,7 +53,7 @@ def create_plan(amount): raise PaypalError(billing_plan.error) if not billing_plan.activate(): raise PaypalError(billing_plan.error) - plan, created = BillingPlanModel.objects.get_or_create(amount=amount, defaults={'plan_id': billing_plan.id}) + plan, created = BillingPlan.objects.get_or_create(amount=amount, defaults={'plan_id': billing_plan.id}) return plan.plan_id @@ -65,17 +63,17 @@ def get_link(links, rel): return link.href -def create_agreement(amount): +def create_agreement(amount, app=False): try: - plan = BillingPlanModel.objects.get(amount=amount) - except BillingPlanModel.DoesNotExist: + plan = BillingPlan.objects.get(amount=amount) + except BillingPlan.DoesNotExist: 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') - billing_agreement = BillingAgreement({ + billing_agreement = paypalrestsdk.BillingAgreement({ "name": "Subskrypcja klubu WL", - "description": "Cykliczne wspieranie Wolnych Lektur kwotą %s złotych" % amount, + "description": "Stałe wsparcie Wolnych Lektur kwotą %s złotych" % amount, "start_date": start, "plan": { "id": plan_id, @@ -84,6 +82,10 @@ def create_agreement(amount): "payment_method": "paypal" }, }) + if app: + billing_agreement['override_merchant_preferences'] = { + 'return_url': absolute_url('paypal_app_return'), + } response = billing_agreement.create() if response: @@ -92,15 +94,15 @@ def create_agreement(amount): raise PaypalError(billing_agreement.error) -def agreement_approval_url(amount): - agreement = create_agreement(amount) +def agreement_approval_url(amount, app=False): + agreement = create_agreement(amount, app=app) return get_link(agreement.links, 'approval_url') def get_agreement(agreement_id): try: - return BillingAgreement.find(agreement_id) - except ResourceNotFound: + return paypalrestsdk.BillingAgreement.find(agreement_id) + except paypalrestsdk.ResourceNotFound: return None @@ -110,5 +112,10 @@ 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 BillingAgreement.execute(token) + return paypalrestsdk.BillingAgreement.execute(token)