Stabilize dependencies.
[wolnelektury.git] / src / paypal / rest.py
index 68ac30f..3590c6f 100644 (file)
@@ -11,7 +11,7 @@ from django.core.urlresolvers import reverse
 from django.utils import timezone
 from paypalrestsdk import BillingPlan, BillingAgreement, ResourceNotFound
 from django.conf import settings
 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 as BillingPlanModel, BillingAgreement as BillingAgreementModel
 
 paypalrestsdk.configure(settings.PAYPAL_CONFIG)
 
 
 paypalrestsdk.configure(settings.PAYPAL_CONFIG)
 
@@ -65,7 +65,7 @@ def get_link(links, rel):
             return link.href
 
 
             return link.href
 
 
-def create_agreement(amount):
+def create_agreement(amount, app=False):
     try:
         plan = BillingPlanModel.objects.get(amount=amount)
     except BillingPlanModel.DoesNotExist:
     try:
         plan = BillingPlanModel.objects.get(amount=amount)
     except BillingPlanModel.DoesNotExist:
@@ -74,8 +74,8 @@ def create_agreement(amount):
         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({
         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({
-        "name": "Subskrypcja klubu WL",
-        "description": "Cykliczne wspieranie Wolnych Lektur kwotą %s złotych" % amount,
+        "name": u"Subskrypcja klubu WL",
+        "description": u"Stałe wsparcie Wolnych Lektur kwotą %s złotych" % amount,
         "start_date": start,
         "plan": {
             "id": plan_id,
         "start_date": start,
         "plan": {
             "id": plan_id,
@@ -84,6 +84,10 @@ def create_agreement(amount):
             "payment_method": "paypal"
         },
     })
             "payment_method": "paypal"
         },
     })
+    if app:
+        billing_agreement['override_merchant_preferences'] = {
+            'return_url': absolute_url('paypal_app_return'),
+        }
 
     response = billing_agreement.create()
     if response:
 
     response = billing_agreement.create()
     if response:
@@ -92,8 +96,8 @@ def create_agreement(amount):
         raise PaypalError(billing_agreement.error)
 
 
         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')
 
 
     return get_link(agreement.links, 'approval_url')
 
 
@@ -110,5 +114,10 @@ def check_agreement(agreement_id):
         return a.state == 'Active'
 
 
         return a.state == 'Active'
 
 
+def user_is_subscribed(user):
+    agreements = BillingAgreementModel.objects.filter(user=user)
+    return any(agreement.check_agreement() for agreement in agreements)
+
+
 def execute_agreement(token):
     return BillingAgreement.execute(token)
 def execute_agreement(token):
     return BillingAgreement.execute(token)