2016-03-28 63 views
2

下面是我在Django條紋的設置,Django的條紋整合給403付款後禁止

view.py

def charge(request): 
    if request.method == 'POST' : 
     resp = " Payment Successful !" 
     try: 
     token = request.POST['stripeToken'] 
     charge = stripe.Charge.create(
      amount=2000, # amount in cents, again 
      currency="usd", 
      source=token, 
      description="Example charge" 
     ) 
     print >>sys.stderr, 'Success Logger !' 
     except stripe.error.CardError as e: 
     resp = str(e) 

urls.py

urlpatterns = patterns('', 
    url(r'^$', views.home, name='home'), 
    url(r'^sign_in$', views.sign_in, name='sign_in'), 
    url(r'^sign_out$', views.sign_out, name='sign_out'), 
    url(r'^register$', views.register, name='register'), 
    url(r'^edit$', views.edit, name='edit'), 
    url(r'^charge$', views.charge, name='charge'), 

    url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
    url(r'^admin/', include(admin.site.urls)), 
) 

user.html

{% extends "page.html" %} 
{% block main %} 
     <div class="row"> 
     <div class="span6 columns"> 
     </div> 
     </div> 
     <p>Welcome {{ user.name }}.</p> 
     <p>Your credit card ends with {{ user.last_4_digits }} (<a href="{% url edit %}">change</a>)</p> 
     <form action="/charge" method="POST" id="payment_form"> 
      <script 
      src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="pk_test_xFLF8rLaykv0tiGXFkhMj5XF" 
      data-amount="2000" 
      data-name="Vevanesca" 
      data-description="2 widgets ($20.00)" 
      data-image="/128x128.png" 
      data-locale="auto"> 
      </script> 
    </form> 
{% endblock %} 

我使用默認的ch條紋提供的eckout.js。一旦支付覆蓋彈出,我輸入測試細節。接下來,在幾秒鐘後,它將重定向到/charge,其中Error 403 Forbidden CSRF verification failed. Request aborted

我該如何解決這個問題?我只想使用stripe設置一個簡單的測試網關。

+1

您的表單中沒有「{%csrf_token%}」。你嘗試過使用它嗎? –

+0

不能繞過嗎?也增加它沒有幫助。 – saruftw

+0

您可以從'settings.py'的'MIDDLEWARE_CLASSES'條目中移除''django.middleware.csrf.CsrfViewMiddleware''。有關更多信息,請參閱https://docs.djangoproject.com/en/1.9/ref/csrf/。 –

回答

0

對於快速而髒的修復,只需從MIDDLEWARE_CLASSES入口settings.py刪除'django.middleware.csrf.CsrfViewMiddleware'

有關更多信息,請參見https://docs.djangoproject.com/en/1.9/ref/csrf/

+5

您不應該這樣做,因爲它會從所有視圖中刪除CSRF,包括您自己的表單。相反,只需在費用視圖周圍放置'@ csrf_exempt'裝飾器即可。 –