From 8887547a016bbf665ffc4175346f655f0dfa63fb Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 17 Jul 2018 17:09:35 +0200 Subject: [PATCH 1/1] app redirect for paypal --- src/paypal/rest.py | 10 +++++++--- src/paypal/urls.py | 2 ++ src/paypal/views.py | 17 +++++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/paypal/rest.py b/src/paypal/rest.py index a2e22322f..1e0811b10 100644 --- a/src/paypal/rest.py +++ b/src/paypal/rest.py @@ -65,7 +65,7 @@ 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: @@ -84,6 +84,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,8 +96,8 @@ 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') diff --git a/src/paypal/urls.py b/src/paypal/urls.py index 924cedd95..468870bfe 100644 --- a/src/paypal/urls.py +++ b/src/paypal/urls.py @@ -7,6 +7,8 @@ from . import views 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'^app-return/$', views.paypal_return, kwargs={'app': True}, name='paypal_api_return'), url(r'^cancel/$', views.paypal_cancel, name='paypal_cancel'), ) diff --git a/src/paypal/views.py b/src/paypal/views.py index 81c198559..6708f2d86 100644 --- a/src/paypal/views.py +++ b/src/paypal/views.py @@ -9,12 +9,13 @@ from django.http import Http404 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 -def paypal_form(request): +def paypal_form(request, app=False): 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: - 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) @@ -32,7 +33,7 @@ def paypal_form(request): @login_required -def paypal_return(request): +def paypal_return(request, app=False): 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) - 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): -- 2.20.1