python Django原生 支付寶支付

2021-08-29 02:41:53 字數 3809 閱讀 3027

views檢視處理

def alipay(request):

if request.is_secure():

notify_url = "https://%s/***/***/" % request.get_host() # 非同步通知

return_url = "https://%s/***/***/" % request.get_host() # 同步通知

# 沙箱環境位址

alipay = alipay(

return_url=return_url,

alipay_public_key_path="",

# 開啟檔案模式

"""alipay_public_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/alipay_public_key.pem"), # 支付寶的公鑰,驗證支付寶回傳訊息使用,不是你自己的公鑰,

""")

data = request.get

money_key = data.get('money_key')

total_key = data.get('total_key')

query_params = alipay.direct_pay(

subject=website_conf[request.get_host()]["website_name"], # 名

out_trade_no=total_key, # 商戶訂單號

total_amount=money_key, # 交易金額

return response

支付寶工具utils

class alipay():

"""支付寶支付介面(pc端支付介面)"""

# 初始化資訊

alipay_public_key_path, return_url, debug=false):

self.alipay_public_key = rsa.importkey(alipay_public_key_path)

self.return_url = return_url # 返回的url

# 使用檔案讀取模式開啟下面

"""with open(self.alipay_public_key_path) as fp:

self.alipay_public_key = rsa.importkey(fp.read())

"""if debug is true:

self.__gateway = ""

else:

self.__gateway = ""

def direct_pay(self, subject, out_trade_no, total_amount, return_url, **kwargs):

biz_content =

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 =

if return_url is not none:

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("=".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("=".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):

# 將字典型別的資料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):

# 開始計算簽名

signer = pkcs1_v1_5.new(key)

signature = signer.sign(sha256.new(unsigned_string))

# base64 編碼,轉換為unicode表示並移除回車

sign = b64encode(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, b64decode(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)

python Django安裝教程

2.解壓到桌面 3 開啟cmd cd到這個目錄執行 python setup.py install命令即可安裝,安裝成功提示 installed d python27 lib site packages pytz 2018.5 py2.7.egg finished processing depend...

Python Django傳送郵件

經常我們登陸一些 或者軟體都會出現簡訊驗證或者郵箱驗證,今天來初步了解一下郵箱驗證的實現。路由配置等這裡不再介紹,具體的郵箱設定請參考我以前的部落格 設定郵箱以及獲得授權碼 引入傳送郵件的模組 from django.core.mail import send mail,send mass mail...

python django 模型查詢

1查詢集表示從資料庫獲取的物件集合 2查詢集可以有多個過濾器 3過濾器就是乙個函式,基於所給的引數限制查詢集結果 4從sql角度來說,查詢集合select語句等價,過濾器就像where條件 查詢集一在管理器上呼叫過濾器方法返回查詢集 二查詢集經過過濾器篩選後返回新的查詢集,所以可以寫成鏈式呼叫 三惰...