其他分享
首页 > 其他分享> > 28Django-接入支付宝3(前置基础准备工作)

28Django-接入支付宝3(前置基础准备工作)

作者:互联网

一,先本地跑通基础逻辑:

点打赏按钮后,页面实现跳转

1新创建一个项目:

D:\PycharmProjects>django-admin startproject alipay_test

2在项目目录下创建templates文件夹,然后在项目目录下static/js文件夹并把jquery.js文件拷贝到js目录下:

alipay_test
    alipay_test
    static
        js
            jquery.mim.js
    templates
    manage.py
External Libraries
Scratches and Consoles

3在setting里注册模板:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],

4在setting里配置静态资源目录

STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)

5在setting.py同目录下创建views.py:

from django.http import JsonResponse
from django.shortcuts import render
from django.views import View

class OrderView(View):
    def get(self,request):
        return render(request,'alipay.html')
    def post(self,request):
        #返回支付地址
        return JsonResponse({'pay_url':'http://www.baidu.com'})

6编写前端页面:

<body>
<script src="/static/js/jquery.min.js"></script>
<button id="btn">打赏</button>
<script>
    var topic_id = '202112191401'
    var post_data = {"topic_id":topic_id}
    $(function (){
        $('#btn').click(function (){
            $.ajax({
                url:"http://127.0.0.1:8000/payment/url",
                type:'post',
                contentType:"application/json",
                data:JSON.stringify(post_data),
                success:function (data){
                    window.location.href = data.pay_url
                }
            })
        })
    })
</script>
</body>

7创建路由:

from . import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('payment/url',views.OrderView.as_view()),
]

8启动项目:

D:\PycharmProjects\alipay_test>python manage.py runserver

9浏览器测试:

http://127.0.0.1:8000/payment/url

二,融合RSA密钥

1在static文件夹下创建key_file文件夹,把之前在centos虚拟机里生成和创建的三把钥匙反倒这个目录里:

static
    js
    key_file
        alipay_public_key.pem
        app_private_key.pem
        app_public_key.pem
templates

2在setting里定义支付路径:

#阿里支付路径
ALIPAY_KEY_DIRS = os.path.join(BASE_DIR,'static/key_file/')

3在视图中读取密钥

from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from django.conf import settings
#读取私钥
app_private_key_string = open(settings.ALIPAY_KEY_DIRS + 'app_private_key.pem').read()
#读取阿里公钥
alipay_public_key_string = open(settings.ALIPAY_KEY_DIRS + 'alipay_public_key.pem').read()

class OrderView(View):
    def get(self,request):
        return render(request,'alipay.html')
    def post(self,request):
        #返回支付地址
        return JsonResponse({'pay_url':'http://www.baidu.com'})

 

标签:alipay,支付宝,28Django,url,前置,django,static,key,import
来源: https://www.cnblogs.com/tyjs09/p/15707533.html