当前位置:网站首页>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如果支付出现错误,要将错误原因错误信息返回给用户
通过订单号查询订单的状态无误即可
然后回调给支付宝,对支付进行最后的判断
判断它是否合法,或者金额是否存在偏差,如若无误的话即可返回给前端购买成功的信息,反正则返回购买失败的信息,让用户重新购买操作
边栏推荐
- {1,2,3,2,5}查重问题
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
- 《统计学》第八版贾俊平第三章课后习题及答案总结
- How to turn wechat applet into uniapp
- xray与burp联动 挖掘
- MySQL learning notes (stage 1)
- Experiment 4 array
- Middleware vulnerability recurrence Apache
- 7-1 output all primes between 2 and n (PTA programming)
猜你喜欢

Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)

Xray and Burp linked Mining

Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence

小程序web抓包-fiddler

Based on authorized access, cross host, and permission allocation under sqlserver

攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)

Harmonyos JS demo application development

Markdown font color editing teaching
![New version of postman flows [introductory teaching chapter 01 send request]](/img/0f/a41a39093a1170cc3f62075fd76182.jpg)
New version of postman flows [introductory teaching chapter 01 send request]

SystemVerilog discusses loop loop structure and built-in loop variable I
随机推荐
Overview of LNMP architecture and construction of related services
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Database monitoring SQL execution
Experiment 8 exception handling
Wu Enda's latest interview! Data centric reasons
The difference between layer 3 switch and router
Only 40% of the articles are original? Here comes the modification method
图书管理系统
HackMyvm靶机系列(2)-warrior
Network technology related topics
xray与burp联动 挖掘
记一次edu,SQL注入实战
Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
AQS details
xray與burp聯動 挖掘
Data mining - a discussion on sample imbalance in classification problems
HackMyvm靶机系列(7)-Tron
. Net6: develop modern 3D industrial software based on WPF (2)
XSS (cross site scripting attack) for security interview
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案