app redirect for paypal
authorJan Szejko <janek37@gmail.com>
Tue, 17 Jul 2018 15:09:35 +0000 (17:09 +0200)
committerJan Szejko <janek37@gmail.com>
Tue, 17 Jul 2018 15:28:07 +0000 (17:28 +0200)
src/paypal/rest.py
src/paypal/urls.py
src/paypal/views.py

index a2e2232..1e0811b 100644 (file)
@@ -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:
@@ -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')
 
 
index 924cedd..468870b 100644 (file)
@@ -7,6 +7,8 @@ from . import views
 
 urlpatterns = (
     url(r'^form/$', views.paypal_form, name='paypal_form'),
 
 urlpatterns = (
     url(r'^form/$', views.paypal_form, name='paypal_form'),
+    url(r'^app-form/$', views.paypal_form, kwargs={'app': True}, name='paypal_api_form'),
     url(r'^return/$', views.paypal_return, name='paypal_return'),
     url(r'^return/$', views.paypal_return, name='paypal_return'),
+    url(r'^app-return/$', views.paypal_return, kwargs={'app': True}, name='paypal_api_return'),
     url(r'^cancel/$', views.paypal_cancel, name='paypal_cancel'),
 )
     url(r'^cancel/$', views.paypal_cancel, name='paypal_cancel'),
 )
index 81c1985..6708f2d 100644 (file)
@@ -9,12 +9,13 @@ from django.http import Http404
 from django.http.response import HttpResponseRedirect, HttpResponseForbidden
 from django.shortcuts import render
 
 from django.http.response import HttpResponseRedirect, HttpResponseForbidden
 from django.shortcuts import render
 
+from api.piston_patch import HttpResponseAppRedirect
 from paypal.forms import PaypalSubscriptionForm
 from paypal.rest import execute_agreement, check_agreement, agreement_approval_url, PaypalError
 from paypal.models import BillingAgreement as BillingAgreementModel, BillingPlan
 
 
 from paypal.forms import PaypalSubscriptionForm
 from paypal.rest import execute_agreement, check_agreement, agreement_approval_url, PaypalError
 from paypal.models import BillingAgreement as BillingAgreementModel, BillingPlan
 
 
-def paypal_form(request):
+def paypal_form(request, app=False):
     if request.POST:
         if not request.user.is_authenticated():
             return HttpResponseForbidden()
     if request.POST:
         if not request.user.is_authenticated():
             return HttpResponseForbidden()
@@ -22,7 +23,7 @@ def paypal_form(request):
         if form.is_valid():
             amount = form.cleaned_data['amount']
             try:
         if form.is_valid():
             amount = form.cleaned_data['amount']
             try:
-                approval_url = agreement_approval_url(amount)
+                approval_url = agreement_approval_url(amount, app=app)
             except PaypalError as e:
                 return render(request, 'paypal/error_page.html', {'error': e.message})
             return HttpResponseRedirect(approval_url)
             except PaypalError as e:
                 return render(request, 'paypal/error_page.html', {'error': e.message})
             return HttpResponseRedirect(approval_url)
@@ -32,7 +33,7 @@ def paypal_form(request):
 
 
 @login_required
 
 
 @login_required
-def paypal_return(request):
+def paypal_return(request, app=False):
     token = request.GET.get('token')
     if not token:
         raise Http404
     token = request.GET.get('token')
     if not token:
         raise Http404
@@ -44,7 +45,15 @@ def paypal_return(request):
             active = check_agreement(resource.id)
             BillingAgreementModel.objects.create(
                 agreement_id=resource.id, user=request.user, plan=plan, active=active, token=token)
             active = check_agreement(resource.id)
             BillingAgreementModel.objects.create(
                 agreement_id=resource.id, user=request.user, plan=plan, active=active, token=token)
-    return render(request, 'paypal/return.html', {'resource': resource})
+    else:
+        resource = None
+    if app:
+        if getattr(resource, 'error'):
+            return HttpResponseAppRedirect('wolnelekturyapp://paypal_error')
+        else:
+            return HttpResponseAppRedirect('wolnelekturyapp://paypal_return')
+    else:
+        return render(request, 'paypal/return.html', {'resource': resource})
 
 
 def paypal_cancel(request):
 
 
 def paypal_cancel(request):