当前位置:网站首页>1.支付系统
1.支付系统
2022-07-06 09:24:00 【小白阿远】
项目场景:
用户购买东西时支付的场景,这里以电脑支付为例
支付的流程
支付宝沙箱的配置
登陆上支付宝之后进入到上边的网址,然后进入到控制台,找到沙箱应用
将app_id,以及认证的公钥和私钥复制保存
进入到flask项目中进行支付宝的一些配置
RSA2密钥可以下载支付宝沙箱工具辅助生成
公钥和私钥可以和视图写到同一个文件中,方便查找,调用
# 支付宝配置
ALIPAY_APPID = '2016101800716047' # 沙箱环境中alipay应用ID
ALIPAY_DEBUG = True
ALIPAY_URL = 'https://openapi.alipaydev.com/gateway.do' # alipay沙箱环境支付宝网管
ALIPAY_RETURN_URL = 'http://127.0.0.1:8888/payment/callback/' # 支付完成后支付宝回调我们应用的地址
app_private_key_string = open("project/private.txt").read()
alipay_public_key_string = open("project/public.txt").read()
公钥和私钥设置为读取状态即可
alipay = AliPay(
appid="",
app_notify_url=None, # 默认回调url
app_private_key_string=app_private_key_string,
alipay_public_key_string=alipay_public_key_string,
# 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
sign_type="RSA2", # RSA 或者 RSA2
debug=True # 默认False
)
支付宝配置信息配置好之后要生成登录支付宝连接
def get_pay_url(out_trade_no, total_amount, subject):
# 生成登录支付宝连接
order_string = alipay.api_alipay_trade_page_pay(
out_trade_no=out_trade_no,
total_amount=str(total_amount),
subject=subject,
return_url=settings.ALIPAY_RETURN_URL,
)
# 响应登录支付宝连接
# 真实环境电脑网站支付网关:https://openapi.alipay.com/gateway.do? + order_string
# 沙箱环境电脑网站支付网关:https://openapi.alipaydev.com/gateway.do? + order_string
alipay_url = settings.ALIPAY_URL + "?" + order_string
return alipay_url
配置信息可以封转一个方法,哪里需要直接即可调用
支付宝配置完之后就可以进行生成订单以及支付的一系列操作
在生成订单以及支付的时候记得一定要判断该用户是否登录
如果未登录是无法生成订单以及支付的:可以理解为下图所示
生成订单时的订单号可以按照年月日拼接随机数的形式,以确保每个订单的订单号不同例如:
def get_order_id():
"""
SYL202008241212121200005/24
生成订单号: 格式: SYL + 年月日时分秒 + 5位随机数
:return:
"""
d = datetime.datetime.now()
base = 'SYL'
time_str = '%04d%02d%02d%02d%02d%02d' % (d.year, d.month, d.day, d.hour, d.minute, d.second)
rand_num = str(random.randint(10000, 99999))
return base + time_str + rand_num
支付的时候需要需要将订单号以及其他的订单信息写入到数据库中,以便后续的操作
order_str = alipay.api_alipay_trade_page_pay(
subject="实验楼消费",
out_trade_no="%s" % order, # 订单号 注意,标准的json格式没有 '' 单引号,只有 "" 双引号,python默认为 '' 单引号
total_amount=price, # 订单价格
)
# 出现debug的时候该路由无法正常使用
# request_url = 'https://openapi.alipaydev.com/gateway.do?' + order_str
request_url = alipay._gateway + '?' + order_str
db.session.commit()
生成订单时需要判断前后端的订单号是否一致,如果不一致就不能进行支付
判断的时候需要将数据库里的订单号解码出来才能进行判断
rds_order = rds.get("order" + str(user_id))
rds_order = rds_order.decode()
order_id = Orders.query.filter_by(order_id=order).first()
if order_id:
resp["code"] = 405
resp["message"] = "该订单已存在,请确认后再次提交"
return resp
# 将订单入库
price = int(float(price) * 100) # 将价格单位改为分
goods = 1
order = Orders(user=user_id, order_id=order, total_amount=price, record=record, goods=goods)
db.session.add(order)
db.session.commit()
支付之后要查询该订单是否完成支付
def get_pay_result(self, order):
alipay = AliPay(
appid="2021000118696349",
app_notify_url=None, # 默认回调url
app_private_key_string=app_private_key_string,
alipay_public_key_string=alipay_public_key_string,
# 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
sign_type="RSA2", # RSA 或者 RSA2
debug=True # 默认False
)
# 此为支付宝交易查询接口
response = alipay.api_alipay_trade_query(trade_no=order, out_trade_no=False)
return response
如果支付出现错误,要将错误原因错误信息返回给用户
通过订单号查询订单的状态无误即可
然后回调给支付宝,对支付进行最后的判断
判断它是否合法,或者金额是否存在偏差,如若无误的话即可返回给前端购买成功的信息,反正则返回购买失败的信息,让用户重新购买操作
边栏推荐
- HackMyvm靶机系列(2)-warrior
- 循环队列(C语言)
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- Intel oneapi - opening a new era of heterogeneity
- 内网渗透之内网信息收集(三)
- Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
- 7-9 make house number 3.0 (PTA program design)
- Detailed explanation of network foundation
- 小程序web抓包-fiddler
- Network technology related topics
猜你喜欢
HackMyvm靶机系列(1)-webmaster
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
Hackmyvm target series (2) -warrior
Hackmyvm target series (7) -tron
SystemVerilog discusses loop loop structure and built-in loop variable I
Library management system
Tencent map circle
Data mining - a discussion on sample imbalance in classification problems
网络基础之路由详解
List and data frame of R language experiment III
随机推荐
List and data frame of R language experiment III
Chain team implementation (C language)
Which is more advantageous in short-term or long-term spot gold investment?
Database monitoring SQL execution
Uibutton status exploration and customization
SystemVerilog discusses loop loop structure and built-in loop variable I
图书管理系统
Tencent map circle
Circular queue (C language)
. Net6: develop modern 3D industrial software based on WPF (2)
The most popular colloquial system explains the base of numbers
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
xray与burp联动 挖掘
SQL注入
7-9 make house number 3.0 (PTA program design)
How does SQLite count the data that meets another condition under the data that has been classified once
Solutions to common problems in database development such as MySQL
How to understand the difference between technical thinking and business thinking in Bi?
循环队列(C语言)
[issue 18] share a Netease go experience