Django URL被调用两次并导致视图崩溃

时间:2020-04-28 12:24:46

标签: python django flutterwave

我现在正尝试从公司文档中实施付款系统,我希望他们的api可以通过

调用我的项目

http://127.0.0.1:5000/cart/payment/verify/?flwref=FLW-MOCK-5254aaf8272ae6c1ebbeaf754a75509b&txref=user-1-cart-40

,然后从“请求”标签中返回正确的网址。但是没有对该URL的响应,而是立即看到带有URL的获取请求

http://127.0.0.1:8000/cart/payment/verify/?resp= {%22name%22%3A%22opop%22%2C%22data%22%3A {%22data%22%3A {%22id%22%3A1249370%2C%22txRef%22%3A% 22user-1-cart-22%22%2C%22orderRef%22%3A%22URF_1588046289399_3226935%22%2C%22flwRef%22%3A%22FLW-MOCK-d92dbe44cad98158ebf026c29eaf91df%22%2C%22redirectUrl%22%3A%22http%3A% 2F ...}

我不知道是什么原因造成的,希望有人能帮助我找到原因。

观看次数

@csrf_exempt
def verifyPayment(request):
    # ref = json.loads(request.GET['resp'])
    # ref_code = ref['tx']['txRef']
    ref_code = request.GET.get('txref')
    if ref_code != None:
        cart_id = ref_code.split('-')[3]
        cart = Cart(request, id=cart_id)
        data = {
            # this is the reference from the payment button response after customer paid.
            # "txref": "user-%s-cart-%s"%(request.user,cart.cart.id),
            "txref": ref_code,
            # this is the secret key of the pay button generated
            "SECKEY": "FLWSECK_TEST"
        }


        if cart.items_available():
            r = requests.post(
                'https://api.ravepay.co/flwv3-pug/getpaidx/api/v2/verify', data=data)
            response = r.json()
            if response['status'] == 'success':
                # confirm that the amount for that transaction is the amount you wanted to charge
                if response['data']['chargecode'] == '00':
                    # a =  cart.summary_delivery()
                    if response['data']['amount'] == float(cart.summary_delivery()):
                        print("Payment successful then give value")
                        payment = response
                        cart.mark_items_paid()
                        cart.complete(request, cart_id)
                        cart.sendResturantOrder(cart.groupCartOrders(), response['data']['meta'][0]['metavalue'])
                        payment = 'success'
    else:
        payment = "failure"
    return render(request, 'payment/payment.html', {'payment': payment})


def processPayment(request):
    cart = Cart(request)
    payment = 'error one or more items may not be available'

    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = CheckoutForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            if cart.items_available():
                address = "%s,Apt: %s, %s, %s" % (
                    form.cleaned_data['street'], form.cleaned_data['apartment'], form.cleaned_data['city'], form.cleaned_data['state'])
                headers = {
                    'content-type': 'application/json',
                }

                data = json.dumps({"txref": "user-%s-cart-%s" % (request.user.id, cart.cart.id), "PBFPubKey": "FLWPUBK_TEST",
                                   "customer_email": request.user.email, "amount": cart.summary_delivery(), "currency": "NGN", "redirect_url": request.build_absolute_uri(reverse("verify-payment")), "meta": [{
                                       'metaname': "address",
                                       'metavalue': address
                                   }], })

                r = requests.post(
                    'https://api.ravepay.co/flwv3-pug/getpaidx/api/v2/hosted/pay', headers=headers, data=data)
                response = r.json()
                return HttpResponseRedirect(response['data']['link'])
            else:
                return render(request, 'payment/payment.html', {'payment': payment})

表格

class CheckoutForm(forms.Form):
    phone_number = PhoneNumberField(label="Phone:")
    street = forms.CharField(label='Street', max_length=150)
    apartment = forms.CharField(label='Apartment', max_length=40,required=False)
    city = forms.CharField(label='City', max_length=40)
    state = forms.CharField(label='State', max_length=40)

模板

<form action="{% url 'process-payment'%}" class="billing-form checkout-form" method="POST">
                                            {% csrf_token %}
                                            <div class="row">
                                                {{form}}
                                            </div>
                                            <div class="row mt-2">
                                               <button type="submit" class="btn food__btn">Place order</button>
                                            </div>

                                        </form>

网址

 path('cart/payment/verify/', views.verifyPayment, name="verify-payment"),
    path('cart/payment/', views.processPayment, name="process-payment"),

0 个答案:

没有答案