【问题标题】:404 Not found error in suds request send - python - django在 suds 请求发送中找不到 404 错误 - python - django
【发布时间】:2017-10-07 12:21:59
【问题描述】:

我正在为我的网站设置在线支付门户。 我使用下面的代码:

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')

但在它显示付款门之前,它会产生一个错误:

/business/upgrade/ 中的异常
(404,“未找到”)
请求方法:POST
请求网址:http://localhost:8000/business/upgrade/ Django 版本:1.11.4 异常类型:异常 异常值:
(404,'未找到')

而错误在于这行代码:

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

有什么问题?我该如何解决? 谢谢

* 更新*
下面的 sn-p 是我的 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'),
]

【问题讨论】:

  • 请显示您的实际视图和网址。
  • @DanielRoseman 感谢您的评论。我更新了有问题的代码并添加了整个views.py文件。

标签: python django python-3.x suds


【解决方案1】:

这里有很多东西没有任何意义。

首先,您不能告诉您的支付服务提供商使用 127.0.0.1 地址回调您的网站。那只是您的本地主机;但显然网关在 Internet 上的其他地方。它需要有一个可以调用的站点的实际地址。

其次,与您的问题无关但仍然是一个非常严重的问题,您绝对不能使用这样的全局变量。这些将由您网站的所有用户共享,因此金额将全部混淆。我对这个支付提供商一无所知,但我绝对确定它会在回调的参数中提供金额。

【讨论】:

  • gate 提供商网站上有一些示例代码显示使用 localhost 没有问题,但我不确定,也许如果我将这些代码上传到我的服务器,问题就解决了。跨度>
  • 问题出在 suds 库上。我使用了“zeep”库,问题解决了。但我有一个问题,你说我不应该在我的代码中使用global amount,那么如何在第二个函数中使用它呢?它将用于verify 函数。那么如何将其发送到第二个函数(verify)?
  • 如我所说,金额绝对肯定是在回调函数的参数中提供的。
猜你喜欢
  • 2023-01-05
  • 2019-12-29
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多