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)
return link.href
-def create_agreement(amount):
+def create_agreement(amount, app=False):
try:
plan = BillingPlanModel.objects.get(amount=amount)
except BillingPlanModel.DoesNotExist:
"payment_method": "paypal"
},
})
+ if app:
+ billing_agreement['override_merchant_preferences'] = {
+ 'return_url': absolute_url('paypal_app_return'),
+ }
response = billing_agreement.create()
if response:
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 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)