Skip to content

Commit 8c0061d

Browse files
upload global sdk python code
1 parent 8c808e0 commit 8c0061d

File tree

128 files changed

+6275
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+6275
-1
lines changed

.gitignore

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/
105+
106+
.iml
107+
.idea
108+
109+
*.pem

README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,77 @@
1-
# global-open-sdk-python
1+
```
2+
Language:Python
3+
Python version:2.6+
4+
Copyright:Ant financial services group
5+
```
6+
7+
#### 1 The sample for pay
8+
```
9+
default_alipay_client = DefaultAlipayClient("https://open-na.alipay.com", client_id, MERCHANT_PRIVATE_KEY, ALIPAY_PUBLICK_KEY)
10+
11+
alipay_pay_request = AlipayPayRequest()
12+
alipay_pay_request.path = "/ams/sandbox/api/v1/payments/pay"
13+
14+
alipay_pay_request.product_code = ProductCodeType.AGREEMENT_PAYMENT
15+
alipay_pay_request.payment_notify_url = "https://www.merchant.com/notify"
16+
alipay_pay_request.payment_redirect_url = "https://www.merchant.com?param1=sl"
17+
alipay_pay_request.payment_request_id = "pay_python_test"
18+
19+
payment_method = PaymentMethod()
20+
payment_method.payment_method_type = "GCASH"
21+
payment_method.payment_method_id = "20200404095550158596535057754730166962669640ZMvNAaATop"
22+
alipay_pay_request.payment_method = payment_method
23+
24+
amount = Amount("PHP", "10000")
25+
alipay_pay_request.payment_amount = amount
26+
27+
order = Order()
28+
order.reference_order_id = "102775765675669"
29+
order.order_description = "Mi Band 3 Wrist Strap Metal Screwless Stainless Steel For Xiaomi Mi Band 3"
30+
order.order_amount = amount
31+
32+
goods_arr = []
33+
good = Goods()
34+
good.goods_brand = "goods_brand"
35+
good.goods_name = "goods_name"
36+
goods_arr.append(good)
37+
order.goods = goods_arr
38+
39+
merchant = Merchant()
40+
merchant.merchant_mcc = "merchantMcc"
41+
merchant.reference_merchant_id = "referenceMerchantId"
42+
43+
order.merchant = merchant
44+
alipay_pay_request.order = order
45+
46+
settlement_strategy = SettlementStrategy()
47+
settlement_strategy.settlement_currency = "USD"
48+
alipay_pay_request.settlement_strategy = settlement_strategy
49+
50+
env = Env()
51+
env.terminal_type = TerminalType.APP
52+
env.os_type = OsType.IOS
53+
alipay_pay_request.env = env
54+
55+
# alipay_pay_request.is_authorization = "true"
56+
57+
print(alipay_pay_request.to_ams_json())
58+
59+
rsp_body = default_alipay_client.execute(alipay_pay_request)
60+
61+
alipay_pay_response = AlipayPayResponse(rsp_body)
62+
63+
if alipay_pay_response.result.result_status.name != ResultStatusType.F.name:
64+
print(alipay_pay_response.payment_id)
65+
print(alipay_pay_response.payment_create_time)
66+
else:
67+
print(alipay_pay_response.result.result_message)
68+
```
69+
70+
#### 2 If you don't care about HTTP calls,the sample for sign and verify
71+
```
72+
from com.alipay.ams.api.tools.signature_tool import *
73+
74+
sign(http_method, path, client_id, req_time, req_body, merchant_private_key)
75+
verify(http_method, path, client_id, rsp_time, rsp_body, rsp_signature, alipay_public_key)
76+
77+
```

com/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
__inti__.py
5+
"""
6+
name = "global-alipay-sdk-python"
7+
__version__ = "1.0.0"

com/alipay/__init__.py

Whitespace-only changes.

com/alipay/ams/__init__.py

Whitespace-only changes.

com/alipay/ams/api/__init__.py

Whitespace-only changes.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from com.alipay.ams.api.tools.signature_tool import *
5+
from com.alipay.ams.api.tools.date_tools import *
6+
from com.alipay.ams.api.net.default_http_rpc import *
7+
8+
9+
class DefaultAlipayClient(object):
10+
11+
def __init__(self, gateway_url, client_id, merchant_private_key, alipay_public_key):
12+
self.__gateway_url = gateway_url
13+
self.__client_id = client_id
14+
self.__merchant_private_key = merchant_private_key
15+
self.__alipay_public_key = alipay_public_key
16+
17+
"""
18+
内部方法,生成请求签名
19+
"""
20+
def __gen_sign(self, http_method, path, client_id, req_time, req_body):
21+
try:
22+
sign_value = sign(http_method, path, client_id, req_time, req_body, self.__merchant_private_key)
23+
except Exception as e:
24+
raise AlipayApiException("request sign failed. " + str(e))
25+
return sign_value
26+
27+
"""
28+
内部方法,生成请求签名
29+
"""
30+
def __verify_sign(self, http_method, path, client_id, rsp_time, rsp_body, rsp_signature):
31+
try:
32+
is_verify = verify(http_method, path, client_id, rsp_time, rsp_body, rsp_signature, self.__alipay_public_key)
33+
except Exception as e:
34+
raise AlipayApiException("response verify failed. " + str(e))
35+
return is_verify
36+
37+
def __parse_header(self, headers):
38+
for item in headers:
39+
header_key = item[0]
40+
if header_key == "signature":
41+
signature = item[1]
42+
if signature:
43+
signature_item = signature.split("signature=")
44+
rsp_signature = signature_item[1]
45+
if header_key == "response-time":
46+
response_time = item[1]
47+
if header_key == "client-id":
48+
client_id = item[1]
49+
50+
if "rsp_signature" not in locals():
51+
raise AlipayApiException("response header exception,signature not exist.")
52+
if "response_time" not in locals():
53+
raise AlipayApiException("response header exception,response-time not exist.")
54+
if "client_id" not in locals():
55+
raise AlipayApiException("response header exception,client-id not exist.")
56+
57+
return rsp_signature, response_time, client_id
58+
59+
def execute(self, request):
60+
61+
if not hasattr(request, "path") or not request.path:
62+
raise AlipayApiException('invalid path')
63+
64+
client_id = self.__client_id
65+
http_method = request.http_method.value
66+
path = request.path
67+
req_time = get_cur_iso8601_time()
68+
req_body = request.to_ams_json()
69+
70+
sign_value = self.__gen_sign(http_method, path, client_id, req_time, req_body)
71+
72+
key_version = DEFAULT_KEY_VERSION
73+
if hasattr(request, "key_version") and request.key_version:
74+
key_version = request.key_version
75+
76+
signature = "algorithm=RSA256,keyVersion=" + key_version + ",signature=" + sign_value
77+
headers = {
78+
"Content-type": "application/json; charset=UTF-8",
79+
"Accept": "text/plain,text/xml,text/javascript,text/html",
80+
"Cache-Control": "no-cache",
81+
"Connection": "Keep-Alive",
82+
"User-Agent": "global-alipay-sdk-python",
83+
"Request-Time": req_time,
84+
"client-id": client_id,
85+
"Signature": signature
86+
}
87+
88+
url = self.__gateway_url + path
89+
headers, response = do_post(url, headers, req_body)
90+
91+
rsp_body = response.decode(DEFAULT_CHARSET)
92+
93+
rsp_signature, response_time, client_id = self.__parse_header(headers)
94+
if not rsp_signature or not response_time:
95+
return rsp_body
96+
97+
is_verify = self.__verify_sign(http_method, path, client_id, response_time, rsp_body, rsp_signature)
98+
if not is_verify:
99+
raise AlipayApiException("response signature verify failed.")
100+
101+
return rsp_body

com/alipay/ams/api/exception/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class AlipayApiException(ValueError):
6+
pass
7+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class BusinessType(object):
6+
7+
HOTEL = "1"
8+
AIR_FLIGHT = "2"
9+
STUDAY_ABROAD = "3"
10+
TRADE = "4"
11+
OTHER = "5"
12+
13+
def to_ams_dict(self):
14+
return self.name

0 commit comments

Comments
 (0)