Python項目實戰(zhàn)——手把手教你使用Django框架實現(xiàn)支付寶付款
一、前言
大家好,我是Python進階者。春節(jié)即將過去,大家過年期間肯定各種掏腰包花花花,小編相信大家在支付時候,微信、支付寶支付肯定是優(yōu)先選擇。今天小編心血來潮,為大家?guī)硪粋€很有趣的項目,那就是使用Python web框架Django來實現(xiàn)支付寶支付,廢話不多說,一起來看看如何實現(xiàn)吧。
二、建立django應用
我們來建立一個Django項目然后在里面創(chuàng)建一個應用,如圖:
三、配置并啟動
然后我們設(shè)置urls文件的內(nèi)容,如圖:
然后再在子應用中創(chuàng)建一個urls.py文件,當然你也可以直接將一些視圖函數(shù)寫在項目中的urls.py文件中。最后我們編寫視圖函數(shù)并把視圖函數(shù)添加到urls.py文件中,如圖:
最后我們需要提交更改,打開該項目manage.py文件所在的目錄并打開cmd,輸入如下命令:
python manage.py migrate
現(xiàn)在讓我們來本地跑跑這個項目,還是在該目錄中,如下:
python manage.py runserver
看到輸出的結(jié)果表明這個子應用已經(jīng)啟動并返回了結(jié)果。我們也可以不用經(jīng)過子應用直接在創(chuàng)建的項目根目錄下運行啟動Django應用,首先在pay目錄下新建一個view.py文件,然后將其添加到該目錄下的urls.py文件中,如下:
運行下看圖:
四、登錄支付寶并生成rsa密鑰
首先登錄咱們要收款的支付寶,地址:
https://auth.alipay.com/login/ant_sso_index.htm?goto=https%3A%2F%2Fopenhome.alipay.com%2Fplatform%2FappDaily.htm%3Ftab%3Dinfo
然后進行登錄,如圖:
然后點擊RSA2(SHA256)后面的設(shè)置,點擊公鑰并下載支付寶密鑰生成器或者openssl來生成密鑰,這里我選擇支付寶密鑰生成器,如圖:
然后點擊它之后跳轉(zhuǎn)到下載界面下載,如圖:
下載好后打開該工具,選擇好密鑰長度和密鑰格式并生成密鑰,如圖:
然后進入公私鑰的目錄,將這個復制到我們的Django項目的子應用目錄中,并重命名,等下用的著,如圖:
緊接著我們進入自己的開發(fā)者中心控制臺,地址:
https://open.alipay.com/platform/developerIndex.htm
然后我們?nèi)?chuàng)建一個應用,如圖:
按照要求如實填寫即可。然后我們來設(shè)置它的接口加密方式,如圖:
驗證好了之后填寫剛剛生成的應用公鑰,如圖:
此時會出現(xiàn)應用公鑰和支付寶公鑰,將支付寶公鑰保存起來,如圖:
然后我們將產(chǎn)生的額應用公私鑰和支付寶公鑰保存為下列內(nèi)容形式的文件,如圖:
將這三個文件都保存在rsakey這個文件夾中?,F(xiàn)在準備工作都做好了,下面開始編寫支付寶支付接口。
注:項目審核通過后才可以使用密鑰調(diào)用支付寶接口噢!
四、PC端支付寶支付接口
這里我們使用一個類將它封裝起來,如下:
- from datetime import datetime
- from Crypto.PublicKey import RSA
- from Crypto.Signature import PKCS1_v1_5
- from Crypto.Hash import SHA256
- from urllib.parse import quote_plus
- from urllib.parse import urlparse, parse_qs
- from base64 import decodebytes, encodebytes
- import json
- class AliPay(object):
- """
- 支付寶支付接口(PC端支付接口)
- """
- def __init__(self, appid, app_notify_url, app_private_key_path,
- alipay_public_key_path, return_url, debug=False):
- self.appid = appid
- self.app_notify_url = app_notify_url
- self.app_private_key_path = app_private_key_path
- self.app_private_key = None
- self.return_url = return_url
- with open(self.app_private_key_path) as fp:
- self.app_private_key = RSA.importKey(fp.read())
- self.alipay_public_key_path = alipay_public_key_path
- with open(self.alipay_public_key_path) as fp:
- self.alipay_public_key = RSA.importKey(fp.read())
- if debug is True:
- self.__gateway = "https://openapi.alipaydev.com/gateway.do"
- else:
- self.__gateway = "https://openapi.alipay.com/gateway.do"
- def direct_pay(self, subject, out_trade_no, total_amount, return_url=None, **kwargs):
- biz_content = {
- "subject": subject,
- "out_trade_no": out_trade_no,
- "total_amount": total_amount,
- "product_code": "FAST_INSTANT_TRADE_PAY",
- # "qr_pay_mode":4
- }
- biz_content.update(kwargs)
- data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)
- return self.sign_data(data)
- def build_body(self, method, biz_content, return_url=None):
- data = {
- "app_id": self.appid,
- "method": method,
- "charset": "utf-8",
- "sign_type": "RSA2",
- "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
- "version": "1.0",
- "biz_content": biz_content
- }
- if return_url is not None:
- data["notify_url"] = self.app_notify_url
- data["return_url"] = self.return_url
- return data
- def sign_data(self, data):
- data.pop("sign", None)
- # 排序后的字符串
- unsigned_items = self.ordered_data(data)
- unsigned_string = "&".join("{0}={1}".format(k, v) for k, v in unsigned_items)
- sign = self.sign(unsigned_string.encode("utf-8"))
- # ordered_items = self.ordered_data(data)
- quoted_string = "&".join("{0}={1}".format(k, quote_plus(v)) for k, v in unsigned_items)
- # 獲得最終的訂單信息字符串
- signed_string = quoted_string + "&sign=" + quote_plus(sign)
- return signed_string
- def ordered_data(self, data):
- complex_keys = []
- for key, value in data.items():
- if isinstance(value, dict):
- complex_keys.append(key)
- # 將字典類型的數(shù)據(jù)dump出來
- for key in complex_keys:
- data[key] = json.dumps(data[key], separators=(',', ':'))
- return sorted([(k, v) for k, v in data.items()])
- def sign(self, unsigned_string):
- # 開始計算簽名
- key = self.app_private_key
- signer = PKCS1_v1_5.new(key)
- signature = signer.sign(SHA256.new(unsigned_string))
- # base64 編碼,轉(zhuǎn)換為unicode表示并移除回車
- sign = encodebytes(signature).decode("utf8").replace("\n", "")
- return sign
- def _verify(self, raw_content, signature):
- # 開始計算簽名
- key = self.alipay_public_key
- signer = PKCS1_v1_5.new(key)
- digest = SHA256.new()
- digest.update(raw_content.encode("utf8"))
- if signer.verify(digest, decodebytes(signature.encode("utf8"))):
- return True
- return False
- def verify(self, data, signature):
- if "sign_type" in data:
- sign_type = data.pop("sign_type")
- # 排序后的字符串
- unsigned_items = self.ordered_data(data)
- message = "&".join(u"{}={}".format(k, v) for k, v in unsigned_items)
- return self._verify(message, signature)
為了便于調(diào)用,我們將這個Python文件放在子應用的目錄中,命名為pay.py。
五、編寫前端頁面
我們通過前端的商品的名稱和價格來生成對應的商品信息并發(fā)起付款請求,如下:
index.html(商品主頁)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style>
- table,table tr th, table tr td { border:1px solid #0094ff; }
- table { width:300px; min-height: 25px; line-height: 25px; text-align: center; border-collapse: collapse; padding:2px;}
- a{
- text-decoration: none;
- }
- </style>
- </head>
- <body>
- <h1>歡迎來到購物商場</h1>
- <table border="1">
- <thead>商品目錄</thead>
- <tr>
- <td>商品名</td>
- <td>商品單價</td>
- <td>商品數(shù)量</td>
- <td>是否購買</td>
- </tr>
- <tr>
- <td>梨子</td>
- <td>0.1</td>
- <td>1</td>
- <td><a href="{% url 'dingdan' %}">購買</a></td>
- </table>
- </body>
- </html>
show.html(支付結(jié)果顯示頁)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <h1>支付結(jié)果:{{msg}}</h1>
- </body>
- </html>
六、編寫視圖函數(shù)處理渲染
- from django.shortcuts import render,redirect
- from django.http import HttpResponse,JsonResponse
- from .pay import AliPay
- import uuid
- from urllib.parse import parse_qs
- # Create your views here.
- def index(request):
- return render(request,'index.html')
- def dingdan(request):
- # 實例化AliPay
- alipay = AliPay(
- appid="自己的APPID",
- app_notify_url='http://127.0.0.1:8000/paypay/check/',#支付寶會向這個地址發(fā)送post請求
- return_url='http://127.0.0.1:8000/paypay/show/',#支付寶會向這個地址發(fā)送get請求
- app_private_key_path=r'C:\Users\Administrator\Desktop\pay\paypay\rsakey\private2048.txt', # 應用私鑰
- alipay_public_key_path=r'C:\Users\Administrator\Desktop\pay\paypay\rsakey\paypublic.txt', # 支付寶公鑰
- debug=True, # 默認是False
- )
- # 定義請求地址傳入的參數(shù)
- res=alipay.direct_pay(
- subject='梨子', # 商品描述
- out_trade_no=str(uuid.uuid4()), # 訂單號
- total_amount='0.1', # 交易金額(單位是元,保留兩位小數(shù))
- )
- #生成跳轉(zhuǎn)到支付寶支付頁面的url
- url='https://openapi.alipaydev.com/gateway.do?{0}'.format(res)
- return redirect(url)
- def show(request):
- if request.method == 'GET':
- alipay = AliPay(
- appid="自己的APPID",
- app_notify_url='http://127.0.0.1:8000/paypay/check/',
- return_url='http://127.0.0.1:8000/paypay/show/',
- app_private_key_path=r'C:\Users\Administrator\Desktop\pay\paypay\rsakey\private2048.txt', # 應用私鑰
- alipay_public_key_path=r'C:\Users\Administrator\Desktop\pay\paypay\rsakey\paypublic.txt', # 支付寶公鑰
- debug=True, # 默認是False
- )
- param=request.GET.dict() # 獲取請求攜帶的參數(shù)并轉(zhuǎn)換成字典類型
- sign=param.pop('sign', None) # 獲取sign的值
- # 對sign參數(shù)進行驗證
- statu = alipay.verify(param,sign)
- if statu:
- return render(request, 'show.html', {'msg': '支付成功'})
- else:
- return render(request, 'show.html', {'msg': '支付失敗'})
- else:
- return render(request, 'show.html', {'msg': '只支持GET請求,不支持其它請求'})
- def check(request):
- if request.method=='POST':
- alipay=AliPay(appid="自己的APPID",
- app_notify_url='http://127.0.0.1:8000/paypay/check/', # 支付寶會向這個地址發(fā)送post請求
- return_url='http://127.0.0.1:8000/show_msg/', # 支付寶會向這個地址發(fā)送get請求
- app_private_key_path=r'C:\Users\Administrator\Desktop\pay\paypay\rsakey\private2048.txt', # 應用私鑰
- alipay_public_key_path=r'C:\Users\Administrator\Desktop\pay\paypay\rsakey\paypublic.txt', # 支付寶公鑰
- debug=True,
- )
- body=request.body.decode('utf-8') # 轉(zhuǎn)成字符串
- post_data = parse_qs(body) # 根據(jù)&符號分割
- post_dict = {}
- for k, v in post_data.items():
- post_dict[k] = v[0]
- sign = post_dict.pop('sign', None)
- status = alipay.verify(post_dict, sign)
- if status: # 支付成功
- return HttpResponse('支付成功')
- else:
- return HttpResponse('支付失敗')
- else:
- return HttpResponse('只支持POST請求')
七、添加路由函數(shù)到url規(guī)則中
- from django.urls import path
- from . import views
- urlpatterns=[
- path('',views.index,name='index'),
- path('dingdan/',views.dingdan,name='dingdan'),
- path('show/',views.show,name='show'),
- path('check/',views.check,name='check'),
- ]
八、運行項目
所有準備工作都做好了,我們趕緊來試著運行下項目吧,如下:
可以看到我們購買商品后鏈接成功跳轉(zhuǎn)到支付界面。
九、總結(jié)
該支付寶支付環(huán)境在沙箱中實現(xiàn),因此安全性毋庸置疑,代碼小編已經(jīng)打包好了,不過里面的appid還有公私鑰需要大家自行填寫噢。
最后需要本文項目代碼的小伙伴,請在公眾號后臺回復“支付寶”關(guān)鍵字進行獲取,如果在運行過程中有遇到任何問題,請隨時留言或者加小編好友,小編看到會幫助大家解決bug噢!
本文轉(zhuǎn)載自微信公眾號「Python爬蟲與數(shù)據(jù)挖掘」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Python爬蟲與數(shù)據(jù)挖掘公眾號。