Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tamp_course_order
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
彭熊
tamp_course_order
Commits
11de885d
Commit
11de885d
authored
Nov 25, 2020
by
pengxiong@wealthgrow.cn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增错误处理类
parent
9c122b8f
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
166 additions
and
25 deletions
+166
-25
__main__.py
app/api/__main__.py
+2
-1
app.py
app/api/app.py
+2
-0
errors.py
app/config/errors.py
+16
-0
errorhandler.py
app/controller/errorhandler.py
+45
-0
order.py
app/controller/order.py
+36
-4
version1.py
app/router/version1.py
+5
-1
alipayWap.py
app/utils/alipay/alipayWap.py
+41
-5
wx_app_pay.py
app/utils/wxpay/wx_app_pay.py
+19
-14
No files found.
app/api/__main__.py
View file @
11de885d
...
...
@@ -9,4 +9,5 @@
from
app.api.app
import
app
if
__name__
==
'__main__'
:
app
.
run
(
'127.0.0.1'
,
port
=
8000
)
app
.
run
(
'127.0.0.1'
,
port
=
8000
,
debug
=
True
)
app/api/app.py
View file @
11de885d
...
...
@@ -10,9 +10,11 @@
from
flask
import
Flask
from
flask_restful
import
Api
from
app.controller.errorhandler
import
add_errorhandler
from
app.router.version1
import
add_route
app
=
Flask
(
__name__
)
api
=
Api
(
app
)
add_route
(
api
)
add_errorhandler
(
app
)
app/config/errors.py
0 → 100644
View file @
11de885d
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name : errors.py
# @Time : 2020/11/25 上午9:20
# @Author : X. Peng
# @Email : acepengxiong@163.com
# @Software : PyCharm
# -----------------------------------------------------------------------------
class
Errors
:
""""""
USER_ALREADY_EXISTS
=
20001
MSG
=
{
USER_ALREADY_EXISTS
:
'用户已经存在'
}
app/controller/errorhandler.py
0 → 100644
View file @
11de885d
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name : errorhandler.py
# @Time : 2020/11/25 上午9:19
# @Author : X. Peng
# @Email : acepengxiong@163.com
# @Software : PyCharm
# -----------------------------------------------------------------------------
from
flask
import
jsonify
from
app.config.errors
import
Errors
class
CustomFlaskErr
(
Exception
):
# 默认的返回码
status_code
=
200
# 自己定义了一个 return_code,作为更细颗粒度的错误代码
def
__init__
(
self
,
return_code
=
None
,
status_code
=
None
,
payload
=
None
):
Exception
.
__init__
(
self
)
self
.
return_code
=
return_code
if
status_code
is
not
None
:
self
.
status_code
=
status_code
self
.
payload
=
payload
# 构造要返回的错误代码和错误信息的 dict
def
to_dict
(
self
):
rv
=
dict
(
self
.
payload
or
())
# 增加 dict key: return code
rv
[
'return_code'
]
=
self
.
return_code
# 增加 dict key: message, 具体内容由常量定义文件中通过 return_code 转化而来
rv
[
'message'
]
=
Errors
.
MSG
[
self
.
return_code
]
return
rv
def
add_errorhandler
(
app
):
@
app
.
errorhandler
(
CustomFlaskErr
)
def
handle_flask_error
(
error
):
# response 的 json 内容为自定义错误代码和错误信息
response
=
jsonify
(
error
.
to_dict
())
# response 返回 error 发生时定义的标准错误代码
response
.
status_code
=
error
.
status_code
return
response
app/controller/order.py
View file @
11de885d
...
...
@@ -7,11 +7,15 @@
# @Software : PyCharm
# -----------------------------------------------------------------------------
from
flask_restful
import
Resource
,
reqparse
from
flask
import
request
from
flask
import
request
,
jsonify
from
app.api
import
app
from
app.controller.errorhandler
import
CustomFlaskErr
from
app.config.errors
import
Errors
class
OrderHandlers
(
Resource
):
"""."""
class
TopUpOrder
(
Resource
):
"""充值订单."""
def
__init__
(
self
):
"""."""
...
...
@@ -19,7 +23,8 @@ class OrderHandlers(Resource):
def
get
(
self
):
"""."""
self
.
parser
.
add_argument
(
'product_id'
,
type
=
str
,
required
=
True
,
help
=
'商品ID不能为空'
)
raise
CustomFlaskErr
(
Errors
.
USER_ALREADY_EXISTS
)
self
.
parser
.
add_argument
(
'product_id'
,
type
=
str
,
required
=
True
,
help
=
'商品ID不能为空'
)
args
=
self
.
parser
.
parse_args
()
print
(
args
)
return
{
'data'
:
'world'
}
...
...
@@ -35,3 +40,30 @@ class OrderHandlers(Resource):
def
delete
(
self
,
id
):
"""."""
pass
class
ConsumeOrder
(
Resource
):
"""消费订单."""
def
__init__
(
self
):
"""."""
self
.
parser
=
reqparse
.
RequestParser
()
def
get
(
self
):
"""."""
self
.
parser
.
add_argument
(
'product_id'
,
type
=
str
,
required
=
True
,
help
=
'商品ID不能为空'
)
args
=
self
.
parser
.
parse_args
()
print
(
args
)
return
{
'data'
:
'world'
}
def
post
(
self
):
"""."""
pass
def
put
(
self
,
id
):
"""."""
pass
def
delete
(
self
,
id
):
"""."""
pass
\ No newline at end of file
app/router/version1.py
View file @
11de885d
...
...
@@ -11,4 +11,8 @@ from app.controller.order import *
def
add_route
(
api
):
"""注册路由"""
api
.
add_resource
(
OrderHandlers
,
'/tamp_course_order/order'
)
# 充值订单
api
.
add_resource
(
TopUpOrder
,
'/tamp_order/micro_shop/topUpOrder'
)
# 消费订单
api
.
add_resource
(
ConsumeOrder
,
'/tamp_order/micro_shop/consumeOrder'
)
app/utils/alipay/alipayWap.py
View file @
11de885d
...
...
@@ -24,12 +24,40 @@ import time
# -----------------------------------------------------------------------------
alipay_public_key_string
=
''''''
alipay_public_key_string
=
'''-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhTT+Cn3Y+BYysAqXsjAqjJ14aUzvUHKz0LnaAifTE10IRyzzcFn+8SXIhzHoJ/OQrj7DPoDV18iSX7PcHPDdRCFyOBP1RQkxFLmnpqFY6BRoEm6k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AAVLo/PAgE+JsAyr4fLCL
1DtJxlKptu793fsMyppRDYu73gpQlokWPGGodEdlXePn0QUaA2YDRUZl6BnS2rTn
f8R0ll5LN96ZNO4DD53BwFRgK515Eg+PtE7YaLVebSGQc6/n1FS+eAkCgYEA4n/h
CuCm5liVzO2lVFJQkaRIXCwXP/C3KZ37BzBRzva2yW6qfGUyY90ZhRyIGgekEzbS
P/UgAk+Q5tDxM+ESufseh9PvFk1WWPPqP86bQ+IlWnKiuS5d5LOUbXa5AaGWolTK
h4FejzVZUsGeMPY07HseIGnavWHHosHg+MZ/nkkCgYEA0J2D9y+F6sedp4B4ZgeZ
Hq4ypaTmbVP8Amxq/fhkhh/PC0JFToH3+yvUDhN0/1yvTL1s5bPtWqSyL7Xj4JaM
jUl5ViKwJVvWATquEvThvXCiZ0xW4RXDOTwyIObecUJPzDy1Dq6TrU3fQ6abI1Hb
BgDERWkE6vylZJohcdShN0kCgYEAmaY6WZCwZ85sQIpt/1efxEz3sV9PnlaDSj4b
TnMe7BE9SkRCbwSPPGN+rmf+g/nu+gHUewKkZiznlUzkc1cZxwRtX6MLq8tm6gz9
Oa/EcDZ3NHZToN+tXjncVK2XL14Yu6JdibLfHLMKFzO1Rm2e1VVnyIFGm/Y64VQ4
c0rkw9ECgYBbv3leaa5N/RGPiZLi2Rc8lb5t4Pt8ZdlHd+MUF+BGNlfo8UgeOJUU
Fkfjdj+UJp3LE/meT53zrK1PWaS3j51vdfb/YLGf6vgiqN9kj0xpyV4uPCZc1dHI
hwtSuAgS7rSrMeMS9omsZro9L1eHDjA8Ja4L0UNGi7i8dGFnp1BtOQ==
-----END RSA PRIVATE KEY-----'''
APP_ID
=
''
URL_PREFIX
=
''
APP_ID
=
'
2021002112638152
'
URL_PREFIX
=
'
https://tampe.tanpuyun.com
'
ssl
.
_create_default_https_context
=
ssl
.
_create_unverified_context
...
...
@@ -49,7 +77,7 @@ def init_alipay(notify_path='/webservice/notify'):
def
prePay
(
subject
,
out_trade_no
,
total_amount
,
notify_path
=
'/webservice/notify'
,
return_path
=
'/home/order'
):
"""创建预付订单."""
result
=
init_alipay
(
notify_path
)
.
api_alipay_trade_
wa
p_pay
(
result
=
init_alipay
(
notify_path
)
.
api_alipay_trade_
ap
p_pay
(
subject
=
subject
,
out_trade_no
=
out_trade_no
,
total_amount
=
total_amount
,
...
...
@@ -58,6 +86,7 @@ def prePay(subject, out_trade_no, total_amount, notify_path='/webservice/notify'
if
not
result
:
return
False
pay_url
=
'https://openapi.alipay.com/gateway.do?'
+
result
print
(
pay_url
)
return
pay_url
def
alipay_transfer
():
...
...
@@ -72,3 +101,10 @@ def alipay_transfer():
}
result
=
init_alipay
()
.
api_alipay_fund_trans_toaccount_transfer
(
out_biz_no
,
payee_type
,
payee_account
,
amount
,
**
kwargs
)
return
result
if
__name__
==
'__main__'
:
out_trade_no
=
'201812102324324134138'
subject
=
'APP支付测试'
total_amount
=
0.01
remote_addr
=
'101.95.188.178'
prePay
(
subject
,
out_trade_no
,
total_amount
,
notify_path
=
'/webservice/notify'
,
return_path
=
'/home/order'
)
\ No newline at end of file
app/utils/wxpay/wx
h5
pay.py
→
app/utils/wxpay/wx
_app_
pay.py
View file @
11de885d
# -----------------------------------------------------------------------------
# File Name: wx
h5
pay.py
# File Name: wx
_app_
pay.py
# Author: X. Peng
# -----------------------------------------------------------------------------
...
...
@@ -35,13 +35,13 @@ def genNonce_str():
class
WXPay
(
object
):
"""微信H5支付."""
def
__init__
(
self
,
out_trade_no
,
body
,
total_fee
,
remote_addr
,
notify_path
=
'/webservice/notify'
,
return_path
=
'/home/order'
):
def
__init__
(
self
,
out_trade_no
,
body
,
total_fee
,
remote_addr
,
notify_path
=
'/webservice/notify'
):
"""线上:tamper.tanpuyun.com
测试:testtamper.tanpuyun.com"""
self
.
timeStamp
=
int
(
time
.
time
())
self
.
req_url
=
'https://api.mch.weixin.qq.com/pay/unifiedorder'
self
.
url_prefix
=
'https://tampe
r
.tanpuyun.com'
self
.
appid
=
'wx
95b0c12b47b758ac
'
self
.
url_prefix
=
'https://tampe.tanpuyun.com'
self
.
appid
=
'wx
3ad4c5856975a2c4
'
self
.
mch_id
=
'1515329071'
self
.
nonce_str
=
genNonce_str
()
self
.
sign
=
None
...
...
@@ -51,12 +51,12 @@ class WXPay(object):
self
.
total_fee
=
int
(
total_fee
*
100
)
self
.
spbill_create_ip
=
remote_addr
self
.
notify_url
=
self
.
url_prefix
+
notify_path
self
.
return_path
=
self
.
url_prefix
+
return_path
self
.
trade_type
=
'MWEB'
self
.
trade_type
=
'APP'
self
.
key
=
'eqwor3wquevncz9384ssp438oarefskl'
self
.
getStringSign
()
self
.
getData
()
# self.getPayUrl()
self
.
getPrepayId
()
self
.
getPaySign
()
def
getStringSign
(
self
):
...
...
@@ -108,19 +108,24 @@ class WXPay(object):
raw
[
child
.
tag
]
=
child
.
text
return
raw
def
getP
ayUrl
(
self
):
def
getP
repayId
(
self
):
"""."""
res
=
requests
.
post
(
self
.
req_url
,
data
=
self
.
dict2xml
(
self
.
data
))
res
=
self
.
xml2dict
(
res
.
content
)
print
(
res
)
return
res
[
'mweb_url'
]
+
'&'
+
parse
.
urlencode
({
'redirect_url'
:
self
.
return_path
})
self
.
prepay_id
=
res
[
'prepay_id'
]
def
getPaySign
(
self
):
"""."""
sign_string
=
'appId={0}&nonceStr={1}&package=prepay_id={2}&signType={3}&timeStamp={4}&key={5}'
.
format
(
self
.
appid
,
self
.
nonce_str
,
self
.
prepay_id
,
self
.
sign_type
,
self
.
timeStamp
,
self
.
key
)
self
.
paySign
=
hashlib
.
md5
(
sign_string
.
encode
(
"utf-8"
))
.
hexdigest
()
.
upper
()
if
__name__
==
'__main__'
:
out_trade_no
=
'20181210232432413413
4
'
body
=
'
H5
支付测试'
out_trade_no
=
'20181210232432413413
8
'
body
=
'
APP
支付测试'
total_fee
=
0.01
remote_addr
=
'101.95.188.178'
wx
=
WXPay
(
out_trade_no
,
body
,
total_fee
,
remote_addr
)
pa
y_url
=
wx
.
getPayUrl
()
print
(
pa
y_url
)
pa
rams
=
{
'prepay_id'
:
wx
.
prepay_id
,
'sign'
:
wx
.
paySign
}
print
(
pa
rams
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment