2017-10-07 90 views
0

我正在爲我的網站設置在線支付門戶。 我用下面的代碼:404沒有發現泡沫請求發送錯誤-python,django,

ZARINPAL_WEBSERVICE ='https://www.zarinpal.com/pg/services/WebGate/wsdl' # Required 
MERCHANT_ID = 'blah-blah-blah' # Required 
amount = 0 

@method_decorator(login_required, name='dispatch') 
class Upgrade(View): 
    def get(self, request): 
     current_plan = UserPlan.objects.filter(user=request.user).first() 
     all_plans = Plans.objects.all() 
     old_plans = None 
     if current_plan: 
      new_plans = all_plans.filter(no__gt=current_plan.no) 
      old_plans = all_plans.filter(no__lte=current_plan.no) 
     else: 
      new_plans = all_plans 

     return render(request, 'business/upgrade.html', {'current_plan': current_plan, 
                'new_plans': new_plans, 
                'old_plans': old_plans}) 

    def post(self, request): 
     current_plan = UserPlan.objects.filter(user=request.user).first() 
     form = UpgradeForm(request.POST) 
     if form.is_valid(): 
      new_plan = form.cleaned_data.get('requested_plan') 
      requested_plan = Plans.objects.filter(no=new_plan).first() 
      global amount 
      if current_plan: 
       amount = requested_plan.price - current_plan.price 
      else: 
       amount = requested_plan.price 

      # redirect to ZarinPal page and send data to it 
      description = u'TEST DESCRIPTION' # Required 
      email = form.cleaned_data.get('email') # Optional 
      mobile = form.cleaned_data.get('phone') # Optional 
      CallbackURL = 'http://127.0.0.1:8000/business/verify/' 
      client = Client(ZARINPAL_WEBSERVICE) 
      result = client.service.PaymentRequest(MERCHANT_ID, amount, description, email, mobile, CallbackURL) 
      if result.Status == 100: 
       return redirect('https://www.zarinpal.com/pg/StartPay/' + result.Authority) 
      else: 
       return HttpResponse('Error') 
     else: 
      messages.error(request, form.errors) 
      print(form.errors) 
      return redirect('upgrade_plan') 


@login_required 
def verify(request): 
    client = Client(ZARINPAL_WEBSERVICE) 
    if request.GET.get('Status') == 'OK': 
     result = client.service.PaymentVerification(MMERCHANT_ID, 
               request.GET['Authority'], 
               amount) 
     if result.Status == 100: 
      # in this step, it must create or update UserPlan row in DB. 
     # also, it should be create a row in Sells table and save transaction defatils. 

      return HttpResponse('Transaction was successfull. RefID: ' + str(result.RefID)) 
     elif result.Status == 101: 
      return HttpResponse('Transaction submitted : ' + str(result.Status)) 
     else: 
      return HttpResponse('Transaction failed. Status: ' + str(result.Status)) 
    else: 
     return HttpResponse('Transaction failed or canceled by user') 

但在此之前它爲我支付門,它會產生一個錯誤:

Exception at /business/upgrade/
(404, 'Not Found')
Request Method: POST
Request URL: http://localhost:8000/business/upgrade/ Django Version: 1.11.4 Exception Type: Exception Exception Value:
(404, 'Not Found')

和錯誤是這行代碼:

result = client.service.PaymentRequest(MERCHANT_ID, amount, description, email, mobile, CallbackURL) 

有什麼問題?我該如何解決這個問題? 感謝

* UPDATE *
下面的代碼片段是我的urls.py文件:

from django.conf.urls import url 
from . import views 
urlpatterns = [ 
    # for explicit urls 
    url(r'^upgrade/$', views.Upgrade.as_view(), name='upgrade_plan'), 
    url(r'^verify/$', views.verify, name='verify'), 
] 
+0

請顯示您的實際視圖和網址。 –

+0

@DanielRoseman感謝您的評論。我更新了有問題的代碼並添加了整個views.py文件。 – msln

回答

0

這裏有相當多的東西,不作任何意義。

首先,您不能告訴您的付款提供商使用127.0.0.1地址回撥您的網站。這只是你的本地主機;但顯然網關是互聯網上的其他地方。它需要有一個可以調用的網站的實際地址。其次,與你的問題無關,但仍然是一個非常嚴重的問題,你絕對不能使用這樣的全局變量。這些將由您網站的所有用戶共享,所以金額會混雜起來。我對這個支付提供商一無所知,但我絕對確定它會提供回調參數中的金額。

+0

門戶提供商網站中有一些示例代碼顯示與localhost一起工作沒有問題,但我不確定,也許如果我將該代碼上傳到我的服務器,問題就解決了。 – msln

+0

問題在於泡沫庫。我使用了「zeep」庫,解決了問題。但我有一個問題,你說我不應該在我的代碼中使用'global amount',那麼如何在第二個函數中使用它?它將用於'verify'功能。所以如何將它發送到第二個函數('verify')? – msln

+0

正如我所說的,絕對可以肯定的是,數量是在回調函數的參數中提供的。 –