我正在使用 Django 为我的网站实施 PayPal 结账,但在 url 方面遇到了一些问题。付款完成后,我想将数据提取到“payment_complete”视图,然后将客户发送到“payment_successful”页面。
但它不起作用。找不到网址“payment_complete”和“payment_successful”。你知道为什么吗 ?谢谢你的帮助。
{% extends 'main.html' %}
{% load static %}
{% block title %}
Checkout
{% endblock title %}
{% block content %}
<div>
<div id = "proceed-to-payment-div">
<div id="paypal-button-container"></div>
</div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=Ac7c5n8LPoPfEjQjK-PlndbIoLLYm5t5z7Pw8YSPVhMtpU5PJDLmjDxDXO5sYZGl4sBNX-AdgjbGxOuv¤cy=EUR" ></script>
<script type="text/javascript">
var basket_total = '{{basket.get_total_price}}';
console.log(basket_total);
</script>
<script type="text/javascript" src=" {% static 'js/payment/checkout-paypal.js' %} "></script>
{% endblock content %}
function initPayPalButton() {
paypal.Buttons({
style:{
color:'white',
shape:'rect',
size:'responsive'
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: parseFloat(basket_total).toFixed(2)
}
}]
});
},
onApprove: function(data) {
var url = "{% url 'payment:payment_complete' %}"
return fetch(url, {
method:'POST',
headers: {
'content-type': 'application/json',
'X-CSRFToken': csrftoken,
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function () {
location.href = "{% url 'payment:payment_successful' %}";
});
},
}).render('#paypal-button-container');
};
initPayPalButton();
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('account/', include('account.urls', namespace='account')),
path('basket/', include('basket.urls', namespace='basket')),
path('orders/', include('orders.urls', namespace='orders')),
path('payment/', include('payment.urls', namespace='payment')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import path
from . import views
app_name = 'payment'
urlpatterns = [
path('', views.checkout, name='checkout'),
path('payment_complete/', views.payment_complete, name='payment_complete'),
path('payment_successful/', views.payment_successful, name='payment_successful'),
]
import json
from django.shortcuts import render, redirect
from django.views import View
from django.conf import settings
from django.http import JsonResponse
from paypalcheckoutsdk.orders import OrdersGetRequest
from .paypal import PayPalClient
from basket.basket import Basket
from orders.models import OrderItem, Order
def checkout(request):
context = {}
return render(request, 'payment/checkout.html', context)
def payment_complete(request):
PPClient = PayPalClient()
body = json.loads(request.body)
data = body["orderID"]
user_id = request.user.id
requestorder = OrdersGetRequest(data)
response = PPClient.client.execute(requestorder)
total_paid = response.result.purchase_units[0].amount.value
basket = Basket(request)
for item in basket:
OrderItem.objects.create(
product=item["product"],
price=item["price"],
quantity=item["qty"],
subtotal = float(item["price"]) * int(item["qty"]),
)
order = Order.objects.create(
user_id = user_id,
name = response.result.purchase_units[0].shipping.name.full_name,
email = response.result.payer.email_address,
address1 = response.result.purchase_units[0].shipping.address.address_line_1,
address2 = response.result.purchase_units[0].shipping.address.address_line_2,
postcode = response.result.purchase_units[0].shipping.address.postal_code,
country = response.result.purchase_units[0].shipping.address.country_code,
city = response.result.purchase_units[0].shipping.address.city,
total_paid = total_paid,
order_key = response.result.id,
payment_option = "paypal",
billing_status = True,
)
order_id = order.pk
return JsonResponse("Payment completed OK", safe=False)
def payment_successful(request):
basket = Basket(request)
basket.clear()
return render(request, "payment/payment_successful.html",{})
[04/Apr/2021 00:00:11] "POST /basket/update/ HTTP/1.1" 200 46
[04/Apr/2021 00:00:13] "GET /payment/ HTTP/1.1" 200 6391
[04/Apr/2021 00:00:13] "GET /static/js/payment/checkout-paypal.js HTTP/1.1" 304 0
Not Found: /payment/{% url 'payment:payment_complete' %}
[04/Apr/2021 00:00:34] "POST /payment/%7B%%20url%20'payment:payment_complete'%20%%7D HTTP/1.1" 404 3846
Not Found: /payment/{% url 'payment:payment_successful' %}
[04/Apr/2021 00:00:34] "GET /payment/%7B%%20url%20'payment:payment_successful'%20%%7D HTTP/1.1" 404 3851
Not Found: /favicon.ico
[04/Apr/2021 00:00:34] "GET /favicon.ico HTTP/1.1" 404 3240
你不能在你的 checkout-paypal.js 中使用 {% url %} 模板标签,它是一个静态文件,不被模板引擎解析。您必须在脚本中硬编码您的网址。
在 paypal OnApprove 部分重新定义您的网址,如下所示。
var url='/payment/payment_complete/'
JavaScript 文件 checkout-paypal.js 没有被模板引擎解析。