禁止(未设置CSRF cookie):/ paypal / | Django的

时间:2019-09-15 23:22:07

标签: django paypal django-rest-framework paypal-sandbox payment

我在使用django-paypal时遇到问题,该问题在成功完成付款时在收到贝宝IPN时发生。

错误:

Forbidden (CSRF cookie not set.): /paypal/
[15/Sep/2019 22:53:04] "POST /paypal/ HTTP/1.1" 403 2896
Forbidden (CSRF cookie not set.): /paypal/
[15/Sep/2019 22:53:19] "POST /paypal/ HTTP/1.1" 403 2896
Forbidden (CSRF cookie not set.): /paypal/
[15/Sep/2019 22:53:42] "POST /paypal/ HTTP/1.1" 403 2896
Forbidden (CSRF cookie not set.): /paypal/
[15/Sep/2019 22:54:24] "POST /paypal/ HTTP/1.1" 403 2896
Forbidden (CSRF cookie not set.): /paypal/
[15/Sep/2019 22:55:45] "POST /paypal/ HTTP/1.1" 403 2896

我不知道发生了什么,我开始调查错误,但没有取得多大进展,调查了我将文件settings.py CSRF_COOKIE_SECURE放入True中,甚至那就没有用,有什么解决方法吗?。


这是进行付款的代码:

def process_payment(request, pk):
    course = get_object_or_404(Course, pk = pk)
    host = request.get_host()

    paypal_dict = {
        'business': settings.PAYPAL_RECEIVER_EMAIL,
        'item_name': course.title,
        'amount': course.price,
        'currency_code': 'USD',
        'notify_url': 'http://{}{}'.format(host, reverse('paypal-ipn')),
        'return_url': 'http://{}{}'.format(host, reverse('course:list')),
        'cancel_return': 'http://{}{}'.format(host, reverse('payment_cancelled')),
    }

    form = PayPalPaymentsForm(initial = paypal_dict)
    return render(request, 'carts/process_payment.html', {'form': form, 'course': course})

更新

提供错误的视图不是我的,来自负责管理Paypal IPN的django-paypal,该视图称为ipn。视图如下:

https://github.com/spookylukey/django-paypal/blob/master/paypal/standard/ipn/views.py

该视图为notify_url

有关更多信息,这是我正在做的指南:https://overiq.com/django-paypal-integration-with-django-paypal/

这些是我的应用程序的urls.py:

urlpatterns = [
    path('cart/', CartDetailView.as_view(), name = 'cart'),
    path('cart-add/', views.add_course, name = 'add_course'),
    path('process-payment/<int:pk>/', views.process_payment, name='process_payment'),
    path('payment-cancelled/', views.payment_canceled, name='payment_cancelled'),

]

这些是django-paypal ipn应用程序的urls.py

urlpatterns = [
    url(r'^$', views.ipn, name="paypal-ipn"),
]

这将是我的urlconf:

urlpatterns = [
    path('django-admin/', admin.site.urls),

    # Paths of My Apps
    path('admin/', include(administration_patterns)),
    path('', include('core.urls')),
    path('', include('carts.urls')),
    path('', include(course_patterns)),
    path('paypal/', include('paypal.standard.ipn.urls')),

    # Paths of Auth
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/', include('registration.urls')),
]

2 个答案:

答案 0 :(得分:1)

尝试将“贝宝(Paypal)” URL模式移到顶部,以确保将请求实际路由到django-paypal

urlpatterns = [
    path('django-admin/', admin.site.urls),

    # Paths of My Apps
    path('paypal/', include('paypal.standard.ipn.urls')),
    path('admin/', include(administration_patterns)),
    path('', include('core.urls')),
    path('', include('carts.urls')),
    path('', include(course_patterns)),

    # Paths of Auth
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/', include('registration.urls')),
]

您为什么会看到此错误?

然后将请求路由到一个非csrf_exempt的视图,因此CSRF中间件需要CSRF cookie。

由于请求是从Paypal服务器发送的,它不包含CSRF cookie,因此您将收到错误Forbidden (CSRF cookie not set.)

为什么将请求路由到错误的视图?

模式以深度优先的方式进行匹配,这意味着Django首先尝试尝试匹配'core.urls'的所有模式,包括'carts.urls'course_patternspath('paypal/', include('paypal.standard.ipn.urls'))

其中一个包含的网址格式与路径/paypal/相匹配,也许类似于path('<slug>/', course_view)中的course_patterns

path('paypal/', include('paypal.standard.ipn.urls')),列表中上移urlpatterns可确保Django首先尝试匹配它。

答案 1 :(得分:0)

只需在呈现表单的模板上添加csrf token

<form method="POST" action="/"> {% csrf_token %}
    {{ form }}

</form>

更新

您需要使正在处理/paypal/ csrf的视图免除。我认为该视图是Paypal在付款后尝试将用户重定向到的位置:

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def paypal_view(req):
...

更新2

尝试一下:

from django.views.decorators.csrf import csrf_exempt

urlpatterns = [
    url(r'^$', csrf_exempt(views.ipn), name="paypal-ipn"),
]